title: Check if CSS selector is valid
body: <p>I have a field for users to input a CSS selector and I want to check if it's valid (according to <a href="https://www.w3.org/TR/css3-selectors/#w3cselgrammar" rel="noreferrer">css3 specification</a>). I tried to use expressions from <a href="https://www.w3.org/TR/css3-selectors/#w3cselgrammar" rel="noreferrer">css3 specification</a> as suggested in <a href="https://stackoverflow.com/questions/4192045/regular-expression-for-valid-css-and-or-jquery-selector">another stackoverflow topic</a>, but it didn't work - the regexp I built just didn't match valid selectors. What I have for now is simply:</p>
  
  <pre><code>try {
       document.querySelector(selector);
  } catch (e) {
       // handle bad input
  }
  </code></pre>
  
  <p>But it doesn't seem like a good solution - querySelector function is designed for getting elements, and checking of selector is just a side effect. Furthermore it doesn't provide any information about what is wrong with the selector. </p>
  
  <p>What I'm looking for is something like <code>document.validateSelector</code> or library for parsing CSS selectors.</p>