-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Block inserter: prevent editor from crashing if blockType.parent
is a string
#66234
Conversation
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
parent
is a stringblockType.parent
is a string
Size Change: +26 B (0%) Total Size: 1.77 MB
ℹ️ View Unchanged
|
@youknowriad does this need to be part of WordPress 6.7 as well? Given the timeline, I presume #65700 wasn't part of it? |
Flaky tests detected in 2ec7a03. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11402623235
|
@@ -1595,7 +1595,7 @@ const isBlockVisibleInTheInserter = ( | |||
checkedBlocks.add( blockName ); | |||
|
|||
// If parent blocks are not visible, child blocks should be hidden too. | |||
if ( !! blockType.parent?.length ) { | |||
if ( Array.isArray( blockType?.parent ) ) { |
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.
I think we should cast to array instead, otherwise, the check won't be applied if there's a single string parent.
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.
Can parent
be a string? It doesn't seem so according to the docs. Not knowing all the details of this, I'm a bit reluctant to cast the type.
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.
I doubt it would work with a single string value correctly.
In the block.json
schema there is also an array enforced:
gutenberg/schemas/json/block.json
Line 65 in fa12080
"type": "array", |
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.
After reading a bit. I’m inclined to follow the advice from @youknowriad and cast string values to array to make it backward compatible if there are existing blocks that used the string during the block type’s validation. It happens at the end of this file:
https://github.com/WordPress/gutenberg/blob/trunk/packages/blocks/src/store/process-block-type.js
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.
Pushed a6425e6
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.
No need to include in 6.7 it seems. |
@youknowriad @gziolo how is this one looking? anything else to do? |
a6425e6
to
91c2aad
Compare
blockType.parent instanceof String | ||
? [ blockType.parent ] | ||
: blockType.parent; | ||
if ( Array.isArray( parent ) ) { |
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.
Is this condition still necessary. I guess right now it's more if ( !! parent )
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.
I'm curious why we'd need both typeof blockType.parent === 'string' || blockType.parent instanceof String
.. 🤔
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.
I'm curious why we'd need both typeof blockType.parent === 'string' || blockType.parent instanceof String..
Otherwise, we don't cover all potential issues. Note the issue was that we checked for .length
in a type that wasn't an array (didn't have .some
). That's the case for both literal & object strings.
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.
… a string (WordPress#66234) Co-authored-by: oandregal <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: gziolo <[email protected]>
Potential fix for https://wordpress.org/support/topic/error-after-update-228/
What?
Fixes an error when blocks'
parent
property is a string.Why?
In the forum, I ran into a report where users have the editor crash when trying to open the inserter. Environment conditions are WordPress 6.6.2 and Gutenberg 15.6. The first error hints at
TypeError: n.parent.some is not a function at ...
.Investigating a bit, it seems related to #65700 The report is recent (matches the Gutenberg version where that PR was introduced), the users report issues when inserting blocks, and there's no other place in the codebase that uses
parent.some
. See conversation.While reproducing the exact user conditions is difficult (e.g.: asking them what 3rd party block libraries do they have, etc.), we can be more explicit about the check.
How?
Update the check for the parent so it verifies it's the type we want it to be: a non-empty array.
Testing Instructions
The best I could come up with is the following:
Using
trunk
, the editor would crash. With this PR, it doesn't.Further work
As suggested in https://github.com/WordPress/gutenberg/pull/65700/files#r1806317308 we should look at enforcing the
blockType.parent
property being an array if that is its only valid value. Given how long we haven't enforced it, doing that now may be side-effects and requires a longer investigation and testing period than this approach — hence why I prioritized preparing this hotfix.