Added xml_document::reset, added append/prepend/insert child overloads for elements (with explicit name)

git-svn-id: http://pugixml.googlecode.com/svn/trunk@779 99668b35-9821-0410-8761-19e4c4f06640
This commit is contained in:
arseny.kapoulkine 2010-10-26 17:09:34 +00:00
parent 948cb037ae
commit 09b5dfdcb0
4 changed files with 147 additions and 1 deletions

View file

@ -3989,6 +3989,42 @@ namespace pugi
return n;
}
xml_node xml_node::append_child(const char_t* name)
{
xml_node result = append_child(node_element);
result.set_name(name);
return result;
}
xml_node xml_node::prepend_child(const char_t* name)
{
xml_node result = prepend_child(node_element);
result.set_name(name);
return result;
}
xml_node xml_node::insert_child_after(const char_t* name, const xml_node& node)
{
xml_node result = insert_child_after(node_element, node);
result.set_name(name);
return result;
}
xml_node xml_node::insert_child_before(const char_t* name, const xml_node& node)
{
xml_node result = insert_child_before(node_element, node);
result.set_name(name);
return result;
}
xml_node xml_node::append_copy(const xml_node& proto)
{
xml_node result = append_child(proto.type());

View file

@ -428,6 +428,12 @@ namespace pugi
xml_node insert_child_after(xml_node_type type, const xml_node& node);
xml_node insert_child_before(xml_node_type type, const xml_node& node);
// Add child element with specified name. Returns added node, or empty node on errors.
xml_node append_child(const char_t* name);
xml_node prepend_child(const char_t* name);
xml_node insert_child_after(const char_t* name, const xml_node& node);
xml_node insert_child_before(const char_t* name, const xml_node& node);
// Add a copy of the specified node as a child. Returns added node, or empty node on errors.
xml_node append_copy(const xml_node& proto);
xml_node prepend_copy(const xml_node& proto);
@ -719,7 +725,6 @@ namespace pugi
xml_document(const xml_document&);
const xml_document& operator=(const xml_document&);
void reset();
void create();
void destroy();
@ -732,6 +737,9 @@ namespace pugi
// Destructor, invalidates all node/attribute handles to this document
~xml_document();
// Removes all nodes, leaving the empty document
void reset();
#ifndef PUGIXML_NO_STL
// Load document from stream.
xml_parse_result load(std::basic_istream<char, std::char_traits<char> >& stream, unsigned int options = parse_default, xml_encoding encoding = encoding_auto);

View file

@ -876,3 +876,33 @@ TEST_XML_FLAGS(document_element_absent, "<!---->", parse_comments)
{
CHECK(doc.document_element() == xml_node());
}
TEST_XML(document_reset, "<node><child/></node>")
{
CHECK(doc.first_child());
doc.reset();
CHECK(!doc.first_child());
CHECK_NODE(doc, STR(""));
doc.reset();
CHECK(!doc.first_child());
CHECK_NODE(doc, STR(""));
CHECK(doc.load(STR("<node/>")));
CHECK(doc.first_child());
CHECK_NODE(doc, STR("<node />"));
doc.reset();
CHECK(!doc.first_child());
CHECK_NODE(doc, STR(""));
}
TEST(document_reset_empty)
{
xml_document doc;
doc.reset();
CHECK(!doc.first_child());
CHECK_NODE(doc, STR(""));
}

View file

@ -469,6 +469,78 @@ TEST_XML(dom_node_insert_child_before, "<node>foo<child/></node>")
CHECK_NODE(doc, STR("<node><?n4?>foo<n1 />n3<n2 /><child /></node>"));
}
TEST_XML(dom_node_prepend_child_name, "<node>foo<child/></node>")
{
CHECK(xml_node().prepend_child(STR("")) == xml_node());
CHECK(doc.child(STR("node")).first_child().prepend_child(STR("")) == xml_node());
xml_node n1 = doc.child(STR("node")).prepend_child(STR("n1"));
CHECK(n1);
xml_node n2 = doc.child(STR("node")).prepend_child(STR("n2"));
CHECK(n2 && n1 != n2);
CHECK_NODE(doc, STR("<node><n2 /><n1 />foo<child /></node>"));
}
TEST_XML(dom_node_append_child_name, "<node>foo<child/></node>")
{
CHECK(xml_node().append_child(STR("")) == xml_node());
CHECK(doc.child(STR("node")).first_child().append_child(STR("")) == xml_node());
xml_node n1 = doc.child(STR("node")).append_child(STR("n1"));
CHECK(n1);
xml_node n2 = doc.child(STR("node")).append_child(STR("n2"));
CHECK(n2 && n1 != n2);
CHECK_NODE(doc, STR("<node>foo<child /><n1 /><n2 /></node>"));
}
TEST_XML(dom_node_insert_child_after_name, "<node>foo<child/></node>")
{
CHECK(xml_node().insert_child_after(STR(""), xml_node()) == xml_node());
CHECK(doc.child(STR("node")).first_child().insert_child_after(STR(""), xml_node()) == xml_node());
xml_node node = doc.child(STR("node"));
xml_node child = node.child(STR("child"));
CHECK(node.insert_child_after(STR(""), node) == xml_node());
CHECK(child.insert_child_after(STR(""), node) == xml_node());
xml_node n1 = node.insert_child_after(STR("n1"), child);
CHECK(n1 && n1 != node && n1 != child);
xml_node n2 = node.insert_child_after(STR("n2"), child);
CHECK(n2 && n2 != node && n2 != child && n2 != n1);
CHECK(child.insert_child_after(STR(""), n2) == xml_node());
CHECK_NODE(doc, STR("<node>foo<child /><n2 /><n1 /></node>"));
}
TEST_XML(dom_node_insert_child_before_name, "<node>foo<child/></node>")
{
CHECK(xml_node().insert_child_before(STR(""), xml_node()) == xml_node());
CHECK(doc.child(STR("node")).first_child().insert_child_before(STR(""), xml_node()) == xml_node());
xml_node node = doc.child(STR("node"));
xml_node child = node.child(STR("child"));
CHECK(node.insert_child_before(STR(""), node) == xml_node());
CHECK(child.insert_child_before(STR(""), node) == xml_node());
xml_node n1 = node.insert_child_before(STR("n1"), child);
CHECK(n1 && n1 != node && n1 != child);
xml_node n2 = node.insert_child_before(STR("n2"), child);
CHECK(n2 && n2 != node && n2 != child && n2 != n1);
CHECK(child.insert_child_before(STR(""), n2) == xml_node());
CHECK_NODE(doc, STR("<node>foo<n1 /><n2 /><child /></node>"));
}
TEST_XML(dom_node_remove_child, "<node><n1/><n2/><n3/><child><n4/></child></node>")
{
CHECK(!xml_node().remove_child(STR("a")));