dperini / nwsapi

Fast CSS Selectors API Engine
MIT License
105 stars 36 forks source link

Query selector, use of `~` in `:not()` throws #55

Open Banou26 opened 2 years ago

Banou26 commented 2 years ago
import { JSDOM } from 'jsdom'

const doc = new JSDOM(`
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
</head>
<body>
</body>
</html>
`).window.document

doc.querySelector('.js-sns-icon-container ~ h2 ~ h2 ~ .spaceit_pad:not(.js-sns-icon-container ~ h2 ~ h2 ~ h2 ~ .spaceit_pad)')

throws

'.js-sns-icon-container ~ h2 ~ h2 ~ .spaceit_pad:not(.js-sns-icon-container ~ h2 ~ h2 ~ h2 ~ .spaceit_pad)' is not a valid selector

Trying to query for .js-sns-icon-container ~ h2 ~ h2 ~ .spaceit_pad:not(.js-sns-icon-container ~ h2 ~ h2 ~ h2 ~ .spaceit_pad) in browsers work completly fine (Tried in chrome and FF).

cryosis7 commented 2 years ago

I am experiencing a similar issue with table:not(table table) i.e. All tables that are not descendants of another table. My difference was that instead of throwing an error, it would not parse the selector in the :not parameters as a complex selector (As is permitted in CSS Selectors Level 4)
I observed the :not would only parse the last tag, for instance if my selector was ul:not(p ul) it would parse it as ul:not(ul)

slavaleleka commented 1 year ago

the same with child combinator > and adjacent sibling combinator + inside :not() pseudo-class arg div:not(div > span) div:not(h1 + p)

there is no error in browser:

svarzee commented 1 year ago

I came across the same issue. It seems selector validation is broken. If you really need it to work you can try disabling validation, keep in mind it is an ugly hack though and it might not work for everyone:

In node_modules/nwsapi/src/nwsapi.js replace: reValidator = RegExp(standardValidator, 'g'); with reValidator = RegExp('.*', 'g');

svarzee commented 4 months ago

@dperini The issue shouldn't be closed, the problem wasn't fixed. Validation still fails for valid selectors mentioned in this issue.

dperini commented 4 months ago

@svarzee you are correct I should at least have pushed the code to fix this before closing. However just for you to know I already have the code for that and I am in the process to get this fixed.

dperini commented 4 months ago

@Banou26 I pushed a commit for the problem you have submitted (b9da5347250ca7c9796f70c86189dfbeacb9aa18) hope it fixes your issue. It could be of help if you can check the github master repository and confirm the fix. Thank you for your time and help.