-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Iframe: simplify/reactify compat styles logic (#46732)
- Loading branch information
Showing
2 changed files
with
103 additions
and
103 deletions.
There are no files selected for viewing
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
95 changes: 95 additions & 0 deletions
95
packages/block-editor/src/components/iframe/use-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,95 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* 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 useCompatibilityStyles() { | ||
// Only memoize the result once on load, since these stylesheets should not | ||
// change. | ||
return useMemo( () => { | ||
// Search the document for stylesheets targetting the editor canvas. | ||
return 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; | ||
|
||
if ( ! cssRules ) { | ||
return accumulator; | ||
} | ||
|
||
// Generally, ignore inline styles. We add inline styles belonging to a | ||
// stylesheet later, which may or may not match the selectors. | ||
if ( ownerNode.tagName !== 'LINK' ) { | ||
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; | ||
} | ||
|
||
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 ) ) { | ||
// Display warning once we have a way to add style dependencies to the editor. | ||
// See: https://github.com/WordPress/gutenberg/pull/37466. | ||
accumulator.push( ownerNode.cloneNode( true ) ); | ||
|
||
// Add inline styles belonging to the stylesheet. | ||
const inlineCssId = ownerNode.id.replace( | ||
'-css', | ||
'-inline-css' | ||
); | ||
const inlineCssElement = | ||
document.getElementById( inlineCssId ); | ||
|
||
if ( inlineCssElement ) { | ||
accumulator.push( inlineCssElement.cloneNode( true ) ); | ||
} | ||
} | ||
|
||
return accumulator; | ||
}, | ||
[] | ||
); | ||
}, [] ); | ||
} |
f3b2861
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.
Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3757794289
📝 Reported issues:
specs/editor/various/block-deletion.test.js
specs/editor/various/multi-block-selection.test.js
specs/editor/plugins/custom-post-types.test.js
specs/site-editor/global-styles-sidebar.test.js