editor/libeditor/html/tests/test_bug460740.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/libeditor/html/tests/test_bug460740.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=460740
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 460740</title>
    1.11 +  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
    1.13 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.14 +</head>
    1.15 +<body>
    1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=460740">Mozilla Bug 460740</a>
    1.17 +<p id="display"></p>
    1.18 +<div id="content">
    1.19 +  <ul>
    1.20 +    <li contenteditable>
    1.21 +      Editable LI
    1.22 +    </li>
    1.23 +    <li>
    1.24 +      <div contenteditable>
    1.25 +        Editable DIV inside LI
    1.26 +      </div>
    1.27 +    </li>
    1.28 +    <li>
    1.29 +      <div>
    1.30 +        <div contenteditable>
    1.31 +          Editable DIV inside DIV inside LI
    1.32 +        </div>
    1.33 +      </div>
    1.34 +    </li>
    1.35 +    <li>
    1.36 +      <h3>
    1.37 +        <div contenteditable>
    1.38 +          Editable DIV inside H3 inside LI
    1.39 +        </div>
    1.40 +      </h3>
    1.41 +    </li>
    1.42 +  </ul>
    1.43 +  <div contenteditable>
    1.44 +    Editable DIV
    1.45 +  </div>
    1.46 +  <h3 contenteditable>
    1.47 +    Editable H3
    1.48 +  </h3>
    1.49 +  <p contenteditable>
    1.50 +    Editable P
    1.51 +  </p>
    1.52 +  <div>
    1.53 +    <p contenteditable>
    1.54 +      Editable P in a DIV
    1.55 +    </p>
    1.56 +  </div>
    1.57 +  <p><span contenteditable>Editable SPAN in a P</span></p>
    1.58 +</div>
    1.59 +
    1.60 +<pre id="test">
    1.61 +<script type="application/javascript">
    1.62 +
    1.63 +/** Test for Bug 460740 **/
    1.64 +SimpleTest.waitForExplicitFinish();
    1.65 +SimpleTest.waitForFocus(runTests);
    1.66 +
    1.67 +const CARET_BEGIN  = 0;
    1.68 +const CARET_MIDDLE = 1;
    1.69 +const CARET_END    = 2;
    1.70 +
    1.71 +function split(element, caretPos) {
    1.72 +  // compute the requested position
    1.73 +  var len = element.textContent.length;
    1.74 +  var pos = -1;
    1.75 +  switch (caretPos) {
    1.76 +    case CARET_BEGIN:
    1.77 +      pos = 0;
    1.78 +      break;
    1.79 +    case CARET_MIDDLE:
    1.80 +      pos = Math.floor(len/2);
    1.81 +      break;
    1.82 +    case CARET_END:
    1.83 +      pos = len;
    1.84 +      break;
    1.85 +  }
    1.86 +
    1.87 +  // put the caret on the requested position
    1.88 +  var range = document.createRange();
    1.89 +  var sel = window.getSelection();
    1.90 +  range.setStart(element.firstChild, pos);
    1.91 +  range.setEnd(element.firstChild, pos);
    1.92 +  sel.addRange(range);
    1.93 +  
    1.94 +  // simulates a [Return] keypress
    1.95 +  synthesizeKey("VK_RETURN", {});
    1.96 +}
    1.97 +
    1.98 +// count the number of non-BR elements in #content
    1.99 +function getBlockCount() {
   1.100 +  return document.querySelectorAll("#content *:not(br)").length;
   1.101 +}
   1.102 +
   1.103 +// count the number of BRs in element
   1.104 +function checkBR(element) {
   1.105 +  return element.querySelectorAll("br").length;
   1.106 +}
   1.107 +
   1.108 +function runTests() {
   1.109 +  var count = getBlockCount();
   1.110 +  var nodes = document.querySelectorAll("#content [contenteditable]");
   1.111 +  for (var i = 0; i < nodes.length; i++) {
   1.112 +    var node = nodes[i];
   1.113 +    node.focus();
   1.114 +    is(checkBR(node), 0, node.textContent.trim() + ": This node should not have any <br> element yet.");
   1.115 +    for (var j = 0; j < 3; j++) { // CARET_BEGIN|MIDDLE|END
   1.116 +      split(node, j);
   1.117 +      ok(checkBR(node) > 0, node.textContent.trim() + " " + j + ": Pressing [Return] should add (at least) one <br> element.");
   1.118 +      is(getBlockCount(), count, node.textContent.trim() + " " + j + ": Pressing [Return] should not change the number of non-<br> elements.");
   1.119 +      document.execCommand("Undo", false, null);
   1.120 +    }
   1.121 +  }
   1.122 +  SimpleTest.finish();
   1.123 +}
   1.124 +</script>
   1.125 +</pre>
   1.126 +</body>
   1.127 +</html>

mercurial