summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_insertParagraph_in_h2_and_li.html
blob: ecc8dfd884fa5c2831cdd4fbb5e012cff7856529 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=449243
-->
<head>
  <title>Test for Bug 449243</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=449243">Mozilla Bug 449243</a>
<p id="display"></p>
<div id="content" contenteditable>
  <h2>This is a title</h2>
  <ul>
    <li>this is a</li>
    <li>bullet list</li>
  </ul>
  <ol>
    <li>this is a</li>
    <li>numbered list</li>
  </ol>
</div>

<pre id="test">
<script type="application/javascript">

/** Test for Bug 449243 **/
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(runTests);

const CARET_BEGIN  = 0;
const CARET_MIDDLE = 1;
const CARET_END    = 2;
const isTraditionalSplitDirection =!SpecialPowers.getBoolPref(
  "editor.join_split_direction.compatible_with_the_other_browsers"
);

function split(element, caretPos, nbKeyPresses) {
  // put the caret on the requested position
  const offset = (() => {
    switch (caretPos) {
      case CARET_BEGIN:
        return 0;
      case CARET_MIDDLE:
        return Math.floor(element.textContent.length / 2);
      case CARET_END:
        return element.textContent.length;
    }
    return 0;
  })();
  getSelection().collapse(element.firstChild, offset);

  // simulates a [Return] keypress
  for (let i = 0; i < nbKeyPresses; i++) {
    synthesizeKey("KEY_Enter");
  }
}

function undo(nbKeyPresses) {
  for (let i = 0; i < nbKeyPresses; i++) {
    document.execCommand("Undo");
  }
}

function getNewElement(element) {
  return isTraditionalSplitDirection
    ? element.previousElementSibling
    : element.nextElementSibling;
}

function runTests() {
  const content = document.querySelector("[contenteditable]");
  const header = content.querySelector("h2");
  const ulItem = content.querySelector("ul > li:last-child");
  const olItem = content.querySelector("ol > li:last-child");
  content.focus();

  // beginning of selection: split current node
  split(header, CARET_BEGIN, 1);
  is(
    getNewElement(header)?.nodeName,
    header.nodeName,
    "Pressing [Return] at the beginning of a header " +
    "should create another header."
  );
  split(ulItem, CARET_BEGIN, 2);
  is(
    getNewElement(ulItem)?.nodeName,
    ulItem.nodeName,
    "Pressing [Return] at the beginning of an unordered list item " +
    "should create another list item."
  );
  split(olItem, CARET_BEGIN, 2);
  is(
    getNewElement(olItem)?.nodeName,
    olItem.nodeName,
    "Pressing [Return] at the beginning of an ordered list item " +
    "should create another list item."
  );
  undo(3);

  // middle of selection: split current node
  split(header, CARET_MIDDLE, 1);
  is(
    getNewElement(header)?.nodeName,
    header.nodeName,
    "Pressing [Return] at the middle of a header " +
    "should create another header."
  );
  split(ulItem, CARET_MIDDLE, 2);
  is(
    getNewElement(ulItem)?.nodeName,
    ulItem.nodeName,
    "Pressing [Return] at the middle of an unordered list item " +
    "should create another list item."
  );
  split(olItem, CARET_MIDDLE, 2);
  is(
    getNewElement(olItem)?.nodeName,
    olItem.nodeName,
    "Pressing [Return] at the middle of an ordered list item " +
    "should create another list item."
  );
  undo(3);

  // end of selection: create a new div/paragraph
  function testEndOfSelection(expected, defaultParagraphSeparator) {
    split(header, CARET_END, 1);
    is(
      content.querySelector("h2+*")?.nodeName,
      expected.toUpperCase(),
      `Pressing [Return] at the end of a header should create a new <${
        expected
      }> (defaultParagraphSeparator: <${defaultParagraphSeparator}>)`
    );
    split(ulItem, CARET_END, 2);
    is(
      content.querySelector("ul+*")?.nodeName,
      expected.toUpperCase(),
      `Pressing [Return] twice at the end of an unordered list item should create a new <${
        expected
      }> (defaultParagraphSeparator: <${defaultParagraphSeparator}>)`
    );
    split(olItem, CARET_END, 2);
    is(
      content.querySelector("ol+*")?.nodeName,
      expected.toUpperCase(),
      `Pressing [Return] twice at the end of an ordered list item should create a new <${
        expected
      }> (defaultParagraphSeparator: <${defaultParagraphSeparator}>)`
    );
    undo(3);
  }

  document.execCommand("defaultParagraphSeparator", false, "div");
  testEndOfSelection("div", "div");
  document.execCommand("defaultParagraphSeparator", false, "p");
  testEndOfSelection("p", "p");
  document.execCommand("defaultParagraphSeparator", false, "br");
  testEndOfSelection("p", "br");

  // done
  SimpleTest.finish();
}

</script>
</pre>
</body>
</html>