Skip to content

Commit

Permalink
test: subtle namespace behavior change when reparenting
Browse files Browse the repository at this point in the history
introduced by 1f483f0 which accidentally fixed #1712.

Closes #1712
  • Loading branch information
flavorjones committed May 20, 2021
1 parent a240869 commit 3ffb313
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -26,6 +26,7 @@ Many thanks to Sam Ruby, Steve Checkoway, and Craig Barnes for creating and main
### Fixed

* [CRuby] Namespaced attributes are handled properly when their parent node is reparented into another document. Previously, the namespace may have gotten dropped. [[#2228](https://github.com/sparklemotion/nokogiri/issues/2228)]
* [CRuby] Reparented nodes no longer inherit their parent's namespace. Previously, a node without a namespace was forced to adopt its parent's namespace. [[#1712](https://github.com/sparklemotion/nokogiri/issues/1712)]


### Improved
Expand Down
20 changes: 20 additions & 0 deletions test/xml/test_builder.rb
Expand Up @@ -107,6 +107,26 @@ def test_non_root_namespace
assert_equal "one", b.doc.at("hello", "xmlns" => "one").namespace.href
end

def test_builder_namespace_children_do_not_inherit
# see https://github.com/sparklemotion/nokogiri/issues/1712
result = Nokogiri::XML::Builder.new(encoding: 'utf-8') do |xml|
xml['soapenv'].Envelope('xmlns:soapenv' => 'http://schemas.xmlsoap.org/soap/envelope/', 'xmlns:emer' => 'http://dashcs.com/api/v1/emergency') do
xml['soapenv'].Header
xml['soapenv'].Body do
xml['emer'].validateLocation do
# these should not have a namespace
xml.location do
xml.address 'Some place over the rainbow'
end
end
end
end
end
assert(result.doc.at_xpath("//emer:validateLocation", {"emer" => "http://dashcs.com/api/v1/emergency"}),
"expected validateLocation node to have a namespace")
assert(result.doc.at_xpath("//location"), "expected location node to not have a namespace")
end

def test_specify_namespace
b = Nokogiri::XML::Builder.new { |xml|
xml.root("xmlns:foo" => "bar") do
Expand Down
18 changes: 17 additions & 1 deletion test/xml/test_node.rb
Expand Up @@ -334,7 +334,7 @@ def test_namespace_definitions_when_no_exist
assert_equal(0, namespace_definitions.length)
end

def test_namespace_goes_to_children
def test_default_namespace_goes_to_children
fruits = Nokogiri::XML(<<~eoxml)
<Fruit xmlns='www.fruits.org'>
</Fruit>
Expand All @@ -347,6 +347,22 @@ def test_namespace_goes_to_children
assert(fruits.at('//fruit:Apple', { 'fruit' => 'www.fruits.org' }))
end

def test_parent_namespace_is_not_inherited
fruits = Nokogiri::XML(<<-eoxml)
<fruit xmlns:fruit="http://fruits.org">
<fruit:apple />
</fruit>
eoxml

apple = fruits.at_xpath('//fruit:apple', {"fruit" => "http://fruits.org"})
assert(apple)

orange = Nokogiri::XML::Node.new('orange', fruits)
apple.add_child(orange)

assert_equal(orange, fruits.at_xpath('//orange'))
end

def test_description
assert_nil(xml.at('employee').description)
end
Expand Down

0 comments on commit 3ffb313

Please sign in to comment.