-
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
Block Editor: Use default layout type when block specifies unknown value #39232
base: trunk
Are you sure you want to change the base?
Conversation
Size Change: +21 B (0%) Total Size: 1.32 MB
ℹ️ View Unchanged
|
62f666c
to
e282980
Compare
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.
LGTM. Thanks for fixing!
Any thoughts there @scruffian on if this is a proper fix vs. a coverup that will lead to corruption? I don't have a good enough feel for what could happen to layouts if we auto-revert to |
If the layout type isn't recognised then things are definitely going to be broken in the editor. The way I see it this will do its best to fix that, but since the change will only be in the editor, the user will have the opportunity to preview it before saving, so if it does cause any issues they should see them. It seems safe to me! |
That's a great point about this only existing inside the editor @scruffian, and I think it gives me enough confidence to push this in. It'll either crash inside the editor (what it does now) and you can see none of it, or it'll display wrong in the editor in which case you can try to fix it. The one sticking point is that the existing crashing behavior does preserve that attribute while picking the default layout I think will end up changing the attribute on save. If a block isn't in view when loading a post that could lead to the common situation of "I opened my post in the editor and now the post is broken." |
e282980
to
f190b64
Compare
…lue. While working on #38923 with invalid blocks it happened that a JavaScript error surfaced when working with a block that specified an incorrect layout type. In the case at hand the block asked for the `flerx` alignment instead of the `flex` alignment. It's reasonable to expect blocks to call for unrecognized layouts, especially if a plugin introduces new layouts and then blocks are loaded without that plugin's support. When `getLayoutType` is unable to find a recognized layout it returns `undefined` which causes numerous issues with calling code that expects a valid layout. In this patch we're guarding for those cases and if no known layout can be found for the specified layout type then we return the default layout instead. This introduces data corruption so it could be that this solution is more of a coverup than a fix. There is no way however to ignore that layout and preserve the behavior while still editing the block. On the other hand a block should not fail validation due to an implementation bug in the core editor and if this fix truly resolves the issue we should find invalidated blocks and prevent editing anyway (unfortunately this is not the case in this patch).
f190b64
to
c594237
Compare
It seems we can rebase and merge this proposal. |
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. |
@Mamaduka please feel free to commandeer this. it’s obviously been a while since I touched it, but I think I never merged it because I was still uncomfortable with the fact that it would create a new situation in which opening a post (and doing nothing other than opening it) in the editor could break a post. I generally like to avoid that at all costs. |
Description
While working on #38923 with invalid blocks it happened that a JavaScript
error surfaced when working with a block that specified an incorrect layout
type. In the case at hand the block asked for the
flerx
alignment insteadof the
flex
alignment.It's reasonable to expect blocks to call for unrecognized layouts,
especially if a plugin introduces new layouts and then blocks are loaded
without that plugin's support.
When
getLayoutType
is unable to find a recognized layout it returnsundefined
which causes numerous issues with calling code that expectsa valid layout.
In this patch we're guarding for those cases and if no known layout can
be found for the specified layout type then we return the default layout
instead. This introduces data corruption so it could be that this solution
is more of a coverup than a fix. There is no way however to ignore that
layout and preserve the behavior while still editing the block. On the
other hand a block should not fail validation due to an implementation bug
in the core editor and if this fix truly resolves the issue we should find
invalidated blocks and prevent editing anyway (unfortunately this is not
the case in this patch).
Can you provide some insight into how this kind of problem should be resolved?
If this is a "right" approach I can finish the PR and make sure it's ready
to merge. If I'm handling this improperly I'm fine taking a different approach.
Testing Instructions
Edit a post and paste the following contents into the text editor to produce invalid markup.
Notice in
trunk
that the block has been replaced with an error message while in this branch it loads fine, though the rendering should be using the default layout type/alignemnt instead of the non-existentflerx
as incorrectly specified in the block comment.Screenshots
The JS error produces the "This block has encountered an error and cannot be previewed" message. It's not a failure to validate the block; instead it's a JS exception thrown when rendering the block and that's the exception isolation boundary replacing the contents with that message.
Thanks to @scruffian for calling this error out
Types of changes
Checklist:
*.native.js
files for terms that need renaming or removal).