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(vscode): color preview with !important #2787

Merged
merged 1 commit into from Jun 23, 2023
Merged

Conversation

varugasu
Copy link
Contributor

fix #2708

Problem

The problem was happening because of the regex in getCssVariables

const regex = /(?<key>--\S+?):\s*(?<value>.+?);/gm

For the given str:

/* layer: default */
.\!text-amber {
  --un-text-opacity: 1 !important;
  color: rgba(251, 191, 36, var(--un-text-opacity)) !important;
}

When match.groups?.key is --un-text-opacity, its match.groups?.value would be 1 !important. Finally, the resulting colorString is rgba(251, 191, 36, 1 !important), which breaks the CSS and wrongly previews the color:

image

Solution

Use the following regex to stop the capturing group until it finds either an ! or ;

const regex = /(?<key>--\S+?):\s*(?<value>.+?).*[!]{0,1}.*;/gm

@varugasu varugasu requested a review from antfu as a code owner June 23, 2023 02:50
@netlify
Copy link

netlify bot commented Jun 23, 2023

Deploy Preview for unocss ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 9de452a
🔍 Latest deploy log https://app.netlify.com/sites/unocss/deploys/6495834fdea2e60008d19f34
😎 Deploy Preview https://deploy-preview-2787--unocss.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@varugasu
Copy link
Contributor Author

Another solution would be to create a sanitizer/cleaner for match.groups?.value. Let me know what is best.

@antfu
Copy link
Member

antfu commented Jun 23, 2023

I am ok with the regex fix. Can you help adding some unit test for that function? Thanks

@varugasu
Copy link
Contributor Author

@antfu done! The function getCssVariables is not exportable, so I tested getColorString instead.

I had to update alias.ts and tsconfig.json as well to test it from tests/utils.test.ts. Let me know if there is anything else you wanna me to do.

@varugasu
Copy link
Contributor Author

Also, I have updated the regex to make it more readable. From

/(?<key>--\S+?):\s*(?<value>.+?).*[!]{0,1}.*;/gm

to

/(?<key>--\S+?):\s*(?<value>.+?)\s*[!;]/gm

@antfu antfu enabled auto-merge (squash) June 23, 2023 12:05
@antfu antfu merged commit 6a81c62 into unocss:main Jun 23, 2023
10 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.

vscode extension provides wrong color in indicator when class has ! prefix
2 participants