Make WordPress Core

Opened 2 months ago

Last modified 3 hours ago

#66134 new defect (bug)

wp_staticize_emoji() converts emoji inside code/pre blocks that have attributes

Reported by: prakritee08 Owned by:
Priority: normal Milestone: 7.3
Component: Emoji Version:
Severity: normal Keywords: tested has-patch has-unit-tests
Cc: Focuses: tests

Description

[45569] / #47489 fixed convert_smilies() so that emoji and smilies inside ignored tags (code, pre, style, script, textarea) are left alone even when the tag has attributes โ€” for example the block editor's Code/Preformatted blocks, which add class="wp-block-code" to the <pre> tag.

That fix was only applied to convert_smilies(). The identical logic in wp_staticize_emoji() was never updated, so it still only recognizes an ignored tag when the tag closes immediately after its name:

// wp_staticize_emoji() โ€” current
if ( '' === $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) ) {

Compare the corrected sibling function:

// convert_smilies() โ€” fixed in #47489
if ( '' === $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')[^>]*>/', $content, $matches ) ) {

Because wp_staticize_emoji() runs on the_content_feed, comment_text_rss, and outgoing mail (wp_staticize_emoji_for_email), an emoji placed inside a <pre class="wp-block-code"> or <code ...> block is wrongly converted to a static <img> in RSS/Atom feeds and email โ€” even though the same content is correctly left untouched on the rendered page thanks to #47489.

Steps to reproduce

  1. Create a post with a Code block (or any <pre class="..."> / <code ...>) containing an emoji, e.g. ๐Ÿ™‚.
  2. View the site feed at /feed/ (or trigger an email containing the content).
  3. Expected: the emoji inside the code/pre block is left as-is. Actual: the emoji is replaced with <img class="wp-smiley" ... />.

Proposed fix

Apply the same [^>]* tolerance to wp_staticize_emoji() that convert_smilies() already uses. Patch with unit tests attached.

Attachments (1)

staticize-emoji.diff​ (2.6 KB ) - added by prakritee08 2 months ago.
Patch with unit tests attached. This completes the #47489 fix, applying the same [^>]* tolerance to wp_staticize_emoji(), which was missed at the time. Verified locally against trunk: the new test fails without the fix and passes with it; the full Emoji suite (24 tests, 79 assertions) passes.

Download all attachments as: .zip

Change History (20)

@prakritee08
2 months ago

Patch with unit tests attached. This completes the #47489 fix, applying the same [^>]* tolerance to wp_staticize_emoji(), which was missed at the time. Verified locally against trunk: the new test fails without the fix and passes with it; the full Emoji suite (24 tests, 79 assertions) passes.

This ticket was mentioned in โ€‹PR #13598 on โ€‹WordPress/wordpress-develop by โ€‹praxxiii.


2 months ago
#1

  • Keywords has-unit-tests added; needs-unit-tests removed

Description

Emoji inside ignored tags (code, pre, style, script, textarea) should not be staticized, but wp_staticize_emoji() only recognised such a tag when it closed immediately after the tag name. Tags with attributes โ€” for example the block editor's Code/Preformatted block, which adds class="wp-block-code" to the <pre> tag โ€” were not detected, so emoji inside them were converted to <img>.

Because wp_staticize_emoji() runs on the_content_feed, comment_text_rss, and outgoing mail (wp_staticize_emoji_for_email), this affects RSS/Atom feeds and email โ€” even though the same content is left untouched on the rendered page.

This is the same bug that was fixed for the sibling function convert_smilies() in [45569] / #47489, but that fix was never applied to wp_staticize_emoji().

Fix

Apply the same [^>]* tolerance to the ignore-block regex in wp_staticize_emoji() that convert_smilies() already uses.

Testing

Added unit tests covering all five ignored tags, with and without attributes. The new test fails without the fix and passes with it.

Verified locally against trunk:

  • New test fails on unpatched code (5 failures), passes with the fix.
  • Full Emoji test file passes: 24 tests, 79 assertions.

#2 @westonruter
2 months ago

  • Milestone Awaiting Review โ†’ 7.2

@prakritee08 Thanks for the ticket and PR!

Note that you don't need to attach a patch if you already opened a pull request. A PR alone is perfect.

#3 @therssoftware
8 weeks ago

  • Keywords tested added; has-patch has-unit-tests removed

Tested โ€‹PR #13598 against trunk on WordPress 7.2-alpha.

Environment

  • OS: macOS
  • PHP: 8.3
  • Local build: wordpress-develop (pr-13598)

Code Review & Verification

  • The regex update from /^<(' . $tags_to_ignore . ')>/ to /^<(' . $tags_to_ignore . ')[^>]*>/ in wp_staticize_emoji() correctly accounts for HTML attributes on ignored elements (pre, code, style, script, textarea).
  • This brings parity with the identical fix made to convert_smilies() in [45569] (#47489).
  • Verified that emojis within <pre class="wp-block-code"> and <code class="..."> remain raw characters instead of being converted into <img> tags.
  • Unit tests in Tests_Formatting_Emoji::test_wp_staticize_emoji_ignores_tags_with_attributes() thoroughly cover all 5 ignored tags both with and without attributes.
  • All 24 tests in tests/formatting/emoji.php pass.
  • Full --group formatting test suite (2,096 tests) passes with zero regressions.

+1 for merging.

โ€‹@therssoftware commented on โ€‹PR #13598:


8 weeks ago
#4

Tested against trunk on WordPress 7.2-alpha (PHP 8.3).

  • Code review: The regex fix to allow [^>]* inside <(' . $tags_to_ignore . ')[^>]*> matches the pattern used by convert_smilies() from [45569] / #47489.
  • Verified that <pre class="wp-block-code"> and <code class="language-php"> containing emojis are no longer converted into <img> elements.
  • All 24 tests in tests/formatting/emoji.php pass, including the new test_wp_staticize_emoji_ignores_tags_with_attributes data provider.
  • Full --group formatting suite (2,096 tests) passes without issues.

LGTM! ๐Ÿ‘

This ticket was mentioned in โ€‹PR #13918 on โ€‹WordPress/wordpress-develop by โ€‹sanjithsarkar.


7 weeks ago
#5

  • Keywords has-patch has-unit-tests added

Trac ticket: https://core.trac.wordpress.org/ticket/66134

Summary

Emoji inside a Code block (<pre class="wp-block-code">) were converted to <img> in feeds and email. wp_staticize_emoji() only recognized an ignored tag when > came right after the tag name. #13598 fixes that with the [^>]* pattern from convert_smilies(), but that pattern also matches longer tag names like <code-snippet> or <preview>. The matching closing tag never comes, so emoji and smilies in the rest of the content stop being converted. This PR fixes both functions so they skip real ignored tags with attributes and still convert everything else.

What Changed

File Change
src/wp-includes/formatting.php:6232 /^<(tags)>/ โ†’ /^<(tags)(?:[\s\/][^>]*)?>/i in wp_staticize_emoji(); the matched name goes through strtolower(). Attributes are allowed, but the tag name must end where the tag name ends.
src/wp-includes/formatting.php:6253 '</' . $ignore_block_element . '>' === $content โ†’ preg_match( '/^<\/' . $ignore_block_element . '\s*>$/i', ... ), so </PRE> and </pre > end the ignored section
src/wp-includes/formatting.php:3624 same opening-tag change in convert_smilies(), replacing the [^>]* pattern from [45569]
src/wp-includes/formatting.php:3634 same closing-tag change in convert_smilies()
tests/phpunit/tests/formatting/emoji.php:390 test_wp_staticize_emoji_ignores_tags_with_attributes: the 5 ignored tags, with and without attributes
tests/phpunit/tests/formatting/emoji.php:427 test_wp_staticize_emoji_ignore_block_boundaries: 9 cases that count class="wp-smiley" in the output
tests/phpunit/tests/formatting/convertSmilies.php:175 test_ignore_smilies_block_boundaries: 6 similar cases for convert_smilies()

Testing

  • [ ] <pre class="wp-block-code">&#x1f642;</pre>&#x1f642; โ†’ only the second emoji is converted
  • [ ] <pre class="wp-block-code"><code>&#x1f642;</code></pre> โ†’ nothing converted
  • [ ] Raw ๐Ÿ™‚ inside <pre class="..."> โ†’ left as is (goes through wp_encode_emoji() first)
  • [ ] <code-snippet>&#x1f642;</code-snippet>&#x1f642; โ†’ both converted (same for <preview> and <script-loader>)
  • [ ] <PRE class="foo">&#x1f642;</PRE>&#x1f642; โ†’ only the second is converted
  • [ ] <pre>&#x1f642;</pre >&#x1f642; โ†’ only the second is converted
  • [ ] Same cases with :) through convert_smilies() โ†’ same results
  • [ ] vendor/bin/phpunit --group formatting โ†’ 2,146 tests pass. The 5 warnings come from wpStripAllTags and wpTrimExcerpt tests, which this PR doesn't touch, and also appear on trunk.
  • [ ] Manual: add a Code block containing ๐Ÿ™‚ to a post, open /feed/ โ†’ the emoji in the block stays as is, and emoji outside it become <img class="wp-smiley">

Results of the new tests on each version:

Code Emoji tests Smilies tests
trunk 10 failures 5 failures
#13598 5 failures 5 failures
This PR pass pass

Implementation Notes

  • (?:[\s\/][^>]*)? is used instead of \b because \b treats a hyphen as a word boundary, so <code-snippet> would still match code.
  • The matched tag name is lowercased so the closing check can compare against one form while the opening tag can be any case.
  • convert_smilies() is changed in the same PR because it uses the same loop. Fixing only one of the two would leave them behaving differently, and #47489 was itself a fix to keep them consistent.
  • Not changed: wp_staticize_emoji() splits on /(<.*>)/U while convert_smilies() uses /(<[^>]*>)/U. Unifying them, or switching to the HTML API, is a larger change and better as a follow-up.

Anything the Reviewer Should Know

  • Builds on #13598 and includes its test. Credit goes to @praxxiii / prakritee08.
  • Behavior change for convert_smilies(): since [45569], <code-snippet>, <preview> and similar tags have stopped smiley conversion for the rest of the content. Content with these tags will now get smilies converted after them. I'm assuming no site relies on the old behavior; please confirm.
  • <textarea/> still starts an ignored section. That matches HTML, where / on a non-void element doesn't close it.
  • The manual /feed/ check has not been done yet. So far only the unit tests have run.

โ€‹@sanjith2023 commented on โ€‹PR #13918:


11 days ago
#6

@propsbot refresh

โ€‹@sanjith2023 commented on โ€‹PR #13918:


11 days ago
#7

@mukeshpanchal27 This PR builds on #13598 and includes its test. Could you add props for @praxxiii (prakritee08 on WordPress.org)? @propsbot add needs write access.

The description is updated with full test results, including a feed check against trunk. Ready for another look when you have time.

#8 @sanjith2023
11 days ago

I opened PR 13918 as an alternative to PR 13598, because the [^>]* pattern there also matches longer tag names. With it, <code-snippet>, <preview> or <script-loader> starts an ignored section whose closing tag never comes, so emoji after them are no longer converted. convert_smilies() has had the same problem since [45569].

PR 13918 uses (?:[\s\/][^>]*)? instead, so attributes are allowed but the tag name has to end, and it applies the same fix to convert_smilies(). It also makes the closing-tag check case-insensitive and allows whitespace, so </PRE> and </pre > end the ignored section. It includes the test from PR 13598, and @prakritee08 should keep props for the original report and patch.

Results with the new tests:

Code Emoji tests Smilies tests
trunk 10 failures 5 failures
PR 13598 5 failures 5 failures
PR 13918 pass pass

The full --group formatting passes on the branch (2,146 tests) and merged into current trunk (2,286 tests).

One note on the steps to reproduce: a default Code block is not affected on trunk, because its inner bare <code> already matches the old pattern. The bug shows with a Preformatted or Verse block, or a Code block whose <code> has a class. I checked this in /?feed=rss2 on trunk and on the PR.

@thenssoftware since you tested PR 13598, could you also take a look at PR 13918?

This ticket was mentioned in โ€‹Slack in #core-test by r1k0. โ€‹View the logs.


11 days ago

โ€‹@peterwilsoncc commented on โ€‹PR #13918:


7 days ago
#10

@propsbot add @praxxiii
@propsbot add @robelsust

#11 @aryanjasala
6 days ago

Test Report

Description

This report validates whether the indicated patch works as expected.

Patch tested: โ€‹https://github.com/WordPress/wordpress-develop/pull/13918

Environment

  • WordPress: 7.2-beta5-65392-src (trunk aca2a318)
  • PHP: 8.3.36
  • Server: WP-CLI (wp eval-file) in Docker
  • Database: MariaDB 11.4.13
  • Browser: n/a
  • OS: macOS
  • Theme: Ipsum
  • MU Plugins: None activated
  • Plugins: None activated

Steps

Run wp_staticize_emoji() on markup with emoji inside ignored tags that have attributes, and outside them.

Actual Results

  1. โœ… Issue resolved with patch.
Input trunk PR 13918
<pre class="wp-block-preformatted">๐Ÿš€</pre> converted to <img> left as emoji
<p><code class="x">๐Ÿš€</code></p> converted to <img> left as emoji
<p>Ship it ๐Ÿš€</p> converted to <img> converted to <img>

Additional Notes

  • The Code block itself is not affected on trunk, because its inner <code> has no attributes. The Preformatted block (<pre class="wp-block-preformatted">) is the block editor case that breaks.
  • The PR's tests: emoji.php (33 tests) and convertSmilies.php (35 tests) pass with the patch. With trunk's formatting.php, 10 of the new emoji.php cases fail.
  • PR 13598 gives the same results for these inputs, and also leaves <script type="text/template"> content alone.

Use of AI tools: I used Claude Code to set up the local test site, write the test script and run the extra checks in the notes. I ran the before/after comparison myself.

โ€‹@sanjith2023 commented on โ€‹PR #13918:


6 days ago
#12

@westonruter Thanks for the review. wp_staticize_emoji() now uses WP_HTML_Tag_Processor instead of the regex loop (8ae84b5), with tests for > in attribute values and markup inside comments. The description is updated with results and timings.

One question: convert_smilies() fails the same cases, but it runs on the_content on every page view and the Tag Processor measured about 5x slower there. Should I convert it in this PR, or leave it for a follow-up ticket?

#13 @sanjith2023
6 days ago

PR 13918 is updated following @westonruter's review: wp_staticize_emoji() now uses WP_HTML_Tag_Processor instead of the regex loop, so > in attribute values and markup in HTML comments are handled. The PR description has the new test results and timings.

convert_smilies() fails the same cases but is left with the smaller regex fix for now, since it runs on every page view. I've asked on the PR whether to convert it there or in a follow-up ticket.

@aryanjasala thanks for testing. The patch changed since your report, so a re-test would be welcome.

#14 @aryanjasala
6 days ago

Test Report

Re-test after the switch to WP_HTML_Tag_Processor (8ae84b5).

Description

This report validates whether the indicated patch works as expected.

Patch tested: โ€‹https://github.com/WordPress/wordpress-develop/pull/13918 (8ae84b5)

Environment

  • WordPress: 7.2-beta5-65392-src (trunk 62fb7f4f)
  • PHP: 8.3.36
  • Server: PHP built-in server and WP-CLI (wp eval-file), both in Docker
  • Database: MariaDB 11.4.13
  • Browser: n/a
  • OS: macOS
  • Theme: Ipsum
  • MU Plugins: None activated
  • Plugins: None activated

Steps

  1. Run wp_staticize_emoji() on the inputs below and count the converted emoji.
  2. Publish a post with Preformatted, Verse, a Code block with <code class="language-php"> and a Paragraph, each with ๐Ÿš€, and read its <content:encoded> in /feed/.
  3. Run emoji.php and convertSmilies.php from the PR on trunk and with the patch.

Actual Results

  1. โœ… Issue resolved with patch.
Input trunk PR 13918
<pre class="wp-block-preformatted">๐Ÿš€</pre> 1 of 1 converted 0 of 1
<pre class="wp-block-verse">๐Ÿš€</pre> 1 of 1 0 of 1
<p><code class="language-php">๐Ÿš€</code></p> 1 of 1 0 of 1
<pre class="wp-block-code"><code>๐Ÿš€</code></pre> 0 of 1 0 of 1
<p>Ship it ๐Ÿš€</p> 1 of 1 1 of 1
<pre title="a > b">๐Ÿš€</pre>๐Ÿš€ 2 of 2 1 of 2, only the text after
<!-- > <pre> -->๐Ÿš€ 0 of 1 1 of 1
<code><code>๐Ÿš€</code>๐Ÿš€</code>๐Ÿš€ 2 of 3 1 of 3, only the last
<img alt="1 > 0 ๐Ÿš€" src="a.png">๐Ÿš€ 2 of 2, alt changed too 1 of 2, alt left alone
<script type="text/template">๐Ÿš€</script>๐Ÿš€ 2 of 2 1 of 2, only the text after
<textarea rows="2">๐Ÿš€</textarea>๐Ÿš€ 2 of 2 1 of 2, only the text after
<style>/* ๐Ÿš€ */</style>๐Ÿš€ 1 of 2 1 of 2
<pre class="x">๐Ÿš€ (no closing tag) 1 of 1 0 of 1
1 < 2 ๐Ÿš€ 1 of 1 1 of 1

Feed (<content:encoded> in /feed/):

Block trunk PR 13918
Preformatted converted to <img> left as emoji
Verse converted to <img> left as emoji
Code with language-php class converted to <img> left as emoji
Paragraph converted to <img> converted to <img>

Additional Notes

  • PR tests: on trunk, emoji.php has 17 failures (42 tests) and convertSmilies.php 5 failures (35 tests). With the patch both pass.
  • The feed check covers this revision, which the PR description says was not repeated after the switch.
  • <script>, <style> and <textarea> are left alone with the patch even though the loop only tracks CODE and PRE, because the Tag Processor treats their content as part of the tag, not as text nodes.
  • An emoji left alone is output as &#x1f680;, since the content is encoded before the loop. Trunk does the same for content it skips.
  • Timing, 21.5 KB of mixed block markup, best of 5: trunk 22.8 ms, PR 29.0 ms, about 1.3x. wp_staticize_emoji() runs on feeds and email, not on every page view, so this looks fine to me.

Use of AI tools: I used Claude Code to set up the local test site and write the test script. I ran the tests and compared trunk with the patch myself.

โ€‹@westonruter commented on โ€‹PR #13918:


6 days ago
#15

I think it's more important to be correct and use HTML Tag Processor than to risk corrupting the HTML.

cc @dmsnell

โ€‹@sanjith2023 commented on โ€‹PR #13918:


6 days ago
#16

@westonruter Agreed. convert_smilies() now uses the Tag Processor as well (cae86eb). Both functions share a private helper, _wp_replace_in_html_text_nodes(), and the regex loops are gone.

One follow-up in a6f7089: the helper only accepts a string, so convert_smilies( null ) threw a TypeError, for example through apply_filters( 'the_excerpt', null ). Non-string values are now returned unchanged.

The description is updated with the test results, timings and behavior changes. convert_smilies() is about 3 to 5 times slower on HTML content.

#17 @rcwpdev
5 days ago

Test Report

Patch tested: โ€‹https://github.com/WordPress/wordpress-develop/pull/13918

Environment

  • WordPress: 7.2-beta5
  • PHP: 8.3.36
  • Server: nginx
  • Database: MySQL
  • Browser: Chromium
  • OS: macOS 26.5.2
  • Theme: ipsum
  • Plugins: none

Steps

  1. Run wp_staticize_emoji() on <pre class="wp-block-preformatted">&#x1f642;</pre>.
  2. Check if the emoji inside the <pre> became an <img>.

Actual Results

  • โœ… Issue resolved with the patch.
  • Before: On trunk, the emoji inside <pre class="wp-block-preformatted"> was converted to <img class="wp-smiley"> instead of being left as is.
  • After: Output was <pre class="wp-block-preformatted">&#x1f642;</pre>. The emoji was not converted and there is no <img>.
  • PHPUnit: pass

โ€‹@sanjith2023 commented on โ€‹PR #13918:


5 days ago
#18

Changed. A closing tag is now compared only with the last item on the stack and pops it when they match. Any other closing tag is skipped, so there is no search for an earlier opener.

With bad nesting such as <pre><code>x</pre>y</code>z, the <pre> stays open and z is left unconverted. Tests updated for that, plus a case for a closing tag with nothing open.

#19 @audrasjb
3 hours ago

  • Milestone 7.2 โ†’ 7.3

This ticket is pretty close from its resolution, but we're too close from 7.2 RC1 and it still needs to be reviewed and committed. Moving to 7.3.

Note: See TracTickets for help on using tickets.