tor-browser

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

Node-insertBefore.html (13046B)


      1 <!DOCTYPE html>
      2 <title>Node.insertBefore</title>
      3 <script src="/resources/testharness.js"></script>
      4 <script src="/resources/testharnessreport.js"></script>
      5 <div id="log"></div>
      6 <!-- First test shared pre-insertion checks that work similarly for replaceChild
      7     and insertBefore -->
      8 <script>
      9  var insertFunc = Node.prototype.insertBefore;
     10 </script>
     11 <script src="pre-insertion-validation-notfound.js"></script>
     12 <script src="pre-insertion-validation-hierarchy.js"></script>
     13 <script>
     14 preInsertionValidateHierarchy("insertBefore");
     15 
     16 function testLeafNode(nodeName, createNodeFunction) {
     17  test(function() {
     18    var node = createNodeFunction();
     19    assert_throws_js(TypeError, function() { node.insertBefore(null, null) })
     20  }, "Calling insertBefore with a non-Node first argument on a leaf node " + nodeName + " must throw TypeError.")
     21  test(function() {
     22    var node = createNodeFunction();
     23    assert_throws_dom("HIERARCHY_REQUEST_ERR", function() { node.insertBefore(document.createTextNode("fail"), null) })
     24    // Would be step 2.
     25    assert_throws_dom("HIERARCHY_REQUEST_ERR", function() { node.insertBefore(node, null) })
     26    // Would be step 3.
     27    assert_throws_dom("HIERARCHY_REQUEST_ERR", function() { node.insertBefore(node, document.createTextNode("child")) })
     28  }, "Calling insertBefore an a leaf node " + nodeName + " must throw HIERARCHY_REQUEST_ERR.")
     29 }
     30 
     31 test(function() {
     32  // WebIDL: first argument.
     33  assert_throws_js(TypeError, function() { document.body.insertBefore(null, null) })
     34  assert_throws_js(TypeError, function() { document.body.insertBefore(null, document.body.firstChild) })
     35  assert_throws_js(TypeError, function() { document.body.insertBefore({'a':'b'}, document.body.firstChild) })
     36 }, "Calling insertBefore with a non-Node first argument must throw TypeError.")
     37 
     38 test(function() {
     39  // WebIDL: second argument.
     40  assert_throws_js(TypeError, function() { document.body.insertBefore(document.createTextNode("child")) })
     41  assert_throws_js(TypeError, function() { document.body.insertBefore(document.createTextNode("child"), {'a':'b'}) })
     42 }, "Calling insertBefore with second argument missing, or other than Node, null, or undefined, must throw TypeError.")
     43 
     44 testLeafNode("DocumentType", function () { return document.doctype; } )
     45 testLeafNode("Text", function () { return document.createTextNode("Foo") })
     46 testLeafNode("Comment", function () { return document.createComment("Foo") })
     47 testLeafNode("ProcessingInstruction", function () { return document.createProcessingInstruction("foo", "bar") })
     48 
     49 test(function() {
     50  // Step 2.
     51  assert_throws_dom("HIERARCHY_REQUEST_ERR", function() { document.body.insertBefore(document.body, document.getElementById("log")) })
     52  assert_throws_dom("HIERARCHY_REQUEST_ERR", function() { document.body.insertBefore(document.documentElement, document.getElementById("log")) })
     53 }, "Calling insertBefore with an inclusive ancestor of the context object must throw HIERARCHY_REQUEST_ERR.")
     54 
     55 // Step 3.
     56 test(function() {
     57  var a = document.createElement("div");
     58  var b = document.createElement("div");
     59  var c = document.createElement("div");
     60  assert_throws_dom("NotFoundError", function() {
     61    a.insertBefore(b, c);
     62  });
     63 }, "Calling insertBefore with a reference child whose parent is not the context node must throw a NotFoundError.")
     64 
     65 // Step 4.1.
     66 test(function() {
     67  var doc = document.implementation.createHTMLDocument("title");
     68  var doc2 = document.implementation.createHTMLDocument("title2");
     69  assert_throws_dom("HierarchyRequestError", function() {
     70    doc.insertBefore(doc2, doc.documentElement);
     71  });
     72 
     73  assert_throws_dom("HierarchyRequestError", function() {
     74    doc.insertBefore(doc.createTextNode("text"), doc.documentElement);
     75  });
     76 }, "If the context node is a document, inserting a document or text node should throw a HierarchyRequestError.")
     77 
     78 // Step 4.2.1.
     79 test(function() {
     80  var doc = document.implementation.createHTMLDocument("title");
     81  doc.removeChild(doc.documentElement);
     82 
     83  var df = doc.createDocumentFragment();
     84  df.appendChild(doc.createElement("a"));
     85  df.appendChild(doc.createElement("b"));
     86  assert_throws_dom("HierarchyRequestError", function() {
     87    doc.insertBefore(df, doc.firstChild);
     88  });
     89 
     90  df = doc.createDocumentFragment();
     91  df.appendChild(doc.createTextNode("text"));
     92  assert_throws_dom("HierarchyRequestError", function() {
     93    doc.insertBefore(df, doc.firstChild);
     94  });
     95 
     96  df = doc.createDocumentFragment();
     97  df.appendChild(doc.createComment("comment"));
     98  df.appendChild(doc.createTextNode("text"));
     99  assert_throws_dom("HierarchyRequestError", function() {
    100    doc.insertBefore(df, doc.firstChild);
    101  });
    102 }, "If the context node is a document, inserting a DocumentFragment that contains a text node or too many elements should throw a HierarchyRequestError.")
    103 
    104 // Step 4.2.2.
    105 test(function() {
    106  // The context node has an element child.
    107  var doc = document.implementation.createHTMLDocument("title");
    108  var comment = doc.appendChild(doc.createComment("foo"));
    109  assert_array_equals(doc.childNodes, [doc.doctype, doc.documentElement, comment]);
    110 
    111  var df = doc.createDocumentFragment();
    112  df.appendChild(doc.createElement("a"));
    113  assert_throws_dom("HierarchyRequestError", function() {
    114    doc.insertBefore(df, doc.doctype);
    115  });
    116  assert_throws_dom("HierarchyRequestError", function() {
    117    doc.insertBefore(df, doc.documentElement);
    118  });
    119  assert_throws_dom("HierarchyRequestError", function() {
    120    doc.insertBefore(df, comment);
    121  });
    122  assert_throws_dom("HierarchyRequestError", function() {
    123    doc.insertBefore(df, null);
    124  });
    125 }, "If the context node is a document, inserting a DocumentFragment with an element if there already is an element child should throw a HierarchyRequestError.")
    126 test(function() {
    127  // /child/ is a doctype.
    128  var doc = document.implementation.createHTMLDocument("title");
    129  var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
    130  doc.removeChild(doc.documentElement);
    131  assert_array_equals(doc.childNodes, [comment, doc.doctype]);
    132 
    133  var df = doc.createDocumentFragment();
    134  df.appendChild(doc.createElement("a"));
    135  assert_throws_dom("HierarchyRequestError", function() {
    136    doc.insertBefore(df, doc.doctype);
    137  });
    138 }, "If the context node is a document and a doctype is following the reference child, inserting a DocumentFragment with an element should throw a HierarchyRequestError.")
    139 test(function() {
    140  // /child/ is not null and a doctype is following /child/.
    141  var doc = document.implementation.createHTMLDocument("title");
    142  var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
    143  doc.removeChild(doc.documentElement);
    144  assert_array_equals(doc.childNodes, [comment, doc.doctype]);
    145 
    146  var df = doc.createDocumentFragment();
    147  df.appendChild(doc.createElement("a"));
    148  assert_throws_dom("HierarchyRequestError", function() {
    149    doc.insertBefore(df, comment);
    150  });
    151 }, "If the context node is a document, inserting a DocumentFragment with an element before the doctype should throw a HierarchyRequestError.")
    152 
    153 // Step 4.3.
    154 test(function() {
    155  // The context node has an element child.
    156  var doc = document.implementation.createHTMLDocument("title");
    157  var comment = doc.appendChild(doc.createComment("foo"));
    158  assert_array_equals(doc.childNodes, [doc.doctype, doc.documentElement, comment]);
    159 
    160  var a = doc.createElement("a");
    161  assert_throws_dom("HierarchyRequestError", function() {
    162    doc.insertBefore(a, doc.doctype);
    163  });
    164  assert_throws_dom("HierarchyRequestError", function() {
    165    doc.insertBefore(a, doc.documentElement);
    166  });
    167  assert_throws_dom("HierarchyRequestError", function() {
    168    doc.insertBefore(a, comment);
    169  });
    170  assert_throws_dom("HierarchyRequestError", function() {
    171    doc.insertBefore(a, null);
    172  });
    173 }, "If the context node is a document, inserting an element if there already is an element child should throw a HierarchyRequestError.")
    174 test(function() {
    175  // /child/ is a doctype.
    176  var doc = document.implementation.createHTMLDocument("title");
    177  var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
    178  doc.removeChild(doc.documentElement);
    179  assert_array_equals(doc.childNodes, [comment, doc.doctype]);
    180 
    181  var a = doc.createElement("a");
    182  assert_throws_dom("HierarchyRequestError", function() {
    183    doc.insertBefore(a, doc.doctype);
    184  });
    185 }, "If the context node is a document, inserting an element before the doctype should throw a HierarchyRequestError.")
    186 test(function() {
    187  // /child/ is not null and a doctype is following /child/.
    188  var doc = document.implementation.createHTMLDocument("title");
    189  var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
    190  doc.removeChild(doc.documentElement);
    191  assert_array_equals(doc.childNodes, [comment, doc.doctype]);
    192 
    193  var a = doc.createElement("a");
    194  assert_throws_dom("HierarchyRequestError", function() {
    195    doc.insertBefore(a, comment);
    196  });
    197 }, "If the context node is a document and a doctype is following the reference child, inserting an element should throw a HierarchyRequestError.")
    198 
    199 // Step 4.4.
    200 test(function() {
    201  var doc = document.implementation.createHTMLDocument("title");
    202  var comment = doc.insertBefore(doc.createComment("foo"), doc.firstChild);
    203  assert_array_equals(doc.childNodes, [comment, doc.doctype, doc.documentElement]);
    204 
    205  var doctype = document.implementation.createDocumentType("html", "", "");
    206  assert_throws_dom("HierarchyRequestError", function() {
    207    doc.insertBefore(doctype, comment);
    208  });
    209  assert_throws_dom("HierarchyRequestError", function() {
    210    doc.insertBefore(doctype, doc.doctype);
    211  });
    212  assert_throws_dom("HierarchyRequestError", function() {
    213    doc.insertBefore(doctype, doc.documentElement);
    214  });
    215  assert_throws_dom("HierarchyRequestError", function() {
    216    doc.insertBefore(doctype, null);
    217  });
    218 }, "If the context node is a document, inserting a doctype if there already is a doctype child should throw a HierarchyRequestError.")
    219 test(function() {
    220  var doc = document.implementation.createHTMLDocument("title");
    221  var comment = doc.appendChild(doc.createComment("foo"));
    222  doc.removeChild(doc.doctype);
    223  assert_array_equals(doc.childNodes, [doc.documentElement, comment]);
    224 
    225  var doctype = document.implementation.createDocumentType("html", "", "");
    226  assert_throws_dom("HierarchyRequestError", function() {
    227    doc.insertBefore(doctype, comment);
    228  });
    229 }, "If the context node is a document, inserting a doctype after the document element should throw a HierarchyRequestError.")
    230 test(function() {
    231  var doc = document.implementation.createHTMLDocument("title");
    232  var comment = doc.appendChild(doc.createComment("foo"));
    233  doc.removeChild(doc.doctype);
    234  assert_array_equals(doc.childNodes, [doc.documentElement, comment]);
    235 
    236  var doctype = document.implementation.createDocumentType("html", "", "");
    237  assert_throws_dom("HierarchyRequestError", function() {
    238    doc.insertBefore(doctype, null);
    239  });
    240 }, "If the context node is a document with and element child, appending a doctype should throw a HierarchyRequestError.")
    241 
    242 // Step 5.
    243 test(function() {
    244  var df = document.createDocumentFragment();
    245  var a = df.appendChild(document.createElement("a"));
    246 
    247  var doc = document.implementation.createHTMLDocument("title");
    248  assert_throws_dom("HierarchyRequestError", function() {
    249    df.insertBefore(doc, a);
    250  });
    251  assert_throws_dom("HierarchyRequestError", function() {
    252    df.insertBefore(doc, null);
    253  });
    254 
    255  var doctype = document.implementation.createDocumentType("html", "", "");
    256  assert_throws_dom("HierarchyRequestError", function() {
    257    df.insertBefore(doctype, a);
    258  });
    259  assert_throws_dom("HierarchyRequestError", function() {
    260    df.insertBefore(doctype, null);
    261  });
    262 }, "If the context node is a DocumentFragment, inserting a document or a doctype should throw a HierarchyRequestError.")
    263 test(function() {
    264  var el = document.createElement("div");
    265  var a = el.appendChild(document.createElement("a"));
    266 
    267  var doc = document.implementation.createHTMLDocument("title");
    268  assert_throws_dom("HierarchyRequestError", function() {
    269    el.insertBefore(doc, a);
    270  });
    271  assert_throws_dom("HierarchyRequestError", function() {
    272    el.insertBefore(doc, null);
    273  });
    274 
    275  var doctype = document.implementation.createDocumentType("html", "", "");
    276  assert_throws_dom("HierarchyRequestError", function() {
    277    el.insertBefore(doctype, a);
    278  });
    279  assert_throws_dom("HierarchyRequestError", function() {
    280    el.insertBefore(doctype, null);
    281  });
    282 }, "If the context node is an element, inserting a document or a doctype should throw a HierarchyRequestError.")
    283 
    284 // Step 7.
    285 test(function() {
    286  var a = document.createElement("div");
    287  var b = document.createElement("div");
    288  var c = document.createElement("div");
    289  a.appendChild(b);
    290  a.appendChild(c);
    291  assert_array_equals(a.childNodes, [b, c]);
    292  assert_equals(a.insertBefore(b, b), b);
    293  assert_array_equals(a.childNodes, [b, c]);
    294  assert_equals(a.insertBefore(c, c), c);
    295  assert_array_equals(a.childNodes, [b, c]);
    296 }, "Inserting a node before itself should not move the node");
    297 </script>