From 88de21bac0c2652beb2b835fbce0ea3e2a1bdf89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Mon, 21 Oct 2024 11:50:54 +0200 Subject: [PATCH] Block inserter: prevent editor from crashing if `blockType.parent` is a string (#66234) Co-authored-by: oandregal Co-authored-by: youknowriad Co-authored-by: gziolo --- packages/block-editor/src/store/selectors.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 3fdaadb24fc129..787225f18a0a57 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -1628,8 +1628,18 @@ const isBlockVisibleInTheInserter = ( checkedBlocks.add( blockName ); // If parent blocks are not visible, child blocks should be hidden too. - if ( !! blockType.parent?.length ) { - return blockType.parent.some( + // + // In some scenarios, blockType.parent may be a string. + // A better approach would be sanitize parent in all the places that can be modified: + // block registration, processBlockType, filters, etc. + // In the meantime, this is a hotfix to prevent the editor from crashing. + const parent = + typeof blockType.parent === 'string' || + blockType.parent instanceof String + ? [ blockType.parent ] + : blockType.parent; + if ( Array.isArray( parent ) ) { + return parent.some( ( name ) => ( blockName !== name && isBlockVisibleInTheInserter( @@ -1643,6 +1653,7 @@ const isBlockVisibleInTheInserter = ( name === 'core/post-content' ); } + return true; };