DOMParser-parseFromString-xml.html (3171B)
1 <!DOCTYPE html> 2 <title>DOMParser</title> 3 <link rel="author" title="Ms2ger" href="mailto:ms2ger@gmail.com"> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <div id="log"></div> 7 <script> 8 function checkMetadata(doc, contentType) { 9 assert_true(doc instanceof Document, "Should be Document"); 10 assert_equals(doc.URL, document.URL, "URL"); 11 assert_equals(doc.documentURI, document.URL, "documentURI"); 12 assert_equals(doc.baseURI, document.URL, "baseURI"); 13 assert_equals(doc.characterSet, "UTF-8", "characterSet"); 14 assert_equals(doc.charset, "UTF-8", "charset"); 15 assert_equals(doc.inputEncoding, "UTF-8", "inputEncoding"); 16 assert_equals(doc.contentType, contentType, "contentType"); 17 assert_equals(doc.location, null, "location"); 18 } 19 20 var allowedTypes = ["text/xml", "application/xml", "application/xhtml+xml", "image/svg+xml"]; 21 22 allowedTypes.forEach(function(type) { 23 test(function() { 24 var p = new DOMParser(); 25 var doc = p.parseFromString("<foo/>", type); 26 assert_true(doc instanceof Document, "Should be Document"); 27 checkMetadata(doc, type); 28 assert_equals(doc.documentElement.namespaceURI, null); 29 assert_equals(doc.documentElement.localName, "foo"); 30 assert_equals(doc.documentElement.tagName, "foo"); 31 }, "Should parse correctly in type " + type); 32 33 test(function() { 34 var p = new DOMParser(); 35 var doc = p.parseFromString("<foo/>", type); 36 assert_false(doc instanceof XMLDocument, "Should not be XMLDocument"); 37 }, "XMLDocument interface for correctly parsed document with type " + type); 38 39 test(function() { 40 var p = new DOMParser(); 41 var doc = p.parseFromString("<foo>", type); 42 checkMetadata(doc, type); 43 assert_equals(doc.documentElement.namespaceURI, "http://www.mozilla.org/newlayout/xml/parsererror.xml"); 44 assert_equals(doc.documentElement.localName, "parsererror"); 45 assert_equals(doc.documentElement.tagName, "parsererror"); 46 }, "Should return an error document for XML wellformedness errors in type " + type); 47 48 test(function() { 49 var p = new DOMParser(); 50 var doc = p.parseFromString("<foo>", type); 51 assert_false(doc instanceof XMLDocument, "Should not be XMLDocument"); 52 }, "XMLDocument interface for incorrectly parsed document with type " + type); 53 54 test(function() { 55 var p = new DOMParser(); 56 var doc = p.parseFromString(` 57 <html> 58 <head></head> 59 <body> 60 <script>document.x = 5;<\/script> 61 <noscript><p>test1</p><p>test2</p></noscript> 62 </body> 63 </html>` 64 , type); 65 66 assert_equals(doc.x, undefined, "script must not be executed on the inner document"); 67 assert_equals(document.x, undefined, "script must not be executed on the outer document"); 68 69 const body = doc.documentElement.children[1]; 70 assert_equals(body.localName, "body"); 71 assert_equals(body.children[1].localName, "noscript"); 72 assert_equals(body.children[1].children.length, 2); 73 assert_equals(body.children[1].children[0].localName, "p"); 74 assert_equals(body.children[1].children[1].localName, "p"); 75 }, "scripting must be disabled with type " + type); 76 }); 77 </script>