Skip to content

Commit

Permalink
test(jruby): capture incorrect ns behavior in a pending test
Browse files Browse the repository at this point in the history
See #2543
  • Loading branch information
flavorjones committed May 11, 2022
1 parent 278af36 commit 9c2ddc3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ext/java/nokogiri/XmlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,18 @@ public class XmlNode extends RubyObject

List<XmlNamespace> namespaces = doc.getNamespaceCache().get(node);
return RubyArray.newArray(context.runtime, namespaces);

// // TODO: I think this implementation would be better but there are edge cases
// // See https://github.com/sparklemotion/nokogiri/issues/2543
// RubyArray<?> nsdefs = RubyArray.newArray(context.getRuntime());
// NamedNodeMap attrs = node.getAttributes();
// for (int j = 0 ; j < attrs.getLength() ; j++) {
// Attr attr = (Attr)attrs.item(j);
// if ("http://www.w3.org/2000/xmlns/" == attr.getNamespaceURI()) {
// nsdefs.append(XmlNamespace.createFromAttr(context.getRuntime(), attr));
// }
// }
// return nsdefs;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions test/namespaces/test_namespace_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,25 @@
assert_equal("<a xmlns:foo=\"fooey\"/>", child1.to_xml)
assert_equal("<b xmlns:foo=\"fooey\"/>", child2.to_xml)
end

it "handles multiple instances of a namespace definition" do
# this describes behavior that is broken in JRuby related to the namespace cache and should be fixed
# see https://github.com/sparklemotion/nokogiri/issues/2543
doc = Nokogiri::XML::Document.parse("<root>")
child1 = doc.create_element("a", "xmlns:foo" => "http://nokogiri.org/ns/foo")
assert_equal(1, child1.namespace_definitions.length)
child1.namespace_definitions.first.tap do |ns|
assert_equal("foo", ns.prefix)
assert_equal("http://nokogiri.org/ns/foo", ns.href)
end

child2 = doc.create_element("b", "xmlns:foo" => "http://nokogiri.org/ns/foo")
pending_if("https://github.com/sparklemotion/nokogiri/issues/2543", Nokogiri.jruby?) do
assert_equal(1, child2.namespace_definitions.length)
child2.namespace_definitions.first.tap do |ns|
assert_equal("foo", ns.prefix)
assert_equal("http://nokogiri.org/ns/foo", ns.href)
end
end
end
end

0 comments on commit 9c2ddc3

Please sign in to comment.