Skip to content

fix: extra protection around tagName accessor #13

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

Merged
merged 2 commits into from
May 19, 2025

Conversation

tbiethman
Copy link

@tbiethman tbiethman commented May 15, 2025

Alert: https://cypressio.slack.com/archives/C0614M4DZV5/p1746718971129419

We have a test case in the cypress-services repo that specifically overrides a form element's tagName property using a nested <input id="tagName"> element. When the form is processed with unique-selector, unique-selector will throw as a result of this overridden property that references an element (rather than the expected string). This is causing a few alerts to come through (and knocking our Cypress reports into a "Processed with errors" state).

So, added a little check to ignore tagName when it's not the expected type.

I looked at the other accessors in use here, and most everything else goes through a getAttribute call. I [don't think we can really be exhaustive here without going overboard, but tagName overrides are something the App has run into as well, so we might as well work around it (and keep our own tests happy). If we notice other goofy things happening to elements, we can evaluate further protections.

src/getTag.js Outdated
Comment on lines 13 to 16
// This can happen when a <form> element contains an <input>
// with an id of `tagName`. In this case, the form element's
// tagName property is a reference to the input element, not
// a string.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wuuuuuuut?

TIL

src/getTag.js Outdated
@@ -6,6 +6,17 @@
*/
export function getTag( el, filter )
{
if (typeof el.tagName !== 'string') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe fall back to try nodeName in this case, and if that's also non-string then all bets are off and return null

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, validating it in the browser and it seems like nodeName is not effected by the tagName override (unless an input has an id of nodeName, in which case it is lol).

So the only place we'd fall down is if we had a form like:

<form>
  <input id="tagName" />
  <input id="nodeName" />
</form>

nodeName is used by the isElement checks so having that overridden would be much more painful. I don't think we've seen that yet 🤞

@tbiethman tbiethman requested a review from mike-plummer May 15, 2025 18:16
@tbiethman tbiethman merged commit b970b33 into master May 19, 2025
3 checks passed
Copy link

🎉 This PR is included in version 2.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants