Document-parseHTMLUnsafe.html (3098B)
1 <!doctype html> 2 <link rel=author href="mailto:jarhar@chromium.org"> 3 <!-- This was adapted from DOMParser-parseFromString-html.html --> 4 <title>parseHTMLUnsafe basic test of HTML parsing</title> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script> 8 // |expected| should be an object indicating the expected type of node. 9 function assert_node(actual, expected) { 10 assert_true(actual instanceof expected.type, 11 'Node type mismatch: actual = ' + actual.constructor.name + ', expected = ' + expected.type.name); 12 if (typeof(expected.id) !== 'undefined') 13 assert_equals(actual.id, expected.id, expected.idMessage); 14 } 15 16 var doc; 17 setup(function() { 18 doc = Document.parseHTMLUnsafe('<html id="root"><head></head><body></body></html>'); 19 }); 20 21 test(function() { 22 var root = doc.documentElement; 23 assert_node(root, { type: HTMLHtmlElement, id: 'root', 24 idMessage: 'documentElement id attribute should be root.' }); 25 }, 'Parsing of id attribute'); 26 27 test(function() { 28 assert_equals(doc.contentType, "text/html") 29 }, 'contentType'); 30 31 test(function() { 32 assert_equals(doc.compatMode, "BackCompat") 33 }, 'compatMode'); 34 35 test(function() { 36 doc = Document.parseHTMLUnsafe('<!DOCTYPE html><html id="root"><head></head><body></body></html>'); 37 assert_equals(doc.compatMode, "CSS1Compat") 38 }, 'compatMode for a proper DOCTYPE'); 39 40 // URL- and encoding-related stuff tested separately. 41 42 test(function() { 43 assert_equals(doc.location, null, 44 'The document must have a location value of null.'); 45 }, 'Location value'); 46 47 test(function() { 48 var htmldoc = Document.parseHTMLUnsafe("<!DOCTYPE foo></><foo></multiple></>"); 49 assert_equals(htmldoc.documentElement.localName, "html"); 50 assert_equals(htmldoc.documentElement.namespaceURI, "http://www.w3.org/1999/xhtml"); 51 }, "Document.parseHTMLUnsafe parses HTML tag soup with no problems"); 52 53 test(function() { 54 const doc = Document.parseHTMLUnsafe('<noembed><a></noembed>'); 55 assert_equals(doc.querySelector('noembed').textContent, '<a>'); 56 }, 'Document.parseHTMLUnsafe should handle the content of <noembed> as raw text'); 57 58 test(() => { 59 const doc = Document.parseHTMLUnsafe(` 60 <html><body> 61 <style> 62 @import url(/dummy.css) 63 </style> 64 <script>document.x = 8<\/script> 65 </body></html>`); 66 67 assert_not_equals(doc.querySelector('script'), null, 'script must be found'); 68 assert_equals(doc.x, undefined, 'script must not be executed on the inner document'); 69 assert_equals(document.x, undefined, 'script must not be executed on the outer document'); 70 }, 'script is found synchronously even when there is a css import'); 71 72 test(() => { 73 const doc = Document.parseHTMLUnsafe(`<body><noscript><p id="test1">test1<p id="test2">test2</noscript>`); 74 assert_node(doc.body.firstChild.childNodes[0], { type: HTMLParagraphElement, id: 'test1' }); 75 assert_node(doc.body.firstChild.childNodes[1], { type: HTMLParagraphElement, id: 'test2' }); 76 }, 'must be parsed with scripting disabled, so noscript works'); 77 </script>