<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[28831] trunk: Optimize the `wptexturize()` loop:</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/28831">28831</a></dd>
<dt>Author</dt> <dd>wonderboymusic</dd>
<dt>Date</dt> <dd>2014-06-25 17:48:20 +0000 (Wed, 25 Jun 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Optimize the `wptexturize()` loop:
* Take the ampersand pattern out of the loop for speed.
* Fix old bugs in the ampersand pattern.
* Refactor `_wptexturize_pushpop_element()` without PCRE for speed.
* Update unit tests.

Props miqrogroove.
Fixes <a href="http://core.trac.wordpress.org/ticket/28623">#28623</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesformattingphp">trunk/src/wp-includes/formatting.php</a></li>
<li><a href="#trunktestsphpunittestsformattingWPTexturizephp">trunk/tests/phpunit/tests/formatting/WPTexturize.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesformattingphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/formatting.php (28830 => 28831)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/formatting.php     2014-06-25 17:38:28 UTC (rev 28830)
+++ trunk/src/wp-includes/formatting.php        2014-06-25 17:48:20 UTC (rev 28831)
</span><span class="lines">@@ -178,7 +178,7 @@
</span><span class="cx">   *
</span><span class="cx">   * @param array $default_no_texturize_tags An array of HTML element names.
</span><span class="cx">   */
</span><del>-       $no_texturize_tags = '(' . implode( '|', apply_filters( 'no_texturize_tags', $default_no_texturize_tags ) ) . ')';
</del><ins>+        $no_texturize_tags = apply_filters( 'no_texturize_tags', $default_no_texturize_tags );
</ins><span class="cx">   /**
</span><span class="cx">   * Filter the list of shortcodes not to texturize.
</span><span class="cx">   *
</span><span class="lines">@@ -186,7 +186,7 @@
</span><span class="cx">   *
</span><span class="cx">   * @param array $default_no_texturize_shortcodes An array of shortcode names.
</span><span class="cx">   */
</span><del>-       $no_texturize_shortcodes = '(' . implode( '|', apply_filters( 'no_texturize_shortcodes', $default_no_texturize_shortcodes ) ) . ')';
</del><ins>+        $no_texturize_shortcodes = apply_filters( 'no_texturize_shortcodes', $default_no_texturize_shortcodes );
</ins><span class="cx"> 
</span><span class="cx">  $no_texturize_tags_stack = array();
</span><span class="cx">  $no_texturize_shortcodes_stack = array();
</span><span class="lines">@@ -206,7 +206,7 @@
</span><span class="cx">          .       '(?:'
</span><span class="cx">          .               '[^\[\]<>]'       // Shortcodes do not contain other shortcodes.
</span><span class="cx">          .       '|'
</span><del>-               .               '<.+?>' // HTML elements permitted. Prevents matching ] before >.
</del><ins>+                .               '<.+?>'   // HTML elements permitted. Prevents matching ] before >.
</ins><span class="cx">           .       ')+'
</span><span class="cx">          .       '\]'            // Find end of shortcode.
</span><span class="cx">          .       '\]?'           // Shortcodes may end with ]]
</span><span class="lines">@@ -221,13 +221,13 @@
</span><span class="cx">                  // This is an HTML delimeter.
</span><span class="cx"> 
</span><span class="cx">                  if ( '<!--' !== substr( $curl, 0, 4 ) ) {
</span><del>-                               _wptexturize_pushpop_element( $curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>' );
</del><ins>+                                _wptexturize_pushpop_element( $curl, $no_texturize_tags_stack, $no_texturize_tags );
</ins><span class="cx">                   }
</span><span class="cx"> 
</span><span class="cx">          } elseif ( '[' === $first && 1 === preg_match( '/^\[(?:[^\[\]<>]|<.+?>)+\]$/', $curl ) ) {
</span><span class="cx">                  // This is a shortcode delimeter.
</span><span class="cx"> 
</span><del>-                       _wptexturize_pushpop_element( $curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']' );
</del><ins>+                        _wptexturize_pushpop_element( $curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes );
</ins><span class="cx"> 
</span><span class="cx">          } elseif ( '[' === $first && 1 === preg_match( '/^\[\[?(?:[^\[\]<>]|<.+?>)+\]\]?$/', $curl ) ) {
</span><span class="cx">                  // This is an escaped shortcode delimeter.
</span><span class="lines">@@ -235,11 +235,11 @@
</span><span class="cx">                  // Do not texturize.
</span><span class="cx">                  // Do not push to the shortcodes stack.
</span><span class="cx"> 
</span><del>-               } elseif ( empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack) ) {
</del><ins>+                } elseif ( empty( $no_texturize_shortcodes_stack ) && empty( $no_texturize_tags_stack ) ) {
</ins><span class="cx">                   // This is neither a delimeter, nor is this content inside of no_texturize pairs.  Do texturize.
</span><span class="cx"> 
</span><del>-                       $curl = str_replace($static_characters, $static_replacements, $curl);
-                       $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
</del><ins>+                        $curl = str_replace( $static_characters, $static_replacements, $curl );
+                       $curl = preg_replace( $dynamic_characters, $dynamic_replacements, $curl );
</ins><span class="cx"> 
</span><span class="cx">                  // 9x9 (times), but never 0x9999
</span><span class="cx">                  if ( 1 === preg_match( '/(?<=\d)x-?\d/', $curl ) ) {
</span><span class="lines">@@ -247,31 +247,54 @@
</span><span class="cx">                          $curl = preg_replace( '/\b(\d(?(?<=0)[\d\.,]+|[\d\.,]*))x(-?\d[\d\.,]*)\b/', '$1&#215;$2', $curl );
</span><span class="cx">                  }
</span><span class="cx">          }
</span><ins>+       }
+       $text = implode( '', $textarr );
</ins><span class="cx"> 
</span><del>-               // Replace each & with &#038; unless it already looks like an entity.
-               $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&#038;$1', $curl);
-       }
-       return implode( '', $textarr );
</del><ins>+        // Replace each & with &#038; unless it already looks like an entity.
+       $text = preg_replace('/&(?!#(?:\d+|x[a-f0-9]+);|[a-z1-4]{1,8};)/i', '&#038;', $text);
+
+       return $text;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="cx">  * Search for disabled element tags. Push element to stack on tag open and pop
</span><del>- * on tag close. Assumes first character of $text is tag opening.
</del><ins>+ * on tag close.
</ins><span class="cx">  *
</span><ins>+ * Assumes first char of $text is tag opening and last char is tag closing.
+ * Assumes second char of $text is optionally '/' to indicate closing as in </html>.
+ *
</ins><span class="cx">  * @since 2.9.0
</span><span class="cx">  * @access private
</span><span class="cx">  *
</span><del>- * @param string $text Text to check. First character is assumed to be $opening
- * @param array $stack Array used as stack of opened tag elements
- * @param string $disabled_elements Tags to match against formatted as regexp sub-expression
- * @param string $opening Tag opening character, assumed to be 1 character long
- * @param string $closing Tag closing character
</del><ins>+ * @param string $text Text to check. Must be a tag like <html> or [shortcode].
+ * @param array $stack List of open tag elements.
+ * @param array $disabled_elements The tag names to match against. Spaces are not allowed in tag names.
</ins><span class="cx">  */
</span><del>-function _wptexturize_pushpop_element($text, &$stack, $disabled_elements, $opening = '<', $closing = '>') {
-       // Check if it is a closing tag -- otherwise assume opening tag
-       if (strncmp($opening . '/', $text, 2)) {
-               // Opening? Check $text+1 against disabled elements
-               if (preg_match('/^' . $disabled_elements . '\b/', substr($text, 1), $matches)) {
</del><ins>+function _wptexturize_pushpop_element($text, &$stack, $disabled_elements) {
+       // Is it an opening tag or closing tag?
+       if ( '/' !== $text[1] ) {
+               $opening_tag = true;
+               $name_offset = 1;
+       } elseif ( 0 == count( $stack ) ) {
+               // Stack is empty. Just stop.
+               return;
+       } else {
+               $opening_tag = false;
+               $name_offset = 2;
+       }
+
+       // Parse out the tag name.
+       $space = strpos( $text, ' ' );
+       if ( FALSE === $space ) {
+               $space = -1;
+       } else {
+               $space -= $name_offset;
+       }
+       $tag = substr( $text, $name_offset, $space );
+
+       // Handle disabled tags.
+       if ( in_array( $tag, $disabled_elements ) ) {
+               if ( $opening_tag ) {
</ins><span class="cx">                   /*
</span><span class="cx">                   * This disables texturize until we find a closing tag of our type
</span><span class="cx">                   * (e.g. <pre>) even if there was invalid nesting before that
</span><span class="lines">@@ -280,21 +303,10 @@
</span><span class="cx">                   *          "baba" won't be texturize
</span><span class="cx">                   */
</span><span class="cx"> 
</span><del>-                       array_push($stack, $matches[1]);
</del><ins>+                        array_push( $stack, $tag );
+               } elseif ( end( $stack ) == $tag ) {
+                       array_pop( $stack );
</ins><span class="cx">           }
</span><del>-       } elseif ( 0 == count( $stack ) ) {
-               // Stack is empty. Just stop.
-       } else {
-               // Closing? Check $text+2 against disabled elements
-               $c = preg_quote($closing, '/');
-               if (preg_match('/^' . $disabled_elements . $c . '/', substr($text, 2), $matches)) {
-                       $last = array_pop($stack);
-
-                       // Make sure it matches the opening tag
-                       if ( $last != $matches[1] ) {
-                               array_push( $stack, $last );
-                       }
-               }
</del><span class="cx">   }
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunktestsphpunittestsformattingWPTexturizephp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/formatting/WPTexturize.php (28830 => 28831)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/formatting/WPTexturize.php     2014-06-25 17:38:28 UTC (rev 28830)
+++ trunk/tests/phpunit/tests/formatting/WPTexturize.php        2014-06-25 17:48:20 UTC (rev 28831)
</span><span class="lines">@@ -830,8 +830,16 @@
</span><span class="cx">                          "word &#038; word",
</span><span class="cx">                  ),
</span><span class="cx">                  array(
</span><ins>+                               "word &#xabc; word",
+                               "word &#xabc; word",
+                       ),
+                       array(
+                               "word &#X394; word",
+                               "word &#X394; word",
+                       ),
+                       array(
</ins><span class="cx">                           "word &# word",
</span><del>-                               "word &# word", // invalid output?
</del><ins>+                                "word &#038;# word",
</ins><span class="cx">                   ),
</span><span class="cx">                  array(
</span><span class="cx">                          "word &44; word",
</span><span class="lines">@@ -839,12 +847,20 @@
</span><span class="cx">                  ),
</span><span class="cx">                  array(
</span><span class="cx">                          "word &&amp; word",
</span><del>-                               "word &&amp; word",
</del><ins>+                                "word &#038;&amp; word",
</ins><span class="cx">                   ),
</span><span class="cx">                  array(
</span><span class="cx">                          "word &!amp; word",
</span><del>-                               "word &!amp; word",
</del><ins>+                                "word &#038;!amp; word",
</ins><span class="cx">                   ),
</span><ins>+                       array(
+                               "word &#",
+                               "word &#038;#",
+                       ),
+                       array(
+                               "word &",
+                               "word &#038;",
+                       ),
</ins><span class="cx">           );
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -1285,6 +1301,10 @@
</span><span class="cx">                          '<ul><li>Hello.</li><!--<li>Goodbye.</li>--></ul>',
</span><span class="cx">                          '<ul><li>Hello.</li><!--<li>Goodbye.</li>--></ul>',
</span><span class="cx">                  ),
</span><ins>+                       array(
+                               'word <img src="http://example.com/wp-content/uploads/2014/06/image-300x216.gif" /> word', // Ensure we are not corrupting image URLs.
+                               'word <img src="http://example.com/wp-content/uploads/2014/06/image-300x216.gif" /> word',
+                       ),
</ins><span class="cx">           );
</span><span class="cx">  }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>