-
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 browser parser APIs for block-editor style transformations. #49483
Use browser parser APIs for block-editor style transformations. #49483
Conversation
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @strarsis! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
The tests may have to be adjusted as now the browser AST is used. |
Fix CSSwhat import. Add root selector ignoring. Fix selector skipping. Update test snapshots (whitespace/newlines).
Fix CSSStyleRule check.
We're so close 🤏 to getting this right. Running this with Which is not ideal because I'm getting the expanded version of the Ideally, we should return the same set of declarations inputted by the user. |
Update test snapshots.
@zaguiini: Is the stringified representation compact again? The browser represents the styles ( I prepended your input string to the PoC and it appears to be OK ( |
Yes, in this case, it's right. What I'm trying to do is to use |
@zaguiini: So you want to parse the input styles, transform them and then get them back as modified string, right? What are you using to modify the background property (the callback function)? The compact representation should be preserved. For more granularity the |
Well my use case is a little different:
Like that:
|
Did we ever consider using |
@zaguiini: What browser are you using? For me in Chrome on Windows 10 the actual, unexpanded properties are listed with numerical index: |
I'm using Brave on MacOS. Can you try it with Also, another important thing: does the CSSOM parser preserve prefixed, cross-browser properties? I don't think so but I'd like to confirm with you. |
@zaguiini: It does support (vendor) prefixed property names (in Chrome were I tested)! https://codepen.io/strarsis/pen/MWmMQgy .example {
-webkit-mask: url('images/test/shape.svg');
mask: url('images/test/shape.svg');
} .example { -webkit-mask: url("https://example.com/test/wp/theme/test/images/test/shape.svg"); mask: url("https://example.com/test/wp/theme/test/images/test/shape.svg"); } |
Can you confirm it works if we're adding URL transformation for example? Does it show up in the |
(https://codepen.io/strarsis/pen/MWmMQgy) .example { -webkit-mask: url("https://example.com/test/wp/theme/test/images/test/shape.svg"); mask: url("https://example.com/test/wp/theme/test/images/test/shape.svg"); } Output: .wrapper .example { -webkit-mask: url("https://example.com/test/wp/theme/test/images/test/shape.svg"); mask: url("https://example.com/test/wp/theme/test/images/test/shape.svg"); } |
@zaguiini: The thing is that the browser will only process what it understands. So a Firefox browser handles the |
@youknowriad, @zaguiini: PostCSS, that @zaguiini proposed, can be used in the browser, I should also give that a try. PR that uses PostCSS: #49521 |
@youknowriad: New editor styles transformation approach using PostCSS is now merged (many thanks @zaguiini)! |
New editor styles transformation approach using PostCSS is now merged (many thanks @zaguiini)! I am closing this browser-level approach PR now in favor of the merged PostCSS approach. |
What?
This PR uses the browser API for parsing and processing the frontend styles.
Where the browser API is yet insufficient, few, small, feature-focused and well-tested libraries are used.
Why?
Because a custom CSS parser is currently used (that is not developed as part of a dedicated parser project), many issues with style parsing and processing occur, often crashing the post-processing completely, resulting in no editor styles applied at all.
Note: A previous attempt that uses the
css-tree
parser has unacceptable size requirements (#25514).How?
This PR implements the CSS post-processing by leveraging the currently supported browser APIs, supplemented by small, feature-focused libraries, where browser support is yet insufficient, allows to increase robustness, performance and size efficiency.
Testing Instructions
note that the editor styles are correctly post-processed and applied, resulting in correctly wrapped selectors and rewritten URLs.
Testing Instructions for Keyboard
(UX is and should not be changed by this PR)
Screenshots or screencast
(Not applicable)
PoC of style post-processing using the browser API: https://codepen.io/strarsis/pen/MWmMQgy
Additional notes
I had issues with installing the
npm
dependencies (Ubuntu22.04
LTS on WSL2
on Windows10
), albeit the correctnode
(by.nvmrc
) andnpm
(bypackage.json
constraints) versions were used,npm install
failed witherror cb() never called!
.The solution was self-upgrading
npm
, but not to the latest available version of it (which would be incompatible with the required constraints bypackage.json
), rather to its version that comes with thatnode
version:nvm install
nvm use
npm install
would result inerror cb() never called!
on a newnode
installation (althoughnpm
is of version6.14.18
).npm install -g [email protected]
# self-upgradenpm
to its latest version in the constraints required bypackage.json
npm install
# installation works now.