testing/web-platform/tests/domparsing/XMLSerializer-serializeToString.html
author Ashish Kulkarni <kulkarni.ashish@gmail.com>
Wed, 24 Jan 2018 16:03:01 +0530
changeset 402421 fe4514c84e7ad28e46bad5da93381deb99b177f3
parent 388141 f95a678186b46649eee6cf8c6adbdf46c320f300
child 404301 eca0907980d2769c449894a6277c60c1a306792f
permissions -rw-r--r--
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

<!DOCTYPE HTML>
<meta charset=utf-8>
<html>
 <head>
  <title>domparsing Test: XMLSerializer.serializeToString</title>
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
 </head>
 <body>
    <h1>domparsing_XMLSerializer_serializeToString</h1>
  <script>
function createXmlDoc(){
  var input = '<?xml version="1.0" encoding="UTF-8"?><root><child1>value1</child1></root>';
  var parser = new DOMParser();
  return parser.parseFromString(input, 'text/xml');
}

test(function() {
  var serializer = new XMLSerializer();
  var root = createXmlDoc().documentElement;
  var xmlString = serializer.serializeToString(root);
  assert_equals(xmlString, '<root><child1>value1</child1></root>');
}, 'check XMLSerializer.serializeToString method could parsing xmldoc to string');

test(function() {
  var serializer = new XMLSerializer();
  var root = createXmlDoc().documentElement;
  var element = root.ownerDocument.createElementNS('urn:foo', 'another');
  var child1 = root.firstChild;
  root.replaceChild(element, child1);
  element.appendChild(child1);
  var xmlString = serializer.serializeToString(root);
  assert_equals(xmlString, '<root><another xmlns="urn:foo"><child1 xmlns="">value1</child1></another></root>');
}, '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>