content/base/test/test_bug409380.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_bug409380.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,378 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=409380
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 409380</title>
    1.11 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.13 +</head>
    1.14 +<body>
    1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=409380">Mozilla Bug 409380</a>
    1.16 +<p id="display"></p>
    1.17 +<div id="content" style="display: none">
    1.18 +  
    1.19 +</div>
    1.20 +<pre id="test">
    1.21 +<script class="testbody" type="text/javascript">
    1.22 +
    1.23 +/** Test for Bug 409380 **/
    1.24 +
    1.25 +function runRangeTest()
    1.26 +{
    1.27 +  // Bug 336381
    1.28 +  // This is a case which can't be supported (at least not at the moment)
    1.29 +  // because DOM Range requires that when the start boundary point is text node,
    1.30 +  // it must be splitted. But in this case boundary point doesn't have parent,
    1.31 +  // so splitting doesn't work.
    1.32 +  var zz = document.getElementById("connectedDiv").firstChild;
    1.33 +  zz.parentNode.removeChild(zz);
    1.34 +  var range = document.createRange();
    1.35 +  var hadException = false;
    1.36 +  try {
    1.37 +    range.setStart(zz, 0);
    1.38 +    range.setEnd(zz, 0);
    1.39 +  } catch (ex) {
    1.40 +    hadException = true;
    1.41 +  }
    1.42 +  ok(!hadException ,
    1.43 +     "It should be possible to select text node even if the node is not in DOM.");
    1.44 +  hadException = false;
    1.45 +  try {
    1.46 +    range.insertNode(document.createTextNode('5'));
    1.47 +  } catch (ex) {
    1.48 +    hadException = true;
    1.49 +  }
    1.50 +  ok(hadException,
    1.51 +     "It shouldn't be possible to insert text node to a detached range.");
    1.52 +
    1.53 +  // Bug 409380
    1.54 +  var element = document.createElement('div');
    1.55 +  var elementContent = "This is the element content";
    1.56 +  element.innerHTML = elementContent;
    1.57 +  range = element.ownerDocument.createRange();
    1.58 +  hadException = false;
    1.59 +  try {
    1.60 +    range.selectNodeContents(element);
    1.61 +  } catch (ex) {
    1.62 +    hadException = true;
    1.63 +  }
    1.64 +  ok(!hadException,
    1.65 +     "It should be possible to select node contents of a detached element.");
    1.66 +  ok(range.toString() == elementContent, "Wrong range selection");
    1.67 +
    1.68 +  // range.selectNode can't succeed because selectNode sets boundary points
    1.69 +  // to be parentNode, which in this testcase is null.
    1.70 +  element = document.createElement('div');
    1.71 +  range = element.ownerDocument.createRange();
    1.72 +  hadException = false;
    1.73 +  try {
    1.74 +    range.selectNode(element);
    1.75 +  } catch (ex) {
    1.76 +    hadException = true;
    1.77 +  }
    1.78 +  ok(hadException, "It shouldn't be possible to select a detached element.");
    1.79 +
    1.80 +  // Testing contextual fragment.
    1.81 +  range = element.ownerDocument.createRange();
    1.82 +  var cf = null;
    1.83 +  var testContent = "<span>foo</span><span>bar</span>";
    1.84 +  try {
    1.85 +    range.selectNodeContents(element);
    1.86 +    cf = range.createContextualFragment(testContent);
    1.87 +    element.appendChild(cf);
    1.88 +  } catch (ex) {
    1.89 +  }
    1.90 +  ok(cf, "Creating contextual fragment didn't succeed!");
    1.91 +  ok(element.innerHTML == testContent, "Wrong innerHTML!");
    1.92 +
    1.93 +  element = document.createElement('div');
    1.94 +  element.textContent = "foobar";
    1.95 +  range = element.ownerDocument.createRange();
    1.96 +  try {
    1.97 +    range.selectNodeContents(element);
    1.98 +    element.firstChild.insertData(3, " ");
    1.99 +  } catch (ex) {
   1.100 +  }
   1.101 +  ok(range.toString() == "foo bar");
   1.102 +
   1.103 +  // Testing contextual fragment, but inserting element to document
   1.104 +  // after creating range.
   1.105 +  element = document.createElement('div');
   1.106 +  range = element.ownerDocument.createRange();
   1.107 +  document.body.appendChild(element);
   1.108 +  cf = null;
   1.109 +  testContent = "<span>foo</span><span>bar</span>";
   1.110 +  try {
   1.111 +    range.selectNodeContents(element);
   1.112 +    cf = range.createContextualFragment(testContent);
   1.113 +    element.appendChild(cf);
   1.114 +  } catch (ex) {
   1.115 +  }
   1.116 +  ok(cf, "Creating contextual fragment didn't succeed!");
   1.117 +  ok(element.innerHTML == testContent, "Wrong innerHTML!");
   1.118 +
   1.119 +  // Testing contextual fragment, but inserting element to document
   1.120 +  // before creating range.
   1.121 +  element = document.createElement('div');
   1.122 +  document.body.appendChild(element);
   1.123 +  range = element.ownerDocument.createRange();
   1.124 +  cf = null;
   1.125 +  testContent = "<span>foo</span><span>bar</span>";
   1.126 +  try {
   1.127 +    range.selectNodeContents(element);
   1.128 +    cf = range.createContextualFragment(testContent);
   1.129 +    element.appendChild(cf);
   1.130 +  } catch (ex) {
   1.131 +  }
   1.132 +  ok(cf, "Creating contextual fragment didn't succeed!");
   1.133 +  ok(element.innerHTML == testContent, "Wrong innerHTML!");
   1.134 +
   1.135 +  element = document.createElement('div');
   1.136 +  var range2 = element.ownerDocument.createRange();
   1.137 +  hadException = false;
   1.138 +  try {
   1.139 +    range2.selectNodeContents(element);
   1.140 +  } catch (ex) {
   1.141 +    hadException = true;
   1.142 +  }
   1.143 +  ok(!hadException,
   1.144 +     "It should be possible to select node contents of a detached element.");
   1.145 +
   1.146 +  // Now the boundary points of range are in DOM, but boundary points of
   1.147 +  // range2 aren't.
   1.148 +  hadException = false;
   1.149 +  try {
   1.150 +    range.compareBoundaryPoints(range.START_TO_START, range2);
   1.151 +  } catch (ex) {
   1.152 +    hadException = true;
   1.153 +  }
   1.154 +  ok(hadException, "Should have got an exception!");
   1.155 +
   1.156 +  hadException = false;
   1.157 +  try {
   1.158 +    range.compareBoundaryPoints(range.START_TO_END, range2);
   1.159 +  } catch (ex) {
   1.160 +    hadException = true;
   1.161 +  }
   1.162 +  ok(hadException, "Should have got an exception!");
   1.163 +
   1.164 +  hadException = false;
   1.165 +  try {
   1.166 +    range.compareBoundaryPoints(range.END_TO_START, range2);
   1.167 +  } catch (ex) {
   1.168 +    hadException = true;
   1.169 +  }
   1.170 +  ok(hadException, "Should have got an exception!");
   1.171 +
   1.172 +  hadException = false;
   1.173 +  try {
   1.174 +    range.compareBoundaryPoints(range.END_TO_END, range2);
   1.175 +  } catch (ex) {
   1.176 +    hadException = true;
   1.177 +  }
   1.178 +  ok(hadException, "Should have got an exception!");
   1.179 +
   1.180 +  hadException = false;
   1.181 +  try {
   1.182 +    range2.compareBoundaryPoints(range.START_TO_START, range);
   1.183 +  } catch (ex) {
   1.184 +    hadException = true;
   1.185 +  }
   1.186 +  ok(hadException, "Should have got an exception!");
   1.187 +
   1.188 +  hadException = false;
   1.189 +  try {
   1.190 +    range2.compareBoundaryPoints(range.START_TO_END, range);
   1.191 +  } catch (ex) {
   1.192 +    hadException = true;
   1.193 +  }
   1.194 +  ok(hadException, "Should have got an exception!");
   1.195 +
   1.196 +  hadException = false;
   1.197 +  try {
   1.198 +    range2.compareBoundaryPoints(range.END_TO_START, range);
   1.199 +  } catch (ex) {
   1.200 +    hadException = true;
   1.201 +  }
   1.202 +  ok(hadException, "Should have got an exception!");
   1.203 +
   1.204 +  hadException = false;
   1.205 +  try {
   1.206 +    range2.compareBoundaryPoints(range.END_TO_END, range);
   1.207 +  } catch (ex) {
   1.208 +    hadException = true;
   1.209 +  }
   1.210 +  ok(hadException, "Should have got an exception!");
   1.211 +
   1.212 +  // range3 will be in document
   1.213 +  element = document.createElement('div');
   1.214 +  document.body.appendChild(element);
   1.215 +  range3 = element.ownerDocument.createRange();
   1.216 +  hadException = false;
   1.217 +  try {
   1.218 +    range3.selectNodeContents(element);
   1.219 +  } catch (ex) {
   1.220 +    hadException = true;
   1.221 +  }
   1.222 +  ok(!hadException,
   1.223 +     "It should be possible to select node contents of a detached element.");
   1.224 +
   1.225 +  hadException = false;
   1.226 +  try {
   1.227 +    range3.compareBoundaryPoints(range.START_TO_START, range);
   1.228 +  } catch (ex) {
   1.229 +    hadException = true;
   1.230 +  }
   1.231 +  ok(!hadException, "Shouldn't have got an exception!");
   1.232 +
   1.233 +  hadException = false;
   1.234 +  try {
   1.235 +    range3.compareBoundaryPoints(range.START_TO_END, range);
   1.236 +  } catch (ex) {
   1.237 +    hadException = true;
   1.238 +  }
   1.239 +  ok(!hadException, "Shouldn't have got an exception!");
   1.240 +
   1.241 +  hadException = false;
   1.242 +  try {
   1.243 +    range3.compareBoundaryPoints(range.END_TO_START, range);
   1.244 +  } catch (ex) {
   1.245 +    hadException = true;
   1.246 +  }
   1.247 +  ok(!hadException, "Shouldn't have got an exception!");
   1.248 +
   1.249 +  hadException = false;
   1.250 +  try {
   1.251 +    range3.compareBoundaryPoints(range.END_TO_END, range);
   1.252 +  } catch (ex) {
   1.253 +    hadException = true;
   1.254 +  }
   1.255 +  ok(!hadException, "Shouldn't have got an exception!");
   1.256 +
   1.257 +  // range4 won't be in document
   1.258 +  element = document.createElement('div');
   1.259 +  var range4 = element.ownerDocument.createRange();
   1.260 +  hadException = false;
   1.261 +  try {
   1.262 +    range4.selectNodeContents(element);
   1.263 +  } catch (ex) {
   1.264 +    hadException = true;
   1.265 +  }
   1.266 +  ok(!hadException,
   1.267 +     "It should be possible to select node contents of a detached element.");
   1.268 +
   1.269 +  hadException = false;
   1.270 +  try {
   1.271 +    range4.compareBoundaryPoints(range.START_TO_START, range);
   1.272 +  } catch (ex) {
   1.273 +    hadException = true;
   1.274 +  }
   1.275 +  ok(hadException, "Should have got an exception!");
   1.276 +
   1.277 +  hadException = false;
   1.278 +  try {
   1.279 +    range2.compareBoundaryPoints(range.START_TO_END, range);
   1.280 +  } catch (ex) {
   1.281 +    hadException = true;
   1.282 +  }
   1.283 +  ok(hadException, "Should have got an exception!");
   1.284 +
   1.285 +  hadException = false;
   1.286 +  try {
   1.287 +    range4.compareBoundaryPoints(range.END_TO_START, range);
   1.288 +  } catch (ex) {
   1.289 +    hadException = true;
   1.290 +  }
   1.291 +  ok(hadException, "Should have got an exception!");
   1.292 +
   1.293 +  hadException = false;
   1.294 +  try {
   1.295 +    range4.compareBoundaryPoints(range.END_TO_END, range);
   1.296 +  } catch (ex) {
   1.297 +    hadException = true;
   1.298 +  }
   1.299 +  ok(hadException, "Should have got an exception!");
   1.300 +
   1.301 +  // Compare range to itself.
   1.302 +  hadException = false;
   1.303 +  try {
   1.304 +    range.compareBoundaryPoints(range.START_TO_START, range);
   1.305 +  } catch (ex) {
   1.306 +    hadException = true;
   1.307 +  }
   1.308 +  ok(!hadException, "Shouldn't have got an exception!");
   1.309 +
   1.310 +  hadException = false;
   1.311 +  try {
   1.312 +    range.compareBoundaryPoints(range.START_TO_END, range);
   1.313 +  } catch (ex) {
   1.314 +    hadException = true;
   1.315 +  }
   1.316 +  ok(!hadException, "Shouldn't have got an exception!");
   1.317 +
   1.318 +  hadException = false;
   1.319 +  try {
   1.320 +    range.compareBoundaryPoints(range.END_TO_START, range);
   1.321 +  } catch (ex) {
   1.322 +    hadException = true;
   1.323 +  }
   1.324 +  ok(!hadException, "Shouldn't have got an exception!");
   1.325 +
   1.326 +  hadException = false;
   1.327 +  try {
   1.328 +    range.compareBoundaryPoints(range.END_TO_END, range);
   1.329 +  } catch (ex) {
   1.330 +    hadException = true;
   1.331 +  }
   1.332 +  ok(!hadException, "Shouldn't have got an exception!");
   1.333 +
   1.334 +  // Attach startContainer of range2 to document.
   1.335 +  ok(range2.startContainer == range2.endContainer, "Wrong container?");
   1.336 +  document.body.appendChild(range2.startContainer);
   1.337 +
   1.338 +  hadException = false;
   1.339 +  try {
   1.340 +    range2.compareBoundaryPoints(range.START_TO_START, range);
   1.341 +  } catch (ex) {
   1.342 +    hadException = true;
   1.343 +  }
   1.344 +  ok(!hadException, "Shouldn't have got an exception!");
   1.345 +
   1.346 +  hadException = false;
   1.347 +  try {
   1.348 +    range2.compareBoundaryPoints(range.START_TO_END, range);
   1.349 +  } catch (ex) {
   1.350 +    hadException = true;
   1.351 +  }
   1.352 +  ok(!hadException, "Shouldn't have got an exception!");
   1.353 +
   1.354 +  hadException = false;
   1.355 +  try {
   1.356 +    range2.compareBoundaryPoints(range.END_TO_START, range);
   1.357 +  } catch (ex) {
   1.358 +    hadException = true;
   1.359 +  }
   1.360 +  ok(!hadException, "Shouldn't have got an exception!");
   1.361 +
   1.362 +  hadException = false;
   1.363 +  try {
   1.364 +    range2.compareBoundaryPoints(range.END_TO_END, range);
   1.365 +  } catch (ex) {
   1.366 +    hadException = true;
   1.367 +  }
   1.368 +  ok(!hadException, "Shouldn't have got an exception!");
   1.369 +  
   1.370 +  SimpleTest.finish();
   1.371 +}
   1.372 +
   1.373 +SimpleTest.waitForExplicitFinish();
   1.374 +addLoadEvent(runRangeTest);
   1.375 +
   1.376 +</script>
   1.377 +</pre>
   1.378 +<div id="connectedDiv">zz</div>
   1.379 +</body>
   1.380 +</html>
   1.381 +

mercurial