Skip to content

Commit

Permalink
Block Editor: Use default layout type when block specifies unknown va…
Browse files Browse the repository at this point in the history
…lue.

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).
  • Loading branch information
dmsnell committed Mar 4, 2022
1 parent 446d001 commit 62f666c
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/block-editor/src/layouts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const layoutTypes = [ flow, flex ];
* @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' )
);
}

/**
Expand Down

0 comments on commit 62f666c

Please sign in to comment.