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
- Create a post with a Code block (or any
<pre class="...">/<code ...>) containing an emoji, e.g. ๐. - View the site feed at
/feed/(or trigger an email containing the content). - 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)
Change History (20)
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
@
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
@
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 . ')[^>]*>/inwp_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.phppass. - Full
--group formattingtest 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 byconvert_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.phppass, including the newtest_wp_staticize_emoji_ignores_tags_with_attributesdata provider. - Full
--group formattingsuite (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">🙂</pre>🙂โ only the second emoji is converted - [ ]
<pre class="wp-block-code"><code>🙂</code></pre>โ nothing converted - [ ] Raw
๐inside<pre class="...">โ left as is (goes throughwp_encode_emoji()first) - [ ]
<code-snippet>🙂</code-snippet>🙂โ both converted (same for<preview>and<script-loader>) - [ ]
<PRE class="foo">🙂</PRE>🙂โ only the second is converted - [ ]
<pre>🙂</pre >🙂โ only the second is converted - [ ] Same cases with
:)throughconvert_smilies()โ same results - [ ]
vendor/bin/phpunit --group formattingโ 2,146 tests pass. The 5 warnings come fromwpStripAllTagsandwpTrimExcerpttests, 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\bbecause\btreats a hyphen as a word boundary, so<code-snippet>would still matchcode.- 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/(<.*>)/Uwhileconvert_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
@
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
@
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
- โ 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) andconvertSmilies.php(35 tests) pass with the patch. With trunk'sformatting.php, 10 of the newemoji.phpcases 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
@
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
@
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
- Run
wp_staticize_emoji()on the inputs below and count the converted emoji. - 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/. - Run
emoji.phpandconvertSmilies.phpfrom the PR on trunk and with the patch.
Actual Results
- โ 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.phphas 17 failures (42 tests) andconvertSmilies.php5 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 tracksCODEandPRE, because the Tag Processor treats their content as part of the tag, not as text nodes.- An emoji left alone is output as
🚀, 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
@
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
- Run
wp_staticize_emoji()on<pre class="wp-block-preformatted">🙂</pre>. - 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">🙂</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.
Patch with unit tests attached. This completes the #47489 fix, applying the same
[^>]*tolerance towp_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.