Fix find_child_by_attribute assertion for attributes with null name/value.

git-svn-id: http://pugixml.googlecode.com/svn/trunk@920 99668b35-9821-0410-8761-19e4c4f06640
This commit is contained in:
arseny.kapoulkine@gmail.com 2012-09-29 06:36:29 +00:00
parent ff715f672f
commit 2876af6773
2 changed files with 27 additions and 2 deletions

View File

@ -4507,7 +4507,7 @@ namespace pugi
if (i->name && impl::strequal(name_, i->name)) if (i->name && impl::strequal(name_, i->name))
{ {
for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute) for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute)
if (impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value)) if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value : PUGIXML_TEXT("")))
return xml_node(i); return xml_node(i);
} }
@ -4520,7 +4520,7 @@ namespace pugi
for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute) for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute)
if (impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value)) if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value : PUGIXML_TEXT("")))
return xml_node(i); return xml_node(i);
return xml_node(); return xml_node();

View File

@ -537,6 +537,31 @@ TEST_XML(dom_node_find_child_by_attribute, "<node><stub attr='value3' /><child1
CHECK(node.find_child_by_attribute(STR("attr3"), STR("value")) == xml_node()); CHECK(node.find_child_by_attribute(STR("attr3"), STR("value")) == xml_node());
} }
TEST(dom_node_find_child_by_attribute_null)
{
xml_document doc;
xml_node node0 = doc.append_child();
xml_node node1 = doc.append_child(STR("a"));
xml_node node2 = doc.append_child(STR("a"));
xml_node node3 = doc.append_child(STR("a"));
// this adds an attribute with null name and/or value in the internal representation
node1.append_attribute(STR(""));
node2.append_attribute(STR("id"));
node3.append_attribute(STR("id")) = STR("1");
// make sure find_child_by_attribute works if name/value is null
CHECK(doc.find_child_by_attribute(STR("unknown"), STR("wrong")) == xml_node());
CHECK(doc.find_child_by_attribute(STR("id"), STR("wrong")) == xml_node());
CHECK(doc.find_child_by_attribute(STR("id"), STR("")) == node2);
CHECK(doc.find_child_by_attribute(STR("id"), STR("1")) == node3);
CHECK(doc.find_child_by_attribute(STR("a"), STR("unknown"), STR("wrong")) == xml_node());
CHECK(doc.find_child_by_attribute(STR("a"), STR("id"), STR("wrong")) == xml_node());
CHECK(doc.find_child_by_attribute(STR("a"), STR("id"), STR("")) == node2);
CHECK(doc.find_child_by_attribute(STR("a"), STR("id"), STR("1")) == node3);
}
struct find_predicate_const struct find_predicate_const
{ {
bool result; bool result;