Skip to content
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

Fix iframe stylesheets cloning for Firefox #38049

Closed

Conversation

miguelpeixe
Copy link

Description

This PR changes the styleSheetsCompat() strategy for stylesheets cloning into iframes to use useRefEffect() in order to fix Firefox compatibility.

Closes #37961

How has this been tested?

Using the sample plugin provided in #37961, compare the result of the post editors' Patterns list in Chrome and Firefox.

Screenshots

Chrome 97.0.4692.71 Firefox 96.0.1
Current image image
This branch image image

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).
  • I've updated related schemas if appropriate.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Jan 18, 2022
@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @miguelpeixe! 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.

@skorasaurus skorasaurus added the Browser Issues Issues or PRs that are related to browser specific problems label Jan 20, 2022
doc.head.appendChild( inlineCssElement.cloneNode( true ) );
}
}
node.addEventListener( 'load', appendStyles );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the fix to move appending to the load event?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not simply move the whole effect into the load handler?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, I propose using useRefEffect() because it's the common strategy for this component DOM manipulation. setRef() is also aware of Firefox iframe behavior.

This PR was missing the same type of check, which I added in 6d6553f.


doc.head.appendChild( ownerNode.cloneNode( true ) );
if ( isMatch ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why has this been removed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The head.appendChild() was moved to the appendStyles() function. The idea is that the initial stylesheets iteration clones and pushes the node to the clonedStyles array. This way the appendStyles() function can also detect the node through doc.contains( style ) and be dedicated to only append the style at the appropriate time.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial stylesheets iteration and cloning don't have to be inside the effect, 0cef19c moves it to the upper level for better performance.

}

const { ownerNode, cssRules } = styleSheet;
const useEditorStyles = () =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: it's better to use named functions for debugging.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also document that this is a compatibility layer, if we remove compat from the name?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing! Updated in e61ecab

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed another arrow function, fixed in e752a13

}

node.addEventListener( 'load', appendStyles );
} );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dependencies are missing for this hook

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I don't understand which dependencies you are referring to.

// Search the document for stylesheets targetting the editor canvas.
const clonedStyles = [];
Array.from( document.styleSheets ).forEach( ( styleSheet ) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move all this into useRefEffect? It would also make it much easier to see the essence of this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but I believe it'd be less performant. The iteration through the root document styles doesn't have to be executed on changes to the iframe node.

@@ -74,21 +73,49 @@ function styleSheetsCompat( doc ) {
selectorText.includes( `.${ BLOCK_PREFIX }` ) )
);

if ( isMatch && ! doc.getElementById( ownerNode.id ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for removing the second check?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc does not exist at this point. This initial iteration through root document styles is dedicated to matching and cloning the nodes to the clonedStyles variable. Please observe lines 100-102 of the PR, in which the checks are made in order to append the style.

@ellatrix
Copy link
Member

I believe this was fixed in #40842. Thank you for your contribution!

@ellatrix ellatrix closed this Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Browser Issues Issues or PRs that are related to browser specific problems First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Iframe stylesheets are not cloned in Firefox
3 participants