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

fix: improve parsing of :nth-of-type(xn+b) #9970

Merged
merged 20 commits into from
Jan 2, 2024
Merged

Conversation

navorite
Copy link
Contributor

@navorite navorite commented Dec 20, 2023

closes #9969

this also fixes the following along the way:
the + in nth-of-type(+xn-b) would be parsed as a combinator.

invalid cases like these are not allowed anymore:

b(+/-)b
-ax
-ax-b
-b

Svelte 5 rewrite

Please note that the Svelte codebase is currently being rewritten for Svelte 5. Changes should target Svelte 5, which lives on the default branch (main).

If your PR concerns Svelte 4 (including updates to svelte.dev.docs), please ensure the base branch is svelte-4 and not main.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Dec 20, 2023

🦋 Changeset detected

Latest commit: 9402b64

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Dec 20, 2023

@navorite is attempting to deploy a commit to the Svelte Team on Vercel.

A member of the Team first needs to authorize it.

@bwbroersma
Copy link
Contributor

Now that I see the fix of #9969:

const REGEX_NTH_OF = /^\s*(even|odd|(-?[0-9]*n?(\s*\+\s*[0-9]+)?))(\s*(?=[,)])|\s+of\s+)/;

I see a (valid) case of -x is also not allowed, nor +xn, which are both valid.
I did not compile it, but I would think this would fix it:

const REGEX_NTH_OF = /^\s*(even|odd|([+-]?[0-9]*n?(\s*[+-]\s*[0-9]+)?))(\s*(?=[,)])|\s+of\s+)/;

@bwbroersma
Copy link
Contributor

The regex also allows for 1+1, which should not be allowed (and isn't in Svelte 4).

@navorite
Copy link
Contributor Author

navorite commented Dec 21, 2023

Now that I see the fix of #9969:

const REGEX_NTH_OF = /^\s*(even|odd|(-?[0-9]*n?(\s*\+\s*[0-9]+)?))(\s*(?=[,)])|\s+of\s+)/;

I see a (valid) case of -x is also not allowed, nor +xn, which are both valid.
I did not compile it, but I would think this would fix it:

const REGEX_NTH_OF = /^\s*(even|odd|([+-]?[0-9]*n?(\s*[+-]\s*[0-9]+)?))(\s*(?=[,)])|\s+of\s+)/;

I've also noticed more problems with the original matcher.

It should fail when matching (the 2nd & 3rd line also work in svelte 4, but they should not)

b(+/-)b
-ax
-ax-b
-b

The new regex covers the correct cases only: (+/-)xn+b or -xn+b or xn-b or (+)b

So now it matches

10n+1
even
odd
-5n+3
-5n+10
10n-1
12
+2n
n+10
2n+10 of 
2n of 

but it doesn't match

1+1
1-1
-2n
-2n-3
-10
-1

@navorite navorite changed the title fix: adjust regex to match more than one digit inside :nth-of-type(xn+y) fix: better :nth-of-type(xn+y) matcher Dec 21, 2023
@navorite navorite changed the title fix: better :nth-of-type(xn+y) matcher fix: better :nth-of-type(xn+b) matcher Dec 21, 2023
@navorite
Copy link
Contributor Author

navorite commented Dec 21, 2023

@dummdidumm Running the parser-modern test with these lines added in the test

    h1:nth-of-type(10n+1)
    {
        background: red;
    }

    h1:nth-of-type(+10n-1)
    {
        background: red;
    }

produces incorrect results, the + in +10n-1 gets parsed as a combinator, this isn't correct, right? it should be part of the selector

we can ofcourse stop matching + at the start of the nth selector but i'd rather match all valid syntax than patching it up

@navorite navorite changed the title fix: better :nth-of-type(xn+b) matcher fix: parse :nth-of-type(xn+b) correctly Dec 29, 2023
@navorite navorite marked this pull request as ready for review December 29, 2023 08:01
@navorite navorite changed the title fix: parse :nth-of-type(xn+b) correctly fix: improve parsing of :nth-of-type(xn+b) Dec 29, 2023
Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@dummdidumm dummdidumm merged commit 6d65b2f into sveltejs:main Jan 2, 2024
6 of 8 checks passed
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

Successfully merging this pull request may close these issues.

Svelte 5: :nth-of-type(10) and :nth-of-type(10n[+1]) (> 1 digit) do not compile
3 participants