Repository navigation
CSS: Fix url() unquoted content manipulation (#2835) - #7592
Conversation
9759256 to
48792d9
Compare
| return false; | ||
| } | ||
|
|
||
| function groupListContainsStringOrFunctionNode(groups) { |
There was a problem hiding this comment.
Please move that to utils and rename to isFunction (maybe best name)
There was a problem hiding this comment.
I'll see what I can do, but note that these new utility functions, along with some existing ones, aren't very portable.
For example, I wanted to use isURLFunctionNode on line 27, instead of manually checking the type and value, but isURLFunctionNode only works in the printer, where the parser has already processed the nodes and converted "func" to "value-func".
Should I be specifying somewhere whether these will work in the parser, printer, or both (or should there be separate utils for these cases)?
There was a problem hiding this comment.
You can use name isGroupListLikeStringOrFunction (maybe less name)
There was a problem hiding this comment.
Moved to utils and renamed to hasStringOrFunction.
|
@mattiacci Looks good. Please add code from #5654 as a test. |
| }); | ||
| node.group.groups = [stringifiedContent.trim()]; | ||
| } | ||
| } |
There was a problem hiding this comment.
Not sure it is a good solution, we really should fix it on parser side, it is still output broken url with interpolation and variables
There was a problem hiding this comment.
Broken in what way? Not formatted? Or really broken? Show an example please. (I totally agree it should be fixed on the parser side, but this solution looks good enough.)
|
I found a regression in CSS-in-JS. Prettier pr-7592 --parser babelInput: styled.div`color:red;background: url(http://example.com?q=${foo})`Output: Error: Couldn't insert all the expressions
at transformCssDoc (https://deploy-preview-7592--prettier.netlify.com/lib/standalone.js:24094:13)
at Object.embed$3 [as embed] (https://deploy-preview-7592--prettier.netlify.com/lib/standalone.js:23938:20)
at Object.printSubtree (https://deploy-preview-7592--prettier.netlify.com/lib/standalone.js:16794:30)
at callPluginPrintFunction (https://deploy-preview-7592--prettier.netlify.com/lib/standalone.js:16915:33)
at https://deploy-preview-7592--prettier.netlify.com/lib/standalone.js:16879:49
at Object.printComments (https://deploy-preview-7592--prettier.netlify.com/lib/standalone.js:16552:21)
at printGenerically (https://deploy-preview-7592--prettier.netlify.com/lib/standalone.js:16879:24)
at FastPath.call (https://deploy-preview-7592--prettier.netlify.com/lib/standalone.js:16672:22)
at printPathNoParens (https://deploy-preview-7592--prettier.netlify.com/lib/standalone.js:27753:92)
at Object.genericPrint$3 [as print] (https://deploy-preview-7592--prettier.netlify.com/lib/standalone.js:26247:32) |
|
Thanks for bringing up the regression, but I'm not sure of the best approach to address it - it's my first time contributing to Prettier. It seems Prettier uses "atwords" to represent the interpolated JS variables, and that case you brought up is broken by this PR. That said, it seems remarkably easy to break Prettier in the css-in-js interpolated string use case today: This presumably happens because the JS variable is replaced with the Prettier placeholder, resulting in the CSS parser seeing: But Prettier seems to assume that the placeholders will be parsed as at "atword" node, so it doesn't find the placeholder and fails. My change triggers the same behavior, since it stringifies the unquoted URL. Perhaps Prettier should look at the raw string output for the placeholders, rather than using the node tree? Something else? |
# Conflicts: # tests/css_inline_url/__snapshots__/jsfmt.spec.js.snap # tests/css_less/__snapshots__/jsfmt.spec.js.snap # tests/css_less/less.less # tests/css_scss/__snapshots__/jsfmt.spec.js.snap # tests/css_scss/scss.scss
|
The CSS-in-JS regression doesn't happen anymore because an underlying issue has been fixed by #8097. |
|
|
||
| .invalidUnquotedUrlsButWeParseThemAnyway { | ||
| background: url(--var(foo-bar, #dadce0)); | ||
| -fb-sprite: url(fbglyph:cross-outline, fig-white); |
There was a problem hiding this comment.
Let's add tests with nested function
@function concat($strings...) {
$result: '';
@each $string in $strings {
$sum: $result + $string;
}
@return $result;
}
a {
backround: url(concat('http://', 'example.com', '/image.png'));
}
alexander-akait
left a comment
There was a problem hiding this comment.
Need test nested functions
|
@mattiacci Thanks for squashing this ancient bug! |
Improve the handling of unquoted URL content in CSS/SCSS/Less. This doesn't
address the underlying parsing issues, but at least ensures Prettier doesn't
modify URLs, by stringifying more cases of unquoted content in
url().(Not changing the API or CLI)
changelog_unreleased/*/pr-XXXX.mdfile followingchangelog_unreleased/TEMPLATE.md.✨Try the playground for this PR✨