Block Editor: Validate options for the 'HeadingLevelDropdown' component#65425
Block Editor: Validate options for the 'HeadingLevelDropdown' component#65425Mamaduka merged 2 commits intoWordPress:trunkfrom
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.
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.
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 <[email protected]> Co-authored-by: Mamaduka <[email protected]> Co-authored-by: ndiego <[email protected]>
|
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
HeaderLevelDropdownWhy?
We do not want to allow options that will result in heading tags that do not exist.
How?
The values passed to
optionsare filtered to only allow values that are included inHEADING_LEVELS. This PR still allows0as an option as well, in order to handle the case where passing 0 will result in aptag.Testing Instructions
Visual editoronlyh4, h5, h6are available as choicesTesting Instructions for Keyboard
Screenshots or screencast