Skip to content

Commit 6755a4d

Browse files
ramonjdtyxla
andauthored
Block Editor: Guard against non-string spacing preset values (#80467)
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org> Co-authored-by: tyxla <tyxla@git.wordpress.org>
1 parent 285d314 commit 6755a4d

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

packages/block-editor/src/components/spacing-sizes-control/test/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ describe( 'getSpacingPresetCssVar', () => {
8282
'var(--wp--preset--spacing--20)'
8383
);
8484
} );
85+
it( 'should return undefined for a malformed non-string value such as an array', () => {
86+
// Malformed blockGap data, e.g. `{ top: [ '1rem' ] }`, would otherwise
87+
// throw when `.match()` is called on the array.
88+
expect( getSpacingPresetCssVar( [ '1rem' ] ) ).toBe( undefined );
89+
} );
90+
it( 'should return undefined for other non-string values', () => {
91+
expect( getSpacingPresetCssVar( { top: '1rem' } ) ).toBe( undefined );
92+
expect( getSpacingPresetCssVar( 20 ) ).toBe( undefined );
93+
} );
8594
} );
8695

8796
describe( 'getSpacingPresetSlug', () => {

packages/block-editor/src/components/spacing-sizes-control/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export function getPresetValueFromCustomValue( value, spacingSizes ) {
126126
* @return {string | undefined} CSS var string for given spacing preset value.
127127
*/
128128
export function getSpacingPresetCssVar( value ) {
129-
if ( ! value ) {
129+
if ( ! value || typeof value !== 'string' ) {
130130
return;
131131
}
132132

0 commit comments

Comments
 (0)