summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/nodes/pre-insertion-validation-notfound.js
blob: 705283fa235093eb4be4251b265729412679baf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
function getNonParentNodes() {
  return [
    document.implementation.createDocumentType("html", "", ""),
    document.createTextNode("text"),
    document.implementation.createDocument(null, "foo", null).createProcessingInstruction("foo", "bar"),
    document.createComment("comment"),
    document.implementation.createDocument(null, "foo", null).createCDATASection("data"),
  ];
}

function getNonInsertableNodes() {
  return [
    document.implementation.createHTMLDocument("title")
  ];
}

function getNonDocumentParentNodes() {
  return [
    document.createElement("div"),
    document.createDocumentFragment(),
  ];
}

// Test that the steps happen in the right order, to the extent that it's
// observable.   The variable names "parent", "child", and "node" match the
// corresponding variables in the replaceChild algorithm in these tests.

// Step 1 happens before step 3.
test(function() {
  var illegalParents = getNonParentNodes();
  var child = document.createElement("div");
  var node = document.createElement("div");
  illegalParents.forEach(function (parent) {
    assert_throws_dom("HierarchyRequestError", function() {
      insertFunc.call(parent, node, child);
    });
  });
}, "Should check the 'parent' type before checking whether 'child' is a child of 'parent'");

// Step 2 happens before step 3.
test(function() {
  var parent = document.createElement("div");
  var child = document.createElement("div");
  var node = document.createElement("div");

  node.appendChild(parent);
  assert_throws_dom("HierarchyRequestError", function() {
    insertFunc.call(parent, node, child);
  });
}, "Should check that 'node' is not an ancestor of 'parent' before checking whether 'child' is a child of 'parent'");

// Step 3 happens before step 4.
test(function() {
  var parent = document.createElement("div");
  var child = document.createElement("div");

  var illegalChildren = getNonInsertableNodes();
  illegalChildren.forEach(function (node) {
    assert_throws_dom("NotFoundError", function() {
      insertFunc.call(parent, node, child);
    });
  });
}, "Should check whether 'child' is a child of 'parent' before checking whether 'node' is of a type that can have a parent.");


// Step 3 happens before step 5.
test(function() {
  var child = document.createElement("div");

  var node = document.createTextNode("");
  var parent = document.implementation.createDocument(null, "foo", null);
  assert_throws_dom("NotFoundError", function() {
    insertFunc.call(parent, node, child);
  });

  node = document.implementation.createDocumentType("html", "", "");
  getNonDocumentParentNodes().forEach(function (parent) {
    assert_throws_dom("NotFoundError", function() {
      insertFunc.call(parent, node, child);
    });
  });
}, "Should check whether 'child' is a child of 'parent' before checking whether 'node' is of a type that can have a parent of the type that 'parent' is.");

// Step 3 happens before step 6.
test(function() {
  var child = document.createElement("div");
  var parent = document.implementation.createDocument(null, null, null);

  var node = document.createDocumentFragment();
  node.appendChild(document.createElement("div"));
  node.appendChild(document.createElement("div"));
  assert_throws_dom("NotFoundError", function() {
    insertFunc.call(parent, node, child);
  });

  node = document.createElement("div");
  parent.appendChild(document.createElement("div"));
  assert_throws_dom("NotFoundError", function() {
    insertFunc.call(parent, node, child);
  });

  parent.firstChild.remove();
  parent.appendChild(document.implementation.createDocumentType("html", "", ""));
  node = document.implementation.createDocumentType("html", "", "")
  assert_throws_dom("NotFoundError", function() {
    insertFunc.call(parent, node, child);
  });
}, "Should check whether 'child' is a child of 'parent' before checking whether 'node' can be inserted into the document given the kids the document has right now.");