Skip to content

Commit

Permalink
Perform transformation a second time after applying filters
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewserong committed Aug 27, 2024
1 parent 8536354 commit 4c2c130
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 17 deletions.
44 changes: 27 additions & 17 deletions packages/blocks/src/store/process-block-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ export const processBlockType =
null
);

// Re-stabilize any experimental supports after applying filters.
// This ensures that any supports updated by filters are also stabilized.
blockType.supports = stabilizeSupports( blockType.supports );

if (
settings.description &&
typeof settings.description !== 'string'
Expand All @@ -145,27 +149,33 @@ export const processBlockType =

if ( settings.deprecated ) {
settings.deprecated = settings.deprecated.map( ( deprecation ) => {
// Stabilize any experimental supports before applying filters.
deprecation.supports = stabilizeSupports(
deprecation.supports
);
const filteredDeprecation = // Only keep valid deprecation keys.
applyFilters(
'blocks.registerBlockType',
// Merge deprecation keys with pre-filter settings
// so that filters that depend on specific keys being
// present don't fail.
{
// Omit deprecation keys here so that deprecations
// can opt out of specific keys like "supports".
...omit( blockType, DEPRECATED_ENTRY_KEYS ),
...deprecation,
},
blockType.name,
deprecation
);
// Re-stabilize any experimental supports after applying filters.
// This ensures that any supports updated by filters are also stabilized.
filteredDeprecation.supports = stabilizeSupports(
filteredDeprecation.supports
);

return Object.fromEntries(
Object.entries(
// Only keep valid deprecation keys.
applyFilters(
'blocks.registerBlockType',
// Merge deprecation keys with pre-filter settings
// so that filters that depend on specific keys being
// present don't fail.
{
// Omit deprecation keys here so that deprecations
// can opt out of specific keys like "supports".
...omit( blockType, DEPRECATED_ENTRY_KEYS ),
...deprecation,
},
blockType.name,
deprecation
)
).filter( ( [ key ] ) =>
Object.entries( filteredDeprecation ).filter( ( [ key ] ) =>
DEPRECATED_ENTRY_KEYS.includes( key )
)
);
Expand Down
137 changes: 137 additions & 0 deletions packages/blocks/src/store/test/process-block-type.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { addFilter, removeFilter } from '@wordpress/hooks';

/**
* Internal dependencies
*/
Expand All @@ -17,6 +22,10 @@ describe( 'processBlockType', () => {
getBootstrappedBlockType: () => null,
};

afterEach( () => {
removeFilter( 'blocks.registerBlockType', 'test/filterSupports' );
} );

it( 'should return the block type with stabilized typography supports', () => {
const blockSettings = {
...baseBlockSettings,
Expand Down Expand Up @@ -109,6 +118,65 @@ describe( 'processBlockType', () => {
} );
} );

it( 'should reapply transformations after supports are filtered', () => {
const blockSettings = {
...baseBlockSettings,
supports: {
typography: {
fontSize: true,
lineHeight: true,
__experimentalFontFamily: true,
__experimentalFontStyle: true,
__experimentalFontWeight: true,
__experimentalLetterSpacing: true,
__experimentalTextTransform: true,
__experimentalTextDecoration: true,
__experimentalWritingMode: true,
__experimentalDefaultControls: {
fontSize: true,
fontAppearance: true,
textTransform: true,
},
},
},
};

addFilter(
'blocks.registerBlockType',
'test/filterSupports',
( settings, name ) => {
if ( name === 'test/block' && settings.supports.typography ) {
settings.supports.typography.__experimentalFontFamily = false;
settings.supports.typography.__experimentalFontStyle = false;
settings.supports.typography.__experimentalFontWeight = false;
}
return settings;
}
);

const processedBlockType = processBlockType(
'test/block',
blockSettings
)( { select } );

expect( processedBlockType.supports.typography ).toEqual( {
fontSize: true,
lineHeight: true,
fontFamily: false,
fontStyle: false,
fontWeight: false,
letterSpacing: true,
textTransform: true,
textDecoration: true,
__experimentalWritingMode: true,
__experimentalDefaultControls: {
fontSize: true,
fontAppearance: true,
textTransform: true,
},
} );
} );

it( 'should stabilize experimental typography supports within block deprecations', () => {
const blockSettings = {
...baseBlockSettings,
Expand Down Expand Up @@ -164,4 +232,73 @@ describe( 'processBlockType', () => {
__experimentalWritingMode: true,
} );
} );

it( 'should reapply transformations after supports are filtered within block deprecations', () => {
const blockSettings = {
...baseBlockSettings,
supports: {
typography: {
fontSize: true,
lineHeight: true,
fontFamily: true,
fontStyle: true,
fontWeight: true,
letterSpacing: true,
textTransform: true,
textDecoration: true,
__experimentalWritingMode: true,
__experimentalDefaultControls: {
fontSize: true,
fontAppearance: true,
textTransform: true,
},
},
},
deprecated: [
{
supports: {
typography: {
__experimentalFontFamily: true,
__experimentalFontStyle: true,
__experimentalFontWeight: true,
__experimentalLetterSpacing: true,
__experimentalTextTransform: true,
__experimentalTextDecoration: true,
__experimentalWritingMode: true,
},
},
},
],
};

addFilter(
'blocks.registerBlockType',
'test/filterSupports',
( settings, name ) => {
if ( name === 'test/block' && settings.supports.typography ) {
settings.supports.typography.__experimentalFontFamily = false;
settings.supports.typography.__experimentalFontStyle = false;
settings.supports.typography.__experimentalFontWeight = false;
}
return settings;
}
);

const processedBlockType = processBlockType(
'test/block',
blockSettings
)( { select } );

expect(
processedBlockType.deprecated[ 0 ].supports.typography
).toEqual( {
fontFamily: false,
fontStyle: false,
fontWeight: false,
letterSpacing: true,
textTransform: true,
textDecoration: true,
__experimentalWritingMode: true,
} );
} );
} );

0 comments on commit 4c2c130

Please sign in to comment.