Repository navigation
Emoji: Skip ignored tags with attributes in wp_staticize_emoji().
#13918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sanjithsarkar
wants to merge
8
commits into
WordPress:trunk
Choose a base branch
from
sanjithsarkar:fix/66134-staticize-emoji-ignored-tags
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+296
−53
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1dd4604
Emoji: Skip ignored tags with attributes in `wp_staticize_emoji()`.
sanjithsarkar a38c9cb
Tests: Make data provider static and add docblock spacing in `convert…
sanjithsarkar 832f3fe
Merge remote-tracking branch 'upstream/trunk' into fix/66134-staticiz…
sanjithsarkar 8ae84b5
Emoji: Use `WP_HTML_Tag_Processor` to find text in `wp_staticize_emoj…
sanjithsarkar cae86eb
Formatting: Use `WP_HTML_Tag_Processor` to find text in `convert_smil…
sanjithsarkar a6f7089
Formatting: Return non-string values unchanged in `convert_smilies()`.
sanjithsarkar b8749f2
Formatting: Inline the Tag Processor loops and track ignored tags wit…
sanjithsarkar 38ef2a9
Formatting: Close only the nearest matching ignored tag.
sanjithsarkar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3612,49 +3612,91 @@ function translate_smiley( $matches ) { | |
| function convert_smilies( $text ) { | ||
| global $wp_smiliessearch; | ||
|
|
||
| if ( ! get_option( 'use_smilies' ) || empty( $wp_smiliessearch ) ) { | ||
| if ( ! is_string( $text ) || ! get_option( 'use_smilies' ) || empty( $wp_smiliessearch ) ) { | ||
| // Return default text. | ||
| return $text; | ||
| } | ||
|
|
||
| // HTML loop taken from texturize function, could possible be consolidated. | ||
| $textarr = preg_split( '/(<[^>]*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // Capture the tags as well as in between. | ||
|
|
||
| if ( false === $textarr ) { | ||
| // Return default text. | ||
| return $text; | ||
| // Without any markup the whole text is a single text node. | ||
| if ( ! str_contains( $text, '<' ) ) { | ||
| return preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $text ) ?? $text; | ||
| } | ||
|
|
||
| // Loop stuff. | ||
| $stop = count( $textarr ); | ||
| // Anonymous subclass of WP_HTML_Tag_Processor which exposes underlying bookmark spans. | ||
| $processor = new class( $text ) extends WP_HTML_Tag_Processor { | ||
| /** | ||
| * Gets the span for the current token. | ||
| * | ||
| * @return WP_HTML_Span Current token span. | ||
| */ | ||
| public function get_span(): WP_HTML_Span { | ||
| // Always called while matched on a token, so setting the bookmark never fails. | ||
| $this->set_bookmark( 'here' ); | ||
| return $this->bookmarks['here']; | ||
| } | ||
| }; | ||
|
|
||
| /* | ||
| * The smilies are replaced by raw HTML, which set_modifiable_text() would | ||
| * escape, so the output is assembled here from the unchanged parts of the | ||
| * text and the converted text nodes. | ||
| */ | ||
| $output = ''; | ||
| $was_at = 0; | ||
|
|
||
| // Ignore processing of specific tags. | ||
| $tags_to_ignore = 'code|pre|style|script|textarea'; | ||
| $ignore_block_element = ''; | ||
| /* | ||
| * Smilies are not converted inside CODE and PRE elements. The Tag Processor | ||
| * does not track document structure, so the open CODE and PRE elements are | ||
| * tracked here. Text is ignored for as long as any of them is open. | ||
| */ | ||
| $ignored_tags = array(); | ||
|
|
||
| for ( $i = 0; $i < $stop; $i++ ) { | ||
| $content = $textarr[ $i ]; | ||
| while ( $processor->next_token() ) { | ||
| $token_type = $processor->get_token_type(); | ||
|
|
||
| // If we're in an ignore block, wait until we find its closing tag. | ||
| if ( '' === $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')[^>]*>/', $content, $matches ) ) { | ||
| $ignore_block_element = $matches[1]; | ||
| if ( '#tag' === $token_type ) { | ||
| $tag_name = $processor->get_tag(); | ||
| $is_closer = $processor->is_tag_closer(); | ||
|
|
||
| // A closing tag is unexpected when no CODE or PRE element is open. | ||
| if ( ( 'CODE' !== $tag_name && 'PRE' !== $tag_name ) || ( $is_closer && 0 === count( $ignored_tags ) ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| if ( ! $is_closer ) { | ||
| $ignored_tags[] = $tag_name; | ||
| continue; | ||
| } | ||
|
|
||
| /* | ||
| * A closing tag closes the nearest open element of its own name and | ||
| * leaves the others open. A CODE element which is still open when its | ||
| * containing PRE closes continues into the text which follows. | ||
| */ | ||
| $opener_at = array_search( $tag_name, array_reverse( $ignored_tags, true ), true ); | ||
| if ( false !== $opener_at ) { | ||
| array_splice( $ignored_tags, $opener_at, 1 ); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| // If it's not a tag and not in ignore block. | ||
| if ( '' === $ignore_block_element && strlen( $content ) > 0 && '<' !== $content[0] ) { | ||
| $content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $content ); | ||
| if ( '#text' !== $token_type || count( $ignored_tags ) > 0 ) { | ||
| continue; | ||
| } | ||
|
|
||
| // Did we exit ignore block? | ||
| if ( '' !== $ignore_block_element && '</' . $ignore_block_element . '>' === $content ) { | ||
| $ignore_block_element = ''; | ||
| $span = $processor->get_span(); | ||
| $content = substr( $text, $span->start, $span->length ); | ||
| $converted = preg_replace_callback( $wp_smiliessearch, 'translate_smiley', $content ); | ||
|
|
||
| if ( null === $converted || $converted === $content ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good job skipping the update if the |
||
| continue; | ||
| } | ||
|
|
||
| $output .= $content; | ||
| $output .= substr( $text, $was_at, $span->start - $was_at ) . $converted; | ||
| $was_at = $span->start + $span->length; | ||
| } | ||
|
|
||
| return $output; | ||
| return $output . substr( $text, $was_at ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -6228,52 +6270,100 @@ function wp_staticize_emoji( $text ) { | |
| /** This filter is documented in wp-includes/formatting.php */ | ||
| $ext = apply_filters( 'emoji_ext', '.png' ); | ||
|
|
||
| // Anonymous subclass of WP_HTML_Tag_Processor which exposes underlying bookmark spans. | ||
| $processor = new class( $text ) extends WP_HTML_Tag_Processor { | ||
| /** | ||
| * Gets the span for the current token. | ||
| * | ||
| * @return WP_HTML_Span Current token span. | ||
| */ | ||
| public function get_span(): WP_HTML_Span { | ||
| // Always called while matched on a token, so setting the bookmark never fails. | ||
| $this->set_bookmark( 'here' ); | ||
| return $this->bookmarks['here']; | ||
| } | ||
| }; | ||
|
|
||
| /* | ||
| * The emoji are replaced by raw HTML, which set_modifiable_text() would | ||
| * escape, so the output is assembled here from the unchanged parts of the | ||
| * text and the converted text nodes. | ||
| */ | ||
| $output = ''; | ||
| $was_at = 0; | ||
|
|
||
| /* | ||
| * HTML loop taken from smiley function, which was taken from texturize function. | ||
| * It'll never be consolidated. | ||
| * | ||
| * First, capture the tags as well as in between. | ||
| * Emoji are not converted inside CODE and PRE elements. The Tag Processor | ||
| * does not track document structure, so the open CODE and PRE elements are | ||
| * tracked here. Text is ignored for as long as any of them is open. | ||
| */ | ||
| $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); | ||
| $stop = count( $textarr ); | ||
| $ignored_tags = array(); | ||
|
|
||
| // Ignore processing of specific tags. | ||
| $tags_to_ignore = 'code|pre|style|script|textarea'; | ||
| $ignore_block_element = ''; | ||
| while ( $processor->next_token() ) { | ||
| $token_type = $processor->get_token_type(); | ||
|
|
||
| for ( $i = 0; $i < $stop; $i++ ) { | ||
| $content = $textarr[ $i ]; | ||
| if ( '#tag' === $token_type ) { | ||
| $tag_name = $processor->get_tag(); | ||
| $is_closer = $processor->is_tag_closer(); | ||
|
|
||
| // If we're in an ignore block, wait until we find its closing tag. | ||
| if ( '' === $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) ) { | ||
| $ignore_block_element = $matches[1]; | ||
| // A closing tag is unexpected when no CODE or PRE element is open. | ||
| if ( ( 'CODE' !== $tag_name && 'PRE' !== $tag_name ) || ( $is_closer && 0 === count( $ignored_tags ) ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| if ( ! $is_closer ) { | ||
| $ignored_tags[] = $tag_name; | ||
| continue; | ||
| } | ||
|
|
||
| /* | ||
| * A closing tag closes the nearest open element of its own name and | ||
| * leaves the others open. A CODE element which is still open when its | ||
| * containing PRE closes continues into the text which follows. | ||
| */ | ||
| $opener_at = array_search( $tag_name, array_reverse( $ignored_tags, true ), true ); | ||
| if ( false !== $opener_at ) { | ||
| array_splice( $ignored_tags, $opener_at, 1 ); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| // If it's not a tag and not in ignore block. | ||
| if ( '' === $ignore_block_element && strlen( $content ) > 0 && '<' !== $content[0] && str_contains( $content, '&#x' ) ) { | ||
| foreach ( $possible_emoji as $emojum => $emoji_char ) { | ||
| if ( ! str_contains( $content, $emojum ) ) { | ||
| continue; | ||
| } | ||
| if ( '#text' !== $token_type || count( $ignored_tags ) > 0 ) { | ||
| continue; | ||
| } | ||
|
|
||
| $span = $processor->get_span(); | ||
| $content = substr( $text, $span->start, $span->length ); | ||
|
|
||
| $file = str_replace( ';&#x', '-', $emojum ); | ||
| $file = str_replace( array( '&#x', ';' ), '', $file ); | ||
| if ( ! str_contains( $content, '&#x' ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $entity = sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', $cdn_url . $file . $ext, $emoji_char ); | ||
| $converted = $content; | ||
|
|
||
| $content = str_replace( $emojum, $entity, $content ); | ||
| foreach ( $possible_emoji as $emojum => $emoji_char ) { | ||
| if ( ! str_contains( $converted, $emojum ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $file = str_replace( ';&#x', '-', $emojum ); | ||
| $file = str_replace( array( '&#x', ';' ), '', $file ); | ||
|
|
||
| $entity = sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="height: 1em; max-height: 1em;" />', $cdn_url . $file . $ext, $emoji_char ); | ||
|
|
||
| $converted = str_replace( $emojum, $entity, $converted ); | ||
| } | ||
|
|
||
| // Did we exit ignore block? | ||
| if ( '' !== $ignore_block_element && '</' . $ignore_block_element . '>' === $content ) { | ||
| $ignore_block_element = ''; | ||
| if ( $converted === $content ) { | ||
| continue; | ||
| } | ||
|
|
||
| $output .= $content; | ||
| $output .= substr( $text, $was_at, $span->start - $was_at ) . $converted; | ||
| $was_at = $span->start + $span->length; | ||
| } | ||
|
|
||
| $output .= substr( $text, $was_at ); | ||
|
|
||
| // Finally, remove any stray U+FE0F characters. | ||
| $output = str_replace( '️', '', $output ); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may have misspoken, or at least have been too terse. this is also not true.
keep in mind that we’re only examining selected elements, which actually is not a great way of parsing HTML. it’s more or less okay because a failure mode is failing to handle emoji.
if we are going to use the Tag Processor, and assume things about the markup, a closing tag should match the last item on the array of open ignored elements.