Skip to content
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

Uncaught DOMException when element id contains a '.' #55

Open
nadine-nguyen opened this issue Jun 25, 2021 · 2 comments
Open

Uncaught DOMException when element id contains a '.' #55

nadine-nguyen opened this issue Jun 25, 2021 · 2 comments

Comments

@nadine-nguyen
Copy link

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;
   });
 }
@nadine-nguyen
Copy link
Author

nadine-nguyen commented Jun 28, 2021

My bad. Turns out the issue was in getID again and not in getClasses

Here was my modified regex for invalid selectors to be more specific:

   if (id !== null && id !== '') {
-    // if the ID starts with a number or contains ":" selecting with a hash will cause a DOMException
-    return id.match(/(?:^\d|:)/) ? '[id="' + id + '"]' : '#' + id;
+    // ID must start with either a letter or a underscore
+    // then must only contain either letters, numbers, underscores or dash to be valid
+    return id.match(/(?:^[a-zA-Z_][\w-]*$)/) ? '#' + id : '[id="' + id + '"]';
   }

@nadine-nguyen nadine-nguyen changed the title Uncaught DOMException when element class start with a number Uncaught DOMException when element id contains a '.' Jun 28, 2021
@tathastu871
Copy link

@ericclemmons
@nadine-nguyen
Just change
const elements = parentNode.querySelectorAll( selector );

to

const elements = parentNode.querySelectorAll( CSS.escape(selector) );

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

No branches or pull requests

2 participants