-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(plugin-forms): give each embedded form its own element ids #3031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eisenbruch
wants to merge
3
commits into
emdash-cms:main
Choose a base branch
from
eisenbruch:fix/form-embed-duplicate-ids
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@emdash-cms/plugin-forms": patch | ||
| --- | ||
|
|
||
| Fixes duplicate element IDs when the same form is embedded more than once on a page. Field IDs were built from the form ID and the field name alone, so a form appearing in, say, a sidebar and a pop-up produced several elements sharing an ID: clicking a label focused the first copy rather than the one beside it, and anything resolving an ID — `aria-describedby`, a password manager, a test selector — reached the wrong instance. Each rendering now suffixes its IDs with a per-instance value, so labels, inputs and the honeypot stay paired within their own copy. Element IDs are not part of the plugin's API and nothing else references them; the client script scopes its lookups to the form element. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/plugins/forms/tests/form-embed-ids.render.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * Renders FormEmbed twice, the way a page embedding the same form in a rail and a pop-up does, and pins that the | ||
| * two renderings share no element ids. | ||
| */ | ||
| import { experimental_AstroContainer as AstroContainer } from "astro/container"; | ||
| import { beforeAll, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| import type { PublicFormDefinition } from "../src/public-definition.js"; | ||
|
|
||
| const definition: PublicFormDefinition = { | ||
| name: "Newsletter", | ||
| slug: "newsletter", | ||
| pages: [ | ||
| { | ||
| fields: [ | ||
| { | ||
| id: "email", | ||
| type: "email", | ||
| label: "Email", | ||
| name: "email", | ||
| required: true, | ||
| width: "full", | ||
| }, | ||
| { id: "name", type: "text", label: "Name", name: "name", required: false, width: "full" }, | ||
| ], | ||
| }, | ||
| ], | ||
| settings: { spamProtection: "honeypot", submitLabel: "Subscribe" }, | ||
| status: "active", | ||
| _turnstileSiteKey: null, | ||
| }; | ||
|
|
||
| vi.mock("emdash/plugin-utils", () => ({ getPublicPluginApiRouteHandler: () => undefined })); | ||
| vi.mock("../src/public-definition.js", () => ({ | ||
| loadPublicFormDefinition: () => Promise.resolve(definition), | ||
| })); | ||
|
|
||
| const idsIn = (html: string): string[] => Array.from(html.matchAll(/\sid="([^"]+)"/g), (m) => m[1]); | ||
| const labelTargetsIn = (html: string): string[] => | ||
| Array.from(html.matchAll(/<label[^>]*\sfor="([^"]+)"/g), (m) => m[1]); | ||
|
|
||
| describe("FormEmbed element ids", () => { | ||
| let first: string; | ||
| let second: string; | ||
|
|
||
| beforeAll(async () => { | ||
| const { default: FormEmbed } = await import("../src/astro/FormEmbed.astro"); | ||
| const container = await AstroContainer.create(); | ||
| const render = () => | ||
| container.renderToString(FormEmbed, { props: { node: { formId: "newsletter" } } }); | ||
| first = await render(); | ||
| second = await render(); | ||
| }); | ||
|
|
||
| test("two renderings of the same form share no element ids", () => { | ||
| const a = idsIn(first); | ||
| const b = idsIn(second); | ||
| expect(a.length).toBeGreaterThan(0); | ||
| expect(a.filter((id) => b.includes(id))).toEqual([]); | ||
| }); | ||
|
|
||
| test("ids are unique within a rendering", () => { | ||
| const a = idsIn(first); | ||
| expect(new Set(a).size).toBe(a.length); | ||
| }); | ||
|
|
||
| test("every label points at an input in its own rendering", () => { | ||
| for (const [html, which] of [ | ||
| [first, "first"], | ||
| [second, "second"], | ||
| ] as const) { | ||
| const ids = new Set(idsIn(html)); | ||
| const targets = labelTargetsIn(html); | ||
| expect(targets.length, `${which} rendering has labels`).toBeGreaterThan(0); | ||
| for (const target of targets) { | ||
| expect(ids.has(target), `${which}: label for="${target}" matches an id`).toBe(true); | ||
| } | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion] A regression test would guard this fix. The PR description mentions rendering the component twice with
experimental_AstroContainerand asserting the IDs are distinct — that test is worth adding, since the current suite has no Astro-component coverage and the fix is otherwise only a string suffix that a future refactor could easily drop.