remove_child and remove_attribute now return operation result

git-svn-id: http://pugixml.googlecode.com/svn/trunk@572 99668b35-9821-0410-8761-19e4c4f06640
This commit is contained in:
arseny.kapoulkine 2010-07-07 20:10:48 +00:00
parent e959098703
commit 5811786ccd
3 changed files with 37 additions and 29 deletions

View File

@ -3774,21 +3774,21 @@ namespace pugi
return result; return result;
} }
void xml_node::remove_attribute(const char_t* name) bool xml_node::remove_attribute(const char_t* name)
{ {
remove_attribute(attribute(name)); return remove_attribute(attribute(name));
} }
void xml_node::remove_attribute(const xml_attribute& a) bool xml_node::remove_attribute(const xml_attribute& a)
{ {
if (!_root || !a._attr) return; if (!_root || !a._attr) return false;
// check that attribute belongs to *this // check that attribute belongs to *this
xml_attribute_struct* attr = a._attr; xml_attribute_struct* attr = a._attr;
while (attr->prev_attribute_c->next_attribute) attr = attr->prev_attribute_c; while (attr->prev_attribute_c->next_attribute) attr = attr->prev_attribute_c;
if (attr != _root->first_attribute) return; if (attr != _root->first_attribute) return false;
if (a._attr->next_attribute) a._attr->next_attribute->prev_attribute_c = a._attr->prev_attribute_c; if (a._attr->next_attribute) a._attr->next_attribute->prev_attribute_c = a._attr->prev_attribute_c;
else if (_root->first_attribute) _root->first_attribute->prev_attribute_c = a._attr->prev_attribute_c; else if (_root->first_attribute) _root->first_attribute->prev_attribute_c = a._attr->prev_attribute_c;
@ -3797,16 +3797,18 @@ namespace pugi
else _root->first_attribute = a._attr->next_attribute; else _root->first_attribute = a._attr->next_attribute;
destroy_attribute(a._attr, get_allocator(_root)); destroy_attribute(a._attr, get_allocator(_root));
return true;
} }
void xml_node::remove_child(const char_t* name) bool xml_node::remove_child(const char_t* name)
{ {
remove_child(child(name)); return remove_child(child(name));
} }
void xml_node::remove_child(const xml_node& n) bool xml_node::remove_child(const xml_node& n)
{ {
if (!_root || !n._root || n._root->parent != _root) return; if (!_root || !n._root || n._root->parent != _root) return false;
if (n._root->next_sibling) n._root->next_sibling->prev_sibling_c = n._root->prev_sibling_c; if (n._root->next_sibling) n._root->next_sibling->prev_sibling_c = n._root->prev_sibling_c;
else if (_root->first_child) _root->first_child->prev_sibling_c = n._root->prev_sibling_c; else if (_root->first_child) _root->first_child->prev_sibling_c = n._root->prev_sibling_c;
@ -3815,6 +3817,8 @@ namespace pugi
else _root->first_child = n._root->next_sibling; else _root->first_child = n._root->next_sibling;
destroy_node(n._root, get_allocator(_root)); destroy_node(n._root, get_allocator(_root));
return true;
} }
xml_node xml_node::find_child_by_attribute(const char_t* name, const char_t* attr_name, const char_t* attr_value) const xml_node xml_node::find_child_by_attribute(const char_t* name, const char_t* attr_name, const char_t* attr_value) const

View File

