-
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 Editor: Validate options for the 'HeadingLevelDropdown' component #65425
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. |
I wonder if we should also add an additional attribute for setting |
@Mamaduka @t-hamano @noisysocks, whenever you have a chance, can you review this implementation? This fix is needed, but I defer to you all the best way to accomplish it. 🙏 |
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.
Thank you, @philwp! The fix makes sense and works as expected ✅
Ideally, similar validation should happen at the attribute level, and unsupported values should be discarded, maybe logging a warning in the console.
Example: Updated attribute definition for Heading block:
"levelOptions": {
"type": "array",
"items": {
"enum": [ 1, 2, 3, 4, 5, 6 ],
"type": "number"
}
}
.filter( | ||
( option ) => option === 0 || HEADING_LEVELS.includes( option ) | ||
) | ||
.sort( ( a, b ) => a - b ); // Sorts numerically in ascending order; |
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.
Note: While usually sorting callback can be omitted in similar cases, this is safer when dealing with numeric values.
…nt (#65425) Co-authored-by: philwp <philwebs@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: ndiego <ndiego@git.wordpress.org>
I just cherry-picked this PR to the wp/6.7 branch to get it included in the next release: d6dfb79 |
Resolves #65422
What?
This PR prevents values that would end up as invalid heading levels from appearing in the
HeaderLevelDropdown
Why?
We do not want to allow options that will result in heading tags that do not exist.
How?
The values passed to
options
are filtered to only allow values that are included inHEADING_LEVELS
. This PR still allows0
as an option as well, in order to handle the case where passing 0 will result in ap
tag.Testing Instructions
Visual editor
onlyh4, h5, h6
are available as choicesTesting Instructions for Keyboard
Screenshots or screencast