Skip to content

Commit a3c2d6e

Browse files
committed
fix: Text.new parameter type checks
(cherry picked from commit 6230318)
1 parent e6c7768 commit a3c2d6e

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

ext/java/nokogiri/XmlText.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public class XmlText extends XmlNode
5353
content = args[0];
5454
IRubyObject rbDocument = args[1];
5555

56+
if (!(rbDocument instanceof XmlNode)) {
57+
String msg = "expected second parameter to be a Nokogiri::XML::Document, received " + rbDocument.getMetaClass();
58+
throw context.runtime.newTypeError(msg);
59+
}
5660
if (!(rbDocument instanceof XmlDocument)) {
5761
// TODO: deprecate allowing Node
5862
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.");

ext/nokogiri/xml_text.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ rb_xml_text_s_new(int argc, VALUE *argv, VALUE klass)
2020

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

23+
if (!rb_obj_is_kind_of(rb_document, cNokogiriXmlNode)) {
24+
rb_raise(rb_eTypeError,
25+
"expected second parameter to be a Nokogiri::XML::Document, received %"PRIsVALUE,
26+
rb_obj_class(rb_document));
27+
}
28+
2329
if (!rb_obj_is_kind_of(rb_document, cNokogiriXmlDocument)) {
2430
xmlNodePtr deprecated_node_type_arg;
2531
// TODO: deprecate allowing Node

test/xml/test_text.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
assert_equal("hello world", node.content)
2828
assert_same(doc, node.document)
2929
end
30+
31+
it "does not accept anything other than Node or Document" do
32+
assert_raises(TypeError) { Nokogiri::XML::Text.new("hello world", 1234) }
33+
assert_raises(TypeError) { Nokogiri::XML::Text.new("hello world", "asdf") }
34+
assert_raises(TypeError) { Nokogiri::XML::Text.new("hello world", {}) }
35+
assert_raises(TypeError) { Nokogiri::XML::Text.new("hello world", nil) }
36+
end
3037
end
3138

3239
it "has a valid css path" do

0 commit comments

Comments
 (0)