-
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
WIP: Recurse into conditional CSS rules for Editor iframe stylesheets #41110
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
Size Change: +26 B (0%) Total Size: 1.24 MB
ℹ️ View Unchanged
|
Looks like tests have been disabled gutenberg/packages/e2e-tests/specs/editor/plugins/iframed-multiple-block-stylesheets.test.js Line 36 in 01f9476
Not sure why 🤔 |
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.
+1 for using conditionText
LGTM
Would have been good to add an e2e test. |
@ellatrix Agreed and I looked into doing this. But ultimately I couldn't find a way of achieving what I wanted. The tests themselves failed as soon as I reenabled them and I couldn't fix them in a reasonable amount of time relative to the ROI I felt they would offer. |
What?
This PR fixes an edge case where stylesheets intended for the Editor canvas that contain only media queries will not be applied in the Site Editor.
This is due to the usage of an iframe in the Site Editor and the
useStylesCompatibility
hook which is designed to ensure that style rules intended for the Site Editor are correctly carried across to the editor iframe.This is an edge case so please read the below...
Why?
The Site Editor renders inside an iframe. Enqueued Block / Editor styles from the top document need to be manually transferred over to the iframe in order that they are applied there and the correct styles are shown in the Site Editor.
Currently only style sheets which have selectors that include either of the following rules are transferred:
.editor-styles-wrapper
.wp-block
gutenberg/packages/block-editor/src/components/iframe/index.js
Lines 68 to 73 in 72a348b
If the code detects that a single ruleset from the stylesheet matches, then it will clone the entire stylesheet
<link />
and append it to a hidden<div>
in the iframe document.Again, it only requires a single match for the stylesheet to be deemed eligible for transfer (there is no clue in the code as why this was deemed to be the case).
gutenberg/packages/block-editor/src/components/iframe/index.js
Line 81 in 72a348b
What's the problem?
There is an edge case in the matching against the css rulesets. If the rules within the stylesheet are wrapped in media queries then they are discarded. For example the following selector would be deemed not to be eligible even though it contains the
wp-block
prefix:At a technical level, this is because the matcher requires a
selectorText
property to be present on each CSSRule object in the stylesheet.gutenberg/packages/block-editor/src/components/iframe/index.js
Line 70 in 72a348b
This is fine when the CSS rule is of type
CSSStyleRule
but if the rule includes a media query (type:CSSMediaRule
) then theselectorText
is not present on the object. Instead aconditionText
property indicates the presence of a Media Query and the object'scssRules
property contains child rules which have their ownselectorText
properties.Edge cases
Consider the following example:
This stylehsheet would be carried into the iframe. This is because the first rule matches and is not contained in a media query. This is because only a single rule is required to match for the entire stylesheet to be deemed eligible.
The following would also work:
This time it's the
editor-styles-wrapper
that is deemed a match.However, should your stylesheet contain only media queries it will be ignored by the matcher.
How?
This PR changes the matcher to take account of
CSSMediaRule
. If aconditionText
property is found then the matcher will recurse into that rule until it finds aCSSStyleRule
withselectorText
.Testing Instructions
test.css
and put the following content:trunk
).Screenshots or screencast