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 mutated global styles context #49623

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,9 @@ export const toStyles = (
hasFallbackGapSupport,
disableLayoutStyles = false
) => {
// Clone the tree to avoid mutating the original object.
tree = JSON.parse( JSON.stringify( tree ) );

const nodesWithStyles = getNodesWithStyles( tree, blockSelectors );
const nodesWithSettings = getNodesWithSettings( tree, blockSelectors );
const useRootPaddingAlign = tree?.settings?.useRootPaddingAwareAlignments;
Expand Down Expand Up @@ -780,6 +783,7 @@ export const toStyles = (
// Process styles for block support features with custom feature level
// CSS selectors set.
if ( featureSelectors ) {
// Mutates the passed styles object.
const featureDeclarations = getFeatureDeclarations(
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is the function mutating the object (I see a delete there), why don't we update the function to avoid the mutation instead of cloning the whole tree.

Copy link
Member

Choose a reason for hiding this comment

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

The deletion/mutation is used to format some styles specially. There's code similar to this:

// special case
if ( duotoneSelector ) {
  ruleset += `${ duotoneSelector } { filter: ${ styles.filter.duotone }; }`;
  delete styles.filter;
}

// standard case (loop inside `getStylesDeclarations`)
if ( styles.filter.duotone ) {
  ruleset += `${ selector } { filter: { ${ styles.filter.duotone }; }`;
}

This is how getFeatureDeclarations extracts (and deletes) certain styles, and outputs them under a special selector, and how code for duotone extracts the filter.duotone styles. The rest is left for the standard getStylesDeclarations to handle.

To avoid the delete mutation, we'd have to rewrite this logic. Like building an exclude list of already processed paths and passing it to getStylesDeclarations.

We can continue cloning the styles object, but I think that cloning the entire tree object is too much. We're mutating only the styles extracted by getNodesWithStyles. So, it could be that function that clones the styles when calling nodes.push( { styles: clonedStyles } ). The pickStyleKeys could do the cloning.

featureSelectors,
styles
Expand Down