tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DOMParser-parseFromString-html.html (3372B)


      1 <!doctype html>
      2 <title>DOMParser basic test of HTML parsing</title>
      3 <script src="/resources/testharness.js"></script>
      4 <script src="/resources/testharnessreport.js"></script>
      5 <script>
      6 // |expected| should be an object indicating the expected type of node.
      7 function assert_node(actual, expected) {
      8    assert_true(actual instanceof expected.type,
      9                'Node type mismatch: actual = ' + actual.constructor.name + ', expected = ' + expected.type.name);
     10    if (typeof(expected.id) !== 'undefined')
     11        assert_equals(actual.id, expected.id, expected.idMessage);
     12 }
     13 
     14 var doc;
     15 setup(function() {
     16    var parser = new DOMParser();
     17    var input = '<html id="root"><head></head><body></body></html>';
     18    doc = parser.parseFromString(input, 'text/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    var parser = new DOMParser();
     37    var input = '<!DOCTYPE html><html id="root"><head></head><body></body></html>';
     38    doc = parser.parseFromString(input, 'text/html');
     39    assert_equals(doc.compatMode, "CSS1Compat")
     40 }, 'compatMode for a proper DOCTYPE');
     41 
     42 // URL- and encoding-related stuff tested separately.
     43 
     44 test(function() {
     45    assert_equals(doc.location, null,
     46                  'The document must have a location value of null.');
     47 }, 'Location value');
     48 
     49 test(function() {
     50    var soup = "<!DOCTYPE foo></><foo></multiple></>";
     51    var htmldoc = new DOMParser().parseFromString(soup, "text/html");
     52    assert_equals(htmldoc.documentElement.localName, "html");
     53    assert_equals(htmldoc.documentElement.namespaceURI, "http://www.w3.org/1999/xhtml");
     54 }, "DOMParser parses HTML tag soup with no problems");
     55 
     56 test(function() {
     57   const doc = new DOMParser().parseFromString('<noembed>&lt;a&gt;</noembed>', 'text/html');
     58   assert_equals(doc.querySelector('noembed').textContent, '&lt;a&gt;');
     59 }, 'DOMParser should handle the content of <noembed> as raw text');
     60 
     61 test(function() {
     62    assert_throws_js(TypeError, function() {
     63        new DOMParser().parseFromString("", "text/foo-this-is-invalid");
     64    })
     65 }, "DOMParser throws on an invalid enum value")
     66 
     67 test(() => {
     68   const doc = new DOMParser().parseFromString(`
     69 <html><body>
     70 <style>
     71  @import url(/dummy.css)
     72 </style>
     73 <script>document.x = 8<\/script>
     74 </body></html>`, 'text/html');
     75 
     76  assert_not_equals(doc.querySelector('script'), null, 'script must be found');
     77  assert_equals(doc.x, undefined, 'script must not be executed on the inner document');
     78  assert_equals(document.x, undefined, 'script must not be executed on the outer document');
     79 }, 'script is found synchronously even when there is a css import');
     80 
     81 test(() => {
     82    const doc = new DOMParser().parseFromString(`<body><noscript><p id="test1">test1<p id="test2">test2</noscript>`, 'text/html');
     83    assert_node(doc.body.firstChild.childNodes[0], { type: HTMLParagraphElement, id: 'test1' });
     84    assert_node(doc.body.firstChild.childNodes[1], { type: HTMLParagraphElement, id: 'test2' });
     85 }, 'must be parsed with scripting disabled, so noscript works');
     86 </script>