summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/nodes/MutationObserver-document.html
blob: 4662b23459bb677c515c8bfba8e24a9a5dc65e1e (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>MutationObservers: takeRecords</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="mutationobservers.js"></script>
<h1>MutationObservers: document mutations</h1>
<div id="log"></div>

<script id='s001'>
  var setupTest = async_test("setup test");
  var insertionTest = async_test("parser insertion mutations");
  var insertionTest2 = async_test("parser script insertion mutation");
  var testCounter = 0;

  function masterMO(sequence, obs) {
    testCounter++;
    if (testCounter == 1) {
      insertionTest.step(
        function () {
          checkRecords(document, sequence,
                       [{type: "childList",
                         addedNodes: function () {
                           return [ document.getElementById("n00") ];
                         },
                         previousSibling: function () {
                           return document.getElementById("s001");
                         },
                         target: document.body},
                        {type: "childList",
                         addedNodes: function () {
                           return [ document.getElementById("s002") ];
                         },
                         previousSibling: function () {
                           return document.getElementById("n00");
                         },
                         target: document.body},
                        {type: "childList",
                         addedNodes: function () {
                           return [ document.getElementById("s002").firstChild ];
                         },
                         target: function () {
                           return document.getElementById("s002");
                         }}]);
        });
    } else if (testCounter == 2) {
      insertionTest2.step(
        function () {
          checkRecords(document, sequence,
                       [{type: "childList",
                         addedNodes: function () {
                           return [ document.getElementById("inserted_script") ];
                         },
                         target: function () {
                          return document.getElementById("n00");
                         }},
                        {type: "childList",
                         addedNodes: function () {
                           return [ document.getElementById("inserted_element") ];
                         },
                         previousSibling: function () {
                           return document.getElementById("s002");
                         },
                         target: document.body}
                        ]);
        });
    }
  }
  var document_observer;
  var newElement;
  setupTest.step(function() {
    document_observer = new MutationObserver(masterMO);
    newElement = document.createElement("span");
    document_observer.observe(document, {subtree:true,childList:true});
    newElement.id = "inserted_element";
    newElement.setAttribute("style", "display: none");
    newElement.textContent = "my new span for n00";
  });
</script><p id='n00'></p><script id='s002'>
  var newScript = document.createElement("script");
  setupTest.step(function() {
    newScript.textContent = "document.body.appendChild(newElement);";
    newScript.id = "inserted_script";
    document.getElementById("n00").appendChild(newScript);
  });
  if (testCounter < 1) {
    insertionTest.step(
      function () {
        assert_unreached("document observer did not trigger");
      });
  }
</script><script id='s003'>
  setupTest.step(function() {
    document_observer.disconnect();
  });
  if (testCounter < 2) {
    insertionTest2.step(
      function () {
        assert_unreached("document observer did not trigger");
      });
  }
  insertionTest.done();
  insertionTest2.done();
</script>

<p id='n012'></p><div id='d01'>
<script id='s011'>
  var removalTest = async_test("removal of parent during parsing");
  var d01 = document.getElementById("d01");
  testCounter = 0;

  function removalMO(sequence, obs) {
    testCounter++;
    if (testCounter == 1) {
      removalTest.step(
        function () {
          checkRecords(document, sequence,
                       [{type: "childList",
                         removedNodes: function () {
                           return [ d01 ];
                         },
                         previousSibling: function () {
                           return document.getElementById("n012");
                         },
                         target: document.body}]);
        });
    } else if (testCounter == 2) {
      removalTest.step(
        function () {
          checkRecords(document, sequence,
                       [{type: "childList",
                         addedNodes: function () {
                           return [ document.getElementById("s012") ];
                         },
                         previousSibling: function () {
                           return document.getElementById("n012");
                         },
                         target: document.body},
                        {type: "childList",
                          addedNodes: function () {
                            return [ document.getElementById("s012").firstChild ];
                          },
                          target: function () {
                            return document.getElementById("s012");
                          }}]);
        });
    }
  }
  var document2_observer;
  setupTest.step(function() {
    document2_observer = new MutationObserver(removalMO);
    document2_observer.observe(document, {subtree:true,childList:true});
    d01.parentNode.removeChild(d01);
  });
</script><p id='n01'></p></div><script id='s012'>
  setupTest.step(function() {
    document2_observer.disconnect();
  });
  if (testCounter < 2) {
    removalTest.step(
      function () {
        assert_unreached("document observer did not trigger");
      });
  }
  removalTest.done();
  setupTest.done();
</script>