Skip to content

Commit

Permalink
fix: CDATA.new parameter type checks
Browse files Browse the repository at this point in the history
(cherry picked from commit 547c27c)
  • Loading branch information
flavorjones committed Jul 5, 2023
1 parent a84f430 commit ffa20ef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ext/java/nokogiri/XmlCdata.java
Expand Up @@ -46,6 +46,10 @@ public class XmlCdata extends XmlText
IRubyObject rbDocument = args[0];
content = args[1];

if (!(rbDocument instanceof XmlNode)) {
String msg = "expected first parameter to be a Nokogiri::XML::Document, received " + rbDocument.getMetaClass();
throw context.runtime.newTypeError(msg);
}
if (!(rbDocument instanceof XmlDocument)) {
// TODO: deprecate allowing Node
context.runtime.getWarnings().warn("Passing a Node as the first parameter to CDATA.new is deprecated. Please pass a Document instead. This will become an error in a future release of Nokogiri.");
Expand Down
6 changes: 6 additions & 0 deletions ext/nokogiri/xml_cdata.c
Expand Up @@ -25,6 +25,12 @@ rb_xml_cdata_s_new(int argc, VALUE *argv, VALUE klass)

rb_scan_args(argc, argv, "2*", &rb_document, &rb_content, &rb_rest);

if (!rb_obj_is_kind_of(rb_document, cNokogiriXmlNode)) {
rb_raise(rb_eTypeError,
"expected first parameter to be a Nokogiri::XML::Document, received %"PRIsVALUE,
rb_obj_class(rb_document));
}

if (!rb_obj_is_kind_of(rb_document, cNokogiriXmlDocument)) {
xmlNodePtr deprecated_node_type_arg;
// TODO: deprecate allowing Node
Expand Down
7 changes: 7 additions & 0 deletions test/xml/test_cdata.rb
Expand Up @@ -40,6 +40,13 @@
assert_raises(TypeError) { Nokogiri::XML::CDATA.new(doc, 1.234) }
assert_raises(TypeError) { Nokogiri::XML::CDATA.new(doc, {}) }
end

it "does not accept anything other than Node or Document" do
assert_raises(TypeError) { Nokogiri::XML::CDATA.new(1234, "hello world") }
assert_raises(TypeError) { Nokogiri::XML::CDATA.new("asdf", "hello world") }.inspect
assert_raises(TypeError) { Nokogiri::XML::CDATA.new({}, "hello world") }
assert_raises(TypeError) { Nokogiri::XML::CDATA.new(nil, "hello world") }
end
end

it "supports #content and #content=" do
Expand Down

0 comments on commit ffa20ef

Please sign in to comment.