tor-browser

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

pre-insertion-validation-hierarchy.js (4363B)


      1 /**
      2 * Validations where `child` argument is irrelevant.
      3 * @param {Function} methodName
      4 */
      5 function preInsertionValidateHierarchy(methodName) {
      6  function insert(parent, node) {
      7    if (parent[methodName].length > 1) {
      8      // This is for insertBefore(). We can't blindly pass `null` for all methods
      9      // as doing so will move nodes before validation.
     10      parent[methodName](node, null);
     11    } else {
     12      parent[methodName](node);
     13    }
     14  }
     15 
     16  // Step 2
     17  test(() => {
     18    const doc = document.implementation.createHTMLDocument("title");
     19    assert_throws_dom("HierarchyRequestError", () => insert(doc.body, doc.body));
     20    assert_throws_dom("HierarchyRequestError", () => insert(doc.body, doc.documentElement));
     21  }, "If node is a host-including inclusive ancestor of parent, then throw a HierarchyRequestError DOMException.");
     22 
     23  // Step 4
     24  test(() => {
     25    const doc = document.implementation.createHTMLDocument("title");
     26    const doc2 = document.implementation.createHTMLDocument("title2");
     27    assert_throws_dom("HierarchyRequestError", () => insert(doc, doc2));
     28  }, "If node is not a DocumentFragment, DocumentType, Element, Text, ProcessingInstruction, or Comment node, then throw a HierarchyRequestError DOMException.");
     29 
     30  // Step 5, in case of inserting a text node into a document
     31  test(() => {
     32    const doc = document.implementation.createHTMLDocument("title");
     33    assert_throws_dom("HierarchyRequestError", () => insert(doc, doc.createTextNode("text")));
     34  }, "If node is a Text node and parent is a document, then throw a HierarchyRequestError DOMException.");
     35 
     36  // Step 5, in case of inserting a doctype into a non-document
     37  test(() => {
     38    const doc = document.implementation.createHTMLDocument("title");
     39    const doctype = doc.childNodes[0];
     40    assert_throws_dom("HierarchyRequestError", () => insert(doc.createElement("a"), doctype));
     41  }, "If node is a doctype and parent is not a document, then throw a HierarchyRequestError DOMException.")
     42 
     43  // Step 6, in case of DocumentFragment including multiple elements
     44  test(() => {
     45    const doc = document.implementation.createHTMLDocument("title");
     46    doc.documentElement.remove();
     47    const df = doc.createDocumentFragment();
     48    df.appendChild(doc.createElement("a"));
     49    df.appendChild(doc.createElement("b"));
     50    assert_throws_dom("HierarchyRequestError", () => insert(doc, df));
     51  }, "If node is a DocumentFragment with multiple elements and parent is a document, then throw a HierarchyRequestError DOMException.");
     52 
     53  // Step 6, in case of DocumentFragment has multiple elements when document already has an element
     54  test(() => {
     55    const doc = document.implementation.createHTMLDocument("title");
     56    const df = doc.createDocumentFragment();
     57    df.appendChild(doc.createElement("a"));
     58    assert_throws_dom("HierarchyRequestError", () => insert(doc, df));
     59  }, "If node is a DocumentFragment with an element and parent is a document with another element, then throw a HierarchyRequestError DOMException.");
     60 
     61  // Step 6, in case of an element
     62  test(() => {
     63    const doc = document.implementation.createHTMLDocument("title");
     64    const el = doc.createElement("a");
     65    assert_throws_dom("HierarchyRequestError", () => insert(doc, el));
     66  }, "If node is an Element and parent is a document with another element, then throw a HierarchyRequestError DOMException.");
     67 
     68  // Step 6, in case of a doctype when document already has another doctype
     69  test(() => {
     70    const doc = document.implementation.createHTMLDocument("title");
     71    const doctype = doc.childNodes[0].cloneNode();
     72    doc.documentElement.remove();
     73    assert_throws_dom("HierarchyRequestError", () => insert(doc, doctype));
     74  }, "If node is a doctype and parent is a document with another doctype, then throw a HierarchyRequestError DOMException.");
     75 
     76  // Step 6, in case of a doctype when document has an element
     77  if (methodName !== "prepend") {
     78    // Skip `.prepend` as this doesn't throw if `child` is an element
     79    test(() => {
     80      const doc = document.implementation.createHTMLDocument("title");
     81      const doctype = doc.childNodes[0].cloneNode();
     82      doc.childNodes[0].remove();
     83      assert_throws_dom("HierarchyRequestError", () => insert(doc, doctype));
     84    }, "If node is a doctype and parent is a document with an element, then throw a HierarchyRequestError DOMException.");
     85  }
     86 }