File: form-reset-callback.html

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (84 lines) | stat: -rw-r--r-- 3,077 bytes parent folder | download | duplicates (10)
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
<!DOCTYPE html>
<body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
class MyControl extends HTMLElement {
  static get formAssociated() { return true; }

  constructor() {
    super();
    this.resetCalled_ = false;
  }

  formResetCallback() {
    this.resetCalled_ = true;
    if (this.outputElement_) {
      this.outputValueDuringResetCallback_ = this.outputElement_.value;
    }
  }
  get resetCalled() { return this.resetCalled_; }

  // This is only used in a single test below; the idea is this: before the form
  // gets reset (i.e., before `formResetCallback()` is queued and run by the
  // platform), we assign an `HTMLOutputElement` whose value we record *while*
  // `formResetCallback()` runs. This lets the test verify that built-in form
  // controls are reset *before* custom element `formResetCallback()`s are run
  // with custom element reaction timing, at the very end of the form reset
  // process.
  set outputToObserve(output) {
    this.outputElement_ = output;
  }
  get outputElementValueDuringResetCallback() {
    return this.outputValueDuringResetCallback_;
  }
}
customElements.define('my-control', MyControl);

test(() => {
  document.body.insertAdjacentHTML('beforeend',
      '<form><my-control></my-control></form>');
  let form = document.body.lastChild;
  let custom = form.firstChild;
  form.reset();
  assert_true(custom.resetCalled);
}, 'form.reset() should trigger formResetCallback');

test(() => {
  document.body.insertAdjacentHTML('beforeend',
      '<form><my-control></my-control><output>default</output></form>');
  let form = document.body.lastChild;
  let custom = form.firstChild;
  let output = form.lastChild;
  output.value = 'updated';

  // This is the `HTMLOutputElement` that `custom` will record the `value` of,
  // when `custom`'s `formResetCallback()` runs.
  custom.outputToObserve = output;

  // Reset the form.
  assert_false(custom.resetCalled,
      "formResetCallback is not called before the form is reset");
  form.reset();
  assert_true(custom.resetCalled, "formResetCallback is called " +
      "synchronously after the form is reset");
  assert_equals(custom.outputElementValueDuringResetCallback, "default",
      "formResetCallback() runs *after* built-in form control reset " +
      "algorithms run, and can observe their effects");
}, 'form.reset(): formResetCallback is called synchronously at the end of ' +
   'form reset, with custom element reaction timing, *after* built-in form ' +
   'control reset algorithms run.');

promise_test(() => {
  document.body.insertAdjacentHTML('beforeend',
      '<form><my-control></my-control><input type=reset></form>');
  let form = document.body.lastChild;
  let custom = form.firstChild;
  let resetButton = form.lastChild;
  assert_false(custom.resetCalled);
  resetButton.click();
  assert_false(custom.resetCalled);
  return Promise.resolve().then(() => assert_true(custom.resetCalled));
}, 'Clicking a reset button invokes formResetCallback in a microtask');
</script>
</body>