|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <!-- |
|
4 https://bugzilla.mozilla.org/show_bug.cgi?id=409380 |
|
5 --> |
|
6 <head> |
|
7 <title>Test for Bug 409380</title> |
|
8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
10 </head> |
|
11 <body> |
|
12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=409380">Mozilla Bug 409380</a> |
|
13 <p id="display"></p> |
|
14 <div id="content" style="display: none"> |
|
15 |
|
16 </div> |
|
17 <pre id="test"> |
|
18 <script class="testbody" type="text/javascript"> |
|
19 |
|
20 /** Test for Bug 409380 **/ |
|
21 |
|
22 function runRangeTest() |
|
23 { |
|
24 // Bug 336381 |
|
25 // This is a case which can't be supported (at least not at the moment) |
|
26 // because DOM Range requires that when the start boundary point is text node, |
|
27 // it must be splitted. But in this case boundary point doesn't have parent, |
|
28 // so splitting doesn't work. |
|
29 var zz = document.getElementById("connectedDiv").firstChild; |
|
30 zz.parentNode.removeChild(zz); |
|
31 var range = document.createRange(); |
|
32 var hadException = false; |
|
33 try { |
|
34 range.setStart(zz, 0); |
|
35 range.setEnd(zz, 0); |
|
36 } catch (ex) { |
|
37 hadException = true; |
|
38 } |
|
39 ok(!hadException , |
|
40 "It should be possible to select text node even if the node is not in DOM."); |
|
41 hadException = false; |
|
42 try { |
|
43 range.insertNode(document.createTextNode('5')); |
|
44 } catch (ex) { |
|
45 hadException = true; |
|
46 } |
|
47 ok(hadException, |
|
48 "It shouldn't be possible to insert text node to a detached range."); |
|
49 |
|
50 // Bug 409380 |
|
51 var element = document.createElement('div'); |
|
52 var elementContent = "This is the element content"; |
|
53 element.innerHTML = elementContent; |
|
54 range = element.ownerDocument.createRange(); |
|
55 hadException = false; |
|
56 try { |
|
57 range.selectNodeContents(element); |
|
58 } catch (ex) { |
|
59 hadException = true; |
|
60 } |
|
61 ok(!hadException, |
|
62 "It should be possible to select node contents of a detached element."); |
|
63 ok(range.toString() == elementContent, "Wrong range selection"); |
|
64 |
|
65 // range.selectNode can't succeed because selectNode sets boundary points |
|
66 // to be parentNode, which in this testcase is null. |
|
67 element = document.createElement('div'); |
|
68 range = element.ownerDocument.createRange(); |
|
69 hadException = false; |
|
70 try { |
|
71 range.selectNode(element); |
|
72 } catch (ex) { |
|
73 hadException = true; |
|
74 } |
|
75 ok(hadException, "It shouldn't be possible to select a detached element."); |
|
76 |
|
77 // Testing contextual fragment. |
|
78 range = element.ownerDocument.createRange(); |
|
79 var cf = null; |
|
80 var testContent = "<span>foo</span><span>bar</span>"; |
|
81 try { |
|
82 range.selectNodeContents(element); |
|
83 cf = range.createContextualFragment(testContent); |
|
84 element.appendChild(cf); |
|
85 } catch (ex) { |
|
86 } |
|
87 ok(cf, "Creating contextual fragment didn't succeed!"); |
|
88 ok(element.innerHTML == testContent, "Wrong innerHTML!"); |
|
89 |
|
90 element = document.createElement('div'); |
|
91 element.textContent = "foobar"; |
|
92 range = element.ownerDocument.createRange(); |
|
93 try { |
|
94 range.selectNodeContents(element); |
|
95 element.firstChild.insertData(3, " "); |
|
96 } catch (ex) { |
|
97 } |
|
98 ok(range.toString() == "foo bar"); |
|
99 |
|
100 // Testing contextual fragment, but inserting element to document |
|
101 // after creating range. |
|
102 element = document.createElement('div'); |
|
103 range = element.ownerDocument.createRange(); |
|
104 document.body.appendChild(element); |
|
105 cf = null; |
|
106 testContent = "<span>foo</span><span>bar</span>"; |
|
107 try { |
|
108 range.selectNodeContents(element); |
|
109 cf = range.createContextualFragment(testContent); |
|
110 element.appendChild(cf); |
|
111 } catch (ex) { |
|
112 } |
|
113 ok(cf, "Creating contextual fragment didn't succeed!"); |
|
114 ok(element.innerHTML == testContent, "Wrong innerHTML!"); |
|
115 |
|
116 // Testing contextual fragment, but inserting element to document |
|
117 // before creating range. |
|
118 element = document.createElement('div'); |
|
119 document.body.appendChild(element); |
|
120 range = element.ownerDocument.createRange(); |
|
121 cf = null; |
|
122 testContent = "<span>foo</span><span>bar</span>"; |
|
123 try { |
|
124 range.selectNodeContents(element); |
|
125 cf = range.createContextualFragment(testContent); |
|
126 element.appendChild(cf); |
|
127 } catch (ex) { |
|
128 } |
|
129 ok(cf, "Creating contextual fragment didn't succeed!"); |
|
130 ok(element.innerHTML == testContent, "Wrong innerHTML!"); |
|
131 |
|
132 element = document.createElement('div'); |
|
133 var range2 = element.ownerDocument.createRange(); |
|
134 hadException = false; |
|
135 try { |
|
136 range2.selectNodeContents(element); |
|
137 } catch (ex) { |
|
138 hadException = true; |
|
139 } |
|
140 ok(!hadException, |
|
141 "It should be possible to select node contents of a detached element."); |
|
142 |
|
143 // Now the boundary points of range are in DOM, but boundary points of |
|
144 // range2 aren't. |
|
145 hadException = false; |
|
146 try { |
|
147 range.compareBoundaryPoints(range.START_TO_START, range2); |
|
148 } catch (ex) { |
|
149 hadException = true; |
|
150 } |
|
151 ok(hadException, "Should have got an exception!"); |
|
152 |
|
153 hadException = false; |
|
154 try { |
|
155 range.compareBoundaryPoints(range.START_TO_END, range2); |
|
156 } catch (ex) { |
|
157 hadException = true; |
|
158 } |
|
159 ok(hadException, "Should have got an exception!"); |
|
160 |
|
161 hadException = false; |
|
162 try { |
|
163 range.compareBoundaryPoints(range.END_TO_START, range2); |
|
164 } catch (ex) { |
|
165 hadException = true; |
|
166 } |
|
167 ok(hadException, "Should have got an exception!"); |
|
168 |
|
169 hadException = false; |
|
170 try { |
|
171 range.compareBoundaryPoints(range.END_TO_END, range2); |
|
172 } catch (ex) { |
|
173 hadException = true; |
|
174 } |
|
175 ok(hadException, "Should have got an exception!"); |
|
176 |
|
177 hadException = false; |
|
178 try { |
|
179 range2.compareBoundaryPoints(range.START_TO_START, range); |
|
180 } catch (ex) { |
|
181 hadException = true; |
|
182 } |
|
183 ok(hadException, "Should have got an exception!"); |
|
184 |
|
185 hadException = false; |
|
186 try { |
|
187 range2.compareBoundaryPoints(range.START_TO_END, range); |
|
188 } catch (ex) { |
|
189 hadException = true; |
|
190 } |
|
191 ok(hadException, "Should have got an exception!"); |
|
192 |
|
193 hadException = false; |
|
194 try { |
|
195 range2.compareBoundaryPoints(range.END_TO_START, range); |
|
196 } catch (ex) { |
|
197 hadException = true; |
|
198 } |
|
199 ok(hadException, "Should have got an exception!"); |
|
200 |
|
201 hadException = false; |
|
202 try { |
|
203 range2.compareBoundaryPoints(range.END_TO_END, range); |
|
204 } catch (ex) { |
|
205 hadException = true; |
|
206 } |
|
207 ok(hadException, "Should have got an exception!"); |
|
208 |
|
209 // range3 will be in document |
|
210 element = document.createElement('div'); |
|
211 document.body.appendChild(element); |
|
212 range3 = element.ownerDocument.createRange(); |
|
213 hadException = false; |
|
214 try { |
|
215 range3.selectNodeContents(element); |
|
216 } catch (ex) { |
|
217 hadException = true; |
|
218 } |
|
219 ok(!hadException, |
|
220 "It should be possible to select node contents of a detached element."); |
|
221 |
|
222 hadException = false; |
|
223 try { |
|
224 range3.compareBoundaryPoints(range.START_TO_START, range); |
|
225 } catch (ex) { |
|
226 hadException = true; |
|
227 } |
|
228 ok(!hadException, "Shouldn't have got an exception!"); |
|
229 |
|
230 hadException = false; |
|
231 try { |
|
232 range3.compareBoundaryPoints(range.START_TO_END, range); |
|
233 } catch (ex) { |
|
234 hadException = true; |
|
235 } |
|
236 ok(!hadException, "Shouldn't have got an exception!"); |
|
237 |
|
238 hadException = false; |
|
239 try { |
|
240 range3.compareBoundaryPoints(range.END_TO_START, range); |
|
241 } catch (ex) { |
|
242 hadException = true; |
|
243 } |
|
244 ok(!hadException, "Shouldn't have got an exception!"); |
|
245 |
|
246 hadException = false; |
|
247 try { |
|
248 range3.compareBoundaryPoints(range.END_TO_END, range); |
|
249 } catch (ex) { |
|
250 hadException = true; |
|
251 } |
|
252 ok(!hadException, "Shouldn't have got an exception!"); |
|
253 |
|
254 // range4 won't be in document |
|
255 element = document.createElement('div'); |
|
256 var range4 = element.ownerDocument.createRange(); |
|
257 hadException = false; |
|
258 try { |
|
259 range4.selectNodeContents(element); |
|
260 } catch (ex) { |
|
261 hadException = true; |
|
262 } |
|
263 ok(!hadException, |
|
264 "It should be possible to select node contents of a detached element."); |
|
265 |
|
266 hadException = false; |
|
267 try { |
|
268 range4.compareBoundaryPoints(range.START_TO_START, range); |
|
269 } catch (ex) { |
|
270 hadException = true; |
|
271 } |
|
272 ok(hadException, "Should have got an exception!"); |
|
273 |
|
274 hadException = false; |
|
275 try { |
|
276 range2.compareBoundaryPoints(range.START_TO_END, range); |
|
277 } catch (ex) { |
|
278 hadException = true; |
|
279 } |
|
280 ok(hadException, "Should have got an exception!"); |
|
281 |
|
282 hadException = false; |
|
283 try { |
|
284 range4.compareBoundaryPoints(range.END_TO_START, range); |
|
285 } catch (ex) { |
|
286 hadException = true; |
|
287 } |
|
288 ok(hadException, "Should have got an exception!"); |
|
289 |
|
290 hadException = false; |
|
291 try { |
|
292 range4.compareBoundaryPoints(range.END_TO_END, range); |
|
293 } catch (ex) { |
|
294 hadException = true; |
|
295 } |
|
296 ok(hadException, "Should have got an exception!"); |
|
297 |
|
298 // Compare range to itself. |
|
299 hadException = false; |
|
300 try { |
|
301 range.compareBoundaryPoints(range.START_TO_START, range); |
|
302 } catch (ex) { |
|
303 hadException = true; |
|
304 } |
|
305 ok(!hadException, "Shouldn't have got an exception!"); |
|
306 |
|
307 hadException = false; |
|
308 try { |
|
309 range.compareBoundaryPoints(range.START_TO_END, range); |
|
310 } catch (ex) { |
|
311 hadException = true; |
|
312 } |
|
313 ok(!hadException, "Shouldn't have got an exception!"); |
|
314 |
|
315 hadException = false; |
|
316 try { |
|
317 range.compareBoundaryPoints(range.END_TO_START, range); |
|
318 } catch (ex) { |
|
319 hadException = true; |
|
320 } |
|
321 ok(!hadException, "Shouldn't have got an exception!"); |
|
322 |
|
323 hadException = false; |
|
324 try { |
|
325 range.compareBoundaryPoints(range.END_TO_END, range); |
|
326 } catch (ex) { |
|
327 hadException = true; |
|
328 } |
|
329 ok(!hadException, "Shouldn't have got an exception!"); |
|
330 |
|
331 // Attach startContainer of range2 to document. |
|
332 ok(range2.startContainer == range2.endContainer, "Wrong container?"); |
|
333 document.body.appendChild(range2.startContainer); |
|
334 |
|
335 hadException = false; |
|
336 try { |
|
337 range2.compareBoundaryPoints(range.START_TO_START, range); |
|
338 } catch (ex) { |
|
339 hadException = true; |
|
340 } |
|
341 ok(!hadException, "Shouldn't have got an exception!"); |
|
342 |
|
343 hadException = false; |
|
344 try { |
|
345 range2.compareBoundaryPoints(range.START_TO_END, range); |
|
346 } catch (ex) { |
|
347 hadException = true; |
|
348 } |
|
349 ok(!hadException, "Shouldn't have got an exception!"); |
|
350 |
|
351 hadException = false; |
|
352 try { |
|
353 range2.compareBoundaryPoints(range.END_TO_START, range); |
|
354 } catch (ex) { |
|
355 hadException = true; |
|
356 } |
|
357 ok(!hadException, "Shouldn't have got an exception!"); |
|
358 |
|
359 hadException = false; |
|
360 try { |
|
361 range2.compareBoundaryPoints(range.END_TO_END, range); |
|
362 } catch (ex) { |
|
363 hadException = true; |
|
364 } |
|
365 ok(!hadException, "Shouldn't have got an exception!"); |
|
366 |
|
367 SimpleTest.finish(); |
|
368 } |
|
369 |
|
370 SimpleTest.waitForExplicitFinish(); |
|
371 addLoadEvent(runRangeTest); |
|
372 |
|
373 </script> |
|
374 </pre> |
|
375 <div id="connectedDiv">zz</div> |
|
376 </body> |
|
377 </html> |
|
378 |