editor/libeditor/html/tests/test_bug449243.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/libeditor/html/tests/test_bug449243.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,136 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=449243
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 449243</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=449243">Mozilla Bug 449243</a>
    1.17 +<p id="display"></p>
    1.18 +<div id="content" contenteditable>
    1.19 +  <h2>This is a title</h2>
    1.20 +  <ul>
    1.21 +    <li>this is a</li>
    1.22 +    <li>bullet list</li>
    1.23 +  </ul>
    1.24 +  <ol>
    1.25 +    <li>this is a</li>
    1.26 +    <li>numbered list</li>
    1.27 +  </ol>
    1.28 +</div>
    1.29 +
    1.30 +<pre id="test">
    1.31 +<script type="application/javascript">
    1.32 +
    1.33 +/** Test for Bug 449243 **/
    1.34 +SimpleTest.waitForExplicitFinish();
    1.35 +SimpleTest.waitForFocus(runTests);
    1.36 +
    1.37 +const CARET_BEGIN  = 0;
    1.38 +const CARET_MIDDLE = 1;
    1.39 +const CARET_END    = 2;
    1.40 +
    1.41 +function split(element, caretPos, nbKeyPresses) {
    1.42 +  // put the caret on the requested position
    1.43 +  var sel = window.getSelection();
    1.44 +  var len = element.textContent.length;
    1.45 +  var pos = -1;
    1.46 +  switch (caretPos) {
    1.47 +    case CARET_BEGIN:
    1.48 +      pos = 0;
    1.49 +      break;
    1.50 +    case CARET_MIDDLE:
    1.51 +      pos = Math.floor(len/2);
    1.52 +      break;
    1.53 +    case CARET_END:
    1.54 +      pos = len;
    1.55 +      break;
    1.56 +  }
    1.57 +  sel.collapse(element.firstChild, pos);
    1.58 +  
    1.59 +  // simulates a [Return] keypress
    1.60 +  for (var i = 0; i < nbKeyPresses; i++)
    1.61 +    synthesizeKey("VK_RETURN", {});
    1.62 +}
    1.63 +
    1.64 +function undo(nbKeyPresses) {
    1.65 +  for (var i = 0; i < nbKeyPresses; i++)
    1.66 +    document.execCommand("Undo", false, null);
    1.67 +}
    1.68 +
    1.69 +function SameTypeAsPreviousSibling(element) {
    1.70 +  var sibling = element.previousSibling;
    1.71 +  while (sibling && sibling.nodeType != 1)
    1.72 +    sibling = element.previousSibling;
    1.73 +  return (element.nodeName == sibling.nodeName);
    1.74 +}
    1.75 +
    1.76 +function isParagraph(element) {
    1.77 +  return element.nodeName.toLowerCase() == "p";
    1.78 +}
    1.79 +
    1.80 +function runTests() {
    1.81 +  const content = document.querySelector("[contenteditable]");
    1.82 +  const header = content.querySelector("h2");
    1.83 +  const ulItem = content.querySelector("ul > li:last-child");
    1.84 +  const olItem = content.querySelector("ol > li:last-child");
    1.85 +  content.focus();
    1.86 +
    1.87 +  // beginning of selection: split current node
    1.88 +  split(header, CARET_BEGIN, 1);
    1.89 +  ok(SameTypeAsPreviousSibling(header),
    1.90 +    "Pressing [Return] at the beginning of a header " + 
    1.91 +    "should create another header.");
    1.92 +  split(ulItem, CARET_BEGIN, 2);
    1.93 +  ok(SameTypeAsPreviousSibling(ulItem),
    1.94 +    "Pressing [Return] at the beginning of an unordered list item " + 
    1.95 +    "should create another list item.");
    1.96 +  split(olItem, CARET_BEGIN, 2);
    1.97 +  ok(SameTypeAsPreviousSibling(olItem),
    1.98 +    "Pressing [Return] at the beginning of an ordered list item " + 
    1.99 +    "should create another list item.");
   1.100 +  undo(3);
   1.101 +
   1.102 +  // middle of selection: split current node
   1.103 +  split(header, CARET_MIDDLE, 1);
   1.104 +  ok(SameTypeAsPreviousSibling(header),
   1.105 +    "Pressing [Return] at the middle of a header " + 
   1.106 +    "should create another header.");
   1.107 +  split(ulItem, CARET_MIDDLE, 2);
   1.108 +  ok(SameTypeAsPreviousSibling(ulItem),
   1.109 +    "Pressing [Return] at the middle of an unordered list item " + 
   1.110 +    "should create another list item.");
   1.111 +  split(olItem, CARET_MIDDLE, 2);
   1.112 +  ok(SameTypeAsPreviousSibling(olItem),
   1.113 +    "Pressing [Return] at the middle of an ordered list item " + 
   1.114 +    "should create another list item.");
   1.115 +  undo(3);
   1.116 +
   1.117 +  // end of selection: create a new paragraph
   1.118 +  split(header, CARET_END, 1);
   1.119 +  ok(isParagraph(content.querySelector("h2+*")),
   1.120 +    "Pressing [Return] at the end of a header " + 
   1.121 +    "should create a new paragraph.");
   1.122 +  split(ulItem, CARET_END, 2);
   1.123 +  ok(isParagraph(content.querySelector("ul+*")),
   1.124 +    "Pressing [Return] twice at the end of an unordered list item " + 
   1.125 +    "should create a new paragraph.");
   1.126 +  split(olItem, CARET_END, 2);
   1.127 +  ok(isParagraph(content.querySelector("ol+*")),
   1.128 +    "Pressing [Return] twice at the end of an ordered list item " + 
   1.129 +    "should create a new paragraph.");
   1.130 +  undo(3);
   1.131 +
   1.132 +  // done
   1.133 +  SimpleTest.finish();
   1.134 +}
   1.135 +
   1.136 +</script>
   1.137 +</pre>
   1.138 +</body>
   1.139 +</html>

mercurial