Skip to content

Commit ad5960d

Browse files
committed
fix(jruby): create_element should not set the doc's default ns
The ns should be set explicitly or when the node is parented. Fixes #3457
1 parent 3ff6b8e commit ad5960d

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

ext/java/nokogiri/XmlNode.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,8 @@ public class XmlNode extends RubyObject
321321

322322
Element element;
323323
String node_name = rubyStringToString(name);
324-
String prefix = NokogiriHelpers.getPrefix(node_name);
325-
String namespace_uri = null;
326-
if (document.getDocumentElement() != null) {
327-
namespace_uri = document.getDocumentElement().lookupNamespaceURI(prefix);
328-
}
329-
element = document.createElementNS(namespace_uri, node_name);
324+
325+
element = document.createElementNS(null, node_name);
330326
setNode(context.runtime, element);
331327
}
332328

test/namespaces/test_serializing_namespaces.rb

+2-10
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@
8080
assert_includes(doc, '<root xmlns="http://outer-namespace.org/">')
8181
assert_includes(doc, "<outer>in outer namespace</outer>")
8282
assert_includes(doc, '<inner xmlns="http://inner-namespace.org/">')
83-
pending_if("https://github.com/sparklemotion/nokogiri/issues/3457", Nokogiri.jruby?) do
84-
# Here JRuby is incorrectly adding the xmlns namespace declaration, i.e.:
85-
# '<element xmlns="http://outer-namespace.org/">in inner namespace</element>'
86-
assert_includes(doc, "<element>in inner namespace</element>")
87-
end
83+
assert_includes(doc, "<element>in inner namespace</element>")
8884
assert_includes(doc, "<another>back in outer namespace</another>")
8985
end
9086
end
@@ -184,11 +180,7 @@
184180
assert_includes(doc, "<default_element>in default namespace</default_element>")
185181
assert_includes(doc, "<ns:prefixed_element>in prefixed namespace</ns:prefixed_element>")
186182
assert_includes(doc, '<mixed xmlns="http://new-default.org/">')
187-
pending_if("https://github.com/sparklemotion/nokogiri/issues/3457", Nokogiri.jruby?) do
188-
# Here JRuby is incorrectly adding the xmlns namespace declaration, i.e.:
189-
# '<new_default xmlns="http://default.org/">in new default namespace</new_default>'
190-
assert_includes(doc, "<new_default>in new default namespace</new_default>")
191-
end
183+
assert_includes(doc, "<new_default>in new default namespace</new_default>")
192184
assert_includes(doc, "<ns:still_prefixed>still using original prefixed namespace</ns:still_prefixed>")
193185
end
194186
end

test/xml/test_document.rb

+8
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ def test_create_element_with_attributes
188188
assert_equal("1", elm["a"])
189189
end
190190

191+
# https://github.com/sparklemotion/nokogiri/issues/3457
192+
def test_create_element_doc_default_namespace_not_automatically_applied
193+
doc = Nokogiri::XML(%(<root xmlns='http://example.org/default'>))
194+
node = doc.create_element("child")
195+
196+
assert_nil(node.namespace)
197+
end
198+
191199
def test_create_element_with_namespace
192200
elm = xml.create_element("foo", "xmlns:foo": "http://tenderlovemaking.com")
193201
assert_equal("http://tenderlovemaking.com", elm.namespaces["xmlns:foo"])

0 commit comments

Comments
 (0)