test_insertParagraph_in_h2_and_li.html (4619B)
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 src="/tests/SimpleTest/SimpleTest.js"></script> 9 <script 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> 26 27 <pre id="test"> 28 <script type="application/javascript"> 29 30 /** Test for Bug 449243 */ 31 SimpleTest.waitForExplicitFinish(); 32 SimpleTest.waitForFocus(runTests); 33 34 const CARET_BEGIN = 0; 35 const CARET_MIDDLE = 1; 36 const CARET_END = 2; 37 38 function split(element, caretPos, nbKeyPresses) { 39 // put the caret on the requested position 40 const offset = (() => { 41 switch (caretPos) { 42 case CARET_BEGIN: 43 return 0; 44 case CARET_MIDDLE: 45 return Math.floor(element.textContent.length / 2); 46 case CARET_END: 47 return element.textContent.length; 48 } 49 return 0; 50 })(); 51 getSelection().collapse(element.firstChild, offset); 52 53 // simulates a [Return] keypress 54 for (let i = 0; i < nbKeyPresses; i++) { 55 synthesizeKey("KEY_Enter"); 56 } 57 } 58 59 function undo(nbKeyPresses) { 60 for (let i = 0; i < nbKeyPresses; i++) { 61 document.execCommand("Undo"); 62 } 63 } 64 65 function getNewElement(element) { 66 return element.nextElementSibling; 67 } 68 69 function runTests() { 70 const content = document.querySelector("[contenteditable]"); 71 const header = content.querySelector("h2"); 72 const ulItem = content.querySelector("ul > li:last-child"); 73 const olItem = content.querySelector("ol > li:last-child"); 74 content.focus(); 75 76 // beginning of selection: split current node 77 split(header, CARET_BEGIN, 1); 78 is( 79 getNewElement(header)?.nodeName, 80 header.nodeName, 81 "Pressing [Return] at the beginning of a header " + 82 "should create another header." 83 ); 84 split(ulItem, CARET_BEGIN, 2); 85 is( 86 getNewElement(ulItem)?.nodeName, 87 ulItem.nodeName, 88 "Pressing [Return] at the beginning of an unordered list item " + 89 "should create another list item." 90 ); 91 split(olItem, CARET_BEGIN, 2); 92 is( 93 getNewElement(olItem)?.nodeName, 94 olItem.nodeName, 95 "Pressing [Return] at the beginning of an ordered list item " + 96 "should create another list item." 97 ); 98 undo(3); 99 100 // middle of selection: split current node 101 split(header, CARET_MIDDLE, 1); 102 is( 103 getNewElement(header)?.nodeName, 104 header.nodeName, 105 "Pressing [Return] at the middle of a header " + 106 "should create another header." 107 ); 108 split(ulItem, CARET_MIDDLE, 2); 109 is( 110 getNewElement(ulItem)?.nodeName, 111 ulItem.nodeName, 112 "Pressing [Return] at the middle of an unordered list item " + 113 "should create another list item." 114 ); 115 split(olItem, CARET_MIDDLE, 2); 116 is( 117 getNewElement(olItem)?.nodeName, 118 olItem.nodeName, 119 "Pressing [Return] at the middle of an ordered list item " + 120 "should create another list item." 121 ); 122 undo(3); 123 124 // end of selection: create a new div/paragraph 125 function testEndOfSelection(expected, defaultParagraphSeparator) { 126 split(header, CARET_END, 1); 127 is( 128 content.querySelector("h2+*")?.nodeName, 129 expected.toUpperCase(), 130 `Pressing [Return] at the end of a header should create a new <${ 131 expected 132 }> (defaultParagraphSeparator: <${defaultParagraphSeparator}>)` 133 ); 134 split(ulItem, CARET_END, 2); 135 is( 136 content.querySelector("ul+*")?.nodeName, 137 expected.toUpperCase(), 138 `Pressing [Return] twice at the end of an unordered list item should create a new <${ 139 expected 140 }> (defaultParagraphSeparator: <${defaultParagraphSeparator}>)` 141 ); 142 split(olItem, CARET_END, 2); 143 is( 144 content.querySelector("ol+*")?.nodeName, 145 expected.toUpperCase(), 146 `Pressing [Return] twice at the end of an ordered list item should create a new <${ 147 expected 148 }> (defaultParagraphSeparator: <${defaultParagraphSeparator}>)` 149 ); 150 undo(3); 151 } 152 153 document.execCommand("defaultParagraphSeparator", false, "div"); 154 testEndOfSelection("div", "div"); 155 document.execCommand("defaultParagraphSeparator", false, "p"); 156 testEndOfSelection("p", "p"); 157 document.execCommand("defaultParagraphSeparator", false, "br"); 158 testEndOfSelection("p", "br"); 159 160 // done 161 SimpleTest.finish(); 162 } 163 164 </script> 165 </pre> 166 </body> 167 </html>