From 946de603b13514a28fdb4f3beaf80ebd0d27b5f3 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 11 Sep 2019 21:27:20 -0700 Subject: [PATCH] Don't escape attribute quotation symbol When using double quotes for attributes, we don't need to escape '; when using single quotes, we don't need to escape ". This changes behavior to match 1.9 by default (where we don't escape '). Contributes to #272. --- src/pugixml.cpp | 10 ++++++++-- tests/test_write.cpp | 6 ++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/pugixml.cpp b/src/pugixml.cpp index a321c14..afe2321 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -3930,11 +3930,17 @@ PUGI__NS_BEGIN ++s; break; case '"': - writer.write('&', 'q', 'u', 'o', 't', ';'); + if (flags & format_attribute_single_quote) + writer.write('"'); + else + writer.write('&', 'q', 'u', 'o', 't', ';'); ++s; break; case '\'': - writer.write('&', 'a', 'p', 'o', 's', ';'); + if (flags & format_attribute_single_quote) + writer.write('&', 'a', 'p', 'o', 's', ';'); + else + writer.write('\''); ++s; break; default: // s is not a usual symbol diff --git a/tests/test_write.cpp b/tests/test_write.cpp index 9a0ed06..797ddd0 100644 --- a/tests/test_write.cpp +++ b/tests/test_write.cpp @@ -193,7 +193,8 @@ TEST_XML(write_escape, "text") doc.child(STR("node")).attribute(STR("attr")) = STR("<>'\"&\x04\r\n\t"); doc.child(STR("node")).first_child().set_value(STR("<>'\"&\x04\r\n\t")); - CHECK_NODE(doc, STR("<>'\"&\r\n\t")); + CHECK_NODE(doc, STR("<>'\"&\r\n\t")); + CHECK_NODE_EX(doc, STR("<>'\"&\r\n\t"), STR(""), format_raw | format_attribute_single_quote); } TEST_XML(write_escape_roundtrip, "text") @@ -207,7 +208,8 @@ TEST_XML(write_escape_roundtrip, "text") // Note: this string is almost identical to the string from write_escape with the exception of \r // \r in PCDATA doesn't roundtrip because it has to go through newline conversion (which could be disabled, but is active by default) - CHECK_NODE(doc, STR("<>'\"&\n\t")); + CHECK_NODE(doc, STR("<>'\"&\n\t")); + CHECK_NODE_EX(doc, STR("<>'\"&\n\t"), STR(""), format_raw | format_attribute_single_quote); } TEST_XML(write_escape_unicode, "")