ericclemmons / ericclemmons/unique-selector
Uncaught DOMException when element id contains a '.'
- Dominant language
- JavaScript
- Stars
- 272
- Forks
- 55
- PR merge metrics
- No merged PRs in 30d
Description
Similar to #28, if an element's class name begins with a number, we get the same DOM error:
```
Uncaught DOMException: Failed to execute 'querySelectorAll' on 'Element': '#x0.987954339334459x1624591192249' is not a valid selector.
```
Following the fix for #28 in #34, below is my local fix of the library using `patch-package`.
But it might be worth just filtering any invalid class names?
```
index 858bfd6..42463ea 100644
--- a/node_modules/unique-selector/lib/getClasses.js
+++ b/node_modules/unique-selector/lib/getClasses.js
@@ -42,6 +42,7 @@ function getClasses(el) {
function getClassSelectors(el) {
var classList = getClasses(el).filter(Boolean);
return classList.map(function (cl) {
- return '.' + cl;
+ // if the CLASS starts with a number or contains ":" selecting with a dot will cause a DOMException
+ return cl.match(/(?:^\d|:)/) ? '[class*="' + cl + '"]' : '.' + cl;
});
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.