editor/libeditor/html/tests/test_bug460740.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <!DOCTYPE HTML>
     2 <html>
     3 <!--
     4 https://bugzilla.mozilla.org/show_bug.cgi?id=460740
     5 -->
     6 <head>
     7   <title>Test for Bug 460740</title>
     8   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     9   <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
    10   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    11 </head>
    12 <body>
    13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=460740">Mozilla Bug 460740</a>
    14 <p id="display"></p>
    15 <div id="content">
    16   <ul>
    17     <li contenteditable>
    18       Editable LI
    19     </li>
    20     <li>
    21       <div contenteditable>
    22         Editable DIV inside LI
    23       </div>
    24     </li>
    25     <li>
    26       <div>
    27         <div contenteditable>
    28           Editable DIV inside DIV inside LI
    29         </div>
    30       </div>
    31     </li>
    32     <li>
    33       <h3>
    34         <div contenteditable>
    35           Editable DIV inside H3 inside LI
    36         </div>
    37       </h3>
    38     </li>
    39   </ul>
    40   <div contenteditable>
    41     Editable DIV
    42   </div>
    43   <h3 contenteditable>
    44     Editable H3
    45   </h3>
    46   <p contenteditable>
    47     Editable P
    48   </p>
    49   <div>
    50     <p contenteditable>
    51       Editable P in a DIV
    52     </p>
    53   </div>
    54   <p><span contenteditable>Editable SPAN in a P</span></p>
    55 </div>
    57 <pre id="test">
    58 <script type="application/javascript">
    60 /** Test for Bug 460740 **/
    61 SimpleTest.waitForExplicitFinish();
    62 SimpleTest.waitForFocus(runTests);
    64 const CARET_BEGIN  = 0;
    65 const CARET_MIDDLE = 1;
    66 const CARET_END    = 2;
    68 function split(element, caretPos) {
    69   // compute the requested position
    70   var len = element.textContent.length;
    71   var pos = -1;
    72   switch (caretPos) {
    73     case CARET_BEGIN:
    74       pos = 0;
    75       break;
    76     case CARET_MIDDLE:
    77       pos = Math.floor(len/2);
    78       break;
    79     case CARET_END:
    80       pos = len;
    81       break;
    82   }
    84   // put the caret on the requested position
    85   var range = document.createRange();
    86   var sel = window.getSelection();
    87   range.setStart(element.firstChild, pos);
    88   range.setEnd(element.firstChild, pos);
    89   sel.addRange(range);
    91   // simulates a [Return] keypress
    92   synthesizeKey("VK_RETURN", {});
    93 }
    95 // count the number of non-BR elements in #content
    96 function getBlockCount() {
    97   return document.querySelectorAll("#content *:not(br)").length;
    98 }
   100 // count the number of BRs in element
   101 function checkBR(element) {
   102   return element.querySelectorAll("br").length;
   103 }
   105 function runTests() {
   106   var count = getBlockCount();
   107   var nodes = document.querySelectorAll("#content [contenteditable]");
   108   for (var i = 0; i < nodes.length; i++) {
   109     var node = nodes[i];
   110     node.focus();
   111     is(checkBR(node), 0, node.textContent.trim() + ": This node should not have any <br> element yet.");
   112     for (var j = 0; j < 3; j++) { // CARET_BEGIN|MIDDLE|END
   113       split(node, j);
   114       ok(checkBR(node) > 0, node.textContent.trim() + " " + j + ": Pressing [Return] should add (at least) one <br> element.");
   115       is(getBlockCount(), count, node.textContent.trim() + " " + j + ": Pressing [Return] should not change the number of non-<br> elements.");
   116       document.execCommand("Undo", false, null);
   117     }
   118   }
   119   SimpleTest.finish();
   120 }
   121 </script>
   122 </pre>
   123 </body>
   124 </html>

mercurial