create-block-theme icon indicating copy to clipboard operation
create-block-theme copied to clipboard

Process inner html of blocks when escaping text content

Open jffng opened this issue 1 year ago • 2 comments

What This PR enables HTML markup inside block content to be processed while still allowing the text content to be safely escaped.

Before After

How Updates the CBT_Theme_Locale::escape_text_content function to handle nested HTML markup.

Uses WP_HTML_Tag_Processor to process every token of the blocks content, and generates a string that can be formatted and then translated.

Why Fixes #573 #682 and #691.

To test

  • Add some markup to the inside of a block in a template.
  • Save the changes to the theme.
  • Verify the template has been patternized correctly, and the template appears as expected.

jffng avatar Sep 09 '24 18:09 jffng

I think I addressed all the feedback:

  • Changed approach so we don't drop any attributes
  • Refactored to its own class.
  • Handled case when text contains % and added a test for it

Ready for another review.

jffng avatar Sep 23 '24 17:09 jffng

Thanks for all the feedback on this one!

I think I've addressed this feedback:

I believe the /* translators: comment needs to be on a separate line, immediately above the call to the translation function (but I'm not 100% sure)

Done in https://github.com/WordPress/create-block-theme/pull/719/commits/1e317ce80b0ef55f28dccad825264b5410434f9f.

When there are multiple placeholders in a string, the comment should be numbered, e.g. /* Translators: 1: start of 'strong' HTML element, 2: ... (see plugin docs)

Done in https://github.com/WordPress/create-block-theme/pull/719/commits/c6652860bd350295ad7c85a00033936b132308a1.

If there's a way to differentiate between tags that are self closing (like
), it would be nice to modify the comment (1: 'br' HTML element, 2: ...), so it's not confusing when there's no corresponding closing tag

Done in https://github.com/WordPress/create-block-theme/commit/6b5fe21f2372f6918cb0e1f6e825b7721d44cfab.

Would appreciate any reviews on this so we can hopefully land this soon 🙇

mikachan avatar Oct 21 '24 15:10 mikachan

Thank you all so much for getting this done!

annezazu avatar Oct 23 '24 17:10 annezazu

Nice! Thank you so much for the fix.

iamtakashi avatar Oct 24 '24 11:10 iamtakashi