Skip to content

[bug] JRuby: reparenting a node does not apply the parent's default namespace #3457

@johnnyshields

Description

@johnnyshields

The JRuby parser has a bug where default namespaces on child elements are reverted to the root node's default namesapce, rather than the default namespace of their immediate parent.

Unlike #3455 which was a "syntax preference", this is a bona-fide "bug". The JRuby-parsed and out XML is semantically different than the MRI-parsed one, in a way that can break consumers.

    it 'handles nested default namespaces' do
      doc = Nokogiri::XML::Builder.new do |xml|
        xml.root(xmlns: 'http://outer-namespace.org/') do
          xml.outer('in outer namespace')
          xml.inner(xmlns: 'http://inner-namespace.org/') do
            xml.element('in inner namespace')
          end
          xml.another('back in outer namespace')
        end
      end.doc.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML)

      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/">')
      # TODO: THE NEXT LINE FAILS ON JRUBY!!
      assert_includes(doc, '<element>in inner namespace</element>')
      # TODO: THE NEXT LINE PASSES ON JRUBY, BUT SHOULD FAIL!!
      # assert_includes(doc, '<element xmlns="http://outer-namespace.org/">in inner namespace</element>')
      assert_includes(doc, '<another>back in outer namespace</another>')
    end

This PR includes test cases which cover this issue: #3456. They are made to pass for now using if Nokogiri.jruby?

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions