summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_focus_caret_navigation_between_nested_editors.html
blob: c0ff01ae100bbfb9fe3b6d813456d46a492a85c0 (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
<!DOCTYPE>
<html>
<head>
<title>Test for bug1318312</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" />
<style type="text/css">
</style>
</head>
<body>
<div id="outerEditor" contenteditable><p>editable in outer editor</p>
<div id="staticInEditor" contenteditable="false"><p>non-editable in outer editor</p>
<div id="innerEditor" contenteditable><p>editable in inner editor</p></div></div></div>
<pre id="test">
<script class="testbody" type="text/javascript">

var outerEditor = document.getElementById("outerEditor");
var innerEditor = document.getElementById("innerEditor");

function getNodeDescription(node) {
  if (!node) {
    return "null";
  }
  switch (node.nodeType) {
    case Node.TEXT_NODE:
    case Node.COMMENT_NODE:
    case Node.CDATA_SECTION_NODE:
      return `${node.nodeName} "${node.data}"`;
    case Node.ELEMENT_NODE:
      return `<${node.nodeName.toLowerCase()}>`;
    default:
      return `${node.nodeName}`;
  }
}

function getRangeDescription(range) {
  if (range === null) {
    return "null";
  }
  if (range === undefined) {
    return "undefined";
  }
  return range.startContainer == range.endContainer &&
    range.startOffset == range.endOffset
    ? `(${getNodeDescription(range.startContainer)}, ${range.startOffset})`
    : `(${getNodeDescription(range.startContainer)}, ${
        range.startOffset
      }) - (${getNodeDescription(range.endContainer)}, ${range.endOffset})`;
}

function runTests() {
  outerEditor.focus();
  is(document.activeElement, outerEditor,
     "outerEditor should have focus");

  // Move cursor into the innerEditor with ArrowDown key.  Then, focus shouldn't
  // be moved to innerEditor from outerEditor.
  // Note that Chrome moves focus in this case.  However, we should align the
  // behavior to Chrome for now because we don't support move caret from a
  // selection limiter to outside of it.
  (() => {
    const description = "Press ArrowDown from start of the outer editor";
    synthesizeKey("KEY_ArrowDown");
    is(
      document.activeElement,
      outerEditor,
      `${description}: The outer editor should keep having focus`
    );
    is(
      getRangeDescription(getSelection().getRangeAt(0)),
      '(#text "editable in inner editor", 0)',
      `${description}: Caret should be moved to start of the inner editor`
    );
  })();

  (() => {
    const description = 'Typing "a" at start of the inner editor';
    sendString("a");
    is(
      document.activeElement,
      outerEditor,
      `${description}: The outer editor should keep having focus`
    );
    is(
      getRangeDescription(getSelection().getRangeAt(0)),
      '(#text "aeditable in inner editor", 1)',
      `${description}: Caret should be moved to after typed character, "a"`
    );
  })();

  (() => {
    const description = 'Pressing Enter next to the typed character "a"';
    synthesizeKey("KEY_Enter");
    is(
      document.activeElement,
      outerEditor,
      `${description}: The outer editor should keep having focus`
    );
    is(
      innerEditor.innerHTML,
      "<p>a</p><p>editable in inner editor</p>",
      `${description}: The paragraph in the inner editor should be split after "a"`
    );
    is(
      getRangeDescription(getSelection().getRangeAt(0)),
      '(#text "editable in inner editor", 0)',
      `${description}: Caret should be moved to start of the second paragraph`
    );
  })();

  (() => {
    const description = 'Pressing Backspace at start of the second paragraph';
    synthesizeKey("KEY_Backspace");
    is(
      document.activeElement,
      outerEditor,
      `${description}: The outer editor should keep having focus`
    );
    is(
      innerEditor.innerHTML,
      "<p>aeditable in inner editor</p>",
      `${description}: The paragraphs should be joined`
    );
    is(
      getRangeDescription(getSelection().getRangeAt(0)),
      '(#text "aeditable in inner editor", 1)',
      `${description}: Caret should be moved to end of text which was in the first paragraph`
    );
  })();

  (() => {
    const description = 'Pressing Shift-ArrowLeft from next to the first character, "a"';
    synthesizeKey("KEY_ArrowLeft", {shiftKey: true});
    is(
      document.activeElement,
      outerEditor,
      `${description}: The outer editor should keep having focus`
    );
    is(
      getRangeDescription(getSelection().getRangeAt(0)),
      '(#text "aeditable in inner editor", 0) - (#text "aeditable in inner editor", 1)',
      `${description}: The first character, "a", should be selected`
    );
  })();

  (() => {
    const description = 'Pressing Delete to delete selected "a"';
    synthesizeKey("KEY_Delete");
    is(
      document.activeElement,
      outerEditor,
      `${description}: The outer editor should keep having focus`
    );
    is(
      innerEditor.innerHTML,
      "<p>editable in inner editor</p>",
      `${description}: The selected "a" should be deleted`
    );
    is(
      getRangeDescription(getSelection().getRangeAt(0)),
      '(#text "editable in inner editor", 0)',
      `${description}: Selection should be collapsed at start of the inner editor`
    );
  })();

  (() => {
    const description = 'Pressing ArrowUp from start of the inner editor';
    synthesizeKey("KEY_ArrowUp");
    is(
      document.activeElement,
      outerEditor,
      `${description}: The outer editor should keep having focus`
    );
    is(
      getRangeDescription(getSelection().getRangeAt(0)),
      '(#text "editable in outer editor", 0)',
      `${description}: Caret should be moved to start of the outer editor`
    );
  })();

  // However, clicking in innerEditor should move focus.
  (() => {
    const description = 'Clicking the inner editor when caret is in the outer editor';
    synthesizeMouseAtCenter(innerEditor, {});
    is(
      document.activeElement,
      innerEditor,
      `${description}: The inner editor should get focus`
    );
    is(
      getNodeDescription(getSelection().focusNode),
      '#text "editable in inner editor"',
      `${description}: Caret should move into the inner editor`
    );
  })();

  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(runTests);

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