From 7c01cf8df31ed0636d12d907d32cfc3cfbc452ea Mon Sep 17 00:00:00 2001 From: "arseny.kapoulkine" Date: Fri, 21 May 2010 05:50:53 +0000 Subject: Nodes/attributes with empty names now are printed as :anonymous git-svn-id: http://pugixml.googlecode.com/svn/trunk@441 99668b35-9821-0410-8761-19e4c4f06640 --- src/pugixml.cpp | 67 +++++++++++++++++++++++++--------------------------- tests/test_write.cpp | 28 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 35 deletions(-) diff --git a/src/pugixml.cpp b/src/pugixml.cpp index 75db295..60e0b2f 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -2705,8 +2705,26 @@ namespace } } + void node_output_attributes(xml_buffered_writer& writer, const xml_node& node) + { + const char_t* default_name = PUGIXML_TEXT(":anonymous"); + + for (xml_attribute a = node.first_attribute(); a; a = a.next_attribute()) + { + writer.write(' '); + writer.write(a.name()[0] ? a.name() : default_name); + writer.write('=', '"'); + + text_output_escaped(writer, a.value(), oct_special_attr); + + writer.write('"'); + } + } + void node_output(xml_buffered_writer& writer, const xml_node& node, const char_t* indent, unsigned int flags, unsigned int depth) { + const char_t* default_name = PUGIXML_TEXT(":anonymous"); + if ((flags & format_indent) != 0 && (flags & format_raw) == 0) for (unsigned int i = 0; i < depth; ++i) writer.write(indent); @@ -2721,19 +2739,12 @@ namespace case node_element: { - writer.write('<'); - writer.write(node.name()); - - for (xml_attribute a = node.first_attribute(); a; a = a.next_attribute()) - { - writer.write(' '); - writer.write(a.name()); - writer.write('=', '"'); + const char_t* name = node.name()[0] ? node.name() : default_name; - text_output_escaped(writer, a.value(), oct_special_attr); + writer.write('<'); + writer.write(name); - writer.write('"'); - } + node_output_attributes(writer, node); if (flags & format_raw) { @@ -2747,7 +2758,7 @@ namespace node_output(writer, n, indent, flags, depth + 1); writer.write('<', '/'); - writer.write(node.name()); + writer.write(name); writer.write('>'); } } @@ -2760,7 +2771,7 @@ namespace text_output_escaped(writer, node.first_child().value(), oct_special_pcdata); writer.write('<', '/'); - writer.write(node.name()); + writer.write(name); writer.write('>', '\n'); } else @@ -2774,7 +2785,7 @@ namespace for (unsigned int i = 0; i < depth; ++i) writer.write(indent); writer.write('<', '/'); - writer.write(node.name()); + writer.write(name); writer.write('>', '\n'); } @@ -2802,37 +2813,23 @@ namespace break; case node_pi: - writer.write('<', '?'); - writer.write(node.name()); - if (node.value()[0]) - { - writer.write(' '); - writer.write(node.value()); - } - writer.write('?', '>'); - if ((flags & format_raw) == 0) writer.write('\n'); - break; - case node_declaration: - { writer.write('<', '?'); - writer.write(node.name()); + writer.write(node.name()[0] ? node.name() : default_name); - for (xml_attribute a = node.first_attribute(); a; a = a.next_attribute()) + if (node.type() == node_declaration) + { + node_output_attributes(writer, node); + } + else if (node.value()[0]) { writer.write(' '); - writer.write(a.name()); - writer.write('=', '"'); - - text_output_escaped(writer, a.value(), oct_special_attr); - - writer.write('"'); + writer.write(node.value()); } writer.write('?', '>'); if ((flags & format_raw) == 0) writer.write('\n'); break; - } default: assert(false); diff --git a/tests/test_write.cpp b/tests/test_write.cpp index c94ab89..64e50b5 100644 --- a/tests/test_write.cpp +++ b/tests/test_write.cpp @@ -306,4 +306,32 @@ TEST(write_unicode_invalid_utf8) CHECK(test_write_unicode_invalid("a\xf8_", L"a_")); } +TEST(write_no_name_element) +{ + xml_document doc; + xml_node root = doc.append_child(); + root.append_child(); + root.append_child().append_child(node_pcdata).set_value("text"); + + CHECK_NODE(doc, "<:anonymous><:anonymous /><:anonymous>text"); + CHECK_NODE_EX(doc, "<:anonymous>\n\t<:anonymous />\n\t<:anonymous>text\n\n", "\t", format_default); +} + +TEST(write_no_name_pi) +{ + xml_document doc; + doc.append_child(node_pi); + + CHECK_NODE(doc, ""); +} + +TEST(write_no_name_attribute) +{ + xml_document doc; + doc.append_child().set_name("root"); + doc.child("root").append_attribute(""); + + CHECK_NODE(doc, ""); +} + #endif -- cgit v1.2.3