Skip to content

Commit

Permalink
Hot fix: Reusable blocks: Prevent recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed Jan 21, 2021
1 parent 9811055 commit c228df2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { store as reusableBlocksStore } from '@wordpress/reusable-blocks';
/**
* Internal dependencies
*/
import hasNestedReusableBlocks from './has-nested-reusable-blocks';

export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) {
const { isMissing, hasResolved } = useSelect(
Expand Down Expand Up @@ -83,6 +84,16 @@ export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) {

const blockProps = useBlockProps();

if ( hasNestedReusableBlocks( blocks ) ) {
return (
<div { ...blockProps }>
<Warning>
{ __( `Recursion is cool, until it isn't.` ) }
</Warning>
</div>
);
}

if ( isMissing ) {
return (
<div { ...blockProps }>
Expand Down
11 changes: 11 additions & 0 deletions packages/block-library/src/block/has-nested-reusable-blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const DISALLOWED_BLOCKS = [ 'core/block', 'core/template-part' ];

export default function hasNestedReusableBlocks( block ) {
if ( Array.isArray( block ) ) {
return block.some( hasNestedReusableBlocks );
}
return (
DISALLOWED_BLOCKS.includes( block.name ) ||
block.innerBlocks.some( hasNestedReusableBlocks )
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Internal dependencies
*/
import hasNestedReusableBlocks from '../has-nested-reusable-blocks';

const badSingleBlock = [
{
name: 'core/block',
innerBlocks: [],
},
];

const goodSingleBlock = [
{
name: 'core/paragraph',
innerBlocks: [],
},
];

const badNestedBlocks = [
{
name: 'core/paragraph',
innerBlocks: [],
},
{
name: 'core/group',
innerBlocks: [
{
name: 'core/template-part',
innerBlocks: [],
},
],
},
];

const goodNestedBlocks = [
{
name: 'core/paragraph',
innerBlocks: [],
},
{
name: 'core/group',
innerBlocks: [
{
name: 'core/paragraph',
innerBlocks: [],
},
],
},
];

describe( 'hasNestedReusableBlocks', () => {
it( 'handles shallow block sets', () => {
expect( hasNestedReusableBlocks( goodSingleBlock ) ).toBe( false );
expect( hasNestedReusableBlocks( badSingleBlock ) ).toBe( true );
} );
it( 'handles nested blocks', () => {
expect( hasNestedReusableBlocks( goodNestedBlocks ) ).toBe( false );
expect( hasNestedReusableBlocks( badNestedBlocks ) ).toBe( true );
} );
} );

0 comments on commit c228df2

Please sign in to comment.