Skip to content

Commit

Permalink
test: add coverage for attribute namespace after reparenting
Browse files Browse the repository at this point in the history
also use xmlNodePtr->properties in place of xmlElementPtr->attributes
because it's clearer and requires less type gymnastics
  • Loading branch information
flavorjones committed May 11, 2021
1 parent dad8728 commit 8f69859
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
9 changes: 5 additions & 4 deletions ext/nokogiri/xml_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static void
relink_namespace(xmlNodePtr reparented)
{
xmlNodePtr child;
xmlAttrPtr attr;

if (reparented->type != XML_ATTRIBUTE_NODE &&
reparented->type != XML_ELEMENT_NODE) { return; }
Expand Down Expand Up @@ -132,10 +133,10 @@ relink_namespace(xmlNodePtr reparented)
}

if (reparented->type == XML_ELEMENT_NODE) {
child = (xmlNodePtr)((xmlElementPtr)reparented)->attributes;
while (NULL != child) {
relink_namespace(child);
child = child->next;
attr = reparented->properties;
while (NULL != attr) {
relink_namespace((xmlNodePtr)attr);
attr = attr->next;
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion test/namespaces/test_namespaces_in_created_doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def setup
super
@doc = Nokogiri::XML('<fruit xmlns="ns:fruit" xmlns:veg="ns:veg" xmlns:xlink="http://www.w3.org/1999/xlink"/>')
pear = @doc.create_element('pear')
bosc = @doc.create_element('bosc')
bosc = @doc.create_element('bosc', {"veg:any" => "none"})
pear.add_child(bosc)
@doc.root << pear
@doc.root.add_child('<orange/>')
Expand Down Expand Up @@ -38,6 +38,9 @@ def test_created_parent_default_ns
def test_created_grandparent_default_ns
assert_equal 'ns:fruit', check_namespace(@doc.root.elements[0].elements[0])
end
def test_created_attribute_with_ns
assert_equal 'ns:veg', check_namespace(@doc.root.elements[0].elements[0].attribute_nodes.first)
end
def test_created_parent_nondefault_ns
assert_equal 'ns:veg', check_namespace(@doc.root.elements[2])
end
Expand Down

0 comments on commit 8f69859

Please sign in to comment.