summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/forms/beforeinput.tentative.html
blob: 7aa51a252300a0bff9d5d338892921faff9e57f8 (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Test the onbeforeinput attribute</title>
<meta name="timeout" content="long">
<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>

<div id="container"></div>
<script>
  const container = document.getElementById("container");
  const events = new Map();

  function handleEvent(event) {
    if (!events.has(event.target)) {
      events.set(event.target, []);
    }
    events.get(event.target).push(event);
  }

  let onInputFired = null;

  const onBeforeInput = handleEvent;
  const onInput = (event) => {
    handleEvent(event);
    onInputFired()
  }

  let elems = [];
  for (let type of ["text", "search", "tel", "url", "email", "password", "number"]) {
    elems.push(`<input type=${type} onbeforeinput="onBeforeInput(event)" oninput="onInput(event)">
<input type=${type}>
`);
  }
  elems.push(`<textarea onbeforeinput="onBeforeInput(event)" oninput="onInput(event)"></textarea>
<textarea></textarea>`)
  container.innerHTML = elems.join("");

for (const element of container.children) {
  promise_test(async t => {
    if (!element.hasAttribute("onbeforeinput")) {
      element.onbeforeinput = e => onBeforeInput(e);
      element.oninput = e => onInput(e);
    };

    let afterOnInput = new Promise(resolve => {onInputFired = resolve});
    await test_driver.send_keys(element, "1"); // has to be a number so <input type=number> works
    // Ensure we're in the post-update state
    await afterOnInput;

    assert_true(events.has(element), "Got events for element");
    let elementEvents = events.get(element);

    assert_array_equals(elementEvents.map(event => event.type), ["beforeinput", "input"], "Got expected events");
  }, `${element.outerHTML}`);
}
</script>