summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/editing/other/editable-state-and-focus-in-shadow-dom-in-designMode.tentative.html
blob: 88e6d291299c2fec70b91d5459c88ac855381f75 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Testing editable state and focus in shadow DOM in design mode</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="../include/editor-test-utils.js"></script>
</head>
<body>
<h3>open</h3>
<my-shadow data-mode="open"></my-shadow>
<h3>closed</h3>
<my-shadow data-mode="closed"></my-shadow>

<script>
"use strict";

document.designMode = "on";
const utils = new EditorTestUtils(document.body);

class MyShadow extends HTMLElement {
  #defaultInnerHTML =
    "<style>:focus { outline: 3px red solid; }</style>" +
    "<div>text" +
      "<div contenteditable=\"\">editable</div>" +
      "<object tabindex=\"0\">object</object>" +
      "<p tabindex=\"0\">paragraph</p>" +
    "</div>";
  #shadowRoot;

  constructor() {
    super();
    this.#shadowRoot = this.attachShadow({mode: this.getAttribute("data-mode")});
    this.#shadowRoot.innerHTML = this.#defaultInnerHTML;
  }

  reset() {
    this.#shadowRoot.innerHTML = this.#defaultInnerHTML;
    this.#shadowRoot.querySelector("div").getBoundingClientRect();
  }

  focusText() {
    this.focus();
    const div = this.#shadowRoot.querySelector("div");
    getSelection().collapse(div.firstChild || div, 0);
  }

  focusContentEditable() {
    this.focus();
    const contenteditable = this.#shadowRoot.querySelector("div[contenteditable]");
    contenteditable.focus();
    getSelection().collapse(contenteditable.firstChild || contenteditable, 0);
  }

  focusObject() {
    this.focus();
    this.#shadowRoot.querySelector("object[tabindex]").focus();
  }

  focusParagraph() {
    this.focus();
    const tabbableP = this.#shadowRoot.querySelector("p[tabindex]");
    tabbableP.focus();
    getSelection().collapse(tabbableP.firstChild || tabbableP, 0);
  }

  getInnerHTML() {
    return this.#shadowRoot.innerHTML;
  }

  getDefaultInnerHTML() {
    return this.#defaultInnerHTML;
  }

  getFocusedElementName() {
    return this.#shadowRoot.querySelector(":focus")?.tagName.toLocaleLowerCase() || "";
  }

  getSelectedRange() {
    // XXX There is no standardized way to retrieve selected ranges in
    //     shadow trees, therefore, we use non-standardized API for now
    //     since the main purpose of this test is checking the behavior of
    //     selection changes in shadow trees, not checking the selection API.
    const selection =
      this.#shadowRoot.getSelection !== undefined
        ? this.#shadowRoot.getSelection()
        : getSelection();
    return selection.getRangeAt(0);
  }
}

customElements.define("my-shadow", MyShadow);

function getRangeDescription(range) {
  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}`;
    }
  }
  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})`;
}

promise_test(async () => {
  await new Promise(resolve => addEventListener("load", resolve, {once: true}));
  assert_true(true, "Load event is fired");
}, "Waiting for load");

/**
 * The expected result of this test is based on Blink and Gecko's behavior.
 */

for (const mode of ["open", "closed"]) {
  const host = document.querySelector(`my-shadow[data-mode=${mode}]`);
  promise_test(async (t) => {
    host.reset();
    host.focusText();
    test(() => {
      assert_equals(
        host.getFocusedElementName(),
        "",
        `No element should have focus after ${t.name}`
      );
    }, `Focus after ${t.name}`);
    await utils.sendKey("A");
    test(() => {
      assert_equals(
        host.getInnerHTML(),
        host.getDefaultInnerHTML(),
        `The shadow DOM shouldn't be modified after ${t.name}`
      );
    }, `Typing "A" after ${t.name}`);
  }, `Collapse selection into text in the ${mode} shadow DOM`);

  promise_test(async (t) => {
    host.reset();
    host.focusContentEditable();
    test(() => {
      assert_equals(
        host.getFocusedElementName(),
        "div",
        `<div contenteditable> should have focus after ${t.name}`
      );
    }, `Focus after ${t.name}`);
    await utils.sendKey("A");
    test(() => {
      assert_equals(
        host.getInnerHTML(),
        host.getDefaultInnerHTML().replace("<div contenteditable=\"\">", "<div contenteditable=\"\">A"),
        `The shadow DOM shouldn't be modified after ${t.name}`
      );
    }, `Typing "A" after ${t.name}`);
  }, `Collapse selection into text in <div contenteditable> in the ${mode} shadow DOM`);

  promise_test(async (t) => {
    host.reset();
    host.focusObject();
    test(() => {
      assert_equals(
        host.getFocusedElementName(),
        "object",
        `The <object> element should have focus after ${t.name}`
      );
    }, `Focus after ${t.name}`);
    await utils.sendKey("A");
    test(() => {
      assert_equals(
        host.getInnerHTML(),
        host.getDefaultInnerHTML(),
        `The shadow DOM shouldn't be modified after ${t.name}`
      );
    }, `Typing "A" after ${t.name}`);
  }, `Set focus to <object> in the ${mode} shadow DOM`);

  promise_test(async (t) => {
    host.reset();
    host.focusParagraph();
    test(() => {
      assert_equals(
        host.getFocusedElementName(),
        "p",
        `The <p tabindex="0"> element should have focus after ${t.name}`
      );
    }, `Focus after ${t.name}`);
    await utils.sendKey("A");
    test(() => {
      assert_equals(
        host.getInnerHTML(),
        host.getDefaultInnerHTML(),
        `The shadow DOM shouldn't be modified after ${t.name}`
      );
    }, `Typing "A" after ${t.name}`);
  }, `Set focus to <p tabindex="0"> in the ${mode} shadow DOM`);

  promise_test(async (t) => {
    host.reset();
    host.focusParagraph();
    await utils.sendSelectAllShortcutKey();
    assert_in_array(
      getRangeDescription(host.getSelectedRange()),
      [
        // Feel free to add reasonable select all result in the <my-shadow>.
        "(#document-fragment, 0) - (#document-fragment, 2)",
        "(#text \"text\", 0) - (#text \"paragraph\", 9)",
      ],
      `Only all children of the ${mode} shadow DOM should be selected`
    );
    getSelection().collapse(document.body, 0);
  }, `SelectAll in the ${mode} shadow DOM`);

  promise_test(async (t) => {
    host.reset();
    host.focusContentEditable();
    await utils.sendSelectAllShortcutKey();
    assert_in_array(
      getRangeDescription(host.getSelectedRange()),
      [
        // Feel free to add reasonable select all result in the <div contenteditable>.
        "(<div>, 0) - (<div>, 1)",
        "(#text \"editable\", 0) - (#text \"editable\", 8)",
      ]
    );
    getSelection().collapse(document.body, 0);
  }, `SelectAll in the <div contenteditable> in the ${mode} shadow DOM`);
}
</script>
</body>
</html>