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

Handle closing parenthesis within data url in transform-styles (#16408) #17146

Closed
wants to merge 3 commits into from

Conversation

oxyc
Copy link
Member

@oxyc oxyc commented Aug 22, 2019

Fix from reworkcss/css#110

Description

I'm not the actual author of this but it fixes the case when url embedded svgs containing parenthesis are not parsed properly as values. See reworkcss/css#67 (comment). Fixes #16408

How has this been tested?

  • I tested on a site I'm building with a full tailwindcss + swiper (which had the offending svg), minified. 310kb of minified css and it works fine now.

Types of changes

Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code is tested. (Sort of...)
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.

@oxyc
Copy link
Member Author

oxyc commented Aug 22, 2019

So this PR causes failure on the = after charset in twentynineteeen styles-editor.css.

To be honest someone else might need to take over from here. I guess this PR now requires either ' or ". I don't see how regex can parse these nested parenthesis properly.

@font-face {
  font-family: 'NonBreakingSpaceOverride';
  src: url(data:application/font-woff2;charset=utf-8;base64,d09GMgABAAAAAAMoAA0AAAAACDQAAALTAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP0ZGVE0cGh4GYACCahEICjx3CywAATYCJANUBCAFhiEHgWwbXQfILgpsY+rQRRARwyAs6uL7pxzYhxEE+32b3aeHmifR6tklkS9hiZA0ewkqGRJE+H7/+6378ASViK/PGeavqJyOzsceKi1s3BCiQsiOdn1r/RBgIJYEgCUhbm/8/8/h4saPssnTNkkiWUBrTRtjmQSajw3Ui3pZ3LYDPD+XG2C3JA/yKAS8/rU5eNfuGqRf4eNNgV4YAlIIgxglEkWe6FYpq10+wi3g+/nUgvgPFczNrz/RsTgVm/zfbPuHZlsuQECxuyqBcQwKFBjFgKO8AqP4bAN9tFJtnM9xPcbNjeXS/x1wY/xU52f5W/X1+9c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7/6DEaLQu66Mrd35BKUWKJSlFv1+rue4cGM7shgR981qSon+ZNwUJ8iDgoYU2OvDRRQ99DDDECAHGmGCKmf80hZSx/Kik/LliFbtmN6xmt+yOjdg9GztV4tROnRwX/Bsaaw51nt4Lc7tWaZYHp/MlzKx51LZs5htNri+2AAAAAQAB//8AD3jaY2BkYGDgAWIxIGZiYARCESBmAfMYAAR6AEMAAAABAAAAANXtRbgAAAAA2AhRFAAAAADYCNuG) format("woff");
}

@oxyc
Copy link
Member Author

oxyc commented Aug 22, 2019

Found this NxtChg/pieces#3

Props to @lykahb. Is it okay if I credit you for these commits as i'm not really the author?

@lykahb
Copy link

lykahb commented Aug 22, 2019

@oxyc Thanks, I appreciate this. A while ago I moved from the JS CSS parsers to the native browser parser. But it it's nice to see the bug getting fixed.

@gziolo gziolo added [Package] Block editor /packages/block-editor [Type] Bug An existing feature does not function as intended labels Aug 27, 2019
@gziolo gziolo requested a review from a team August 27, 2019 07:26
@noisysocks
Copy link
Member

I'm not very knowledgeable about the history of this function, but I wonder if it still makes sense for us to have https://github.com/reworkcss/css vendored into Gutenberg rather than included as a package dependency? If it were a dependency then we would get fixes like this simply by bumping the version we depend on.

All that aside, this change looks like a perfect candidate for a unit test. Could we add a test to packages/block-editor/src/utils/transform-styles/test/traverse.js?

@oxyc
Copy link
Member Author

oxyc commented Aug 29, 2019

If it were a dependency then we would get fixes like this simply by bumping the version we depend on.

This fix has not been merged upstream either and in general the project is quite unmaintained with no releases since September 2018.

I can definitely add a unit test here though.

@oxyc oxyc force-pushed the transform-styles-svg-fix branch 2 times, most recently from 0da0c72 to e9cae9c Compare September 13, 2019 14:36
@oxyc
Copy link
Member Author

oxyc commented Oct 22, 2019

This might be a PR where the performance impact should be measured but I cant for the life of me get test-performance e2e suits running...

UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Target closed

@oxyc
Copy link
Member Author

oxyc commented Mar 23, 2020

Doesnt affect performance.

Before:

Average time to load: 11464ms
Average time to DOM content load: 11422ms
Average time to type character: 33.84ms
Slowest time to type character: 65.16ms
Fastest time to type character: 29.61ms

Average time to load: 10118ms
Average time to DOM content load: 10070ms
Average time to type character: 35.15ms
Slowest time to type character: 77.79ms
Fastest time to type character: 31.23ms

Average time to load: 12113ms
Average time to DOM content load: 12061ms
Average time to type character: 37.24ms
Slowest time to type character: 78.57ms
Fastest time to type character: 32.45ms

Average time to load: 11232ms

After:

Average time to load: 10055ms
Average time to DOM content load: 10010ms
Average time to type character: 34.02ms
Slowest time to type character: 69.81ms
Fastest time to type character: 30.46ms

Average time to load: 12436ms
Average time to DOM content load: 12391ms
Average time to type character: 40.58ms
Slowest time to type character: 90.63ms
Fastest time to type character: 34.67ms

Average time to load: 10252ms
Average time to DOM content load: 10209ms
Average time to type character: 36.49ms
Slowest time to type character: 70.36ms
Fastest time to type character: 31ms

Average time to load: 10914ms

@oxyc
Copy link
Member Author

oxyc commented Apr 2, 2020

Noteworthy that I've been running a patched version of Gutenberg with this fix since August 2019 with no issues. Only on one site, but the site pushes it's entire frontend stylesheet through the transform. 964kb in development mode (tailwind before purgecss), 142kb (minified, purged).

Copy link
Member

@noisysocks noisysocks left a comment

Choose a reason for hiding this comment

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

Thanks for updating the PR and the patience here 🙂

I still would love if we made this a package dependency instead of vendoring the library, but let's not let it stop us fixing this bug.

* Internal dependencies
*/
import traverse from '../traverse';
import parse from '../ast/parse';
Copy link
Member

Choose a reason for hiding this comment

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

I think let's move the describe( 'CSS traverse' ) back into test/traverse.js and put the describe( 'CSS parse' ) into a new test/parse.js file.


describe( 'CSS parse', () => {
it( 'Should handle closing parenthesis within url', () => {
const input = `.a { b: url(")"); d: url(";a"); }`;
Copy link
Member

Choose a reason for hiding this comment

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

String interpolation isn't necessary.

Suggested change
const input = `.a { b: url(")"); d: url(";a"); }`;
const input = '.a { b: url(")"); d: url(";a"); }';

@oxyc
Copy link
Member Author

oxyc commented Jun 13, 2020

Thanks for the review @noisysocks! and sorry for not responding earlier, but shortly after your review, @strarsis came up with a better approach of replacing reworkcss with css-tree as a package dependency #21936, like you preferred :)

So I'll close this PR in favor of that one now that I've verified that it would fix my issue

@strarsis
Copy link
Contributor

New editor styles transformation approach using PostCSS is now merged (many thanks @zaguiini)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Block editor /packages/block-editor [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

transformStyles not working correctly
5 participants