dperini / nwsapi

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

In unit tests: RangeError: Invalid string length #50

Closed silvanaweb closed 1 year ago

silvanaweb commented 2 years ago

While using react testing library that uses this library, I have encountered a difficult-to-find error when running unit tests that were just breaking and showing the following message:

node_modules/nwsapi/src/nwsapi.js:893
      source = ‘n=e;while((e=e.parentElement)){’ + source + ‘}e=n;’;

After hours of investigation, I have found out that one of the React styled components I was using had a tiny mistake in its styles. Something like:

  tbody th {
    vertical-align: middle;

    > span {
      display: inline-block;
      padding: 0;
    }
  }ç

Where a character was out of the parenthesis.

omeralper commented 2 years ago

I spent a day to find this out also.

Twonky-Chris commented 2 years ago

I had the same problem.

richard-ling commented 2 years ago

I just got the same thing today, too. My jest test bombs out with the same error on the same line. In my case, compileSelector is called with expression = '.makeStyles-root _:-ms-fullscreen'. The parser gets stuck in an endless cycle with selector = ' _:-ms-fullscreen' (note the leading space):

NOTE: the recommendation for using '_:-ms-fullscreen' comes from here: https://stackoverflow.com/questions/45973976/apply-only-to-ie-11-within-media-query-for-screen-size The full string is created by Material-UI V4 CSS compiler . I'm not sure if it is valid CSS, as _ is not a valid tagname, but in any case nwsapi should not crash.

Seems that this error will occur whenever there is a space followed by an invalid selector.

richard-ling commented 2 years ago

Turns out issue #46 is the exact problem I described above

dperini commented 1 year ago

@silvanaweb / @richard-ling Your issues should have been solved by this commit: https://github.com/dperini/nwsapi/commit/12f9f59c2dc53f8c86cd8f19fe64116ca2adc638 it is part of the 2.2.3 release.

Closing this issue.