Skip to content

Commit

Permalink
fix: Text.new parameter type checks
Browse files Browse the repository at this point in the history
(cherry picked from commit 6230318)
  • Loading branch information
flavorjones committed Jul 5, 2023
1 parent e6c7768 commit a3c2d6e
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/XmlText.java
Expand Up @@ -53,6 +53,10 @@ public class XmlText extends XmlNode
content = args[0];
IRubyObject rbDocument = args[1];

if (!(rbDocument instanceof XmlNode)) {
String msg = "expected second 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 second parameter to Text.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_text.c
Expand Up @@ -20,6 +20,12 @@ rb_xml_text_s_new(int argc, VALUE *argv, VALUE klass)

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

if (!rb_obj_is_kind_of(rb_document, cNokogiriXmlNode)) {
rb_raise(rb_eTypeError,
"expected second 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_text.rb
Expand Up @@ -27,6 +27,13 @@
assert_equal("hello world", node.content)
assert_same(doc, node.document)
end

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

it "has a valid css path" do
Expand Down

0 comments on commit a3c2d6e

Please sign in to comment.