Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jruby): create_element should not set the doc's default ns #3463

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions ext/java/nokogiri/XmlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

@johnnyshields johnnyshields Mar 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove:

    Element element;
    String node_name = rubyStringToString(name);

Then do:

    setNode(context.runtime, document.createElementNS(null, node_name));

}

Expand Down
12 changes: 2 additions & 10 deletions test/namespaces/test_serializing_namespaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@
assert_includes(doc, '<root xmlns="http://outer-namespace.org/">')
assert_includes(doc, "<outer>in outer namespace</outer>")
assert_includes(doc, '<inner xmlns="http://inner-namespace.org/">')
pending_if("https://github.com/sparklemotion/nokogiri/issues/3457", Nokogiri.jruby?) do
# Here JRuby is incorrectly adding the xmlns namespace declaration, i.e.:
# '<element xmlns="http://outer-namespace.org/">in inner namespace</element>'
assert_includes(doc, "<element>in inner namespace</element>")
end
assert_includes(doc, "<element>in inner namespace</element>")
assert_includes(doc, "<another>back in outer namespace</another>")
end
end
Expand Down Expand Up @@ -184,11 +180,7 @@
assert_includes(doc, "<default_element>in default namespace</default_element>")
assert_includes(doc, "<ns:prefixed_element>in prefixed namespace</ns:prefixed_element>")
assert_includes(doc, '<mixed xmlns="http://new-default.org/">')
pending_if("https://github.com/sparklemotion/nokogiri/issues/3457", Nokogiri.jruby?) do
# Here JRuby is incorrectly adding the xmlns namespace declaration, i.e.:
# '<new_default xmlns="http://default.org/">in new default namespace</new_default>'
assert_includes(doc, "<new_default>in new default namespace</new_default>")
end
assert_includes(doc, "<new_default>in new default namespace</new_default>")
assert_includes(doc, "<ns:still_prefixed>still using original prefixed namespace</ns:still_prefixed>")
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/xml/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(%(<root xmlns='http://example.org/default'>))
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"])
Expand Down
Loading