Skip to content

Block editor: Self nesting and circular nesting block fix#66121

Merged
youknowriad merged 5 commits intoWordPress:trunkfrom
SH4LIN:fix/self-nesting-circular-nesting-blocks
Oct 16, 2024
Merged

Block editor: Self nesting and circular nesting block fix#66121
youknowriad merged 5 commits intoWordPress:trunkfrom
SH4LIN:fix/self-nesting-circular-nesting-blocks

Conversation

@SH4LIN
Copy link
Copy Markdown
Contributor

@SH4LIN SH4LIN commented Oct 15, 2024

closes #66090

What?

  • This PR checks the current block name when determining if a block is allowed in the inserter. If the parent block name and the current block name are the same, we immediately return false.
  • This PR also checks for circular nesting by maintaining a list of blocks that have already been checked. If circular nesting is detected, we return false.
  • Additionally, this PR prevents the registration of a block if it is supposed to be added within itself.

Why?

  • Self nesting blocks and circular nesting block causes infinite loops.

How?

  • This PR checks the current block name when determining if a block is allowed in the inserter. If the parent block name and the current block name are the same, we immediately return false.
  • This PR also checks for circular nesting by maintaining a list of blocks that have already been checked. If circular nesting is detected, we return false.
  • Additionally, this PR prevents the registration of a block if it is supposed to be added within itself.

Testing Instructions

Testing Instructions for Keyboard

Screenshots or screencast

Fixes/Covers

@SH4LIN SH4LIN requested a review from ellatrix as a code owner October 15, 2024 10:58
@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 15, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: SH4LIN <sh4lin@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: yuliyan <yuliyan@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @SH4LIN! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Oct 15, 2024
@youknowriad youknowriad requested a review from t-hamano October 15, 2024 12:23
Copy link
Copy Markdown
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me. Do you think we can add some unit tests?

@t-hamano t-hamano linked an issue Oct 16, 2024 that may be closed by this pull request
2 tasks
Copy link
Copy Markdown
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've confirmed that nested blocks cannot be inserted at the root level, and I've also confirmed the browser warning when registering a block that is itself a parent.

At the very least, we may need to add unit tests for registering blocks here.

Comment on lines +235 to +240
if (
settings?.parent &&
Array.isArray( settings.parent ) &&
settings.parent.length === 1 &&
settings.parent[ 0 ] === name
) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (
settings?.parent &&
Array.isArray( settings.parent ) &&
settings.parent.length === 1 &&
settings.parent[ 0 ] === name
) {
if ( settings?.parent?.[ 0 ] === name && settings.parent.length === 1 ) {

Nit: I don't have a strong opinion, but this might be simpler.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll update the code and add test case

@SH4LIN
Copy link
Copy Markdown
Contributor Author

SH4LIN commented Oct 16, 2024

@t-hamano @youknowriad I have updated the code LMK if any further changes are required

@youknowriad youknowriad added the [Type] Bug An existing feature does not function as intended label Oct 16, 2024
@youknowriad youknowriad enabled auto-merge (squash) October 16, 2024 19:21
@youknowriad youknowriad merged commit 2177aed into WordPress:trunk Oct 16, 2024
@youknowriad
Copy link
Copy Markdown
Contributor

Thanks for your contribution :)

@github-actions github-actions bot added this to the Gutenberg 19.6 milestone Oct 16, 2024
@cbravobernal cbravobernal changed the title GH-66090: Self nesting and circular nesting block fix Block editor: Self nesting and circular nesting block fix Oct 30, 2024
@cbravobernal cbravobernal added the [Package] Block editor /packages/block-editor label Oct 30, 2024
karthick-murugan pushed a commit to karthick-murugan/gutenberg that referenced this pull request Nov 13, 2024
Co-authored-by: SH4LIN <sh4lin@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: yuliyan <yuliyan@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Block editor /packages/block-editor [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Self-nested blocks causing infinite recursion and crashing the editor

4 participants