<!DOCTYPE html>
<meta charset="utf-8">
<link rel="help" href="https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#form-submission-algorithm">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./resources/targetted-form.js"></script>
<!--
  The submit() in event handler should *not* get superseded in this case by the
  default action submit(), because event handler here calls preventDefault().
-->
<body>
<script>
promise_test(async () => {
  let form = populateForm('<input name=n1 value=v1><input type=submit>');
  let iframe = form.previousSibling;
  let input = form.querySelector("input[name=n1]");
  let submitter = form.querySelector("input[type=submit]");
  submitter.addEventListener('click', (e) => {
    e.preventDefault();
    input.value = 'v2';
    form.submit();
    input.value = 'v3';
    form.submit();
    input.value = 'v4';
  });
  submitter.click();
  await loadPromise(iframe);
  assert_equals(getParamValue(iframe, "n1"), "v3");
}, 'PreventDefaulting input onclick should allow submit() to succeed');

promise_test(async () => {
  let form = populateForm('<input name=n1 value=v1><button>submit</button>');
  let iframe = form.previousSibling;
  let input = form.querySelector("input[name=n1]");
  let submitter = form.querySelector("button");
  submitter.addEventListener('click', (e) => {
    e.preventDefault();
    input.value = 'v2';
    form.submit();
    input.value = 'v3';
    form.submit();
    input.value = 'v4';
  });
  submitter.click();
  await loadPromise(iframe);
  assert_equals(getParamValue(iframe, "n1"), "v3");
}, 'PreventDefaulting button onclick should allow submit() to succeed');

promise_test(async () => {
  let form = populateForm('<input name=n1 value=v1><input type=submit>');
  let iframe = form.previousSibling;
  let input = form.querySelector("input[name=n1]");
  let submitter = form.querySelector("input[type=submit]");
  form.addEventListener('click', (e) => {
    e.preventDefault();
    input.value = 'v2';
    form.submit();
    input.value = 'v3';
    form.submit();
    input.value = 'v4';
  });
  submitter.click();
  await loadPromise(iframe);
  assert_equals(getParamValue(iframe, "n1"), "v3");
}, 'PreventDefaulting form onclick should allow submit() to succeed');
</script>
</body>
