From 8103dc5ade56e1fbb90d1b32fa22d06e62ba54cf Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 26 Jun 2014 17:14:47 +0000 Subject: [PATCH] Use less greedy regex in `wptexturize()`. Adds unit tests. Props miqrogroove. See #28564. git-svn-id: https://develop.svn.wordpress.org/trunk@28852 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/formatting.php | 8 ++++---- tests/phpunit/tests/formatting/WPTexturize.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 5af533932f..77ecbcda72 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -198,7 +198,7 @@ function wptexturize($text, $reset = false) { . '(?(?=!--)' // Is this a comment? . '.+?--\s*>' // Find end of comment . '|' - . '.+?>' // Find end of element + . '[^>]+>' // Find end of element . ')' . '|' . '\[' // Find start of shortcode. @@ -206,7 +206,7 @@ function wptexturize($text, $reset = false) { . '(?:' . '[^\[\]<>]' // Shortcodes do not contain other shortcodes. . '|' - . '<.+?>' // HTML elements permitted. Prevents matching ] before >. + . '<[^>]+>' // HTML elements permitted. Prevents matching ] before >. . ')+' . '\]' // Find end of shortcode. . '\]?' // Shortcodes may end with ]] @@ -224,12 +224,12 @@ function wptexturize($text, $reset = false) { _wptexturize_pushpop_element( $curl, $no_texturize_tags_stack, $no_texturize_tags ); } - } elseif ( '[' === $first && 1 === preg_match( '/^\[(?:[^\[\]<>]|<.+?>)+\]$/', $curl ) ) { + } elseif ( '[' === $first && 1 === preg_match( '/^\[(?:[^\[\]<>]|<[^>]+>)+\]$/', $curl ) ) { // This is a shortcode delimeter. _wptexturize_pushpop_element( $curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes ); - } elseif ( '[' === $first && 1 === preg_match( '/^\[\[?(?:[^\[\]<>]|<.+?>)+\]\]?$/', $curl ) ) { + } elseif ( '[' === $first && 1 === preg_match( '/^\[\[?(?:[^\[\]<>]|<[^>]+>)+\]\]?$/', $curl ) ) { // This is an escaped shortcode delimeter. // Do not texturize. diff --git a/tests/phpunit/tests/formatting/WPTexturize.php b/tests/phpunit/tests/formatting/WPTexturize.php index ea22b62500..7ba7346251 100644 --- a/tests/phpunit/tests/formatting/WPTexturize.php +++ b/tests/phpunit/tests/formatting/WPTexturize.php @@ -1305,6 +1305,22 @@ class Tests_Formatting_WPTexturize extends WP_UnitTestCase { 'word word', // Ensure we are not corrupting image URLs. 'word word', ), + array( + '[ do texturize "[quote]" here ]', + '[ do texturize “[quote]” here ]', + ), + array( + '[ regex catches this here ]', + '[ regex catches this here ]', + ), + array( + '[ but also catches the styled "[quote]" here ]', + '[ but also catches the styled “[quote]” here ]', + ), + array( + '[Let\'s get crazy[plugin code="hello"]world]', + '[Let’s get crazy[plugin code="hello"]world]', + ), ); }