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(css): var in image-set #7921
Conversation
I think it would be better to do a review of the current flow. In MDN, there is an example without using background-image: image-set(
"large-balloons.avif" type("image/avif"),
"large-balloons.jpg" type("image/jpeg")); Maybe the regex could be extended like:
to
So we stop processing image sets that have |
packages/vite/src/node/utils.ts
Outdated
.replace(escapedSpaceCharacters, ' ') | ||
.trim() | ||
.split(' ', 2) | ||
return { url, descriptor } | ||
.split(' ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we split after )
, '
, "
here? What happens if we have something like var( --my-var)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for review!
Yes, the space inside var()
or url()
will cause some trouble.Maybe I can clean space inside it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for pushing forward with a fix! ;)
Maybe a regex is better to match the first part? var(), url(), '...', "..." ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update.
According to spec, support 2 condition.
- function call like
var()
,url()
,linear-gradient()
- url like
'test.png'
,"test.png"
.css-image-set-multiple-descriptor { | ||
background-image: -webkit-image-set( | ||
'../nested/asset.png' type('image/png') 1x, | ||
'../nested/asset.png' type('image/png') 2x | ||
); | ||
background-size: 10px; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs to test inline CSS in HTML because it doesn't have url(
and also needs to compile, may have had a problem with inline CSS in HTML.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you share a failing example to add to the PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vite/packages/vite/src/node/plugins/html.ts
Lines 359 to 365 in 8e1d6ae
const inlineStyle = node.props.find( | |
(prop) => | |
prop.name === 'style' && | |
prop.type === NodeTypes.ATTRIBUTE && | |
prop.value && | |
prop.value.content.includes('url(') // only url(...) in css need to emit file | |
) as AttributeNode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! It is working though, are we good then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because I no add the assertion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@poyoho gotcha. Should the fix for this case be part of this PR, though? This wasn't working before even without var
. I think we could merge this PR and then create a new PR or issue for inline style support, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I think this issue had two solutions. one is to remove the judge. other is add the judge. remove this judge may be better. But anyway it is also not part of this PR. (it seems to part of html instead of css).
@patak-dev @poyoho The fail test seems not related, could you plz rerun it? |
Thanks again @Bigfish8! |
Thanks for the work |
Description
fix #7874
Additional context
the case in #7874
input:
background-image: image-set(var(--hero--image-webp) 1x, var(--hero--image-jpg) 1x);
output:
background-image: image-set(url(var(--hero--image-webp) ) 1x, var(--hero--image-jpg) 1x);
step:
cssImageSetRE
match theimage-set(var(--hero--image-webp)
(note: there only one)
)var(--hero--image-webp
tourl(var(--hero--image-webp)
it means that if the css is:
.some-class { --some-var: './a.png'; background-image: -webkit-image-set(var(--some-var) 1x, './b.png' 2x)
The
cssImageSetRE
will only matchimage-set(var(--some-var)
and do not handle'./b.png'
.But I think this is OK, because of the demo metioned in #7874. Url like
'./b.png'
in image-set is invalid, onlyurl('./b.png')
is valid.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).