Emoji: There's a little tear in my eye as I remove DOMDocument from the Emoji staticizer. It was a beautiful dream, but it wasn't to be.

Instead, let's use the tried and trusted smiley replacement algorithm, which has stood the test of time.

See #31242


Built from https://develop.svn.wordpress.org/trunk@31752


git-svn-id: http://core.svn.wordpress.org/trunk@31733 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast 2015-03-12 13:17:26 +00:00
parent e8a600ac94
commit 351ea2d6f8
2 changed files with 49 additions and 39 deletions

View File

@ -4106,14 +4106,14 @@ function wp_encode_emoji( $content ) {
* *
* @since 4.2.0 * @since 4.2.0
* *
* @param string $content The content to encode. * @param string $text The content to encode.
* @return string The encoded content. * @return string The encoded content.
*/ */
function wp_staticize_emoji( $content ) { function wp_staticize_emoji( $text ) {
$content = wp_encode_emoji( $content ); $text = wp_encode_emoji( $text );
if ( ! class_exists( 'DOMDocument' ) ) { if ( ! class_exists( 'DOMDocument' ) ) {
return $content; return $text;
} }
/** This filter is documented in wp-includes/script-loader.php */ /** This filter is documented in wp-includes/script-loader.php */
@ -4121,21 +4121,27 @@ function wp_staticize_emoji( $content ) {
/** This filter is documented in wp-includes/script-loader.php */ /** This filter is documented in wp-includes/script-loader.php */
$ext = apply_filters( 'emoji_ext', '.png' ); $ext = apply_filters( 'emoji_ext', '.png' );
$html = '<!DOCTYPE html><html><head></head><body>' . $content . '</body></html>'; $output = '';
// HTML loop taken from smiley function, which was taking from texturize function. It'll never be consolidated.
$textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // capture the tags as well as in between
$stop = count( $textarr );// loop stuff
$document = new DOMDocument; // Ignore proessing of specific tags
if ( ! $document->loadHTML( $html ) ) { $tags_to_ignore = 'code|pre|style|script|textarea';
return $content; $ignore_block_element = '';
for ( $i = 0; $i < $stop; $i++ ) {
$content = $textarr[$i];
// 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];
} }
$xpath = new DOMXPath( $document ); // If it's not a tag and not in ignore block
$textnodes = $xpath->query( '//text()' ); if ( '' == $ignore_block_element && strlen( $content ) > 0 && '<' != $content[0] ) {
foreach( $textnodes as $node ) {
$originalText = $text = wp_encode_emoji( $node->nodeValue );
$matches = array(); $matches = array();
if ( preg_match_all( '/(&#x1f1(e[6-9a-f]|f[0-9a-f]);){2}/', $text, $matches ) ) { if ( preg_match_all( '/(&#x1f1(e[6-9a-f]|f[0-9a-f]);){2}/', $content, $matches ) ) {
if ( ! empty( $matches[0] ) ) { if ( ! empty( $matches[0] ) ) {
foreach ( $matches[0] as $flag ) { foreach ( $matches[0] as $flag ) {
$chars = str_replace( array( '&#x', ';'), '', $flag ); $chars = str_replace( array( '&#x', ';'), '', $flag );
@ -4143,7 +4149,7 @@ function wp_staticize_emoji( $content ) {
list( $char1, $char2 ) = str_split( $chars, 5 ); list( $char1, $char2 ) = str_split( $chars, 5 );
$entity = '<img src="https:' . $cdn_url . $char1 . '-' . $char2 . $ext . '" class="wp-smiley" style="height: 1em;" />'; $entity = '<img src="https:' . $cdn_url . $char1 . '-' . $char2 . $ext . '" class="wp-smiley" style="height: 1em;" />';
$text = str_replace( $flag, $entity, $text ); $content = str_replace( $flag, $entity, $content );
} }
} }
} }
@ -4152,23 +4158,27 @@ function wp_staticize_emoji( $content ) {
$regex = '/(&#x[2-3][0-9a-f]{3};|&#x1f[1-6][0-9a-f]{2};)/'; $regex = '/(&#x[2-3][0-9a-f]{3};|&#x1f[1-6][0-9a-f]{2};)/';
$matches = array(); $matches = array();
if ( preg_match_all( $regex, $text, $matches ) ) { if ( preg_match_all( $regex, $content, $matches ) ) {
if ( ! empty( $matches[1] ) ) { if ( ! empty( $matches[1] ) ) {
foreach ( $matches[1] as $emoji ) { foreach ( $matches[1] as $emoji ) {
$char = str_replace( array( '&#x', ';'), '', $emoji ); $char = str_replace( array( '&#x', ';'), '', $emoji );
$entity = '<img src="https:' . $cdn_url . $char . $ext . '" class="wp-smiley" style="height: 1em;" />'; $entity = '<img src="https:' . $cdn_url . $char . $ext . '" class="wp-smiley" style="height: 1em;" />';
$text = str_replace( $emoji, $entity, $text ); $content = str_replace( $emoji, $entity, $content );
}
} }
} }
} }
if ( $originalText !== $text ) { // did we exit ignore block
$content = str_replace( $originalText, $text, $content ); if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content ) {
} $ignore_block_element = '';
} }
return $content; $output .= $content;
}
return $output;
} }
/** /**

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.2-alpha-31751'; $wp_version = '4.2-alpha-31752';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.