| <!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 parser = new DOMParser(); |
| var root = parser.parseFromString('<root />', 'text/xml').documentElement; |
| root.setAttribute('attr', '\t'); |
| assert_in_array(serializer.serializeToString(root), [ |
| '<root attr="	"/>', '<root attr="	"/>']); |
| root.setAttribute('attr', '\n'); |
| assert_in_array(serializer.serializeToString(root), [ |
| '<root attr="
"/>', '<root attr=" "/>']); |
| root.setAttribute('attr', '\r'); |
| assert_in_array(serializer.serializeToString(root), [ |
| '<root attr="
"/>', '<root attr=" "/>']); |
| }, 'check XMLSerializer.serializeToString escapes attribute values for roundtripping'); |
| |
| test(function() { |
| const input = '<root><child1/><child2/></root>'; |
| const root = (new DOMParser()).parseFromString(input, 'text/xml').documentElement; |
| root.firstChild.setAttributeNS('uri1', 'attr1', 'value1'); |
| root.firstChild.setAttributeNS('uri2', 'attr2', 'value2'); |
| root.lastChild.setAttributeNS('uri3', 'attr3', 'value3'); |
| const xmlString = (new XMLSerializer()).serializeToString(root); |
| assert_equals(xmlString, '<root><child1 xmlns:ns1="uri1" ns1:attr1="value1" xmlns:ns2="uri2" ns2:attr2="value2"/><child2 xmlns:ns3="uri3" ns3:attr3="value3"/></root>'); |
| }, 'Check if generated prefixes match to "ns${index}".'); |
| |
| </script> |
| </body> |
| </html> |