summaryrefslogtreecommitdiff
path: root/dom/base/test/test_mutationobserver_anonymous.html
blob: 98ff6e3a47cfd47224d17eebaa326cd2ec84119d (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1034110
-->
<head>
  <title>Test for Bug 1034110</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript"  src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1034110">Mozilla Bug 1034110</a>
<style type="text/css">
  #pseudo.before::before { content: "before"; }
  #pseudo.after::after { content: "after"; }
</style>
<div id="pseudo"></div>
<video id="video"></video>
<p id="display"></p>
<div id="content" style="display: none">

</div>

<pre id="test">
<script type="application/javascript;version=1.7">

/** Test for Bug 1034110 **/

SimpleTest.waitForExplicitFinish();

function getWalker(node) {
  return SpecialPowers.createDOMWalker(node, true);
}

function getFirstChild(parent) {
  return SpecialPowers.unwrap(getWalker(parent).firstChild);
}

function getLastChild(parent) {
  return SpecialPowers.unwrap(getWalker(parent).lastChild);
}

function assertSamePseudoElement(which, node1, node2) {
  is(SpecialPowers.wrap(node1).nodeName, "_moz_generated_content_" + which,
     "Correct pseudo element type");
  is(node1, node2,
     "Referencing the same ::after element");
}

window.onload = function () {
  testOneAdded();
};

function testOneAdded() {
  let parent = document.getElementById("pseudo");
  var m = new MutationObserver(function(records, observer) {
    is(records.length, 1, "Correct number of records");
    is(records[0].type, "nativeAnonymousChildList", "Correct record type");
    is(records[0].target, parent, "Correct target");

    is(records[0].addedNodes.length, 1, "Should have got addedNodes");
    assertSamePseudoElement("before", records[0].addedNodes[0], getFirstChild(parent));
    is(records[0].removedNodes.length, 0, "Shouldn't have got removedNodes");

    observer.disconnect();
    testAddedAndRemoved();
  });

  SpecialPowers.observeMutationEvents(m, parent, true);
  parent.className = "before";
}

function testAddedAndRemoved() {
  let parent = document.getElementById("pseudo");
  let originalBeforeElement = getFirstChild(parent);
  var m = new MutationObserver(function(records, observer) {
    is(records.length, 2, "Correct number of records");
    is(records[0].type, "nativeAnonymousChildList", "Correct record type (1)");
    is(records[1].type, "nativeAnonymousChildList", "Correct record type (2)");
    is(records[0].target, parent, "Correct target (1)");
    is(records[1].target, parent, "Correct target (2)");

    // Two records are sent - one for removed and one for added.
    is(records[0].addedNodes.length, 0, "Shouldn't have got addedNodes");
    is(records[0].removedNodes.length, 1, "Should have got removedNodes");
    assertSamePseudoElement("before", records[0].removedNodes[0], originalBeforeElement);

    is(records[1].addedNodes.length, 1, "Should have got addedNodes");
    assertSamePseudoElement("after", records[1].addedNodes[0], getLastChild(parent));
    is(records[1].removedNodes.length, 0, "Shouldn't have got removedNodes");

    observer.disconnect();
    testRemoved();
  });

  SpecialPowers.observeMutationEvents(m, parent, true);
  parent.className = "after";
}

function testRemoved() {
  let parent = document.getElementById("pseudo");
  let originalAfterElement = getLastChild(parent);
  var m = new MutationObserver(function(records, observer) {
    is(records.length, 1, "Correct number of records");
    is(records[0].type, "nativeAnonymousChildList", "Correct record type");
    is(records[0].target, parent, "Correct target");

    is(records[0].addedNodes.length, 0, "Shouldn't have got addedNodes");
    is(records[0].removedNodes.length, 1, "Should have got removedNodes");
    assertSamePseudoElement("after", records[0].removedNodes[0], originalAfterElement);

    observer.disconnect();
    testMultipleAdded();
  });

  SpecialPowers.observeMutationEvents(m, parent, true);
  parent.className = "";
}

function testMultipleAdded() {
  let parent = document.getElementById("pseudo");
  var m = new MutationObserver(function(records, observer) {
    is(records.length, 2, "Correct number of records");
    is(records[0].type, "nativeAnonymousChildList", "Correct record type (1)");
    is(records[1].type, "nativeAnonymousChildList", "Correct record type (2)");
    is(records[0].target, parent, "Correct target (1)");
    is(records[1].target, parent, "Correct target (2)");

    is(records[0].addedNodes.length, 1, "Should have got addedNodes");
    assertSamePseudoElement("before", records[0].addedNodes[0], getFirstChild(parent));
    is(records[0].removedNodes.length, 0, "Shouldn't have got removedNodes");

    is(records[1].addedNodes.length, 1, "Should have got addedNodes");
    assertSamePseudoElement("after", records[1].addedNodes[0], getLastChild(parent));
    is(records[1].removedNodes.length, 0, "Shouldn't have got removedNodes");

    observer.disconnect();
    testRemovedDueToDisplay();
  });

  SpecialPowers.observeMutationEvents(m, parent, true);
  parent.className = "before after";
}

function testRemovedDueToDisplay() {
  let parent = document.getElementById("pseudo");
  let originalBeforeElement = getFirstChild(parent);
  let originalAfterElement = getLastChild(parent);
  var m = new MutationObserver(function(records, observer) {
    is(records.length, 2, "Correct number of records");
    is(records[0].type, "nativeAnonymousChildList", "Correct record type (1)");
    is(records[1].type, "nativeAnonymousChildList", "Correct record type (2)");
    is(records[0].target, parent, "Correct target (1)");
    is(records[1].target, parent, "Correct target (2)");

    is(records[0].addedNodes.length, 0, "Shouldn't have got addedNodes");
    is(records[0].removedNodes.length, 1, "Should have got removedNodes");
    assertSamePseudoElement("before", records[0].removedNodes[0], originalBeforeElement);

    is(records[1].addedNodes.length, 0, "Shouldn't have got addedNodes");
    is(records[1].removedNodes.length, 1, "Should have got removedNodes");
    assertSamePseudoElement("after", records[1].removedNodes[0], originalAfterElement);

    observer.disconnect();
    testAddedDueToDisplay();
  });

  SpecialPowers.observeMutationEvents(m, parent, true);
  parent.style.display = "none";
}

function testAddedDueToDisplay() {
  let parent = document.getElementById("pseudo");
  var m = new MutationObserver(function(records, observer) {
    is(records.length, 2, "Correct number of records");
    is(records[0].type, "nativeAnonymousChildList", "Correct record type (1)");
    is(records[1].type, "nativeAnonymousChildList", "Correct record type (2)");
    is(records[0].target, parent, "Correct target (1)");
    is(records[1].target, parent, "Correct target (2)");

    is(records[0].addedNodes.length, 1, "Should have got addedNodes");
    assertSamePseudoElement("before", records[0].addedNodes[0], getFirstChild(parent));
    is(records[0].removedNodes.length, 0, "Shouldn't have got removedNodes");

    is(records[1].addedNodes.length, 1, "Should have got addedNodes");
    assertSamePseudoElement("after", records[1].addedNodes[0], getLastChild(parent));
    is(records[1].removedNodes.length, 0, "Shouldn't have got removedNodes");

    observer.disconnect();
    testDifferentTargetNoSubtree();
  });

  SpecialPowers.observeMutationEvents(m, parent, true);
  parent.style.display = "block";
}

function testDifferentTargetNoSubtree() {
  let parent = document.getElementById("pseudo");
  var m = new MutationObserver(function(records, observer) {
    ok(false,
       "No mutation should fire when observing on a parent without subtree option.");
  });
  SpecialPowers.observeMutationEvents(m, document, true);
  parent.style.display = "none";

  // Wait for the actual mutation to come through, making sure that
  // the original observer never fires.
  var m2 = new MutationObserver(function(records, observer) {
    ok(!getFirstChild(parent), "Pseudo element has been removed, but no mutation");
    ok(!getLastChild(parent), "Pseudo element has been removed, but no mutation");
    observer.disconnect();
    testSubtree();
  });
  SpecialPowers.observeMutationEvents(m2, parent, true);
}

function testSubtree() {
  let parent = document.getElementById("pseudo");
  var m = new MutationObserver(function(records, observer) {
    is(records.length, 2, "Correct number of records");
    is(records[0].type, "nativeAnonymousChildList", "Correct record type (1)");
    is(records[1].type, "nativeAnonymousChildList", "Correct record type (2)");
    is(records[0].target, parent, "Correct target (1)");
    is(records[1].target, parent, "Correct target (2)");

    is(records[0].addedNodes.length, 1, "Should have got addedNodes");
    assertSamePseudoElement("before", records[0].addedNodes[0], getFirstChild(parent));
    is(records[0].removedNodes.length, 0, "Shouldn't have got removedNodes");

    is(records[1].addedNodes.length, 1, "Should have got addedNodes");
    assertSamePseudoElement("after", records[1].addedNodes[0], getLastChild(parent));
    is(records[1].removedNodes.length, 0, "Shouldn't have got removedNodes");

    observer.disconnect();
    testDictionaryWithoutChromePriv();
  });
  SpecialPowers.observeMutationEvents(m, document, true, true);
  parent.style.display = "block";
}

function testDictionaryWithoutChromePriv()
{
  var m = new MutationObserver(function() {});
  try {
    m.observe(document, { childList: true, get nativeAnonymousChildList() { throw "Foo1"; } } );
    ok(true, "Shouldn't throw!");
  } catch(ex) {
    ok(false, "Did throw " + ex);
  }

  try {
    m.observe(document, { childList: true, get animations() { throw "Foo2"; } } );
    ok(true, "Shouldn't throw!");
  } catch(ex) {
    ok(false, "Did throw " + ex);
  }
  
  SimpleTest.finish();
}

</script>
</pre>
</body>
</html>