WordPress / create-block-theme

A WordPress plugin to create block themes
https://wordpress.org/plugins/create-block-theme/
GNU General Public License v2.0
310 stars 44 forks source link

Fix template texts localizing/escaping #641

Closed matiasbenedetto closed 1 month ago

matiasbenedetto commented 1 month ago

What?

Fix template texts localizing/escaping.

Fixes: https://github.com/WordPress/create-block-theme/issues/632

Why?

The current implementation consisting of:

  1. Collect all the strings from the templates markup using regex.
  2. Search and replace the strings collected.

This approach is flawed because it fails, for example, when there are substrings. The problem is explained in this comment: https://github.com/WordPress/create-block-theme/issues/632#issuecomment-2112602045

☣️ The severity of the problem is high because the resulting templates/patterns are broken because the plugin is producing invalid markup.

How?

This PR improves the approach around escaping block strings for translation. It avoids the substring problem by just considering the HTML content block by block. The new implementation consist of:

  1. Parse the template markup with the core's function parse_blocks().
  2. In the blocks that can contain text, use regex to update the HTML content of the block 'innetContent' property with the escaped text.
  3. Serialize the template blocks to markup again using core's serialize_blocks() function.
pbking commented 1 month ago

I had one teeny tiny inconsequential nit.

This is great to bring in and fixes the problem at hand and enhances the tests quite a bit.