editor/libeditor/html/tests/test_bug449243.html

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 <!DOCTYPE HTML>
     2 <html>
     3 <!--
     4 https://bugzilla.mozilla.org/show_bug.cgi?id=449243
     5 -->
     6 <head>
     7   <title>Test for Bug 449243</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=449243">Mozilla Bug 449243</a>
    14 <p id="display"></p>
    15 <div id="content" contenteditable>
    16   <h2>This is a title</h2>
    17   <ul>
    18     <li>this is a</li>
    19     <li>bullet list</li>
    20   </ul>
    21   <ol>
    22     <li>this is a</li>
    23     <li>numbered list</li>
    24   </ol>
    25 </div>
    27 <pre id="test">
    28 <script type="application/javascript">
    30 /** Test for Bug 449243 **/
    31 SimpleTest.waitForExplicitFinish();
    32 SimpleTest.waitForFocus(runTests);
    34 const CARET_BEGIN  = 0;
    35 const CARET_MIDDLE = 1;
    36 const CARET_END    = 2;
    38 function split(element, caretPos, nbKeyPresses) {
    39   // put the caret on the requested position
    40   var sel = window.getSelection();
    41   var len = element.textContent.length;
    42   var pos = -1;
    43   switch (caretPos) {
    44     case CARET_BEGIN:
    45       pos = 0;
    46       break;
    47     case CARET_MIDDLE:
    48       pos = Math.floor(len/2);
    49       break;
    50     case CARET_END:
    51       pos = len;
    52       break;
    53   }
    54   sel.collapse(element.firstChild, pos);
    56   // simulates a [Return] keypress
    57   for (var i = 0; i < nbKeyPresses; i++)
    58     synthesizeKey("VK_RETURN", {});
    59 }
    61 function undo(nbKeyPresses) {
    62   for (var i = 0; i < nbKeyPresses; i++)
    63     document.execCommand("Undo", false, null);
    64 }
    66 function SameTypeAsPreviousSibling(element) {
    67   var sibling = element.previousSibling;
    68   while (sibling && sibling.nodeType != 1)
    69     sibling = element.previousSibling;
    70   return (element.nodeName == sibling.nodeName);
    71 }
    73 function isParagraph(element) {
    74   return element.nodeName.toLowerCase() == "p";
    75 }
    77 function runTests() {
    78   const content = document.querySelector("[contenteditable]");
    79   const header = content.querySelector("h2");
    80   const ulItem = content.querySelector("ul > li:last-child");
    81   const olItem = content.querySelector("ol > li:last-child");
    82   content.focus();
    84   // beginning of selection: split current node
    85   split(header, CARET_BEGIN, 1);
    86   ok(SameTypeAsPreviousSibling(header),
    87     "Pressing [Return] at the beginning of a header " + 
    88     "should create another header.");
    89   split(ulItem, CARET_BEGIN, 2);
    90   ok(SameTypeAsPreviousSibling(ulItem),
    91     "Pressing [Return] at the beginning of an unordered list item " + 
    92     "should create another list item.");
    93   split(olItem, CARET_BEGIN, 2);
    94   ok(SameTypeAsPreviousSibling(olItem),
    95     "Pressing [Return] at the beginning of an ordered list item " + 
    96     "should create another list item.");
    97   undo(3);
    99   // middle of selection: split current node
   100   split(header, CARET_MIDDLE, 1);
   101   ok(SameTypeAsPreviousSibling(header),
   102     "Pressing [Return] at the middle of a header " + 
   103     "should create another header.");
   104   split(ulItem, CARET_MIDDLE, 2);
   105   ok(SameTypeAsPreviousSibling(ulItem),
   106     "Pressing [Return] at the middle of an unordered list item " + 
   107     "should create another list item.");
   108   split(olItem, CARET_MIDDLE, 2);
   109   ok(SameTypeAsPreviousSibling(olItem),
   110     "Pressing [Return] at the middle of an ordered list item " + 
   111     "should create another list item.");
   112   undo(3);
   114   // end of selection: create a new paragraph
   115   split(header, CARET_END, 1);
   116   ok(isParagraph(content.querySelector("h2+*")),
   117     "Pressing [Return] at the end of a header " + 
   118     "should create a new paragraph.");
   119   split(ulItem, CARET_END, 2);
   120   ok(isParagraph(content.querySelector("ul+*")),
   121     "Pressing [Return] twice at the end of an unordered list item " + 
   122     "should create a new paragraph.");
   123   split(olItem, CARET_END, 2);
   124   ok(isParagraph(content.querySelector("ol+*")),
   125     "Pressing [Return] twice at the end of an ordered list item " + 
   126     "should create a new paragraph.");
   127   undo(3);
   129   // done
   130   SimpleTest.finish();
   131 }
   133 </script>
   134 </pre>
   135 </body>
   136 </html>

mercurial