summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/editing/other/select-all-and-delete-in-html-element-having-contenteditable.html
blob: ab324c6d034b0ca975dfc6eba72e36ab0fbb2a7e (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
<!doctype html>
<html contenteditable>
<head>
<meta charset=utf-8>
<title>Test "Select all" and deletion work with &lt;html contenteditable&gt;</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>
</head>
<body>
<script>
"use strict";

const kBackspaceKey = "\uE003";
const kDeleteKey = "\uE017";
const kMeta = "\uE03d";
const kControl = "\uE009";

async function selectAllWithKey(elementToSelectAll) {
  if (elementToSelectAll.length === 0) {
    throw "element to select all must not be empty";
  }
  getSelection().collapse(elementToSelectAll, 0);
  try {
    await new test_driver.Actions()
      .keyDown(kControl)
      .keyDown("a")
      .keyUp("a")
      .keyUp(kControl)
      .send();
    if (!getSelection().isCollapsed) {
      return;
    }
    await new test_driver.Actions()
      .keyDown(kMeta)
      .keyDown("a")
      .keyUp("a")
      .keyUp(kMeta)
      .send();
    if (!getSelection().isCollapsed) {
      return;
    }
  } catch (ex) {
    throw ex;
  }
  throw "Neither Control-A nor Meta-A does select all contents";
}

function deleteWithBackspaceKey() {
  return new test_driver.Actions()
    .keyDown(kBackspaceKey)
    .keyUp(kBackspaceKey)
    .send();
}

function deleteWithDeleteKey() {
  return new test_driver.Actions()
    .keyDown(kDeleteKey)
    .keyUp(kDeleteKey)
    .send();
}

promise_test(async () => {
  document.body.innerHTML = "abc";
  await selectAllWithKey(document.body);
  await deleteWithBackspaceKey();
  assert_in_array(document.body.innerHTML, ["", "<br>"]);
}, "Select All, then, Backspace");

promise_test(async () => {
  document.body.innerHTML = "abc";
  await selectAllWithKey(document.body);
  await deleteWithDeleteKey();
  assert_in_array(document.body.innerHTML, ["", "<br>"]);
}, "Select All, then, Delete");

promise_test(async () => {
  document.body.innerHTML = "abc";
  document.execCommand("selectall");
  await deleteWithBackspaceKey();
  assert_in_array(document.body.innerHTML, ["", "<br>"]);
}, 'execCommand("selectall"), then, Backspace');

promise_test(async () => {
  document.body.innerHTML = "abc";
  document.execCommand("selectall");
  await deleteWithDeleteKey();
  assert_in_array(document.body.innerHTML, ["", "<br>"]);
}, 'execCommand("selectall"), then, Delete');

promise_test(async () => {
  document.body.innerHTML = "abc";
  await selectAllWithKey(document.body);
  document.execCommand("forwarddelete", false, false);
  assert_in_array(document.body.innerHTML, ["", "<br>"]);
}, 'Select All, then, execCommand("forwarddelete")');

promise_test(async () => {
  document.body.innerHTML = "abc";
  await selectAllWithKey(document.body);
  document.execCommand("delete", false, false);
  assert_in_array(document.body.innerHTML, ["", "<br>"]);
}, 'Select All, then, execCommand("delete")');

test(() => {
  document.body.innerHTML = "abc";
  document.execCommand("selectall");
  document.execCommand("forwarddelete", false, false);
  assert_in_array(document.body.innerHTML, ["", "<br>"]);
}, 'execCommand("selectall"), then, execCommand("forwarddelete")');

test(() => {
  document.body.innerHTML = "abc";
  document.execCommand("selectall");
  document.execCommand("delete", false, false);
  assert_in_array(document.body.innerHTML, ["", "<br>"]);
}, 'execCommand("selectall"), then, execCommand("delete")');

promise_test(async () => {
  document.body.innerHTML = "abc";
  getSelection().selectAllChildren(document.documentElement);
  await deleteWithBackspaceKey();
  assert_in_array(document.body.innerHTML, ["", "<br>"]);
}, 'getSelection().selectAllChildren(document.documentElement), then, Backspace');

promise_test(async () => {
  document.body.innerHTML = "abc";
  getSelection().selectAllChildren(document.documentElement);
  await deleteWithDeleteKey();
  assert_in_array(document.body.innerHTML, ["", "<br>"]);
}, 'getSelection().selectAllChildren(document.documentElement), then, Delete');

test(() => {
  document.body.innerHTML = "abc";
  getSelection().selectAllChildren(document.documentElement);
  document.execCommand("forwarddelete", false, false);
  assert_in_array(document.body.innerHTML, ["", "<br>"]);
}, 'getSelection().selectAllChildren(document.documentElement), then, execCommand("forwarddelete")');

test(() => {
  document.body.innerHTML = "abc";
  getSelection().selectAllChildren(document.documentElement);
  document.execCommand("delete", false, false);
  assert_in_array(document.body.innerHTML, ["", "<br>"]);
}, 'getSelection().selectAllChildren(document.documentElement), then, execCommand("delete")');

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