Fix: repair genuinely-invalid blocks in canonicalize() - #1
Open
saarnilauri wants to merge 1 commit into
Open
Conversation
canonicalize() (the `fix` command) round-trips markup through serialize(parse()). That canonicalizes near-misses, but the WP serializer re-emits `originalContent` verbatim for a block whose stored HTML fails validation (isValid === false), so a genuinely broken block round-trips still broken and `fix` reports it invalid rather than repairing it. Rebuild invalid *registered* blocks from their parsed attributes via createBlock() before serializing, so save() re-runs and produces valid markup. Valid blocks keep their parsed form (with repaired descendants); unregistered blocks (core/missing, custom) pass through untouched. This mirrors what the token-repair path already does after mutating attributes. Note: cases covered by a block deprecation (e.g. a heading missing wp-block-heading) are already repaired by parse-time migration; this handles the rest (an image without its <figure> wrapper, a button without wp-block-button__link, …). Adds gate tests covering repair of an invalid image block and pass-through of an unregistered block alongside a repaired core block.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
canonicalize()(thefixcommand) produces its output withserialize(parse(markup)). This canonicalizes near-misses attribute order,whitespace, generated classes, but it does not repair a block whose stored
HTML actually fails validation. WordPress's serializer re-emits a block's
originalContentverbatim whenisValid === false, so a genuinely-invalidblock round-trips unchanged and
fixjust reports it invalid.Fix
Before serializing, rebuild invalid registered blocks from their parsed
attributes via
createBlock(), so each block'ssave()re-runs and emits validmarkup. Valid blocks keep their parsed form (with any repaired descendants), and
unregistered blocks (
core/missing, custom blocks) pass through untouched. Thisis the same technique the token-repair path already uses in
tokens/apply.tsafter it mutates attributes, wrapped in
withMutedWordPressConsolefor the samereason.
Scope note
Blocks whose invalidity is covered by a deprecation (e.g. a heading missing
wp-block-heading) are already repaired byparse()'s migration and areunaffected. This targets the rest, near-miss structures with no matching
deprecation, such as an image without its
<figure>wrapper or a buttonwithout
wp-block-button__link. Rebuilding regenerates a block's inner markupfrom
save(), so it's applied only to blocks that were already invalid.Tests
repairs a genuinely-invalid block by rebuilding from parsed attributes—an image without
<figure>becomes valid (wp-block-image, src preserved).leaves unregistered blocks untouched while repairing invalid core blocks—a custom block round-trips verbatim while an invalid core image is repaired.
Both fail on
mainand pass with this change. Full suite green (198 tests),typecheckclean.Use of AI
The code changes were created with AI agent, the changes reviewed by me.