From 0c8039be21478c340f7f483f775da20cc65b4525 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Thu, 20 Nov 2014 14:40:09 +0000 Subject: [PATCH] Anchor texturize to shortcodes to improve regex efficiency. Merges [30452] to the 3.7 branch. props miqrogroove. see #29557 for segfault issues. Built from https://develop.svn.wordpress.org/branches/3.7@30456 git-svn-id: http://core.svn.wordpress.org/branches/3.7@30447 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/formatting.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index 904c99b17b..17133ae09d 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -107,7 +107,14 @@ function wptexturize($text) { $no_texturize_tags_stack = array(); $no_texturize_shortcodes_stack = array(); - $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE); + // Look for shortcodes and HTML elements. + + $shortcode_regex = + '\[' // Find start of shortcode. + . '[^\[\]<>]++' // Shortcodes do not contain other shortcodes. Possessive critical. + . '\]'; // Find end of shortcode. + + $textarr = preg_split("/(<[^>]*>|$shortcode_regex)/s", $text, -1, PREG_SPLIT_DELIM_CAPTURE); foreach ( $textarr as &$curl ) { if ( empty( $curl ) ) @@ -117,7 +124,7 @@ function wptexturize($text) { $first = $curl[0]; if ( '<' === $first ) { _wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>'); - } elseif ( '[' === $first ) { + } elseif ( '[' === $first && 1 === preg_match( '/^' . $shortcode_regex . '$/', $curl ) ) { _wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']'); } elseif ( empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack) ) { // This is not a tag, nor is the texturization disabled static strings @@ -158,6 +165,8 @@ function _wptexturize_pushpop_element($text, &$stack, $disabled_elements, $openi array_push($stack, $matches[1]); } + } elseif ( 0 == count( $stack ) ) { + // Stack is empty. Just stop. } else { // Closing? Check $text+2 against disabled elements $c = preg_quote($closing, '/');