Add support for using single quotes to enclose attribute values

This commit is contained in:
Maximilian Naumann 2019-06-07 11:30:10 -07:00
parent 7247a823b7
commit 14cccfad2c
3 changed files with 7 additions and 3 deletions

View File

@ -4064,6 +4064,7 @@ 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"); const char_t* default_name = PUGIXML_TEXT(":anonymous");
const char_t enquotation_char = (flags & format_attribute_single_quote) ? '\'' : '"';
for (xml_attribute_struct* a = node->first_attribute; a; a = a->next_attribute) for (xml_attribute_struct* a = node->first_attribute; a; a = a->next_attribute)
{ {
@ -4079,12 +4080,12 @@ PUGI__NS_BEGIN
} }
writer.write_string(a->name ? a->name + 0 : default_name); writer.write_string(a->name ? a->name + 0 : default_name);
writer.write('=', '"'); writer.write('=', enquotation_char);
if (a->value) if (a->value)
text_output(writer, a->value, ctx_special_attr, flags); text_output(writer, a->value, ctx_special_attr, flags);
writer.write('"'); writer.write(enquotation_char);
} }
} }

View File

@ -255,6 +255,9 @@ namespace pugi
// Skip characters belonging to range [0; 32) instead of "&#xNN;" encoding. This flag is off by default. // Skip characters belonging to range [0; 32) instead of "&#xNN;" encoding. This flag is off by default.
const unsigned int format_skip_control_chars = 0x100; const unsigned int format_skip_control_chars = 0x100;
// Use single quotes ' instead of double quotes " for enclosing attribute values. This flag is off by default.
const unsigned int format_attribute_single_quote = 0x200;
// The default set of formatting flags. // 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. // 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_default = format_indent;

View File

@ -632,7 +632,7 @@ TEST_XML(write_no_empty_element_tags, "<node><child1/><child2>text</child2><chil
TEST_XML_FLAGS(write_roundtrip, "<node><child1 attr1='value1' attr2='value2'/><child2 attr='value'>pre<![CDATA[data]]>mid&lt;text&amp;escape<!--comment--><test/>post<?pi value?>fin</child2><child3/></node>", parse_full) TEST_XML_FLAGS(write_roundtrip, "<node><child1 attr1='value1' attr2='value2'/><child2 attr='value'>pre<![CDATA[data]]>mid&lt;text&amp;escape<!--comment--><test/>post<?pi value?>fin</child2><child3/></node>", parse_full)
{ {
const unsigned int flagset[] = { format_indent, format_raw, format_no_declaration, format_indent_attributes, format_no_empty_element_tags }; const unsigned int flagset[] = { format_indent, format_raw, format_no_declaration, format_indent_attributes, format_no_empty_element_tags, format_attribute_single_quote };
size_t flagcount = sizeof(flagset) / sizeof(flagset[0]); size_t flagcount = sizeof(flagset) / sizeof(flagset[0]);
for (size_t i = 0; i < (size_t(1) << flagcount); ++i) for (size_t i = 0; i < (size_t(1) << flagcount); ++i)