Skip to content

Conversation

@botandrose
Copy link
Collaborator

Turns out we can't simply call .id on element nodes, because that property is unreliable on <form> and <fieldset> elements. It could return the id of the node as expected, OR (problematically) a child element that has a name attribute of id. The latter breaks persistence of the form element, which can have further consequences involving lost state.

This PR has two commits:

  1. A failing test exposing the issue
  2. A commit that fixes it, but at the cost of a 5% perf hit on my machine.

I'm marking this PR as WIP in the hopes I can find a faster way to resolve this.

For more information, see: https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement#issues_with_naming_elements
Fixes #135

@botandrose
Copy link
Collaborator Author

Interestingly, it appears that the majority of the perf loss is simply in the function call itself... if I replace the implementation of the new id function with simply return node.id, the perf loss is about the same! I expected that JIT inlining would negate this, but apparently not.

Maybe a manual inline then? I don't love it, since we're accesing element ids all over the place... hmm.

@botandrose
Copy link
Collaborator Author

Okay, I got something working here with inlining the function. Perf is mostly unaffected now, and the bug is fixed, but code readability suffers. Hmm.

@botandrose botandrose changed the title [WIP] Fix persistence bug caused by untrustworthy HTMLFormElement.id getter Fix persistence bug caused by untrustworthy HTMLFormElement.id getter Jul 22, 2025
@botandrose botandrose merged commit 87b84db into main Jul 27, 2025
12 checks passed
@botandrose botandrose deleted the id-name-bug branch July 27, 2025 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Form with id field

3 participants