4 https://bugzilla.mozilla.org/show_bug.cgi?id=650493
7 <title>Test for Bug
650493</title>
8 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
9 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
12 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=650493">Mozilla Bug
650493</a>
14 <div id=
"content" style=
"display: none">
18 <script class=
"testbody" type=
"text/javascript">
21 var walker = document.createTreeWalker($('content'), NodeFilter.SHOW_ALL, null);
24 nodes.push(walker.currentNode);
25 } while(walker.nextNode());
31 var current = getNodes();
32 is(nodes.length, current.length,
"length after " + testName);
33 nodes.forEach(function(val, index) {
34 ok(current.indexOf(val)
> -
1,
"nodes[" + index +
"] (" + val +
") shouldn't exist after " + testName);
38 var nodes = getNodes();
39 var testName =
"empty";
46 root.addEventListener(
"DOMNodeInserted", function(e) {
48 is(e.isTrusted, true,
"untrusted mutation event");
49 var w = document.createTreeWalker(e.target, NodeFilter.SHOW_ALL, null);
51 is(nodes.indexOf(w.currentNode), -
1,
"already have inserted node (" + w.currentNode +
") when " + testName);
52 nodes.push(w.currentNode);
53 } while(w.nextNode());
55 root.addEventListener(
"DOMNodeRemoved", function(e) {
57 is(e.isTrusted, true,
"untrusted mutation event");
58 var w = document.createTreeWalker(e.target, NodeFilter.SHOW_ALL, null);
60 var index = nodes.indexOf(w.currentNode);
61 ok(index != -
1,
"missing removed node (" + w.currentNode +
") when " + testName);
62 nodes.splice(index,
1);
63 } while(w.nextNode());
66 testName =
"text-only innerHTML";
67 root.innerHTML =
"hello world";
70 testName =
"innerHTML with <b>";
71 root.innerHTML =
"<b>bold</b> world";
74 testName =
"complex innerHTML";
75 root.innerHTML =
"<b>b<span>old</span></b> <strong>world";
78 testName =
"replacing using .textContent";
79 root.textContent =
"i'm just a plain text minding my own business";
82 testName =
"clearing using .textContent";
83 root.textContent =
"";
86 testName =
"inserting using .textContent";
87 root.textContent =
"i'm new text!!";
90 testName =
"inserting using .textContent";
91 root.textContent =
"i'm new text!!";
94 testName =
"preparing to normalize";
95 root.innerHTML =
"<u><b>foo</b></u> ";
96 var u = root.firstChild;
97 is(u.nodeName,
"U",
"got the right node");
99 is(b.nodeName,
"B",
"got the right node");
100 b.insertBefore(document.createTextNode(
""), b.firstChild);
101 b.insertBefore(document.createTextNode(
""), b.firstChild);
102 b.appendChild(document.createTextNode(
""));
103 b.appendChild(document.createTextNode(
"hello"));
104 b.appendChild(document.createTextNode(
"world"));
105 u.appendChild(document.createTextNode(
"foo"));
106 u.appendChild(document.createTextNode(
""));
107 u.appendChild(document.createTextNode(
"bar"));
110 testName =
"normalizing";
114 testName =
"self replace firstChild";
116 root.replaceChild(root.firstChild, root.firstChild);
118 is(mutateCount,
2,
"should remove and reinsert " + testName);
120 testName =
"self replace second child";
122 root.replaceChild(root.firstChild.nextSibling, root.firstChild.nextSibling);
124 is(mutateCount,
2,
"should remove and reinsert " + testName);
126 testName =
"self replace lastChild";
128 root.replaceChild(root.lastChild, root.lastChild);
130 is(mutateCount,
2,
"should remove and reinsert " + testName);
132 testName =
"self insertBefore firstChild";
134 root.insertBefore(root.firstChild, root.firstChild);
136 is(mutateCount,
2,
"should remove and reinsert " + testName);
138 testName =
"self insertBefore second child";
140 root.insertBefore(root.firstChild.nextSibling, root.firstChild.nextSibling);
142 is(mutateCount,
2,
"should remove and reinsert " + testName);
144 testName =
"self insertBefore lastChild";
146 root.insertBefore(root.lastChild, root.lastChild);
148 is(mutateCount,
2,
"should remove and reinsert " + testName);
150 testName =
"appendChild last";
152 root.appendChild(root.lastChild);
154 is(mutateCount,
2,
"should remove and reinsert " + testName);
156 testName =
"prepare script/style";
157 script = document.createElement(
"script");
158 script.appendChild(document.createTextNode(
"void(0);"));
159 root.appendChild(script);
160 style = document.createElement(
"style");
161 root.appendChild(style);
164 testName =
"set something in script";
165 script.text =
"something";
168 testName =
"set something in style";
169 style.innerHTML =
"something { dislay: none; }";
172 testName =
"moving style";
173 root.insertBefore(style, root.firstChild);
176 testName =
"replacing script";
177 root.replaceChild(b, script);
180 testName =
"doc-fragment insert in the middle";
181 frag = document.createDocumentFragment();
182 frag.addEventListener(
"DOMNodeRemoved", function(e) {
183 var index = children.indexOf(e.target);
184 ok(index != -
1,
"unknown child removed from fragment");
185 children.splice(index,
1);
188 children.push(document.createTextNode(
"foo"));
189 children.push(document.createTextNode(
"bar"));
190 children.push(document.createElement(
"span"));
191 children.push(document.createElement(
"b"));
192 children[
2].appendChild(document.createElement(
"i"));
193 children.forEach(function(child) { frag.appendChild(child); });
194 ok(root.firstChild,
"need to have children in order to test inserting before end");
195 root.replaceChild(frag, root.firstChild);
197 is(children.length,
0,
"should have received DOMNodeRemoved for all frag children when inserting");
198 is(frag.childNodes.length,
0,
"fragment should be empty when inserting");
200 testName =
"doc-fragment append at the end";
201 children.push(document.createTextNode(
"foo"));
202 children.push(document.createTextNode(
"bar"));
203 children.push(document.createElement(
"span"));
204 children.push(document.createElement(
"b"));
205 children[
2].appendChild(document.createElement(
"i"));
206 children.forEach(function(child) { frag.appendChild(child); });
207 root.appendChild(frag);
209 is(children.length,
0,
"should have received DOMNodeRemoved for all frag children when appending");
210 is(frag.childNodes.length,
0,
"fragment should be empty when appending");