// scripts/check-form-button-browser.ts import assert from "node:assert/strict"; import type { Page } from "playwright"; /** Form controls can hide their own form's named methods after association. */ export async function checkFormButtonNames(page: Page, check: (name: string) => void): Promise { const rows = await page.evaluate(() => { const iframe = document.body.appendChild(document.createElement("iframe")); const measurements = []; try { for (const doc of [document, iframe.contentDocument!]) { const foreignForm = doc !== document; for (const tag of ["button", "m-button", "m-icon-button", "m-fab", "m-extended-fab"]) { for (const type of ["reset", "submit"] as const) { for (const attribute of ["name", "id"]) { const method = type === "reset" ? "reset" : "requestSubmit"; const form = doc.createElement("form"); const input = document.createElement("input"); input.name = "sample"; input.defaultValue = "initial"; input.value = "edited"; const button = document.createElement(tag); button.setAttribute("type", type); button.setAttribute(attribute, method); button.setAttribute("aria-label", "Action"); button.textContent = "Action"; // Keep the form's original prototype while the controls render in this document. form.append(input, button); document.body.append(form); const errors: string[] = []; const recordError = (event: ErrorEvent) => { errors.push(event.message); event.preventDefault(); }; window.addEventListener("error", recordError); let events = 0; let submitted: FormDataEntryValue | null = null; form.addEventListener(type, event => { events++; if (type === "submit") { event.preventDefault(); submitted = new FormData(form).get("sample"); } }); const realmMatches = (form instanceof HTMLFormElement) === !foreignForm; const shadowed = (Reflect.get(form, method) as unknown) === button; try { const activation = tag === "button" ? button : button.shadowRoot?.querySelector("button"); if (!activation) throw new Error(`Missing inner button for ${tag}`); activation.click(); measurements.push({ where: `${tag} ${type} ${attribute}=${method} (${foreignForm ? "iframe form prototype" : "same realm"})`, type, realmMatches, shadowed, events, value: input.value, submitted, errors, }); } finally { window.removeEventListener("error", recordError); form.remove(); } } } } } } finally { iframe.remove(); } return measurements; }); const failures = []; for (const { where, type, realmMatches, shadowed, ...actual } of rows) { const expected = type === "reset" ? { events: 1, value: "initial", submitted: null } : { events: 1, value: "edited", submitted: "edited" }; if (!realmMatches || !shadowed || actual.events !== expected.events || actual.value !== expected.value || actual.submitted !== expected.submitted || actual.errors.length) { failures.push(`${where}: ${JSON.stringify({ realmMatches, shadowed, ...actual })}`); } else { check(`form button: ${where} performs the form action once`); } } assert.equal(failures.length, 0, `Form button naming collisions: ${failures.length} of ${rows.length} failed\n${failures.join("\n")}`); }