summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_caret_move_in_vertical_content.html
blob: 3e0854a752faea4302278d42bd0cfc1ab226b021 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1103374
-->
<head>
  <title>Testing caret move in vertical content</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=289384">Mozilla Bug 1103374</a>
<input value="ABCD"><textarea>ABCD
EFGH</textarea>
<div contenteditable></div>
<script>
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
  await (async function testContentEditable() {
    const editor = document.querySelector("div[contenteditable]");
    const selection = getSelection();
    function nodeStr(node) {
      if (node.nodeType === Node.TEXT_NODE) {
        return `#text ("${node.data}")`;
      }
      if (node.nodeType === Node.ELEMENT_NODE) {
        return `<${node.tagName.toLowerCase()}>`;
      }
      return node;
    }
    function rangeStr(container, offset) {
      return `{ container: ${nodeStr(container)}, offset: ${offset} }`;
    }
    function selectionStr() {
      if (selection.rangeCount === 0) {
        return "<no range>";
      }
      if (selection.isCollapsed) {
        return rangeStr(selection.focusNode, selection.focusOffset);
      }
      return `${
        rangeStr(selection.anchorNode, selection.anchorOffset)
      } - ${
        rangeStr(selection.focusNode, selection.focusOffset)
      }`;
    }
    await (async function testVerticalRL() {
      editor.innerHTML = '<p style="font-family: monospace; writing-mode: vertical-rl">ABCD<br>EFGH</p>';
      editor.focus();
      const firstLine = editor.querySelector("p").firstChild;
      const secondLine = firstLine.nextSibling.nextSibling;
      await new Promise(resolve => requestAnimationFrame(
        () => requestAnimationFrame(resolve)) // guarantee sending focus notification to the widget
      );
      selection.collapse(firstLine, firstLine.length / 2);
      synthesizeKey("KEY_ArrowUp");
      is(selectionStr(), rangeStr(firstLine, firstLine.length / 2 - 1),
        "contenteditable: ArrowUp should move caret to previous character in vertical-rl content");
      selection.collapse(firstLine, firstLine.length / 2 - 1);
      synthesizeKey("KEY_ArrowDown");
      is(selectionStr(), rangeStr(firstLine, firstLine.length / 2),
        "contenteditable: ArrowDown should move caret to next character in vertical-rl content");
      selection.collapse(firstLine, firstLine.length / 2);
      synthesizeKey("KEY_ArrowLeft");
      is(selectionStr(), rangeStr(secondLine, secondLine.length / 2),
        "contenteditable: ArrowLeft should move caret to next line in vertical-rl content");
      selection.collapse(secondLine, secondLine.length / 2);
      synthesizeKey("KEY_ArrowRight");
      is(selectionStr(), rangeStr(firstLine, firstLine.length / 2),
        "contenteditable: ArrowRight should move caret to previous line in vertical-rl content");
      selection.removeAllRanges();
      editor.blur();
    })();
    await (async function testVerticalLR() {
      editor.innerHTML = '<p style="font-family: monospace; writing-mode: vertical-lr">ABCD<br>EFGH</p>';
      editor.focus();
      const firstLine = editor.querySelector("p").firstChild;
      const secondLine = firstLine.nextSibling.nextSibling;
      await new Promise(resolve => requestAnimationFrame(
        () => requestAnimationFrame(resolve)) // guarantee sending focus notification to the widget
      );
      selection.collapse(firstLine, firstLine.length / 2);
      synthesizeKey("KEY_ArrowUp");
      is(selectionStr(), rangeStr(firstLine, firstLine.length / 2 - 1),
        "contenteditable: ArrowUp should move caret to previous character in vertical-lr content");
      selection.collapse(firstLine, firstLine.length / 2 - 1);
      synthesizeKey("KEY_ArrowDown");
      is(selectionStr(), rangeStr(firstLine, firstLine.length / 2),
        "contenteditable: ArrowDown should move caret to next character in vertical-lr content");
      selection.collapse(firstLine, firstLine.length / 2);
      synthesizeKey("KEY_ArrowRight");
      is(selectionStr(), rangeStr(secondLine, secondLine.length / 2),
        "contenteditable: ArrowRight should move caret to next line in vertical-lr content");
      selection.collapse(secondLine, secondLine.length / 2);
      synthesizeKey("KEY_ArrowLeft");
      is(selectionStr(), rangeStr(firstLine, firstLine.length / 2),
        "contenteditable: ArrowLeft should move caret to previous line in vertical-lr content");
      selection.removeAllRanges();
      editor.blur();
    })();
  })();
  await (async function testInputTypeText() {
    const editor = document.querySelector("input");
    await (async function testVerticalRL() {
      editor.style.writingMode = "vertical-rl";
      editor.focus();
      await new Promise(resolve => requestAnimationFrame(
        () => requestAnimationFrame(resolve)) // guarantee sending focus notification to the widget
      );
      editor.selectionStart = editor.selectionEnd = editor.value.length / 2;
      synthesizeKey("KEY_ArrowRight");
      is(editor.selectionStart, 0,
        "<input>: ArrowRight should move caret to beginning of the input element if vertical-rl");
      editor.selectionStart = editor.selectionEnd = editor.value.length / 2;
      synthesizeKey("KEY_ArrowLeft");
      is(editor.selectionStart, editor.value.length,
        "<input>: ArrowLeft should move caret to end of the input element if vertical-rl");
      editor.selectionStart = editor.selectionEnd = editor.value.length / 2;
      synthesizeKey("KEY_ArrowDown");
      is(editor.selectionStart, editor.value.length / 2 + 1,
        "<input>: ArrowDown should move caret to next character if vertical-rl");
      editor.selectionStart = editor.selectionEnd = editor.value.length / 2;
      synthesizeKey("KEY_ArrowUp");
      is(editor.selectionStart, editor.value.length / 2 - 1,
        "<input>: ArrowUp should move caret to previous character if vertical-rl");
      editor.blur();
    })();
    await (async function testVerticalLR() {
      editor.style.writingMode = "vertical-lr";
      editor.focus();
      await new Promise(resolve => requestAnimationFrame(
        () => requestAnimationFrame(resolve)) // guarantee sending focus notification to the widget
      );
      editor.selectionStart = editor.selectionEnd = editor.value.length / 2;
      synthesizeKey("KEY_ArrowLeft");
      is(editor.selectionStart, 0,
        "<input>: ArrowLeft should move caret to beginning of the input element if vertical-lr");
      editor.selectionStart = editor.selectionEnd = editor.value.length / 2;
      synthesizeKey("KEY_ArrowRight");
      is(editor.selectionStart, editor.value.length,
        "<input>: ArrowRight should move caret to end of the input element if vertical-lr");
      editor.selectionStart = editor.selectionEnd = editor.value.length / 2;
      synthesizeKey("KEY_ArrowDown");
      is(editor.selectionStart, editor.value.length / 2 + 1,
        "<input>: ArrowDown should move caret to next character if vertical-lr");
      editor.selectionStart = editor.selectionEnd = editor.value.length / 2;
      synthesizeKey("KEY_ArrowUp");
      is(editor.selectionStart, editor.value.length / 2 - 1,
        "<input>: ArrowUp should move caret to previous character if vertical-lr");
      editor.blur();
    })();
  })();
  await (async function testTextArea() {
    const editor = document.querySelector("textarea");
    await (async function testVerticalRL() {
      editor.style.writingMode = "vertical-rl";
      editor.focus();
      await new Promise(resolve => requestAnimationFrame(
        () => requestAnimationFrame(resolve)) // guarantee sending focus notification to the widget
      );
      editor.selectionStart = editor.selectionEnd = "ABCD\nEF".length;
      synthesizeKey("KEY_ArrowRight");
      isfuzzy(editor.selectionStart, "AB".length, 1,
        "<textarea>: ArrowRight should move caret to previous line of textarea element if vertical-rl");
      editor.selectionStart = editor.selectionEnd = "AB".length;
      synthesizeKey("KEY_ArrowLeft");
      isfuzzy(editor.selectionStart, "ABCD\nEF".length, 1,
        "<textarea>: ArrowLeft should move caret to next line of textarea element if vertical-rl");
      editor.selectionStart = editor.selectionEnd = "AB".length;
      synthesizeKey("KEY_ArrowDown");
      is(editor.selectionStart, "ABC".length,
        "<textarea>: ArrowDown should move caret to next character if vertical-rl");
      editor.selectionStart = editor.selectionEnd = "AB".length;
      synthesizeKey("KEY_ArrowUp");
      is(editor.selectionStart, "A".length,
        "<textarea>: ArrowUp should move caret to previous character if vertical-rl");
      editor.blur();
    })();
    await (async function testVerticalLR() {
      editor.style.writingMode = "vertical-lr";
      editor.focus();
      await new Promise(resolve => requestAnimationFrame(
        () => requestAnimationFrame(resolve)) // guarantee sending focus notification to the widget
      );
      editor.selectionStart = editor.selectionEnd = "ABCD\nEF".length;
      synthesizeKey("KEY_ArrowLeft");
      isfuzzy(editor.selectionStart, "AB".length, 1,
        "<textarea>: ArrowLeft should move caret to previous line of textarea element if vertical-rl");
      editor.selectionStart = editor.selectionEnd = "AB".length;
      synthesizeKey("KEY_ArrowRight");
      isfuzzy(editor.selectionStart, "ABCD\nEF".length, 1,
        "<textarea>: ArrowRight should move caret to next line of textarea element if vertical-rl");
      editor.selectionStart = editor.selectionEnd = "AB".length;
      synthesizeKey("KEY_ArrowDown");
      is(editor.selectionStart, "ABC".length,
        "<textarea>: ArrowDown should move caret to next character if vertical-rl");
      editor.selectionStart = editor.selectionEnd = "AB".length;
      synthesizeKey("KEY_ArrowUp");
      is(editor.selectionStart, "A".length,
        "<textarea>: ArrowUp should move caret to previous character if vertical-rl");
      editor.blur();
    })();
  })();
  SimpleTest.finish();
});
</script>
</body>
</html>