-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hot fix: Reusable blocks: Prevent recursion
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/block-library/src/block/has-nested-reusable-blocks.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) | ||
); | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/block-library/src/block/test/has-nested-reusable-blocks.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); |