Skip to content

Fix replace_text() destroying nested markup in wrapper blocks - #12

Draft
kadamwhite with Copilot wants to merge 3 commits into
mainfrom
copilot/replace-text-destroys-nested-markup
Draft

Fix replace_text() destroying nested markup in wrapper blocks#12
kadamwhite with Copilot wants to merge 3 commits into
mainfrom
copilot/replace-text-destroys-nested-markup

Conversation

Copilot AI commented Jul 29, 2026

Copy link
Copy Markdown

update_block_text_content() rebuilt innerHTML wholesale from the outermost tag, wiping any nested elements. Calling replace_text() on a core/button block 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 — Rewrites update_block_text_content() to walk tokens via WP_HTML_Tag_Processor::next_token(), find the first non-whitespace #text node, and replace it with set_modifiable_text(). All surrounding markup is preserved untouched.

  • Escaping behaviour (intentional breaking change)replace_text() now HTML-encodes its argument (&&amp;). Previously it injected text verbatim. Callers needing raw HTML should use the existing replace_html() method.

  • Tests — New unit tests for nested-markup preservation and HTML encoding; existing ampersand serialisation test updated to expect the encoded form.

// Before: <a> tag was dropped
$template->replace_text( 'theme/cta', 'core/button', 0, 'Buy Now' );
// → <div class="wp-block-button">Buy Now</div>  ❌

// After: outer div and inner anchor are preserved
// → <div class="wp-block-button"><a class="wp-block-button__link" href="#">Buy Now</a></div>  ✅

Copilot AI and others added 2 commits July 29, 2026 15:46
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
Copilot AI requested a review from kadamwhite July 29, 2026 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

replace_text() destroys nested markup in wrapper blocks

2 participants