From c594237afa090f2730b3b7d71b7aea7d7e3749df Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Fri, 4 Mar 2022 16:11:29 -0700 Subject: [PATCH] Block Editor: Use default layout type when block specifies unknown value. While working on #38923 with invalid blocks it happened that a JavaScript error surfaced when working with a block that specified an incorrect layout type. In the case at hand the block asked for the `flerx` alignment instead of the `flex` alignment. It's reasonable to expect blocks to call for unrecognized layouts, especially if a plugin introduces new layouts and then blocks are loaded without that plugin's support. When `getLayoutType` is unable to find a recognized layout it returns `undefined` which causes numerous issues with calling code that expects a valid layout. In this patch we're guarding for those cases and if no known layout can be found for the specified layout type then we return the default layout instead. This introduces data corruption so it could be that this solution is more of a coverup than a fix. There is no way however to ignore that layout and preserve the behavior while still editing the block. On the other hand a block should not fail validation due to an implementation bug in the core editor and if this fix truly resolves the issue we should find invalidated blocks and prevent editing anyway (unfortunately this is not the case in this patch). --- packages/block-editor/src/layouts/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/layouts/index.js b/packages/block-editor/src/layouts/index.js index 4ec1ff6f33191..50e15f5c513ec 100644 --- a/packages/block-editor/src/layouts/index.js +++ b/packages/block-editor/src/layouts/index.js @@ -14,7 +14,10 @@ const layoutTypes = [ flow, flex, constrained ]; * @return {Object} Layout type. */ export function getLayoutType( name = 'default' ) { - return layoutTypes.find( ( layoutType ) => layoutType.name === name ); + return ( + layoutTypes.find( ( layoutType ) => layoutType.name === name ) ?? + layoutTypes.find( ( layoutType ) => layoutType.name === 'default' ) + ); } /**