Skip to content
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

GH-66090: Self nesting and circular nesting block fix #66121

Merged

Conversation

SH4LIN
Copy link
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
Copy link

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.

Copy link

👋 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
Copy link
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
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
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
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
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
65 checks passed
@youknowriad
Copy link
Contributor

Thanks for your contribution :)

@github-actions github-actions bot added this to the Gutenberg 19.6 milestone Oct 16, 2024
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 [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
3 participants