mirror of
https://github.com/zeux/pugixml.git
synced 2024-12-27 13:33:17 +08:00
Refactor format_indent_attributes implementation
Fix code style and revert redundant parameters/whitespace changes. Also remove format_each_attribute_on_new_line - we're only introducing one extra formatting flag. The flag implies format_indent but does not include its bitmask. Also add a few more tests. Fixes #14.
This commit is contained in:
parent
950693be7f
commit
2a3435274f
@ -3480,26 +3480,23 @@ PUGI__NS_BEGIN
|
||||
}
|
||||
}
|
||||
|
||||
PUGI__FN void node_output_attributes(xml_buffered_writer &writer, xml_node_struct *node, const char_t *indent, size_t indent_length, unsigned int flags, unsigned int depth)
|
||||
PUGI__FN void node_output_attributes(xml_buffered_writer& writer, xml_node_struct* node, const char_t* indent, size_t indent_length, unsigned int flags, unsigned int depth)
|
||||
{
|
||||
const char_t* default_name = PUGIXML_TEXT(":anonymous");
|
||||
|
||||
bool eachAttributeOnNewLine = PUGI__NODETYPE(node) != node_declaration
|
||||
&& (flags & format_indent)
|
||||
&& (flags & format_each_attribute_on_new_line)
|
||||
&& (flags & format_raw) == 0;
|
||||
|
||||
for (xml_attribute_struct* a = node->first_attribute; a; a = a->next_attribute)
|
||||
{
|
||||
if (eachAttributeOnNewLine)
|
||||
if ((flags & (format_indent_attributes | format_raw)) == format_indent_attributes)
|
||||
{
|
||||
writer.write('\n');
|
||||
|
||||
text_output_indent(writer, indent, indent_length, depth + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.write(' ');
|
||||
}
|
||||
|
||||
writer.write_string(a->name ? a->name : default_name);
|
||||
writer.write('=', '"');
|
||||
|
||||
@ -3510,7 +3507,7 @@ PUGI__NS_BEGIN
|
||||
}
|
||||
}
|
||||
|
||||
PUGI__FN bool node_output_start(xml_buffered_writer &writer, xml_node_struct *node, const char_t *indent, size_t indent_length, unsigned int flags, unsigned int depth)
|
||||
PUGI__FN bool node_output_start(xml_buffered_writer& writer, xml_node_struct* node, const char_t* indent, size_t indent_length, unsigned int flags, unsigned int depth)
|
||||
{
|
||||
const char_t* default_name = PUGIXML_TEXT(":anonymous");
|
||||
const char_t* name = node->name ? node->name : default_name;
|
||||
@ -3524,11 +3521,13 @@ PUGI__NS_BEGIN
|
||||
if (!node->first_child)
|
||||
{
|
||||
writer.write(' ', '/', '>');
|
||||
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.write('>');
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -3543,7 +3542,7 @@ PUGI__NS_BEGIN
|
||||
writer.write('>');
|
||||
}
|
||||
|
||||
PUGI__FN void node_output_simple(xml_buffered_writer &writer, xml_node_struct *node, const char_t *indent, size_t indent_length, unsigned int flags, unsigned int depth)
|
||||
PUGI__FN void node_output_simple(xml_buffered_writer& writer, xml_node_struct* node, unsigned int flags)
|
||||
{
|
||||
const char_t* default_name = PUGIXML_TEXT(":anonymous");
|
||||
|
||||
@ -3577,7 +3576,7 @@ PUGI__NS_BEGIN
|
||||
case node_declaration:
|
||||
writer.write('<', '?');
|
||||
writer.write_string(node->name ? node->name : default_name);
|
||||
node_output_attributes(writer, node, indent, indent_length, flags, depth);
|
||||
node_output_attributes(writer, node, PUGIXML_TEXT(""), 0, flags | format_raw, 0);
|
||||
writer.write('?', '>');
|
||||
break;
|
||||
|
||||
@ -3607,7 +3606,7 @@ PUGI__NS_BEGIN
|
||||
|
||||
PUGI__FN void node_output(xml_buffered_writer& writer, xml_node_struct* root, const char_t* indent, unsigned int flags, unsigned int depth)
|
||||
{
|
||||
size_t indent_length = ((flags & (format_indent | format_raw)) == format_indent) ? strlength(indent) : 0;
|
||||
size_t indent_length = ((flags & (format_indent | format_indent_attributes)) && (flags & format_raw) == 0) ? strlength(indent) : 0;
|
||||
unsigned int indent_flags = indent_indent;
|
||||
|
||||
xml_node_struct* node = root;
|
||||
@ -3619,7 +3618,7 @@ PUGI__NS_BEGIN
|
||||
// begin writing current node
|
||||
if (PUGI__NODETYPE(node) == node_pcdata || PUGI__NODETYPE(node) == node_cdata)
|
||||
{
|
||||
node_output_simple(writer, node, indent, indent_length, flags, depth);
|
||||
node_output_simple(writer, node, flags);
|
||||
|
||||
indent_flags = 0;
|
||||
}
|
||||
@ -3654,7 +3653,7 @@ PUGI__NS_BEGIN
|
||||
}
|
||||
else
|
||||
{
|
||||
node_output_simple(writer, node, indent, indent_length, flags, depth);
|
||||
node_output_simple(writer, node, flags);
|
||||
|
||||
indent_flags = indent_newline | indent_indent;
|
||||
}
|
||||
|
@ -203,12 +203,12 @@ namespace pugi
|
||||
// Open file using text mode in xml_document::save_file. This enables special character (i.e. new-line) conversions on some systems. This flag is off by default.
|
||||
const unsigned int format_save_file_text = 0x20;
|
||||
|
||||
// Set each attribute to new line
|
||||
const unsigned int format_each_attribute_on_new_line = 0x40;
|
||||
// Write every attribute on a new line with appropriate indentation. This flag is off by default.
|
||||
const unsigned int format_indent_attributes = 0x40;
|
||||
|
||||
// The default set of formatting flags.
|
||||
// Nodes are indented depending on their depth in DOM tree, a default declaration is output if document has none.
|
||||
const unsigned int format_default = format_indent;
|
||||
const unsigned int format_indent_attributes = format_indent | format_each_attribute_on_new_line;
|
||||
|
||||
// Forward declarations
|
||||
struct xml_attribute_struct;
|
||||
|
@ -21,21 +21,31 @@ TEST_XML(write_indent, "<node attr='1'><child><sub>text</sub></child></node>")
|
||||
CHECK_NODE_EX(doc, STR("<node attr=\"1\">\n\t<child>\n\t\t<sub>text</sub>\n\t</child>\n</node>\n"), STR("\t"), format_indent);
|
||||
}
|
||||
|
||||
TEST_XML(write_indent_attribute, "<node attr='1' other='2'><child><sub>text</sub></child></node>")
|
||||
TEST_XML(write_indent_attributes, "<node attr='1' other='2'><child><sub>text</sub></child></node>")
|
||||
{
|
||||
CHECK_NODE_EX(doc, STR("<node\n\tattr=\"1\"\n\tother=\"2\">\n\t<child>\n\t\t<sub>text</sub>\n\t</child>\n</node>\n"), STR("\t"), format_indent_attributes);
|
||||
}
|
||||
|
||||
TEST_XML(write_indent_attribute_empty_tag, "<node attr='1' other='2' />")
|
||||
TEST_XML(write_indent_attributes_empty_element, "<node attr='1' other='2' />")
|
||||
{
|
||||
CHECK_NODE_EX(doc, STR("<node\n\tattr=\"1\"\n\tother=\"2\" />\n"), STR("\t"), format_indent_attributes);
|
||||
}
|
||||
|
||||
TEST_XML_FLAGS(write_indent_attribute_on_declaration, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><node attr='1' other='2' />", pugi::parse_full)
|
||||
TEST_XML_FLAGS(write_indent_attributes_declaration, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><node attr='1' other='2' />", parse_full)
|
||||
{
|
||||
CHECK_NODE_EX(doc, STR("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<node\n\tattr=\"1\"\n\tother=\"2\" />\n"), STR("\t"), format_indent_attributes);
|
||||
}
|
||||
|
||||
TEST_XML(write_indent_attributes_raw, "<node attr='1' other='2'><child><sub>text</sub></child></node>")
|
||||
{
|
||||
CHECK_NODE_EX(doc, STR("<node attr=\"1\" other=\"2\"><child><sub>text</sub></child></node>"), STR("\t"), format_indent_attributes | format_raw);
|
||||
}
|
||||
|
||||
TEST_XML(write_indent_attributes_empty_indent, "<node attr='1' other='2'><child><sub>text</sub></child></node>")
|
||||
{
|
||||
CHECK_NODE_EX(doc, STR("<node\nattr=\"1\"\nother=\"2\">\n<child>\n<sub>text</sub>\n</child>\n</node>\n"), STR(""), format_indent_attributes);
|
||||
}
|
||||
|
||||
TEST_XML(write_pcdata, "<node attr='1'><child><sub/>text</child></node>")
|
||||
{
|
||||
CHECK_NODE_EX(doc, STR("<node attr=\"1\">\n\t<child>\n\t\t<sub />text</child>\n</node>\n"), STR("\t"), format_indent);
|
||||
|
Loading…
x
Reference in New Issue
Block a user