-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Use css-tree for block-editor style transformations (3). #25514
Conversation
Use WHATWG URL for block-editor style URL manipulations.
The CI linter task complains: |
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.
The linter is complaining because the library you're including (css-tree probably) is not transpiled so it leaves "const" in the built files which don't work on IE11. I'm not certain how to solve this though.
packages/block-editor/src/utils/transform-styles/transforms/url-rewrite.js
Outdated
Show resolved
Hide resolved
@@ -54,10 +55,11 @@ | |||
"@wordpress/wordcount": "file:../wordcount", | |||
"classnames": "^2.2.5", | |||
"css-mediaquery": "^0.1.2", | |||
"css-tree": "^1.0.0-alpha.39", |
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.
Why is this using an "alpha" version? can we use the latest stable instead?
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.
Yes, this doesn't make sense, css-tree
is in use for many years and very stable, see this issue csstree/csstree#134 for having it finally published at least as a beta release now.
Use normal function type for trimOutmostChars.
@youknowriad: The |
I've been looking at the bundle-size impact of this PR. See the comment here #25533 (comment) Unfortunately, it's way too big. |
@youknowriad: The linter still complains about |
@youknowriad: Have the sizes changed now favourably? |
Looks like the size diff is pretty similar regardless 🤔 |
Of course, using a dedicated browser API for this would be great, but sadly it isn't supported at all yet (notably the |
Hm, what about using the native Browser API and a CSS selector parser (as there is no Browser API for this yet) to achieve this? |
This library is indeed a little bit heavy. Tree-shaking (picking some methods directly from the https://github.com/adobe/css-tools is promising -- it has a |
function isRemotePath( filePath ) { | ||
return /^(?:https?:)?\/\//.test( filePath ); | ||
} | ||
import { URL } from 'whatwg-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.
I don't think this dependency is necessary anymore. URL
has achieved a good level of browser standardization.
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.
Yes, good point. Browser support is basically 100% now. NodeJS supports it, too.
Picking methods directly looks promising, hopefully this shaves off enough bytes to make it acceptable. |
Alright, I rebase and resolve all conflicts to get this up to date. Then I clean things up and try to get the size as small as possible. |
I managed to shave some bytes by cherry-picking the
EDIT: Surprisingly, we're actually using I tried using I think we'll need to bite the bullet here... Considering the improvements and the reliability of |
Personally, I would have been ok with the bundle size change, if the fixes are for all users. The reality though is that block themes use iframes and there's a goal to actually move to iframes for everyone which means we'll be paying an extra price for code that is not used for most users. WDYT? |
I think realistically a lot of sites will not run on block themes for quite some time (for various, valid reasons) so this is still very much worth fixing. Especially since this is not some kind of new feature but a bug. |
@kraftner: CSS Layers are supported by the @youknowriad, @kraftner, @zaguiini; @dsturm: |
Okay, sorry, I didn't actually check this myself. But I think the general assumption that an outdated, broken parser will inevitably only continue to have more bugs when new CSS features get introduced over time still stands. And since WP is strong on back compat this will become a worsening problem for many years to come.
No personal opinion on the how, you're much more competent on that @strarsis 😄 |
@kraftner: I am also unhappy with the state of JS CSS parsers and the ongoing addition of new CSS stuff that has to be supported. This is not a perfect solution of course, this should rather fix the most blocking issues with CSS parsing. The only reason for why I picked |
@strarsis What's the downsides of your browser based parsing? Seems like the best option for me so far based on your description? |
The browser parsers could be subtly different, that's the only real downside I can think of right now. Browser CSS parsers are all much more advanced and tested than most libraries. And when something is not understood by the browser CSS parser, the original particular styles would not be usable anyway by that browser. Gutenberg frontend testing is already performed in a headless but otherwise complete browser (JS API-wise), so this should not be a problem. So yes, this solution appears to be indeed the best one. I can make a PR following this approach instead. |
@strarsis I don't quite understand the solution using I haven't had the time to fully play with it, though, but it looks like it won't let the user add additional transformers. |
@zaguiini: The browser does the CSS parsing on rule level. There is no browser API yet for parsing the selectors themselves, hence Concerning the extensibility: Well, for Gutenberg style isolation only two tasks are needed: 1. Wrap the selectors (prefix with a class selector), 2. Rewrite the |
@youknowriad, @kraftner, @zaguiini; @dsturm: This uses Shadow DOM (Web Components API) (no No style post-processing necessary, just creating the shadow DOM for the editor - in |
@zaguiini, @youknowriad: The Lightning CSS parser is written in Rust, but can be used in browser (WASM (Web Assembly)). It has a size of 20 kB (minified) / 5.4 kB (minified+gzipped): |
@strarsis Lightning CSS is not this small 😅 Been there, done that. From above:
|
@youknowriad, @kraftner, @zaguiini; @dsturm: |
@youknowriad, @oxyc, @noahtallen: Note: This PR supersedes the older PRs (css-tree-transform-styles and css-tree-transform-styles-2) with changes on top of latest upstream master. Special thanks @oxyc for his fixes for the code in this PR.
Description
This PR adds the CSSTree parser for wrapping the theme styles into a styles wrapper and WHATWG URL for URL manipulation of URLs in styles.
How has this been tested?
I tested this on my local WordPress site.
Types of changes
(PoCs here: https://codepen.io/strarsis/pen/RwWRqZv and also https://codesandbox.io/s/busy-williamson-r1frp). (Not implemented with this PR, but
CSSTree
adds the foundation for it).Checklist: