-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Check for allowed blocks recursively in patterns #30366
Check for allowed blocks recursively in patterns #30366
Conversation
Size Change: +638 B (0%) Total Size: 1.32 MB
ℹ️ View Unchanged
|
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.
In general, besides the recursion for innerBlocks that wasn't taken into account this solution seems to align with my previous commit here: 48b703f.
This was only there because __experimentalGetParsedPattern
is called in BlockPreview
. The allowed
should be checked with canInsertBlockType
I think. The problem though is that canInsertBlockType
uses information from the state, that the parsed patterns are not in :)
Finally in __experimentalGetAllowedPatterns
we still check for top level blocks only. canInsertBlockType
makes more checks besides the allowedBlockTypes
, based on the provided rootClientId
.
A bit confusing, no 😄
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.
Hey David, this looks good to me and it might the time to update PR's description, add some tests probably and I was wondering if we can have some performance checks.
c1ab1f6
to
22cd6f9
Compare
I have a question for @david-szabo97 and @ntsekouras: would it be helpful to distinguish “stage I” parsing from “stage II” parsing when it comes to deciding which patterns to display? Let me explain:
I am asking this for two reasons:
What are your thoughts? |
stage 2 gutenberg/packages/blocks/src/api/parser.js Line 467 in b88ae76
Because of this, we might end up with different blocks in stage 1 and stage 2. So if we use stage 1 blocks to do the check, then that might report a false positive. Is that correct? |
@david-szabo97: no, the difference should only be in the amount of information available for each block in the pattern. See the screenshot below. On the left is the arrangement of blocks we are dealing with. On the right, in the console, we use the serialised markup of those blocks and run it through the stage I parser and then the full parser. Notice how both represent the same tree structure, but compare the attributes for the Heading block: in the first output, These are the differences at play, along with knowledge of whether each block is considered valid, i.e. in conformance with its own definitions. |
@mcsf stage 1 only: 42ms stage 1 + 2: 11329ms let startTime;
startTime = Date.now()
for (let i = 0; i < 10_000; i++) wp.blockSerializationDefaultParser.parse(markup + `<p>Stage 1 ${i}</p>`);
console.log(Date.now() - startTime) // 42
startTime = Date.now()
for (let i = 0; i < 10_000; i++) wp.blocks.parse(markup + `<p>Stage 1 + 2 ${i}</p>`);
console.log(Date.now() - startTime) // 11329 |
I am not too surprised, but it's a delight to see it. 😄 (cc @dmsnell just for fun — no input required). |
d70daf8
to
854d9e9
Compare
Had to fix a few usages of Any smoke tests are appreciated.
|
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.
Thanks for your work here David and @mcsf for the great stage 1 input 😄 !
I've left a comment for a change regarding the BlockPatternSetup
component, but other than that this looks great!
packages/block-editor/src/components/block-pattern-setup/index.js
Outdated
Show resolved
Hide resolved
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.
🚀
Fixes #23275
Description
Currently, patterns show up in the inserter even if they contain a block that's not on the allowed blocks list. Blocks aren't checked recursively in the patterns, only top level blocks. Even though they show up, we can't insert them since the blocks are disallowed. This PR checks recursively for disallowed blocks.
A new (private) selector is introduced:
getAllAllowedPatterns
. This intermediary function memoizes the available allowed patterns. This is used to avoid iterating over patterns that contain disallowed blocks and to avoid parsing all the patterns every time we call the__experimentalGetAllowedPatterns
with a differentclientId
.We use "Stage I" parser here for performance reasons. See
#30366 (comment)
#30366 (comment)
Why don't we use
canInsertBlockType
instead ofcheckAllowListRecursive
?Recursively checking all blocks with
canInsertBlockType
every time we select a different block would have a huge performance impact. That's why we only check top level blocks withcanInsertBlockType
and the nested ones withcheckAllowListRecursive
.How has this been tested?
Types of changes
Bug Fix
Checklist:
*.native.js
files for terms that need renaming or removal).