Fix replace_text() destroying nested markup in wrapper blocks - #12
Draft
kadamwhite with Copilot wants to merge 3 commits into
Draft
Fix replace_text() destroying nested markup in wrapper blocks#12kadamwhite with Copilot wants to merge 3 commits into
kadamwhite with Copilot wants to merge 3 commits into
Conversation
Co-authored-by: kadamwhite <442115+kadamwhite@users.noreply.github.com>
…g instead) Co-authored-by: kadamwhite <442115+kadamwhite@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix replace_text() to preserve nested markup in wrapper blocks
Fix replace_text() destroying nested markup in wrapper blocks
Jul 29, 2026
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.
update_block_text_content()rebuilt innerHTML wholesale from the outermost tag, wiping any nested elements. Callingreplace_text()on acore/buttonblock would collapse<div class="wp-block-button"><a href="#">Link Text</a></div>to<div class="wp-block-button">New Text</div>— no<a>, silent data loss.Changes
inc/pattern-transformer.php— Rewritesupdate_block_text_content()to walk tokens viaWP_HTML_Tag_Processor::next_token(), find the first non-whitespace#textnode, and replace it withset_modifiable_text(). All surrounding markup is preserved untouched.Escaping behaviour (intentional breaking change) —
replace_text()now HTML-encodes its argument (&→&). Previously it injected text verbatim. Callers needing raw HTML should use the existingreplace_html()method.Tests — New unit tests for nested-markup preservation and HTML encoding; existing ampersand serialisation test updated to expect the encoded form.