@ -1217,29 +1217,33 @@ namespace pugi
* Remove specified attribute * Remove specified attribute
* *
* \param a - attribute to be removed * \param a - attribute to be removed
* \return success flag
*/ */
void remove_attribute(const xml_attribute& a); bool remove_attribute(const xml_attribute& a);
/** /**
* Remove attribute with the specified name, if any * Remove attribute with the specified name, if any
* *
* \param name - attribute name * \param name - attribute name
* \return success flag
*/ */
void remove_attribute(const char_t* name); bool remove_attribute(const char_t* name);
/** /**
* Remove specified child * Remove specified child
* *
* \param n - child node to be removed * \param n - child node to be removed
* \return success flag
*/ */
void remove_child(const xml_node& n); bool remove_child(const xml_node& n);
/** /**
* Remove child with the specified name, if any * Remove child with the specified name, if any
* *
* \param name - child name * \param name - child name
* \return success flag
*/ */
void remove_child(const char_t* name); bool remove_child(const char_t* name);
public: public:
/** /**

View File

@ -273,21 +273,21 @@ TEST_XML(dom_node_insert_copy_before_attribute, "<node a1='v1'><child a2='v2'/><
TEST_XML(dom_node_remove_attribute, "<node a1='v1' a2='v2' a3='v3'><child a4='v4'/></node>") TEST_XML(dom_node_remove_attribute, "<node a1='v1' a2='v2' a3='v3'><child a4='v4'/></node>")
{ {
xml_node().remove_attribute(STR("a")); CHECK(!xml_node().remove_attribute(STR("a")));
xml_node().remove_attribute(xml_attribute()); CHECK(!xml_node().remove_attribute(xml_attribute()));
xml_node node = doc.child(STR("node")); xml_node node = doc.child(STR("node"));
xml_node child = node.child(STR("child")); xml_node child = node.child(STR("child"));
node.remove_attribute(STR("a")); CHECK(!node.remove_attribute(STR("a")));
node.remove_attribute(xml_attribute()); CHECK(!node.remove_attribute(xml_attribute()));
node.remove_attribute(child.attribute(STR("a4"))); CHECK(!node.remove_attribute(child.attribute(STR("a4"))));
CHECK_NODE(doc, STR("<node a1=\"v1\" a2=\"v2\" a3=\"v3\"><child a4=\"v4\" /></node>")); CHECK_NODE(doc, STR("<node a1=\"v1\" a2=\"v2\" a3=\"v3\"><child a4=\"v4\" /></node>"));
node.remove_attribute(STR("a1")); CHECK(node.remove_attribute(STR("a1")));
node.remove_attribute(node.attribute(STR("a3"))); CHECK(node.remove_attribute(node.attribute(STR("a3"))));
child.remove_attribute(STR("a4")); CHECK(child.remove_attribute(STR("a4")));
CHECK_NODE(doc, STR("<node a2=\"v2\"><child /></node>")); CHECK_NODE(doc, STR("<node a2=\"v2\"><child /></node>"));
} }
@ -388,21 +388,21 @@ TEST_XML(dom_node_insert_child_before, "<node>foo<child/></node>")
TEST_XML(dom_node_remove_child, "<node><n1/><n2/><n3/><child><n4/></child></node>") TEST_XML(dom_node_remove_child, "<node><n1/><n2/><n3/><child><n4/></child></node>")
{ {
xml_node().remove_child(STR("a")); CHECK(!xml_node().remove_child(STR("a")));
xml_node().remove_child(xml_node()); CHECK(!xml_node().remove_child(xml_node()));
xml_node node = doc.child(STR("node")); xml_node node = doc.child(STR("node"));
xml_node child = node.child(STR("child")); xml_node child = node.child(STR("child"));
node.remove_child(STR("a")); CHECK(!node.remove_child(STR("a")));
node.remove_child(xml_node()); CHECK(!node.remove_child(xml_node()));
node.remove_child(child.child(STR("n4"))); CHECK(!node.remove_child(child.child(STR("n4"))));
CHECK_NODE(doc, STR("<node><n1 /><n2 /><n3 /><child><n4 /></child></node>")); CHECK_NODE(doc, STR("<node><n1 /><n2 /><n3 /><child><n4 /></child></node>"));
node.remove_child(STR("n1")); CHECK(node.remove_child(STR("n1")));
node.remove_child(node.child(STR("n3"))); CHECK(node.remove_child(node.child(STR("n3"))));
child.remove_child(STR("n4")); CHECK(child.remove_child(STR("n4")));
CHECK_NODE(doc, STR("<node><n2 /><child /></node>")); CHECK_NODE(doc, STR("<node><n2 /><child /></node>"));
} }