Make WordPress Core


Ignore:
Timestamp:
11/20/2014 02:31:09 PM (12 years ago)
Author:
nacin
Message:

Anchor texturize to shortcodes to improve regex efficiency.

For the 3.9 branch; see [30449] for trunk.

props miqrogroove.
see #29557 for segfault issues.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.9/src/wp-includes/formatting.php

    r27844 r30452  
    153153        $no_texturize_shortcodes_stack = array();
    154154
    155         $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
     155        // Look for shortcodes and HTML elements.
     156       
     157        $shortcode_regex =
     158                  '\['          // Find start of shortcode.
     159                . '[^\[\]<>]++' // Shortcodes do not contain other shortcodes. Possessive critical.
     160                . '\]';         // Find end of shortcode.
     161
     162        $textarr = preg_split("/(<[^>]*>|$shortcode_regex)/s", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
    156163
    157164        foreach ( $textarr as &$curl ) {
     
    164171                if ( '<' === $first ) {
    165172                        _wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>');
    166                 } elseif ( '[' === $first ) {
     173                } elseif ( '[' === $first && 1 === preg_match( '/^' . $shortcode_regex . '$/', $curl ) ) {
    167174                        _wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']');
    168175                } elseif ( empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack) ) {
     
    215222                        array_push($stack, $matches[1]);
    216223                }
     224        } elseif ( 0 == count( $stack ) ) {
     225                // Stack is empty. Just stop.
    217226        } else {
    218227                // Closing? Check $text+2 against disabled elements
Note: See TracChangeset for help on using the changeset viewer.