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

Ensure namespace pruning is not over zealous #1444

Closed
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
2 changes: 1 addition & 1 deletion ext/nokogiri/xml_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static void relink_namespace(xmlNodePtr reparented)
);
/* If we find the namespace is already declared, remove it from this
* definition list. */
if(ns && ns != curr) {
if(ns && ns != curr && xmlStrEqual(ns->prefix, curr->prefix)) {
if (prev) {
prev->next = curr->next;
} else {
Expand Down
54 changes: 54 additions & 0 deletions test/xml/test_node_reparenting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,60 @@ class TestNodeReparenting < Nokogiri::TestCase
assert @doc.at('//foo:second', "foo" => "http://flavorjon.es/")
end
end

describe "and a child node with a namespace matching the parent's default namespace, but with a different prefix" do
it "inserts a node that keeps its namespace" do
assert node = @doc.at('//xmlns:first')
child = Nokogiri::XML::Node.new('second', @doc)
ns = child.add_namespace('foo2', 'http://tenderlovemaking.com/')
child.namespace = ns

node.add_child(child)
assert newchild = @doc.at('//foo2:second', "foo2" => "http://tenderlovemaking.com/")
assert newchild.namespace_definitions.include?(ns)
end
end

describe "and a child node with a namespace matching the parent's non-default namespace, but with a different prefix" do
it "inserts a node that keeps its namespace" do
assert node = @doc.at('//xmlns:first')
child = Nokogiri::XML::Node.new('second', @doc)
ns = child.add_namespace('foo2', 'http://flavorjon.es/')
child.namespace = ns

node.add_child(child)
assert newchild = @doc.at('//foo2:second', "foo2" => "http://flavorjon.es/")
assert newchild.namespace_definitions.include?(ns)
end
end

describe "and a child node with a default namespace prefix matching the parent's non-default namespace" do
it "inserts a node that keeps its namespace" do
assert node = @doc.at('//xmlns:first')
child = Nokogiri::XML::Node.new('second', @doc)
ns = child.add_namespace(nil, 'http://flavorjon.es/')
child.namespace = ns

node.add_child(child)
assert newchild = @doc.at('//*[local-name()="second" and namespace-uri()="http://flavorjon.es/"]')
assert newchild.namespace_definitions.include?(ns)
end
end

describe "and a child node with a default namespace not matching the parent's default namespace and a namespace matching a parent namespace but with a different prefix" do
it "inserts a node that keeps its namespace" do
assert node = @doc.at('//xmlns:first')
child = Nokogiri::XML::Node.new('second', @doc)
ns = child.add_namespace(nil, 'http://example.org/')
child.namespace = ns
ns2 = child.add_namespace('foo2', 'http://tenderlovemaking.com/')

node.add_child(child)
assert newchild = @doc.at('//*[local-name()="second" and namespace-uri()="http://example.org/"]')
assert newchild.namespace_definitions.include?(ns)
assert newchild.namespace_definitions.include?(ns2)
end
end
end
end

Expand Down