-
Notifications
You must be signed in to change notification settings - Fork 418
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
Consider escaping selectors #337
Comments
Alternatively couldn't we just escape it when you run your querySelector?
Just worried about that escaped sequence causing more confusion... |
You have to escape the individual parts, otherwise it'll escape the It's possible the escaped sequence could cause more confusion, looking for something with class Also, this is just to handle the "illegal" characters, mostly characters used to indicate other things in selectors, like |
Yeah, I remember encountering this exact problem when trying to debug INP issues on web.dev myself, and it stumped me for a bit until I realized the issue with invalid selector. So, at minimum, I think it warrants a mention in the attribution docs because it could potentially be stumping other people as well. However, I'm hesitant to change the default behavior of the library for a few reasons:
My sense is we should do what we can to help people (and tooling authors) avoid this problem by making it clear in the docs, but I'm hesitant change the library due to how it could potentially affect people's analytics data. |
OK, but if it separately stumped you, me, and @mmocny for a bit, clearly searching in the page is a use case and it's going to cause issues when it happens :) FWIW I didn't think to search in the web-vitals.js docs to see what was going on—I could see the Also remember this isn't all selectors class names, this is just class names with weird whitespace or characters like The point about analytics grouping over time is a good one, so I think it's just a question of weighing the downsides. My biased viewpoint:
Breaking changes are bad and we should feel bad, but our advice is to go from RUM attribution -> recreate in the lab, so to me at least it feels like we should just rip off the bandaid (for a library steadily growing in usage, the best time to make a breaking change was before you shipped, the second best time is as soon as possible :) |
Happy to discuss more. Don't worry about it somehow shipping before you're back @philipwalton :) We could pair the change with making the CSS selector unique by using |
web.dev
has amd:hidden-yes
CSS class it likes to use these days, which won't work in a selector when you go back to debug using attribution data (e.g.document.querySelector('button.md:hidden-yes')
->Uncaught DOMException: Failed to execute 'querySelector' on 'Document': 'button.md:hidden-yes' is not a valid selector
). Illegal characters have to be escaped to function in a selector (so e.g.'button.md\\:hidden-yes'
orbutton.md\\3Ahidden-yes
).Ideally these characters would be automatically escaped to ease the debugging process. This could be done on the report side, but not every viewer will remember to do this.
In the browser, you can use
CSS.escape()
, so ifgetSelector.ts
is only ever used in the browser (I believe so since no jsdom?),el.className.trim().replace(/\s+/g, '.')
could become[...el.classList].map(CSS.escape).join('.')
and call it a day (though probably worth escaping theid
/name as well).The text was updated successfully, but these errors were encountered: