XMLSerializer-serializeToString.html (12979B)
1 <!DOCTYPE HTML> 2 <meta charset=utf-8> 3 <html> 4 <head> 5 <title>domparsing Test: XMLSerializer.serializeToString</title> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 </head> 9 <body> 10 <h1>domparsing_XMLSerializer_serializeToString</h1> 11 <script> 12 const XMLNS_URI = 'http://www.w3.org/2000/xmlns/'; 13 14 function createXmlDoc(){ 15 var input = '<?xml version="1.0" encoding="UTF-8"?><root><child1>value1</child1></root>'; 16 var parser = new DOMParser(); 17 return parser.parseFromString(input, 'text/xml'); 18 } 19 20 // Returns the root element. 21 function parse(xmlString) { 22 return (new DOMParser()).parseFromString(xmlString, 'text/xml').documentElement; 23 } 24 25 function serialize(node) { 26 return (new XMLSerializer()).serializeToString(node); 27 } 28 29 test(function() { 30 var root = createXmlDoc().documentElement; 31 assert_equals(serialize(root), '<root><child1>value1</child1></root>'); 32 }, 'check XMLSerializer.serializeToString method could parsing xmldoc to string'); 33 34 test(function() { 35 var root = parse('<html><head></head><body><div></div><span></span></body></html>'); 36 assert_equals(serialize(root.ownerDocument), '<html><head/><body><div/><span/></body></html>'); 37 }, 'check XMLSerializer.serializeToString method could parsing document to string'); 38 39 test(function() { 40 var root = createXmlDoc().documentElement; 41 var element = root.ownerDocument.createElementNS('urn:foo', 'another'); 42 var child1 = root.firstChild; 43 root.replaceChild(element, child1); 44 element.appendChild(child1); 45 assert_equals(serialize(root), '<root><another xmlns="urn:foo"><child1 xmlns="">value1</child1></another></root>'); 46 }, 'Check if the default namespace is correctly reset.'); 47 48 test(function() { 49 var root = parse('<root xmlns="urn:bar"><outer xmlns=""><inner>value1</inner></outer></root>'); 50 assert_equals(serialize(root), '<root xmlns="urn:bar"><outer xmlns=""><inner>value1</inner></outer></root>'); 51 }, 'Check if there is no redundant empty namespace declaration.'); 52 53 // https://github.com/w3c/DOM-Parsing/issues/47 54 test(function() { 55 assert_equals(serialize(parse('<root><child xmlns=""/></root>')), 56 '<root><child/></root>'); 57 assert_equals(serialize(parse('<root xmlns=""><child xmlns=""/></root>')), 58 '<root><child/></root>'); 59 assert_equals(serialize(parse('<root xmlns="u1"><child xmlns="u1"/></root>')), 60 '<root xmlns="u1"><child/></root>'); 61 }, 'Check if redundant xmlns="..." is dropped.'); 62 63 test(function() { 64 const root = parse('<root xmlns="uri1"/>'); 65 const child = root.ownerDocument.createElement('child'); 66 child.setAttributeNS(XMLNS_URI, 'xmlns', 'FAIL1'); 67 root.appendChild(child); 68 const child2 = root.ownerDocument.createElementNS('uri2', 'child2'); 69 child2.setAttributeNS(XMLNS_URI, 'xmlns', 'FAIL2'); 70 root.appendChild(child2); 71 const child3 = root.ownerDocument.createElementNS('uri1', 'child3'); 72 child3.setAttributeNS(XMLNS_URI, 'xmlns', 'FAIL3'); 73 root.appendChild(child3); 74 const child4 = root.ownerDocument.createElementNS('uri4', 'child4'); 75 child4.setAttributeNS(XMLNS_URI, 'xmlns', 'uri4'); 76 root.appendChild(child4); 77 const child5 = root.ownerDocument.createElement('child5'); 78 child5.setAttributeNS(XMLNS_URI, 'xmlns', ''); 79 root.appendChild(child5); 80 assert_equals(serialize(root), '<root xmlns="uri1"><child xmlns=""/><child2 xmlns="uri2"/><child3/><child4 xmlns="uri4"/><child5 xmlns=""/></root>'); 81 }, 'Check if inconsistent xmlns="..." is dropped.'); 82 83 test(function() { 84 const root1 = parse('<package></package>'); 85 root1.setAttribute('xmlns', 'http://www.idpf.org/2007/opf'); 86 const manifest1 = root1.appendChild(root1.ownerDocument.createElement('manifest')); 87 manifest1.setAttribute('xmlns', 'http://www.idpf.org/2007/opf'); 88 assert_equals(serialize(root1), '<package><manifest/></package>'); 89 90 const root2 = parse('<package xmlns="http://www.idpf.org/2007/opf"></package>'); 91 const manifest2 = root2.appendChild(root2.ownerDocument.createElement('manifest')); 92 manifest2.setAttribute('xmlns', 'http://www.idpf.org/2007/opf'); 93 assert_equals(serialize(root2), 94 '<package xmlns="http://www.idpf.org/2007/opf"><manifest xmlns=""/></package>'); 95 96 const root3 = parse('<package xmlns="http://www.idpf.org/2007/opf"></package>'); 97 const manifest3 = root3.appendChild(root3.ownerDocument.createElement('manifest')); 98 assert_equals(serialize(root3), 99 '<package xmlns="http://www.idpf.org/2007/opf"><manifest xmlns=""/></package>'); 100 }, 'Drop inconsistent xmlns="..." by matching on local name'); 101 102 test(function() { 103 let root = parse('<r xmlns:xx="uri"></r>'); 104 root.setAttributeNS('uri', 'name', 'v'); 105 assert_equals(serialize(root), '<r xmlns:xx="uri" xx:name="v"/>'); 106 107 let root2 = parse('<r xmlns:xx="uri"><b/></r>'); 108 let child = root2.firstChild; 109 child.setAttributeNS('uri', 'name', 'v'); 110 assert_equals(serialize(root2), '<r xmlns:xx="uri"><b xx:name="v"/></r>'); 111 112 let root3 = parse('<r xmlns:x0="uri" xmlns:x2="uri"><b xmlns:x1="uri"/></r>'); 113 let child3 = root3.firstChild; 114 child3.setAttributeNS('uri', 'name', 'v'); 115 assert_equals(serialize(root3), 116 '<r xmlns:x0="uri" xmlns:x2="uri"><b xmlns:x1="uri" x1:name="v"/></r>', 117 'Should choose the nearest prefix'); 118 }, 'Check if an attribute with namespace and no prefix is serialized with the nearest-declared prefix'); 119 120 // https://github.com/w3c/DOM-Parsing/issues/45 121 test(function() { 122 let root = parse('<el1 xmlns:p="u1" xmlns:q="u1"><el2 xmlns:q="u2"/></el1>'); 123 root.firstChild.setAttributeNS('u1', 'name', 'v'); 124 assert_equals(serialize(root), 125 '<el1 xmlns:p="u1" xmlns:q="u1"><el2 xmlns:q="u2" q:name="v"/></el1>'); 126 }, 'Check if an attribute with namespace and no prefix is serialized with the nearest-declared prefix even if the prefix is assigned to another namespace.'); 127 128 test(function() { 129 let root = parse('<r xmlns:xx="uri"></r>'); 130 root.setAttributeNS('uri', 'p:name', 'v'); 131 assert_equals(serialize(root), '<r xmlns:xx="uri" xx:name="v"/>'); 132 133 let root2 = parse('<r xmlns:xx="uri"><b/></r>'); 134 let child = root2.firstChild; 135 child.setAttributeNS('uri', 'p:name', 'value'); 136 assert_equals(serialize(root2), 137 '<r xmlns:xx="uri"><b xx:name="value"/></r>'); 138 }, 'Check if the prefix of an attribute is replaced with another existing prefix mapped to the same namespace URI.'); 139 140 // https://github.com/w3c/DOM-Parsing/issues/29 141 test(function() { 142 let root = parse('<r xmlns:xx="uri"></r>'); 143 root.setAttributeNS('uri2', 'p:name', 'value'); 144 assert_equals(serialize(root), 145 '<r xmlns:xx="uri" xmlns:ns1="uri2" ns1:name="value"/>'); 146 }, 'Check if the prefix of an attribute is NOT preserved in a case where neither its prefix nor its namespace URI is not already used.'); 147 148 test(function() { 149 let root = parse('<r xmlns:xx="uri"></r>'); 150 root.setAttributeNS('uri2', 'xx:name', 'value'); 151 assert_equals(serialize(root), 152 '<r xmlns:xx="uri" xmlns:ns1="uri2" ns1:name="value"/>'); 153 }, 'Check if the prefix of an attribute is replaced with a generated one in a case where the prefix is already mapped to a different namespace URI.'); 154 155 test(function() { 156 var root = parse('<root />'); 157 root.setAttribute('attr', '\t'); 158 assert_in_array(serialize(root), [ 159 '<root attr="	"/>', '<root attr="	"/>']); 160 root.setAttribute('attr', '\n'); 161 assert_in_array(serialize(root), [ 162 '<root attr="
"/>', '<root attr=" "/>']); 163 root.setAttribute('attr', '\r'); 164 assert_in_array(serialize(root), [ 165 '<root attr="
"/>', '<root attr=" "/>']); 166 }, 'check XMLSerializer.serializeToString escapes attribute values for roundtripping'); 167 168 test(function() { 169 const root = (new Document()).createElement('root'); 170 root.setAttributeNS('uri1', 'p:foobar', 'value1'); 171 root.setAttributeNS(XMLNS_URI, 'xmlns:p', 'uri2'); 172 assert_equals(serialize(root), '<root xmlns:ns1="uri1" ns1:foobar="value1" xmlns:p="uri2"/>'); 173 }, 'Check if attribute serialization takes into account of following xmlns:* attributes'); 174 175 test(function() { 176 const root = parse('<root xmlns:p="uri1"><child/></root>'); 177 root.firstChild.setAttributeNS('uri2', 'p:foobar', 'v'); 178 assert_equals(serialize(root), '<root xmlns:p="uri1"><child xmlns:ns1="uri2" ns1:foobar="v"/></root>'); 179 }, 'Check if attribute serialization takes into account of the same prefix declared in an ancestor element'); 180 181 test(function() { 182 assert_equals(serialize(parse('<root><child/></root>')), '<root><child/></root>'); 183 assert_equals(serialize(parse('<root xmlns="u1"><p:child xmlns:p="u1"/></root>')), '<root xmlns="u1"><child xmlns:p="u1"/></root>'); 184 }, 'Check if start tag serialization drops element prefix if the namespace is same as inherited default namespace.'); 185 186 test(function() { 187 const root = parse('<root xmlns:p1="u1"><child xmlns:p2="u1"/></root>'); 188 const child2 = root.ownerDocument.createElementNS('u1', 'child2'); 189 root.firstChild.appendChild(child2); 190 assert_equals(serialize(root), '<root xmlns:p1="u1"><child xmlns:p2="u1"><p2:child2/></child></root>'); 191 }, 'Check if start tag serialization finds an appropriate prefix.'); 192 193 test(function() { 194 const root = (new Document()).createElementNS('uri1', 'p:root'); 195 root.setAttributeNS(XMLNS_URI, 'xmlns:p', 'uri2'); 196 assert_equals(serialize(root), '<ns1:root xmlns:ns1="uri1" xmlns:p="uri2"/>'); 197 }, 'Check if start tag serialization takes into account of its xmlns:* attributes'); 198 199 test(function() { 200 const root = (new Document()).createElement('root'); 201 root.setAttributeNS(XMLNS_URI, 'xmlns:p', 'uri2'); 202 const child = root.ownerDocument.createElementNS('uri1', 'p:child'); 203 root.appendChild(child); 204 assert_equals(serialize(root), '<root xmlns:p="uri2"><p:child xmlns:p="uri1"/></root>'); 205 }, 'Check if start tag serialization applied the original prefix even if it is declared in an ancestor element.'); 206 207 // https://github.com/w3c/DOM-Parsing/issues/52 208 test(function() { 209 assert_equals(serialize(parse('<root xmlns:x="uri1"><table xmlns="uri1"></table></root>')), 210 '<root xmlns:x="uri1"><x:table xmlns="uri1"/></root>'); 211 }, 'Check if start tag serialization does NOT apply the default namespace if its namespace is declared in an ancestor.'); 212 213 test(function() { 214 const root = parse('<root><child1/><child2/></root>'); 215 root.firstChild.setAttributeNS('uri1', 'attr1', 'value1'); 216 root.firstChild.setAttributeNS('uri2', 'attr2', 'value2'); 217 root.lastChild.setAttributeNS('uri3', 'attr3', 'value3'); 218 assert_equals(serialize(root), '<root><child1 xmlns:ns1="uri1" ns1:attr1="value1" xmlns:ns2="uri2" ns2:attr2="value2"/><child2 xmlns:ns3="uri3" ns3:attr3="value3"/></root>'); 219 }, 'Check if generated prefixes match to "ns${index}".'); 220 221 // https://github.com/w3c/DOM-Parsing/issues/44 222 // According to 'DOM Parsing and Serialization' draft as of 2018-12-11, 223 // 'generate a prefix' result can conflict with an existing xmlns:ns* declaration. 224 test(function() { 225 const root = parse('<root xmlns:ns2="uri2"><child xmlns:ns1="uri1"/></root>'); 226 root.firstChild.setAttributeNS('uri3', 'attr1', 'value1'); 227 assert_equals(serialize(root), '<root xmlns:ns2="uri2"><child xmlns:ns1="uri1" xmlns:ns1="uri3" ns1:attr1="value1"/></root>'); 228 }, 'Check if "ns1" is generated even if the element already has xmlns:ns1.'); 229 230 test(function() { 231 const root = (new Document()).createElement('root'); 232 root.setAttributeNS('http://www.w3.org/1999/xlink', 'href', 'v'); 233 assert_equals(serialize(root), '<root xmlns:ns1="http://www.w3.org/1999/xlink" ns1:href="v"/>'); 234 235 const root2 = (new Document()).createElement('root'); 236 root2.setAttributeNS('http://www.w3.org/1999/xlink', 'xl:type', 'v'); 237 assert_equals(serialize(root2), '<root xmlns:xl="http://www.w3.org/1999/xlink" xl:type="v"/>'); 238 }, 'Check if no special handling for XLink namespace unlike HTML serializer.'); 239 240 test(function() { 241 var root = new DocumentFragment; 242 root.append(document.createElement('div')); 243 root.append(document.createElement('span')); 244 assert_equals(serialize(root), '<div xmlns="http://www.w3.org/1999/xhtml"></div><span xmlns="http://www.w3.org/1999/xhtml"></span>'); 245 }, 'Check if document fragment serializes.'); 246 247 test(function () { 248 const root = document.createElement("img"); 249 root.append(document.createElement("style")); 250 root.append(document.createElement("style")); 251 assert_equals(serialize(root), '<img xmlns=\"http://www.w3.org/1999/xhtml\"><style></style><style></style></img>'); 252 }, 'Check children were included for void elements'); 253 254 test(function () { 255 const root = parse('<root xmlns="" xmlns:foo="urn:bar"/>'); 256 root.setAttributeNS(XMLNS_URI, 'xmlns:foo', ''); 257 assert_equals(serialize(root), '<root xmlns="" xmlns:foo=""/>'); 258 }, 'Check if a prefix bound to an empty namespace URI ("no namespace") serialize'); 259 260 test(function() { 261 assert_equals(serialize(document.createAttribute("foobar")), "") 262 }, 'Attribute nodes are serialized as the empty string') 263 </script> 264 </body> 265 </html>