fix: update code as per comments

This commit is contained in:
Tuan Anh Tran 2019-09-17 22:30:45 +07:00
parent da99f38681
commit 8dcc278d18
2 changed files with 23 additions and 11 deletions

View File

@ -6061,17 +6061,22 @@ namespace pugi
PUGI__FN bool xml_node::remove_attributes()
{
for (xml_attribute_struct* attr = this->_root->first_attribute; attr; attr = attr->next_attribute)
if (!_root) return false;
impl::xml_allocator& alloc = impl::get_allocator(_root);
if (!alloc.reserve()) return false;
for (xml_attribute_struct* attr = _root->first_attribute; attr;)
{
if (!_root || !attr) return false;
if (!attr) return false;
if (!impl::is_attribute_of(attr, _root)) return false;
impl::xml_allocator& alloc = impl::get_allocator(_root);
if (!alloc.reserve()) return false;
xml_attribute_struct* next = attr->next_attribute;
impl::destroy_attribute(attr, alloc);
attr = next;
}
this->_root->first_attribute = 0;
_root->first_attribute = 0;
return true;
}
@ -6096,16 +6101,21 @@ namespace pugi
PUGI__FN bool xml_node::remove_children()
{
for (xml_node_struct* child = this->_root->first_child; child; child = child->next_sibling)
if (!_root) return false;
impl::xml_allocator& alloc = impl::get_allocator(_root);
if (!alloc.reserve()) return false;
for (xml_node_struct* child = _root->first_child; child; )
{
if (!_root || !child || child->parent != _root) return false;
xml_node_struct* next = child->next_sibling;
impl::xml_allocator& alloc = impl::get_allocator(_root);
if (!alloc.reserve()) return false;
destroy_node(child, alloc);
impl::destroy_node(child, alloc);
child = next;
}
this->_root->first_child = 0;
_root->first_child = 0;
return true;
}

View File

@ -494,6 +494,7 @@ TEST_XML(dom_node_remove_attribute, "<node a1='v1' a2='v2' a3='v3'><child a4='v4
TEST_XML(dom_node_remove_attributes, "<node a1='v1' a2='v2' a3='v3'><child a4='v4'/></node>")
{
CHECK(!xml_node().remove_attributes());
xml_node node = doc.child(STR("node"));
xml_node child = node.child(STR("child"));
@ -721,6 +722,7 @@ TEST_XML(dom_node_remove_child, "<node><n1/><n2/><n3/><child><n4/></child></node
TEST_XML(dom_node_remove_children, "<node><n1/><n2/><n3/><child><n4/></child></node>")
{
CHECK(!xml_node().remove_children());
xml_node node = doc.child(STR("node"));
xml_node child = node.child(STR("child"));