-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Reusable blocks: prevent infinite recursion #28405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c228df2
Hot fix: Reusable blocks: Prevent recursion
mcsf 1c5f5da
FIXME Don't let editor-styles-wrapper style Warning
mcsf 9392953
Revert "Hot fix: Reusable blocks: Prevent recursion"
mcsf 51deb88
Reusable blocks: stop client-side infinite recursion
mcsf f9c9dd6
Reusable blocks: stop server-side infinite recursion
mcsf 2f93b2b
Trigger user warning on the front end
mcsf 28d8c42
Add tests for useNoRecursiveRenders
mcsf 9c7d4af
Include reusable block's title in front-end warning
mcsf 90eb586
Trigger PHP warning only in front end, not admin
mcsf 1ee9767
Revert "FIXME Don't let editor-styles-wrapper style Warning"
mcsf bee83e8
Honor WP_DEBUG_DISPLAY before displaying failure point
mcsf 0537efe
Translate "[block rendering halted]" snippet
mcsf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
100 changes: 100 additions & 0 deletions
100
packages/block-library/src/block/test/use-no-recursive-renders.js
This file contains hidden or 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,100 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { render } from '@testing-library/react'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Fragment } from '@wordpress/element'; | ||
|
|
||
| // Mimics a block's Edit component, such as ReusableBlockEdit, which is capable | ||
| // of calling itself depending on its `ref` attribute. | ||
| function Edit( { attributes: { ref } } ) { | ||
| const [ hasAlreadyRendered, RecursionProvider ] = useNoRecursiveRenders( | ||
| ref | ||
| ); | ||
|
|
||
| if ( hasAlreadyRendered ) { | ||
| return <div className="wp-block__reusable-block--halted">Halt</div>; | ||
| } | ||
|
|
||
| return ( | ||
| <RecursionProvider> | ||
| <div className="wp-block__reusable-block"> | ||
| { ref === 'SIMPLE' && <p>Done</p> } | ||
| { ref === 'SINGLY-RECURSIVE' && ( | ||
| <Edit attributes={ { ref } } /> | ||
| ) } | ||
| { ref === 'MUTUALLY-RECURSIVE-1' && ( | ||
| <Edit attributes={ { ref: 'MUTUALLY-RECURSIVE-2' } } /> | ||
| ) } | ||
| { ref === 'MUTUALLY-RECURSIVE-2' && ( | ||
| <Edit attributes={ { ref: 'MUTUALLY-RECURSIVE-1' } } /> | ||
| ) } | ||
| </div> | ||
| </RecursionProvider> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import useNoRecursiveRenders from '../use-no-recursive-renders'; | ||
|
|
||
| describe( 'useNoRecursiveRenders', () => { | ||
| it( 'allows a single block to render', () => { | ||
| const { container } = render( | ||
| <Edit attributes={ { ref: 'SIMPLE' } } /> | ||
| ); | ||
| expect( | ||
| container.querySelectorAll( '.wp-block__reusable-block' ) | ||
| ).toHaveLength( 1 ); | ||
| expect( | ||
| container.querySelectorAll( '.wp-block__reusable-block--halted' ) | ||
| ).toHaveLength( 0 ); | ||
| } ); | ||
|
|
||
| it( 'allows equal but sibling blocks to render', () => { | ||
| const { container } = render( | ||
| <Fragment> | ||
| <Edit attributes={ { ref: 'SIMPLE' } } /> | ||
| <Edit attributes={ { ref: 'SIMPLE' } } /> | ||
| </Fragment> | ||
| ); | ||
| expect( | ||
| container.querySelectorAll( '.wp-block__reusable-block' ) | ||
| ).toHaveLength( 2 ); | ||
| expect( | ||
| container.querySelectorAll( '.wp-block__reusable-block--halted' ) | ||
| ).toHaveLength( 0 ); | ||
| } ); | ||
|
|
||
| it( 'prevents a block from rendering itself', () => { | ||
| const { container } = render( | ||
| <Fragment> | ||
| <Edit attributes={ { ref: 'SINGLY-RECURSIVE' } } /> | ||
| </Fragment> | ||
| ); | ||
| expect( | ||
| container.querySelectorAll( '.wp-block__reusable-block' ) | ||
| ).toHaveLength( 1 ); | ||
| expect( | ||
| container.querySelectorAll( '.wp-block__reusable-block--halted' ) | ||
| ).toHaveLength( 1 ); | ||
| } ); | ||
|
|
||
| it( 'prevents mutual recursion between two blocks', () => { | ||
| const { container } = render( | ||
| <Fragment> | ||
| <Edit attributes={ { ref: 'MUTUALLY-RECURSIVE-1' } } /> | ||
| </Fragment> | ||
| ); | ||
| expect( | ||
| container.querySelectorAll( '.wp-block__reusable-block' ) | ||
| ).toHaveLength( 2 ); | ||
| expect( | ||
| container.querySelectorAll( '.wp-block__reusable-block--halted' ) | ||
| ).toHaveLength( 1 ); | ||
| } ); | ||
| } ); |
29 changes: 29 additions & 0 deletions
29
packages/block-library/src/block/use-no-recursive-renders.js
This file contains hidden or 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,29 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { | ||
| createContext, | ||
| useCallback, | ||
| useContext, | ||
| useMemo, | ||
| } from '@wordpress/element'; | ||
|
|
||
| const RenderedRefsContext = createContext( [] ); | ||
|
|
||
| export default function useNoRecursiveRenders( ref ) { | ||
| const previouslyRenderedRefs = useContext( RenderedRefsContext ); | ||
| const hasAlreadyRendered = previouslyRenderedRefs.includes( ref ); | ||
| const newRenderedRefs = useMemo( () => [ ...previouslyRenderedRefs, ref ], [ | ||
| ref, | ||
| previouslyRenderedRefs, | ||
| ] ); | ||
| const Provider = useCallback( | ||
| ( { children } ) => ( | ||
| <RenderedRefsContext.Provider value={ newRenderedRefs }> | ||
| { children } | ||
| </RenderedRefsContext.Provider> | ||
| ), | ||
| [ newRenderedRefs ] | ||
| ); | ||
| return [ hasAlreadyRendered, Provider ]; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker for this PR. I wanted to share my thought on how to make it general enough to use with other blocks that require the same guard (Post Content or Template Part). It looks like it would be enough to rename
refto something more generic likeuniqueIdand useSetrather than array of strings for checks to support compound values for more complex blocks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, thanks for the note!