summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/nodes/Element-insertAdjacentElement.html
blob: 9eafee6a76ef81812e5e47616ebecd5885f684e6 (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
<!doctype html>
<meta charset=utf-8>
<title></title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>

<div id="target"></div>
<div id="parent"><span id=target2></span></div>
<div id="log" style="visibility:visible"></div>
<span id="test1"></span>
<span id="test2"></span>
<span id="test3"></span>
<span id="test4"></span>
<script>
var target = document.getElementById("target");
var target2 = document.getElementById("target2");

test(function() {
  assert_throws_dom("SyntaxError", function() {
    target.insertAdjacentElement("test", document.getElementById("test1"))
  });

  assert_throws_dom("SyntaxError", function() {
    target2.insertAdjacentElement("test", document.getElementById("test1"))
  });
}, "Inserting to an invalid location should cause a Syntax Error exception")

test(function() {
  var el = target.insertAdjacentElement("beforebegin", document.getElementById("test1"));
  assert_equals(target.previousSibling.id, "test1");
  assert_equals(el.id, "test1");

  el = target2.insertAdjacentElement("beforebegin", document.getElementById("test1"));
  assert_equals(target2.previousSibling.id, "test1");
  assert_equals(el.id, "test1");
}, "Inserted element should be target element's previous sibling for 'beforebegin' case")

test(function() {
  var el = target.insertAdjacentElement("afterbegin", document.getElementById("test2"));
  assert_equals(target.firstChild.id, "test2");
  assert_equals(el.id, "test2");

  el = target2.insertAdjacentElement("afterbegin", document.getElementById("test2"));
  assert_equals(target2.firstChild.id, "test2");
  assert_equals(el.id, "test2");
}, "Inserted element should be target element's first child for 'afterbegin' case")

test(function() {
  var el = target.insertAdjacentElement("beforeend", document.getElementById("test3"));
  assert_equals(target.lastChild.id, "test3");
  assert_equals(el.id, "test3");

  el = target2.insertAdjacentElement("beforeend", document.getElementById("test3"));
  assert_equals(target2.lastChild.id, "test3");
  assert_equals(el.id, "test3");
}, "Inserted element should be target element's last child for 'beforeend' case")

test(function() {
  var el = target.insertAdjacentElement("afterend", document.getElementById("test4"));
  assert_equals(target.nextSibling.id, "test4");
  assert_equals(el.id, "test4");

  el = target2.insertAdjacentElement("afterend", document.getElementById("test4"));
  assert_equals(target2.nextSibling.id, "test4");
  assert_equals(el.id, "test4");
}, "Inserted element should be target element's next sibling for 'afterend' case")

test(function() {
  var docElement = document.documentElement;
  docElement.style.visibility="hidden";

  assert_throws_dom("HierarchyRequestError", function() {
    var el = docElement.insertAdjacentElement("beforebegin", document.getElementById("test1"));
    assert_equals(el, null);
  });

  var el = docElement.insertAdjacentElement("afterbegin", document.getElementById("test2"));
  assert_equals(docElement.firstChild.id, "test2");
  assert_equals(el.id, "test2");

  el = docElement.insertAdjacentElement("beforeend", document.getElementById("test3"));
  assert_equals(docElement.lastChild.id, "test3");
  assert_equals(el.id, "test3");

  assert_throws_dom("HierarchyRequestError", function() {
    var el = docElement.insertAdjacentElement("afterend", document.getElementById("test4"));
    assert_equals(el, null);
  });
}, "Adding more than one child to document should cause a HierarchyRequestError exception")

</script>