Bug 169521: fix XML attribute serialization for proper roundtripping r?bz draft
authorAshish Kulkarni <kulkarni.ashish@gmail.com>
Wed, 24 Jan 2018 16:03:01 +0530
changeset 751225 201df243be00
parent 723950 b0baaec09caf
push id97910
push userbmo:kulkarni.ashish@gmail.com
push dateMon, 05 Feb 2018 20:00:44 +0000
reviewersbz
bugs169521, 418531
milestone60.0a1
Bug 169521: fix XML attribute serialization for proper roundtripping r?bz This is due to incomplete specification, see discussion on Chromium bug https://bugs.chromium.org/p/chromium/issues/detail?id=418531 Behavior is now in line with Edge and Chromium. MozReview-Commit-ID: AxIRtIj5j8r
dom/base/nsXMLContentSerializer.cpp
testing/web-platform/tests/domparsing/XMLSerializer-serializeToString.html
--- a/dom/base/nsXMLContentSerializer.cpp
+++ b/dom/base/nsXMLContentSerializer.cpp
@@ -1204,33 +1204,36 @@ static const uint8_t kEntities[] = {
   _, _, _, _, _, _, _, _, 2, _,
   _, _, _, _, _, _, _, _, _, _,
   _, _, _, _, _, _, _, _, _, _,
   3, _, 4
 };
 
 // This table indexes into kEntityStrings[].
 static const uint8_t kAttrEntities[] = {
-  _, _, _, _, _, _, _, _, _, _,
-  _, _, _, _, _, _, _, _, _, _,
+  _, _, _, _, _, _, _, _, _, 5,
+  6, _, _, 7, _, _, _, _, _, _,
   _, _, _, _, _, _, _, _, _, _,
   _, _, _, _, 1, _, _, _, 2, _,
   _, _, _, _, _, _, _, _, _, _,
   _, _, _, _, _, _, _, _, _, _,
   3, _, 4
 };
 
 #undef _
 
 static const char* const kEntityStrings[] = {
   /* 0 */ nullptr,
   /* 1 */ "&quot;",
   /* 2 */ "&amp;",
   /* 3 */ "&lt;",
   /* 4 */ "&gt;",
+  /* 5 */ "&#9;",
+  /* 6 */ "&#xA;",
+  /* 7 */ "&#xD;",
 };
 
 bool
 nsXMLContentSerializer::AppendAndTranslateEntities(const nsAString& aStr,
                                                    nsAString& aOutputStr)
 {
   nsReadingIterator<char16_t> done_reading;
   aStr.EndReading(done_reading);
--- a/testing/web-platform/tests/domparsing/XMLSerializer-serializeToString.html
+++ b/testing/web-platform/tests/domparsing/XMLSerializer-serializeToString.html
@@ -34,11 +34,20 @@ test(function() {
 }, 'Check if the default namespace is correctly reset.');
 
 test(function() {
   var input = '<root xmlns="urn:bar"><outer xmlns=""><inner>value1</inner></outer></root>';
   var root = (new DOMParser()).parseFromString(input, 'text/xml').documentElement;
   var xmlString = (new XMLSerializer()).serializeToString(root);
   assert_equals(xmlString, '<root xmlns="urn:bar"><outer xmlns=""><inner>value1</inner></outer></root>');
 }, 'Check if there is no redundant empty namespace declaration.');
+
+test(function() {
+  var serializer = new XMLSerializer();
+  var root = createXmlDoc().documentElement;
+  root.firstChild.setAttribute('attr1', 'value1\tvalue2\r\n');
+  var xmlString = serializer.serializeToString(root);
+  assert_equals(xmlString, '<root><child1 attr1="value1&#9;value2&#xD;&#xA;">value1</child1></root>');
+}, 'check XMLSerializer.serializeToString escapes attribute values for roundtripping');
+
 </script>
  </body>
 </html>