fix(mutator): keep moved blocks inside the destination wrapper's markup - #40
Merged
Merged
Conversation
The move op's destination placeholder used a bare Nth-null scan, so two ordinary moves corrupted saved content by appending the null AFTER the closing-tag string — serializing the moved block OUTSIDE the wrapper: (1) moving into a childless container, whose innerContent parses back as a single unsplit wrapper string, and (2) moving to a container's end position, where no Nth null exists. insert-child already guards both shapes; the guards now live in one shared insert_child_placeholders() helper used by both ops. Regression tests fail pre-fix on both shapes and assert the serialized markup places the moved block between the wrapper's tags. Claude-Session: https://claude.ai/code/session_01B5BxSkbRy8w6hzuuNhJqn2
WalkthroughRefactors ChangesPlaceholder Insertion Refactor
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant Caller
participant Block_Mutator
participant insert_child_placeholders
Caller->>Block_Mutator: mutate(insert-child or move)
Block_Mutator->>Block_Mutator: normalize innerContent to array
Block_Mutator->>insert_child_placeholders: child_position or dest_index, count
insert_child_placeholders->>insert_child_placeholders: split wrapper if unsplit
insert_child_placeholders->>insert_child_placeholders: locate Nth null or fallback to last piece
insert_child_placeholders-->>Block_Mutator: updated innerContent with placeholders
Block_Mutator-->>Caller: mutated block
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
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.
The bug
edit_block_tree'smoveop corrupted saved content in two ordinary scenarios, serializing the moved block outside the destination wrapper's markup (…</div><!-- wp:paragraph -->…), which the editor then flags as invalid content:innerContentparses back as a single unsplit wrapper string (['<div class="wp-block-group"></div>']), so the Nth-null scan finds nothing and appends the placeholder after the closing tag.insert-childwas hardened against exactly these shapes (the BLOCK-20 work);move's destination handling never got the guards. Root cause: duplicated placeholder-splice logic, fixed in one copy.The fix
Extract the guarded logic into one shared
insert_child_placeholders()helper (wrapper-split normalization + Nth-null scan + before-closing-string fallback) and route bothinsert-childandmovethrough it. Net: −95/+203 including tests; the op-facing behavior ofinsert-childis unchanged (its full regression suite passes through the helper).Evidence
composer lint0/0 ·composer analyze[OK] · PHPUnit 1100 single-site + 24 yoast + 5 multisite green.https://claude.ai/code/session_01B5BxSkbRy8w6hzuuNhJqn2
Summary by CodeRabbit
💾 Build file (9488bb3).