Skip to content

CSS: Fix url() unquoted content manipulation (#2835) - #7592

Merged
thorn0 merged 6 commits into
prettier:masterfrom
mattiacci:issue-2835
May 29, 2020
Merged

thorn0 merged 6 commits into
prettier:masterfrom
mattiacci:issue-2835

Conversation

@mattiacci

@mattiacci mattiacci commented Feb 16, 2020 •

Copy link
Copy Markdown
Contributor

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().

  • I’ve added tests to confirm my change works.
    (Not changing the API or CLI)
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/pr-XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

✨Try the playground for this PR✨

Comment thread src/language-css/parser-postcss.js Resolved
Comment thread src/language-css/parser-postcss.js Outdated
return false;
}

function groupListContainsStringOrFunctionNode(groups) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please move that to utils and rename to isFunction (maybe best name)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can use name isGroupListLikeStringOrFunction (maybe less name)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved to utils and renamed to hasStringOrFunction.

Comment thread src/language-css/parser-postcss.js Resolved
Comment thread src/language-css/parser-postcss.js Resolved
@thorn0
thorn0 changed the base branch from master to next March 17, 2020 21:11
@thorn0 thorn0 closed this Mar 22, 2020
@thorn0 thorn0 reopened this Mar 22, 2020
@thorn0
thorn0 changed the base branch from next to master March 22, 2020 21:08
@thorn0 thorn0 added this to the 2.1 milestone Mar 23, 2020
@thorn0

thorn0 commented Mar 31, 2020

Copy link
Copy Markdown
Member

@mattiacci Looks good. Please add code from #5654 as a test.

});
node.group.groups = [stringifiedContent.trim()];
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

@thorn0 thorn0 Mar 31, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.)

@thorn0

thorn0 commented Mar 31, 2020

Copy link
Copy Markdown
Member

I found a regression in CSS-in-JS.

Prettier pr-7592
Playground link

--parser babel

Input:

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)

@mattiacci

mattiacci commented May 3, 2020 •

Copy link
Copy Markdown
Contributor Author

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:

styled.div`background: url("http://example.com?q=${foo}")

This presumably happens because the JS variable is replaced with the Prettier placeholder, resulting in the CSS parser seeing:

styled.div`background: url("http://example.com?q=@prettier-placeholder-1-id")

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?

thorn0 added 2 commits May 29, 2020 08:32
# 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
@thorn0 thorn0 linked an issue May 29, 2020 that may be closed by this pull request
@thorn0

thorn0 commented May 29, 2020

Copy link
Copy Markdown
Member

The CSS-in-JS regression doesn't happen anymore because an underlying issue has been fixed by #8097.

@thorn0
thorn0 requested a review from alexander-akait May 29, 2020 06:29
@thorn0
thorn0 requested a review from fisker May 29, 2020 06:30

@fisker fisker left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tried on bootstrap and some private projects, seem not breaking anything, and there are enough weird cases in the tests well handled.


.invalidUnquotedUrlsButWeParseThemAnyway {
background: url(--var(foo-bar, #dadce0));
-fb-sprite: url(fbglyph:cross-outline, fig-white);

@alexander-akait alexander-akait May 29, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 alexander-akait left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Need test nested functions

@alexander-akait alexander-akait left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks better

@thorn0
thorn0 merged commit ffceb60 into prettier:master May 29, 2020
@thorn0

thorn0 commented May 29, 2020

Copy link
Copy Markdown
Member

@mattiacci Thanks for squashing this ancient bug!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

4 participants