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