feat: add remove_attributes() and remove_children()
This commit is contained in:
parent
ccb63a9186
commit
d1b566fc26
@ -6059,6 +6059,23 @@ namespace pugi
|
||||
return true;
|
||||
}
|
||||
|
||||
PUGI__FN bool xml_node::remove_attributes()
|
||||
{
|
||||
for(pugi::xml_attribute a: this->attributes())
|
||||
{
|
||||
if (!_root || !a._attr) return false;
|
||||
if (!impl::is_attribute_of(a._attr, _root)) return false;
|
||||
|
||||
impl::xml_allocator& alloc = impl::get_allocator(_root);
|
||||
if (!alloc.reserve()) return false;
|
||||
|
||||
impl::destroy_attribute(a._attr, alloc);
|
||||
}
|
||||
this->_root->first_attribute = nullptr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
PUGI__FN bool xml_node::remove_child(const char_t* name_)
|
||||
{
|
||||
return remove_child(child(name_));
|
||||
@ -6077,6 +6094,22 @@ namespace pugi
|
||||
return true;
|
||||
}
|
||||
|
||||
PUGI__FN bool xml_node::remove_children()
|
||||
{
|
||||
for (pugi::xml_node child: this->children())
|
||||
{
|
||||
if (!_root || !child._root || child._root->parent != _root) return false;
|
||||
|
||||
impl::xml_allocator& alloc = impl::get_allocator(_root);
|
||||
if (!alloc.reserve()) return false;
|
||||
|
||||
impl::destroy_node(child._root, alloc);
|
||||
}
|
||||
this->_root->first_child = nullptr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
PUGI__FN xml_parse_result xml_node::append_buffer(const void* contents, size_t size, unsigned int options, xml_encoding encoding)
|
||||
{
|
||||
// append_buffer is only valid for elements/documents
|
||||
|
||||
@ -576,10 +576,16 @@ namespace pugi
|
||||
bool remove_attribute(const xml_attribute& a);
|
||||
bool remove_attribute(const char_t* name);
|
||||
|
||||
// Remove all attributes
|
||||
bool remove_attributes();
|
||||
|
||||
// Remove specified child
|
||||
bool remove_child(const xml_node& n);
|
||||
bool remove_child(const char_t* name);
|
||||
|
||||
// Remove all children
|
||||
bool remove_children();
|
||||
|
||||
// Parses buffer as an XML document fragment and appends all nodes as children of the current node.
|
||||
// Copies/converts the buffer, so it may be deleted or changed after the function returns.
|
||||
// Note: append_buffer allocates memory that has the lifetime of the owning document; removing the appended nodes does not immediately reclaim that memory.
|
||||
|
||||
@ -492,6 +492,18 @@ TEST_XML(dom_node_remove_attribute, "<node a1='v1' a2='v2' a3='v3'><child a4='v4
|
||||
CHECK_NODE(doc, STR("<node a2=\"v2\"><child/></node>"));
|
||||
}
|
||||
|
||||
TEST_XML(dom_node_remove_attributes, "<node a1='v1' a2='v2' a3='v3'><child a4='v4'/></node>")
|
||||
{
|
||||
xml_node node = doc.child(STR("node"));
|
||||
xml_node child = node.child(STR("child"));
|
||||
|
||||
CHECK(child.remove_attributes());
|
||||
CHECK_NODE(child, STR("<child/>"));
|
||||
|
||||
CHECK(node.remove_attributes());
|
||||
CHECK_NODE(node, STR("<node><child/></node>"));
|
||||
}
|
||||
|
||||
TEST_XML(dom_node_prepend_child, "<node>foo<child/></node>")
|
||||
{
|
||||
CHECK(xml_node().prepend_child() == xml_node());
|
||||
@ -707,6 +719,18 @@ TEST_XML(dom_node_remove_child, "<node><n1/><n2/><n3/><child><n4/></child></node
|
||||
CHECK_NODE(doc, STR("<node><n2/><child/></node>"));
|
||||
}
|
||||
|
||||
TEST_XML(dom_node_remove_children, "<node><n1/><n2/><n3/><child><n4/></child></node>")
|
||||
{
|
||||
xml_node node = doc.child(STR("node"));
|
||||
xml_node child = node.child(STR("child"));
|
||||
|
||||
CHECK(child.remove_children());
|
||||
CHECK_NODE(child, STR("<child/>"));
|
||||
|
||||
CHECK(node.remove_children());
|
||||
CHECK_NODE(node, STR("<node/>"));
|
||||
}
|
||||
|
||||
TEST_XML(dom_node_remove_child_complex, "<node id='1'><n1 id1='1' id2='2'/><n2/><n3/><child><n4/></child></node>")
|
||||
{
|
||||
CHECK(doc.child(STR("node")).remove_child(STR("n1")));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user