Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

6.3 KB
Raw Blame
<!doctype html>
<meta charset="utf-8">
<script src="/wpt/resources/testharness.js"></script>
<script src="/wpt/resources/testharnessreport.js"></script>
<script src="/wpt/resources/moli-wpt-adapter.js"></script>
<body></body>
<script>
function read_frame_text(frame) {
  return frame.contentDocument?.body?.textContent?.trim() ?? "";
}

function read_frame_search(frame) {
  return frame.contentWindow?.location?.search ?? "";
}

function wait(ms) {
  return new Promise(function (resolve) {
    setTimeout(resolve, ms);
  });
}

function wait_for_value(read, expected, description) {
  return Promise.race([
    new Promise(function (resolve, reject) {
      const deadline = Date.now() + 1000;
      function poll() {
        const value = read();
        if (value === expected) {
          resolve(value);
          return;
        }
        if (Date.now() >= deadline) {
          reject(new Error("timed out waiting for " + description + ": got " + value));
          return;
        }
        setTimeout(poll, 10);
      }
      poll();
    }),
    wait(1100).then(function () {
      throw new Error("timed out waiting for " + description);
    }),
  ]);
}

const buttonFrame = document.createElement("iframe");
buttonFrame.name = "submit-click-button-target";
document.body.appendChild(buttonFrame);

const buttonForm = document.createElement("form");
buttonForm.target = "submit-click-button-target";
buttonForm.action = "resources/formaction-button-submitter.html?ignored=button-click";
const buttonSubmitter = document.createElement("button");
buttonSubmitter.type = "submit";
buttonSubmitter.textContent = "button submit";
buttonForm.appendChild(buttonSubmitter);
const buttonEvents = [];
let buttonSubmitEvent = null;
buttonSubmitter.addEventListener("click", function () {
  buttonEvents.push("click");
});
buttonForm.addEventListener("submit", function (event) {
  buttonEvents.push("submit");
  buttonSubmitEvent = event;
});
document.body.appendChild(buttonForm);

const inputFrame = document.createElement("iframe");
inputFrame.name = "submit-click-input-target";
document.body.appendChild(inputFrame);

const inputForm = document.createElement("form");
inputForm.target = "submit-click-input-target";
inputForm.action = "resources/formaction-input-submitter.html?ignored=input-click";
const inputSubmitter = document.createElement("input");
inputSubmitter.type = "submit";
inputSubmitter.value = "input submit";
inputForm.appendChild(inputSubmitter);
const inputEvents = [];
let inputSubmitEvent = null;
inputSubmitter.addEventListener("click", function () {
  inputEvents.push("click");
});
inputForm.addEventListener("submit", function (event) {
  inputEvents.push("submit");
  inputSubmitEvent = event;
});
document.body.appendChild(inputForm);

const preventedFrame = document.createElement("iframe");
preventedFrame.name = "submit-click-prevented-target";
document.body.appendChild(preventedFrame);

const preventedForm = document.createElement("form");
preventedForm.target = "submit-click-prevented-target";
preventedForm.action = "resources/formaction-form-submit.html?ignored=prevented-click";
const preventedSubmitter = document.createElement("button");
preventedSubmitter.type = "submit";
preventedSubmitter.textContent = "prevented submit";
preventedForm.appendChild(preventedSubmitter);
const preventedEvents = [];
let preventedSubmitEvent = null;
preventedSubmitter.addEventListener("click", function () {
  preventedEvents.push("click");
});
preventedForm.addEventListener("submit", function (event) {
  preventedEvents.push("submit");
  preventedSubmitEvent = event;
  event.preventDefault();
});
document.body.appendChild(preventedForm);

buttonSubmitter.click();
inputSubmitter.click();
preventedSubmitter.click();

test(function () {
  assert_array_equals(buttonEvents, ["click", "submit"], "button click should fire click before submit");
  assert_true(buttonSubmitEvent instanceof SubmitEvent, "button click should dispatch SubmitEvent");
  assert_equals(buttonSubmitEvent.submitter, buttonSubmitter, "button click should preserve the button submitter");
  assert_equals(buttonSubmitEvent.target, buttonForm, "button submit target should remain the form");
  assert_true(buttonSubmitEvent.bubbles, "button click should dispatch a bubbling submit event");
  assert_true(buttonSubmitEvent.cancelable, "button click should dispatch a cancelable submit event");

  assert_array_equals(inputEvents, ["click", "submit"], "input click should fire click before submit");
  assert_true(inputSubmitEvent instanceof SubmitEvent, "input click should dispatch SubmitEvent");
  assert_equals(inputSubmitEvent.submitter, inputSubmitter, "input click should preserve the input submitter");
  assert_equals(inputSubmitEvent.target, inputForm, "input submit target should remain the form");
  assert_true(inputSubmitEvent.bubbles, "input click should dispatch a bubbling submit event");
  assert_true(inputSubmitEvent.cancelable, "input click should dispatch a cancelable submit event");

  assert_array_equals(preventedEvents, ["click", "submit"], "prevented click should still fire click before submit");
  assert_true(preventedSubmitEvent instanceof SubmitEvent, "prevented click should dispatch SubmitEvent");
  assert_equals(
    preventedSubmitEvent.submitter,
    preventedSubmitter,
    "prevented click should preserve the button submitter",
  );
  assert_equals(preventedSubmitEvent.target, preventedForm, "prevented submit target should remain the form");
}, "submit button click dispatches click then SubmitEvent with the active submitter");

promise_test(async function () {
  await wait_for_value(function () {
    return read_frame_text(buttonFrame);
  }, "formaction-button-submitter", "button click navigation");
  await wait_for_value(function () {
    return read_frame_text(inputFrame);
  }, "formaction-input-submitter", "input click navigation");

  assert_equals(
    read_frame_text(buttonFrame),
    "formaction-button-submitter",
    "button click should navigate the named target",
  );
  assert_equals(
    read_frame_text(inputFrame),
    "formaction-input-submitter",
    "input click should navigate the named target",
  );
  assert_equals(read_frame_text(preventedFrame), "", "prevented submit click should keep the named target unchanged");
  assert_equals(read_frame_search(preventedFrame), "", "prevented submit click should keep the target URL unchanged");
}, "submit button click couples SubmitEvent cancellation to navigation");
</script>