Skip to content

Commit

Permalink
Add back test and warning about parent as a string
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Oct 22, 2024
1 parent f35fb12 commit 4369ec0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
10 changes: 8 additions & 2 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,8 +749,14 @@ describe( 'blocks', () => {
title: 'block title',
parent: 'core/paragraph',
};
registerBlockType( 'core/test-block-parent-string', blockType );
expect( getBlockType( 'core/test-block-parent-string' ) ).toEqual( {
const block = registerBlockType(
'core/test-block-parent-string',
blockType
);
expect( console ).toHaveWarnedWith(
'Parent must be undefined or an array of strings (block types), but it is a string.'
);
expect( block ).toEqual( {
name: 'core/test-block-parent-string',
save: noop,
category: 'text',
Expand Down
14 changes: 10 additions & 4 deletions packages/blocks/src/store/process-block-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,28 @@ export const processBlockType =
return;
}

// Parent being an array hasn't been enforced in the past,
// so this is a way to maintain backwards compatibility
// with 3rd party blocks that may have been using it as a string.
if (
typeof settings?.parent === 'string' ||
settings?.parent instanceof String
) {
settings.parent = [ settings.parent ];
warning(
'Parent must be undefined or an array of strings (block types), but it is a string.'
);
// Intentionally continue:
//
// While string values were never supported, they appeared to work with some unintended side-effects
// that have been fixed by [#66250](https://github.com/WordPress/gutenberg/pull/66250).
//
// To be backwards-compatible, this code that automatically migrates strings to arrays.
}

if (
! Array.isArray( settings?.parent ) &&
settings?.parent !== undefined
) {
warning(
'Block parent must be undefined or an array of block types, but it is ',
'Parent must be undefined or an array of block types, but it is ',
settings.parent
);
return;
Expand Down

0 comments on commit 4369ec0

Please sign in to comment.