4 https://bugzilla.mozilla.org/show_bug.cgi?id=421765
7 <title>Text.replaceWholeText tests
</title>
8 <script type=
"text/javascript" src=
"/MochiKit/packed.js"></script>
9 <script type=
"text/javascript" src=
"/tests/SimpleTest/SimpleTest.js"></script>
10 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
13 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=421765">Mozilla Bug
421765</a>
15 <div id=
"content" style=
"display: none"></div>
17 <iframe id=
"xmlDocument" src=
"wholeTexty-helper.xml"></iframe>
20 <script class=
"testbody" type=
"text/javascript">
22 /** Test for Bug
421765 **/
24 SimpleTest.waitForExplicitFinish();
28 function entity(n) { return xmlDoc.createEntityReference(n); }
29 function text(t) { return document.createTextNode(t); }
30 function element() { return document.createElement(
"div"); }
33 xmlDoc = $(
"xmlDocument").contentDocument;
34 // document.createCDATASection isn't implemented; clone for the win
35 var node = xmlDoc.documentElement.firstChild.cloneNode(false);
36 is(node.nodeType, Node.CDATA_SECTION_NODE,
37 "er, why isn't this a CDATA section node?");
45 var outer = element();
46 var first = text(
"first");
47 var second = element();
48 second.appendChild(text(
"element contents"));
49 outer.appendChild(first);
50 outer.appendChild(second);
52 is(first.wholeText,
"first",
"wrong initial wholeText");
54 is(first.replaceWholeText(
"start"), first,
55 "should have gotten first back");
56 is(first.data,
"start",
"should have modified first's data");
57 is(first.wholeText,
"start",
"should have gotten new wholeText");
59 var cdataNode = cdata(
"-cdata");
60 outer.insertBefore(cdataNode, second);
62 is(first.wholeText,
"start-cdata",
63 "should have gotten first+cdataNode as wholeText");
65 var outer2 = outer.cloneNode(true); // save
67 is(first.replaceWholeText(
"first"), first,
68 "replaceWholeText on first returned wrong object");
69 is(first.nodeType, Node.TEXT_NODE,
"node changed type?");
70 is(first.data,
"first",
"wrong data in first");
71 is(first.previousSibling, null,
"wrong previousSibling for first");
72 is(first.nextSibling, second,
"wrong nextSibling for first");
73 is(cdataNode.previousSibling, null,
"wrong previousSibling for cdataNode");
74 is(cdataNode.nextSibling, null,
"wrong nextSibling for cdataNode");
76 ok(first.replaceWholeText(
"") === null,
77 "empty string should cause a return of null");
78 is(first.data,
"first",
"wrong data after replacing with empty string");
80 is(outer.firstChild, second,
"replaceWholeText('') removes the node");
82 // switcheroo, with sanity tests
84 is(outer.nodeType, Node.ELEMENT_NODE,
"outer not element?");
85 first = outer.firstChild;
86 is(first.nodeType, Node.TEXT_NODE,
"first not text?");
87 cdataNode = first.nextSibling;
88 is(cdataNode.nodeType, Node.CDATA_SECTION_NODE,
"cdataNode not cdata?");
89 second = outer.lastChild;
90 is(second.nodeType, Node.ELEMENT_NODE,
"second not element?");
92 is(cdataNode.replaceWholeText(
"cdata"), cdataNode,
93 "replaceWholeText on cdataNode returned wrong object");
94 is(cdataNode.nodeType, Node.CDATA_SECTION_NODE,
"node changed type?");
95 is(cdataNode.nodeValue,
"cdata",
"wrong node value?");
96 is(cdataNode.previousSibling, null,
"wrong previousSibling");
97 is(cdataNode.nextSibling, second,
"wrong nextSibling");
99 ok(cdataNode.replaceWholeText(
"") === null,
100 "empty string should cause a return of null");
101 is(cdataNode.data,
"cdata",
"wrong data after replacing with empty string");
102 is(outer.firstChild, second,
"should be no more text at start");
105 function middleTests()
107 var outer = element();
108 var first = element();
109 var middle = text(
"middle");
110 var last = element();
111 first.appendChild(text(
"first element contents"));
112 last.appendChild(text(
"last element contents"));
113 outer.appendChild(first);
114 outer.appendChild(middle);
115 outer.appendChild(last);
117 is(middle.wholeText,
"middle",
"wrong initial wholeText");
119 is(middle.replaceWholeText(
"center"), middle,
120 "should have gotten middle back");
121 is(middle.data,
"center",
"should have modified middle's data");
122 is(middle.wholeText,
"center",
"should have gotten new wholeText");
124 var cdataNode = cdata(
"-cdata");
125 outer.insertBefore(cdataNode, last);
127 is(middle.wholeText,
"center-cdata",
128 "should have gotten middle+cdataNode as wholeText");
130 var outer2 = outer.cloneNode(true); // save
132 is(middle.replaceWholeText(
"middle"), middle,
133 "replaceWholeText on middle returned wrong object");
134 is(middle.nodeType, Node.TEXT_NODE,
"node changed type?");
135 is(middle.data,
"middle",
"wrong data in middle");
136 is(middle.previousSibling, first,
"wrong previousSibling");
137 is(middle.nextSibling, last,
"wrong nextSibling");
139 ok(middle.replaceWholeText(
"") === null,
140 "empty string should cause a return of null");
141 is(middle.data,
"middle",
"wrong data after replacing with empty string");
143 // switcheroo, with sanity tests
145 is(outer.nodeType, Node.ELEMENT_NODE,
"outer not element?");
146 first = outer.firstChild;
147 is(first.nodeType, Node.ELEMENT_NODE,
"first not element?");
148 middle = first.nextSibling;
149 is(middle.nodeType, Node.TEXT_NODE,
"middle not text?");
150 cdataNode = middle.nextSibling;
151 is(cdataNode.nodeType, Node.CDATA_SECTION_NODE,
"cdataNode not cdata?");
152 last = outer.lastChild;
153 is(last.nodeType, Node.ELEMENT_NODE,
"last not element?");
155 is(cdataNode.replaceWholeText(
"cdata"), cdataNode,
156 "replaceWholeText on cdataNode returned wrong object");
157 is(cdataNode.nodeType, Node.CDATA_SECTION_NODE,
"node changed type?");
158 is(cdataNode.nodeValue,
"cdata",
"wrong node value?");
159 is(cdataNode.previousSibling, first,
"wrong previousSibling");
160 is(cdataNode.nextSibling, last,
"wrong nextSibling");
162 ok(cdataNode.replaceWholeText(
"") === null,
163 "empty string should cause a return of null");
164 is(cdataNode.data,
"cdata",
"wrong data after replacing with empty string");
165 is(middle.wholeText,
"center",
"wrong wholeText after removal");
166 is(first.nextSibling, last,
"wrong nextSibling");
167 is(last.previousSibling, first,
"wrong previousSibling");
172 var outer = element();
173 var first = element();
174 var second = text(
"second");
175 first.appendChild(text(
"element contents"));
176 outer.appendChild(first);
177 outer.appendChild(second);
179 is(second.wholeText,
"second",
"wrong initial wholeText");
181 is(second.replaceWholeText(
"end"), second,
182 "should have gotten second back");
183 is(second.data,
"end",
"should have modified second's data");
184 is(second.wholeText,
"end",
"should have gotten new wholeText");
186 var cdataNode = cdata(
"cdata-");
187 outer.insertBefore(cdataNode, second);
189 is(second.wholeText,
"cdata-end",
190 "should have gotten cdataNode+second as wholeText");
191 is(cdataNode.wholeText,
"cdata-end",
192 "should have gotten cdataNode+second as wholeText");
194 var outer2 = outer.cloneNode(true); // save
196 is(second.replaceWholeText(
"second"), second,
197 "replaceWholeText on second returned wrong object");
198 is(second.nodeType, Node.TEXT_NODE,
"node changed type?");
199 is(second.data,
"second",
"wrong data in second");
200 is(second.previousSibling, first,
"wrong previousSibling for second");
201 is(second.nextSibling, null,
"wrong nextSibling for second");
202 is(cdataNode.previousSibling, null,
"wrong previousSibling for cdataNode");
203 is(cdataNode.nextSibling, null,
"wrong nextSibling for cdataNode");
205 ok(second.replaceWholeText(
"") === null,
206 "empty string should cause a return of null");
207 is(second.data,
"second",
"wrong data after replacing with empty string");
209 is(outer.lastChild, first,
"replaceWholeText('') removes the node");
211 // switcheroo, with sanity tests
213 is(outer.nodeType, Node.ELEMENT_NODE,
"outer not element?");
214 first = outer.firstChild;
215 is(first.nodeType, Node.ELEMENT_NODE,
"first not element?");
216 cdataNode = first.nextSibling;
217 is(cdataNode.nodeType, Node.CDATA_SECTION_NODE,
"cdataNode not cdata?");
218 second = outer.lastChild;
219 is(second.nodeType, Node.TEXT_NODE,
"middle not text?");
221 is(cdataNode.replaceWholeText(
"cdata"), cdataNode,
222 "replaceWholeText on cdataNode returned wrong object");
223 is(cdataNode.nodeType, Node.CDATA_SECTION_NODE,
"node changed type?");
224 is(cdataNode.nodeValue,
"cdata",
"wrong node value?");
225 is(cdataNode.previousSibling, first,
"wrong previousSibling for cdataNode");
226 is(cdataNode.nextSibling, null,
"wrong nextSibling for cdataNode");
227 is(second.previousSibling, null,
"wrong previousSibling for second");
228 is(second.nextSibling, null,
"wrong nextSibling for second");
230 ok(cdataNode.replaceWholeText(
"") === null,
231 "empty string should cause a return of null");
232 is(cdataNode.data,
"cdata",
"wrong data after replacing with empty string");
233 is(outer.lastChild, first,
"should be no more text at end");
236 function entityTests()
238 todo_isnot(entity(
"bar"), null,
239 "need implementation update if we ever support entity nodes!");
241 var root = xmlDoc.documentElement;
242 is(root.lastChild.firstChild.nodeType, Node.TEXT_NODE,
243 "uh-oh, did we start supporting entity references as nodes?");
244 is(root.lastChild.lastChild.nodeType, Node.ELEMENT_NODE,
245 "uh-oh, did we start supporting entity references as nodes?");
247 // If any of the above ever fails, add tests here!
261 ok(false,
"exception thrown: " + e);
269 window.addEventListener(
"load", test, false);