From ad5960d36650bef8cf3d313c9198d639f78f62b1 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sun, 9 Mar 2025 21:47:59 -0400 Subject: [PATCH] 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 --- ext/java/nokogiri/XmlNode.java | 8 ++------ test/namespaces/test_serializing_namespaces.rb | 12 ++---------- test/xml/test_document.rb | 8 ++++++++ 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/ext/java/nokogiri/XmlNode.java b/ext/java/nokogiri/XmlNode.java index cae416cd69..38bc551e2c 100644 --- a/ext/java/nokogiri/XmlNode.java +++ b/ext/java/nokogiri/XmlNode.java @@ -321,12 +321,8 @@ public class XmlNode extends RubyObject Element element; String node_name = rubyStringToString(name); - String prefix = NokogiriHelpers.getPrefix(node_name); - String namespace_uri = null; - if (document.getDocumentElement() != null) { - namespace_uri = document.getDocumentElement().lookupNamespaceURI(prefix); - } - element = document.createElementNS(namespace_uri, node_name); + + element = document.createElementNS(null, node_name); setNode(context.runtime, element); } diff --git a/test/namespaces/test_serializing_namespaces.rb b/test/namespaces/test_serializing_namespaces.rb index 1a1a4f5515..d88b480434 100644 --- a/test/namespaces/test_serializing_namespaces.rb +++ b/test/namespaces/test_serializing_namespaces.rb @@ -80,11 +80,7 @@ assert_includes(doc, '') assert_includes(doc, "in outer namespace") assert_includes(doc, '') - pending_if("https://github.com/sparklemotion/nokogiri/issues/3457", Nokogiri.jruby?) do - # Here JRuby is incorrectly adding the xmlns namespace declaration, i.e.: - # 'in inner namespace' - assert_includes(doc, "in inner namespace") - end + assert_includes(doc, "in inner namespace") assert_includes(doc, "back in outer namespace") end end @@ -184,11 +180,7 @@ assert_includes(doc, "in default namespace") assert_includes(doc, "in prefixed namespace") assert_includes(doc, '') - pending_if("https://github.com/sparklemotion/nokogiri/issues/3457", Nokogiri.jruby?) do - # Here JRuby is incorrectly adding the xmlns namespace declaration, i.e.: - # 'in new default namespace' - assert_includes(doc, "in new default namespace") - end + assert_includes(doc, "in new default namespace") assert_includes(doc, "still using original prefixed namespace") end end diff --git a/test/xml/test_document.rb b/test/xml/test_document.rb index f56f344390..0c8aafcd3c 100644 --- a/test/xml/test_document.rb +++ b/test/xml/test_document.rb @@ -188,6 +188,14 @@ def test_create_element_with_attributes assert_equal("1", elm["a"]) end + # https://github.com/sparklemotion/nokogiri/issues/3457 + def test_create_element_doc_default_namespace_not_automatically_applied + doc = Nokogiri::XML(%()) + node = doc.create_element("child") + + assert_nil(node.namespace) + end + def test_create_element_with_namespace elm = xml.create_element("foo", "xmlns:foo": "http://tenderlovemaking.com") assert_equal("http://tenderlovemaking.com", elm.namespaces["xmlns:foo"])