-
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
Fix deletion of custom block classes #8232
Conversation
7abf7c0
to
fd92be9
Compare
If you add a custom class to a block and then remove that custom class it would flag the block as invalid. The reason is that the custom class became part of the blocks ‘default’ attributes, and it would then add it back and this then the expected HTML wouldn’t match the current HTML. This change ensures the block contains the default class names + whatever classes are currently in the HTML, ignoring any custom ones in the block itself. It also handles the situation where the block's default class doesn't exist in the HTML The unit tests have been beefed up to cope with several edge case situations
fd92be9
to
5d986b5
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.
Thanks for the ping @johngodley . I agree this is a bug. I think there might be an easier approach here in retrieving the custom classes as simply the difference between innerHTML and the default block (i.e. a block serialized with a className
attribute of undefined
). I've done this in 1f2a7a1 . All tests still pass.
Let me know what you think. On my end, this is good to go 👍
That's great, thanks! It seemed like there should be a simpler way but I wasn't entirely sure what happened beyond this code. Merging. |
It looks like this behavior regressed custom classes for dynamic blocks because they don't have any serialized content #9991 |
If you add a custom class to a block and then remove that custom class it will flag the block as invalid. This is because the custom class becomes part of the block attributes, and is then added back by the 'custom class name' filter. The expected HTML then doesn't match the current HTML, and a warning is triggered.
You can reproduce the problem as follows:
<blockquote>
:<blockquote class="wp-block-quote newclass”><p>fdsfsdfsdd</p></blockquote>
newclass
This may or may not be expected behaviour, but it seems like removing the custom class shouldn't trigger a warning, and so I've treated it as a bug.
This PR ensures the block contains the default class names + whatever classes are currently in the HTML, ignoring any custom ones in the block itself.
The unit tests have been beefed up to cope with several edge case situations
Fixes #8131
Note that I don't fully understand the context of this change, and I may totally have misunderstood the code. The change keeps the existing unit tests running while fixing the additional cases of removing classes. I'm not sure if this is the purpose of the code, so the fix may be misplaced.
How has this been tested?
Existing unit tests work, and additional unit tests have been added. Manual verification will show the problem in different situations:
<p class="test">paragraph</p>
And then remove the class to trigger an invalid block warning
<blockquote class="wp-block-quote a b c"><p>fdsfsdfsdd</p></blockquote>
Then remove class
b
to trigger an invalid block warning. A variation of above, but worth testingTypes of changes
This is a bug fix that changes some core block HTML code and so could potentially be a breaking fix.
Checklist: