-
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
Iframe: calc compat styles once per page load #57798
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
packages/block-editor/src/components/iframe/get-compatibility-styles.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
let compatibilityStyles = null; | ||
|
||
/** | ||
* Returns a list of stylesheets that target the editor canvas. A stylesheet is | ||
* considered targetting the editor a canvas if it contains the | ||
* `editor-styles-wrapper`, `wp-block`, or `wp-block-*` class selectors. | ||
* | ||
* Ideally, this hook should be removed in the future and styles should be added | ||
* explicitly as editor styles. | ||
*/ | ||
export function getCompatibilityStyles() { | ||
if ( compatibilityStyles ) { | ||
return compatibilityStyles; | ||
} | ||
|
||
// Only memoize the result once on load, since these stylesheets should not | ||
// change. | ||
compatibilityStyles = Array.from( document.styleSheets ).reduce( | ||
( accumulator, styleSheet ) => { | ||
try { | ||
// May fail for external styles. | ||
// eslint-disable-next-line no-unused-expressions | ||
styleSheet.cssRules; | ||
} catch ( e ) { | ||
return accumulator; | ||
} | ||
|
||
const { ownerNode, cssRules } = styleSheet; | ||
|
||
// Stylesheet is added by another stylesheet. See | ||
// https://developer.mozilla.org/en-US/docs/Web/API/StyleSheet/ownerNode#notes. | ||
if ( ownerNode === null ) { | ||
return accumulator; | ||
} | ||
|
||
if ( ! cssRules ) { | ||
return accumulator; | ||
} | ||
|
||
// Don't try to add the reset styles, which were removed as a dependency | ||
// from `edit-blocks` for the iframe since we don't need to reset admin | ||
// styles. | ||
if ( ownerNode.id === 'wp-reset-editor-styles-css' ) { | ||
return accumulator; | ||
} | ||
|
||
// Don't try to add styles without ID. Styles enqueued via the WP dependency system will always have IDs. | ||
if ( ! ownerNode.id ) { | ||
return accumulator; | ||
} | ||
|
||
function matchFromRules( _cssRules ) { | ||
return Array.from( _cssRules ).find( | ||
( { | ||
selectorText, | ||
conditionText, | ||
cssRules: __cssRules, | ||
} ) => { | ||
// If the rule is conditional then it will not have selector text. | ||
// Recurse into child CSS ruleset to determine selector eligibility. | ||
if ( conditionText ) { | ||
return matchFromRules( __cssRules ); | ||
} | ||
|
||
return ( | ||
selectorText && | ||
( selectorText.includes( | ||
'.editor-styles-wrapper' | ||
) || | ||
selectorText.includes( '.wp-block' ) ) | ||
); | ||
} | ||
); | ||
} | ||
|
||
if ( matchFromRules( cssRules ) ) { | ||
const isInline = ownerNode.tagName === 'STYLE'; | ||
|
||
if ( isInline ) { | ||
// If the current target is inline, | ||
// it could be a dependency of an existing stylesheet. | ||
// Look for that dependency and add it BEFORE the current target. | ||
const mainStylesCssId = ownerNode.id.replace( | ||
'-inline-css', | ||
'-css' | ||
); | ||
const mainStylesElement = | ||
document.getElementById( mainStylesCssId ); | ||
if ( mainStylesElement ) { | ||
accumulator.push( mainStylesElement.cloneNode( true ) ); | ||
} | ||
} | ||
|
||
accumulator.push( ownerNode.cloneNode( true ) ); | ||
|
||
if ( ! isInline ) { | ||
// If the current target is not inline, | ||
// we still look for inline styles that could be relevant for the current target. | ||
// If they exist, add them AFTER the current target. | ||
const inlineStylesCssId = ownerNode.id.replace( | ||
'-css', | ||
'-inline-css' | ||
); | ||
const inlineStylesElement = | ||
document.getElementById( inlineStylesCssId ); | ||
if ( inlineStylesElement ) { | ||
accumulator.push( | ||
inlineStylesElement.cloneNode( true ) | ||
); | ||
} | ||
} | ||
} | ||
|
||
return accumulator; | ||
}, | ||
[] | ||
); | ||
|
||
return compatibilityStyles; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 0 additions & 122 deletions
122
packages/block-editor/src/components/iframe/use-compatibility-styles.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@ellatrix On WP.com, I'm seeing an error being thrown on this line:
Any idea what could it be? It happens in Firefox, Chrome seems to be fine.
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.
This didn't happen previously? I didn't really change anything here, I just moved the code to a new file.
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 know, it just started happening randomly on a draft post. And after closing and opening the same post in a new tab the error disappeared again, so I don't really have any steps to reproduce.
But I'm wondering what "Permission denied to access property" even means. It's like the stylesheet object was from a cross-origin iframe, and therefore behind some security boundary.
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.
That's why there's a try/catch for accessing
styleSheet.cssRules
, normally it fails there for cross origin stylesheets