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: parser error when using semicolon inside quotes in style #10221

Merged
merged 14 commits into from
Jan 26, 2024

Conversation

n0n3br
Copy link
Contributor

@n0n3br n0n3br commented Jan 18, 2024

fix #9637

This PR fixes the parsing error when we use semicolon inside quotes in style tag, like described in the issue.

Copy link

changeset-bot bot commented Jan 18, 2024

🦋 Changeset detected

Latest commit: a7226db

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

@n0n3br n0n3br changed the title fix parser error when using semicolon inside quotes in style fix: parser error when using semicolon inside quotes in style Jan 18, 2024
Copy link
Contributor

@navorite navorite left a comment

Choose a reason for hiding this comment

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

Don't forget to add a test and changeset

@@ -9,7 +9,7 @@ const REGEX_PERCENTAGE = /^\d+(\.\d+)?%/;
const REGEX_NTH_OF =
/^(even|odd|\+?(\d+|\d*n(\s*[+-]\s*\d+)?)|-\d*n(\s*\+\s*\d+))((?=\s*[,)])|\s+of\s+)/;
const REGEX_WHITESPACE_OR_COLON = /[\s:]/;
const REGEX_BRACE_OR_SEMICOLON = /[{;]/;
const REGEX_BRACE_OR_SEMICOLON = /[{;](?=(?:[^']*'[^']*')*[^']*$)/;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const REGEX_BRACE_OR_SEMICOLON = /[{;](?=(?:[^']*'[^']*')*[^']*$)/;
const REGEX_BRACE_OR_SEMICOLON = /[{;](?=[^"']*$)/;

I think the extra complexity here isn't needed — just checking that it doesn't end with a quote would be sufficient. Also don't forget the " character.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about testing, I'm starting in this world of contributing to open source now and even don't know how I would write a test for that.
As for the changeset, is there a style guide or other PR that I could use as example ?

Copy link
Contributor

Choose a reason for hiding this comment

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

No worries! The test case for this should be added in parser-modern. You can read more about contributing to Svelte here.

Unfortunately, I can't help you add a test case as I don't have write access to this PR. You can look at similar tests inside parser-modern though to understand how it works. In short, it is a snapshot test, meaning it takes an input and expects a certain output, if the generated output is different than the expected it fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@navorite added a changeset and commited your sugestion. as for tests I'm still stucked

n0n3br and others added 3 commits January 18, 2024 14:43
Co-authored-by: navorite <navorite@gmail.com>
Copy link
Contributor

@navorite navorite Jan 18, 2024

Choose a reason for hiding this comment

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

Revert this change, it's not relevant to the PR at all. Wait until hopefully a maintainer with write access can add a test to your PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a test in the folder you told me. Hope I did it right. Thanks for your help

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about the unecessary file change. Reverted it. Thanks for your help again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@navorite just fixed the support-font-face test not passing. Had to revert the regex to the first one I've used. Your suggestion breaks the test. Already commited. 100% of tests are passing now.

@n0n3br n0n3br marked this pull request as draft January 24, 2024 19:05
@Rich-Harris
Copy link
Member

Thanks! This wasn't quite the right fix — it would basically match any semi/brace that didn't have quote marks later in the string, which meant incorrect results like this:

image

We just need to parse it properly, one character at a time. Luckily we already have the code to do that, it just needed a very modest tweak

@Rich-Harris Rich-Harris merged commit a58d93f into sveltejs:main Jan 26, 2024
8 checks passed
trueadm pushed a commit that referenced this pull request Jan 31, 2024
* fix error when using semicolon inside quotes in style

* refactor to include { like in the original code

* simplified version of regex

Co-authored-by: navorite <navorite@gmail.com>

* add changeset

* add changeset

* add test

* Update .changeset/seven-hornets-smile.md

Co-authored-by: navorite <navorite@gmail.com>

* undo demo.css change

* fix support-font-face test not passing

* add double quotes

* beef up test

* robustify parsing

* Update .changeset/seven-hornets-smile.md

---------

Co-authored-by: navorite <navorite@gmail.com>
Co-authored-by: Rogerio Luiz Aques de Amorim <Rogerio Amorim>
Co-authored-by: Rich Harris <rich.harris@vercel.com>
Co-authored-by: Rich Harris <hello@rich-harris.dev>
@Rich-Harris Rich-Harris mentioned this pull request Feb 6, 2024
8 tasks
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] Error when parsing a semicolon inside a CSS url() function when used in <style>
3 participants