editor/libeditor/html/tests/test_bug449243.html

changeset 2
7e26c7da4463
equal deleted inserted replaced
-1:000000000000 0:4fb56d49bf5b
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>
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 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);
55
56 // simulates a [Return] keypress
57 for (var i = 0; i < nbKeyPresses; i++)
58 synthesizeKey("VK_RETURN", {});
59 }
60
61 function undo(nbKeyPresses) {
62 for (var i = 0; i < nbKeyPresses; i++)
63 document.execCommand("Undo", false, null);
64 }
65
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 }
72
73 function isParagraph(element) {
74 return element.nodeName.toLowerCase() == "p";
75 }
76
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();
83
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);
98
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);
113
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);
128
129 // done
130 SimpleTest.finish();
131 }
132
133 </script>
134 </pre>
135 </body>
136 </html>

mercurial