summaryrefslogtreecommitdiffstats
path: root/dom/events/test/test_submitevent_on_form.html
blob: 01cd8f28ee23365a7348e73f3f71ba402d42246d (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test submit event on form</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
  <form action="javascript:doDefault()" id="form">
    <input name="n1" value="v1">
    <input type="submit" value="Do Default Action">
  </form>
  <pre id="test">
  <script type="application/javascript">
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(runTests);

var defaultActionCount = 0;

function doDefault()
{
  defaultActionCount++;
}

async function runTests()
{
  const form = document.getElementById("form");
  let formDataInEvent = null;

  // Test if submission from submit() is deferred.
  await SpecialPowers.pushPrefEnv({
    set: [["dom.forms.submit.trusted_event_only", false]],
  });

  form.addEventListener("submit", function(e) {
    form.elements[0].value = "v1";
    form.submit();
    form.elements[0].value = "v2";
  }, { once: true });

  form.addEventListener('formdata', e => {
    formDataInEvent = e.formData;
  });

  form.dispatchEvent(new Event('submit'));
  await new Promise(resolve => SimpleTest.executeSoon(resolve));

  is(defaultActionCount, 1, "should only submit once");
  is(formDataInEvent.get('n1'), 'v2', "submission from submit() should be deferred");

  // Test untrusted submit event shouldn't trigger form default action.
  defaultActionCount = 0;
  await SpecialPowers.pushPrefEnv({
    set: [["dom.forms.submit.trusted_event_only", true]],
  });

  form.dispatchEvent(new Event('submit'));
  setTimeout(() => {
    is(defaultActionCount, 0, "untrusted submit event shouldn't trigger form default action");
    SimpleTest.finish();
  });
}
  </script>
  </pre>
</body>
</html>