1

The first answer that popped into my head was: "using querySelector method wrapped into a try - catch error handler" Something like this:

const cssString = '* > a[href *= "://adclick"]';
try {
    document.querySelector(cssString);
    console.log('Valid')
} catch {
    console.log('Not Valid')
}

I was puzzled when I found that removing either the closing square bracket or the closing double quote the syntax was still correct (the exception is not raised). Is there a reason for this? I mean: how does the syntax be correct without one or both of the above characters? Is there an efficient and safe way to test these syntaxes?

Thanks

Robbi
  • 702
  • 1
  • 5
  • 8
  • There are [several methods](https://stackoverflow.com/questions/34849001). I've added an answer there that checks validity via CSSStyleSheet so it detects these broken selectors. As for querySelector, apparently it auto-closes the tokens. – wOxxOm Mar 01 '22 at 04:16

0 Answers0