|
| 1 | +// a11y/form-field-multiple-labels counts label ELEMENTS (HTMLInputElement.labels), so a label that |
| 2 | +// wraps a control and also points at it with for= is one label (#1249). |
| 3 | + |
| 4 | +import { describe, expect, test } from "bun:test"; |
| 5 | + |
| 6 | +import { parsePage } from "@squirrelscan/parser"; |
| 7 | + |
| 8 | +import { formFieldMultipleLabelsRule } from "../src/a11y/form-field-multiple-labels"; |
| 9 | +import type { CheckResult, RuleContext } from "../src/types"; |
| 10 | + |
| 11 | +function run(body: string): CheckResult | undefined { |
| 12 | + const html = `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Gift card amount selection form test page</title></head><body><main>${body}</main></body></html>`; |
| 13 | + const url = "https://example.com/"; |
| 14 | + const ctx = { |
| 15 | + page: { url, html, statusCode: 200, loadTime: 0, headers: {} }, |
| 16 | + parsed: parsePage(html, url), |
| 17 | + options: {}, |
| 18 | + } as unknown as RuleContext; |
| 19 | + const result = formFieldMultipleLabelsRule.run(ctx); |
| 20 | + if (result instanceof Promise) throw new Error("form-field-multiple-labels is async"); |
| 21 | + return result.checks.find((c) => c.name === "form-field-multiple-labels"); |
| 22 | +} |
| 23 | + |
| 24 | +describe("a11y/form-field-multiple-labels counts label elements (#1249)", () => { |
| 25 | + test("the reporter's gift card page passes", () => { |
| 26 | + const result = run(`<h1>Gift card</h1><form><fieldset><legend>Amount</legend> |
| 27 | +<label for="a"><input type="radio" name="amt" id="a" value="50"> 50</label> |
| 28 | +<label for="b"><input type="radio" name="amt" id="b" value="100"> 100</label> |
| 29 | +</fieldset></form>`); |
| 30 | + expect(result?.status).toBe("pass"); |
| 31 | + expect(result?.items).toBeUndefined(); |
| 32 | + }); |
| 33 | + |
| 34 | + test.each([ |
| 35 | + ["wrap only", `<label><input id="x" type="text"> X</label>`], |
| 36 | + ["for only", `<label for="x">X</label><input id="x" type="text">`], |
| 37 | + ["wrap plus for to itself", `<label for="x"><input id="x" type="text"> X</label>`], |
| 38 | + [ |
| 39 | + "wrap plus for, no id to match twice", |
| 40 | + `<label for="x"><input id="x" name="n" type="text"></label>`, |
| 41 | + ], |
| 42 | + ])("%s passes", (_name, body) => { |
| 43 | + expect(run(body)?.status).toBe("pass"); |
| 44 | + }); |
| 45 | + |
| 46 | + test("two label elements for one id still warn, with the real count", () => { |
| 47 | + const result = run( |
| 48 | + `<label for="y">One</label><label for="y">Two</label><input id="y" name="y" type="text">`, |
| 49 | + ); |
| 50 | + expect(result?.status).toBe("warn"); |
| 51 | + expect(result?.items?.[0]?.id).toBe("y (2 labels)"); |
| 52 | + }); |
| 53 | + |
| 54 | + test("a wrapping label plus a separate label for= still warns", () => { |
| 55 | + const result = run( |
| 56 | + `<label>Wrap <input id="z" name="z" type="text"></label><label for="z">Other</label>`, |
| 57 | + ); |
| 58 | + expect(result?.status).toBe("warn"); |
| 59 | + expect(result?.items?.[0]?.id).toBe("z (2 labels)"); |
| 60 | + }); |
| 61 | + |
| 62 | + test("a wrapping label whose for= points at another control does not label this one", () => { |
| 63 | + const result = run( |
| 64 | + `<label for="other">Other <input id="inner" name="inner" type="text"></label><input id="other" name="other" type="text"><label for="inner">Inner</label>`, |
| 65 | + ); |
| 66 | + // `inner` is labelled by its for= label only; the wrapper belongs to `other`. |
| 67 | + expect(result?.status).toBe("pass"); |
| 68 | + }); |
| 69 | + |
| 70 | + test("a wrapping label labels only its first labelable control", () => { |
| 71 | + const result = run( |
| 72 | + `<label>Both <input id="p" name="p" type="text"><input id="q" name="q" type="text"></label><label for="q">Q</label>`, |
| 73 | + ); |
| 74 | + expect(result?.status).toBe("pass"); |
| 75 | + }); |
| 76 | + |
| 77 | + test("an id with selector characters is matched literally", () => { |
| 78 | + const result = run( |
| 79 | + `<label for='a"]b'>A</label><label for='a"]b'>B</label><input id='a"]b' name="w" type="text">`, |
| 80 | + ); |
| 81 | + expect(result?.status).toBe("warn"); |
| 82 | + expect(result?.items?.[0]?.id).toBe("w (2 labels)"); |
| 83 | + }); |
| 84 | +}); |
0 commit comments