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