summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/events/no-focus-events-at-clicking-editable-content-in-link.html
blob: dc08636c467fdfa23caedc2cccb1e5f570a90b82 (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
<!doctype html>
<html>
<head>
<meta chareset="utf-8">
<title>Clicking editable content in link shouldn't cause redundant focus related events</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
</head>
<body>
<a href="#"><span contenteditable>Hello</span></a>
<a href="#" contenteditable><span>Hello</span></a>
<script>
function promiseTicks() {
  return new Promise(resolve => {
    requestAnimationFrame(() => {
      requestAnimationFrame(resolve);
    });
  });
}

async function clickElementAndCollectFocusEvents(x, y, options) {
  await promiseTicks();
  let events = [];
  for (const eventType of ["focus", "blur", "focusin", "focusout"]) {
    document.addEventListener(eventType, event => {
      events.push(`type: ${event.type}, target: ${event.target.nodeName}`);
    }, {capture: true});
  }

  const waitForClickEvent = new Promise(resolve => {
    addEventListener("click", resolve, {capture: true, once: true});
  });

  await new test_driver
    .Actions()
    .pointerMove(x, y, options)
    .pointerDown()
    .pointerUp()
    .send();

  await waitForClickEvent;
  await promiseTicks();
  return events;
}

promise_test(async t => {
  document.activeElement?.blur();
  const editingHost = document.querySelector("span[contenteditable]");
  editingHost.blur();
  const focusEvents =
    await clickElementAndCollectFocusEvents(5, 5, {origin: editingHost});
  assert_array_equals(
    focusEvents,
    [
      "type: focus, target: SPAN",
      "type: focusin, target: SPAN",
    ],
    "Click event shouldn't cause redundant focus events");
}, "Click editable element in link");

promise_test(async t => {
  document.activeElement?.blur();
  const editingHost = document.querySelector("a[contenteditable]");
  editingHost.blur();
  const focusEvents =
    await clickElementAndCollectFocusEvents(5, 5, {origin: editingHost});
  assert_array_equals(
    focusEvents,
    [
      "type: focus, target: A",
      "type: focusin, target: A",
    ],
    "Click event shouldn't cause redundant focus events");
}, "Click editable link");
</script>
</body>
</html>