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 a bug: css urls are incorrectly stripped of commas #14476

Merged

Conversation

seiyab
Copy link
Sponsor Contributor

@seiyab seiyab commented Mar 9, 2023

Description

Fix #14237

Checklist

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

Try the playground for this PR

@seiyab seiyab marked this pull request as ready for review March 9, 2023 21:29
.join(node.groups[0]?.type === "comma_group" ? "," : "");
.join(node.type === "paren_group" ? "," : "");
Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

Commas had disappeard if the first comma group is flattened.
Groups in paren group should always be separated by comma.

@fisker
Copy link
Member

fisker commented Mar 10, 2023

Fix #14476

Wrong link

@fisker
Copy link
Member

fisker commented Mar 10, 2023

Can you try get original text instead of using stringifyNode? There are still edge cases broken.

Prettier pr-14476
Playground link

--parser css

Input:

@font-face {
  src: url(foo.ttf?query=foo,bar,);
  src: url(foo.woff2?foo=rgb(255,255,0));
}

Output:

@font-face {
  src: url(foo.ttf?query=foo,bar);
  src: url(foo.woff2?foo=rgb(255, 255, 0));
}

Co-authored-by: fisker Cheung <lionkay@gmail.com>
@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Mar 10, 2023

Thanks for pointing edge cases.
And throwing error for @import url(https://example.com/image\).png) also seems a bug.

If you choose to write the URL without quotes, use a backslash (\) before any parentheses, whitespace characters, single quotes (') and double quotes (") that are part of the URL.

@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Mar 11, 2023

Trying to the original text isn't going smoothly.

  • Approach 1. use text at value-root
    • It contains some texts other than url() node such as comment. Reveaing only url() seems to be hard.
  • Approach 2. use toString() of postcss-value-parser
    • It throws in prettier css parser because it removes nodes from parsed value.
    • Edit: Removing delete statement made this approach work. 🌵

I'm still trying 🚀

@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Mar 11, 2023

↑postcss-value-parser seems to go wrong.
Can't we give up this case at least in this PR?

Part of parse result against `url(https://example.com/\)\ \(.json)`
nodes: [
  FunctionNode { 
    raws: [Object],
    value: 'url',
    source: [Object],
    sourceIndex: 0,
    nodes: [Array],
    type: 'func',
    unbalanced: 0,
    parent: [Circular *1]
  },
  Parenthesis { 
    raws: [Object],
    value: '(',
    source: [Object],
    sourceIndex: 26,
    type: 'paren',
    parenType: '',
    parent: [Circular *1]
  },
  Word { 
    raws: [Object],
    value: '.json',
    source: [Object],
    sourceIndex: 27,
    type: 'word',
    isHex: false,
    isColor: false,
    parent: [Circular *1]
  },
  Parenthesis { 
    raws: [Object],
    value: ')',
    source: [Object],
    sourceIndex: 32,
    type: 'paren',
    parenType: '',
    parent: [Circular *1]
  } 
],

@seiyab seiyab requested a review from fisker March 11, 2023 04:59
@seiyab seiyab force-pushed the 14237-css-urls-are-incorrectly-stripped-of-commas-2 branch from 220064f to 2ca6512 Compare March 11, 2023 04:59
@@ -197,7 +194,6 @@ function parseNestedValue(node, options) {
parseNestedValue(node[key], options);
if (key === "nodes") {
node.group = flattenGroups(parseValueNode(node, options));
delete node[key];

This comment was marked as outdated.

@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Mar 13, 2023

I'm sorry for annoying commits.
I welcome to change the approach because I'm not sure which approach is good enough.

* @param {*} node
* @returns {string}
*/
function stringifyFuncParam(node) {
Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

We can also use this here.

getHighestAncestor(valueNode).text.slice(

I'll change it if you want.

@seiyab
Copy link
Sponsor Contributor Author

seiyab commented Mar 17, 2023

Can I rerun CI?

[2023-03-13T23:07:46.123Z] ['error'] There was an error running the uploader: Error uploading to [https://codecov.io:](https://codecov.io/) Error: There was an error fetching the storage URL during POST: 404 - {'detail': ErrorDetail(string='Unable to locate build via Github Actions API. Please upload with the Codecov repository upload token to resolve issue.', code='not_found')}
Error: Codecov: Failed to properly upload: The process '/home/runner/work/_actions/codecov/codecov-action/v3.1.0/dist/codecov' failed with exit code 255

content: url(https://example.com/\\ \\ .jpg);
content: url( https://example.com/\\)\\).jpg );
content: url( https://example.com/\\(\\(.jpg );
content: url(https://example.com/\\ \\ .jpg);
Copy link
Member

Choose a reason for hiding this comment

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

Should we trim here?

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

I thought yes, at the first time.
But I feel I need to know CSS Specification more...

Copy link
Member

Choose a reason for hiding this comment

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

As I tested with in chrome

<style>
body {
  background: url(              1.png?_=      )
}
</style>

and

<style>
body {
  background: url(              1.png      )
}
</style>

They all requesting urls without space.

Copy link
Sponsor Contributor Author

@seiyab seiyab Mar 23, 2023

Choose a reason for hiding this comment

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

postcss-values-parser throws.
I think it should be a bug.
I'll try newer versions.

const valueParser = require("postcss-values-parser");
valueParser('url(   https://example.com/\\(\\(.jpg   )', {loose: true}).parse()
Uncaught ParserError: Expected closing parenthesis at line: 1, column 4
    at Parser.error (~/Projects/oss/prettier/node_modules/postcss-values-parser/lib/parser.js:127:11)
    at Parser.parenOpen (~/Projects/oss/prettier/node_modules/postcss-values-parser/lib/parser.js:279:12)
    at Parser.parseTokens (~/Projects/oss/prettier/node_modules/postcss-values-parser/lib/parser.js:235:14)
    at Parser.loop (~/Projects/oss/prettier/node_modules/postcss-values-parser/lib/parser.js:132:12)
    at Parser.parse (~/Projects/oss/prettier/node_modules/postcss-values-parser/lib/parser.js:51:17)

Copy link
Sponsor Contributor Author

@seiyab seiyab Mar 23, 2023

Choose a reason for hiding this comment

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

v3.2.1 seems good.

const { parse } = require('postcss-values-parser');
parse('url(   https://example.com/\\(\\(.jpg   )');
<ref *1> Root {
  raws: { semicolon: false, after: '' },
  type: 'root',
  nodes: [
    Func {
      raws: [Object],
      name: 'url',
      type: 'func',
      isColor: false,
      isVar: false,
      nodes: [Array],
      parent: [Circular *1],
      source: [Object],
      params: '(   https://example.com/\\(\\(.jpg   )'
    }
  ],
  source: {
    input: Input {
      css: 'url(   https://example.com/\\(\\(.jpg   )',
      hasBOM: false,
      id: '<input css 1>'
    },
    start: { line: 1, column: 1 }
  },
  toString: [Function: bound toString]
}

But v3.0.0 is a big breaking release. https://github.com/shellscape/postcss-values-parser/releases
Which way do you like?

  • Bump postcss-values-parser on next branch via another PR first, and then work on this bug.
  • Leave 'url( https://example.com/\\(\\(.jpg )' and merge this PR first. And then try to bump postcss-values-parser on next branch. Finally, tackle 'url( https://example.com/\\(\\(.jpg )' (or, perhaps it will be resolved by update).

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

Will it conflict with your work if I'll try to bump postcss-value-parser?
Referring following, I'm asking you.

I want to make some change to the value-comma_group part

#14480

Copy link
Member

@fisker fisker Mar 30, 2023

Choose a reason for hiding this comment

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

No. Go a head, but it won't be easy, I've tried few times since maybe 2~3 years ago.

Copy link
Member

Choose a reason for hiding this comment

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

Last attempt by other maintainer. #11667

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

Thank you for your replies.
Sounds so hard!
Any way, I'll try it, though I will be likely to give up 🔥

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

@fisker
Hi, I have tried migrating postcss-values parser from v2 to v6.
Eventually, I gave up because shellscape/postcss-values-parser#140 prevents us from migrating.
May I summarize what I investigated for migration somewhere (perhaps an issue or a discussion)?

Copy link
Member

@fisker fisker left a comment

Choose a reason for hiding this comment

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

Thank you!

@fisker fisker added this to the 3.0 milestone Mar 28, 2023
@fisker
Copy link
Member

fisker commented Mar 28, 2023

Can you fix the conflicts?

@fisker fisker merged commit 9500403 into prettier:main Mar 29, 2023
15 checks passed
@fisker
Copy link
Member

fisker commented Mar 29, 2023

Good job @seiyab

@seiyab seiyab deleted the 14237-css-urls-are-incorrectly-stripped-of-commas-2 branch March 29, 2023 05:26
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Feb 13, 2024
Co-authored-by: fisker Cheung <lionkay@gmail.com>
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.

@font-face urls are incorrectly stripped of commas
2 participants