From 0f7d055621d83c676dade382260d73f250e8fddc Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 15 May 2009 21:37:18 +0000 Subject: [PATCH] Handle nested tag in wptexturize(). Props nbachiyski. fixes #7056 see #6969 git-svn-id: http://svn.automattic.com/wordpress/trunk@11345 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/formatting.php | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index c8561e3af2..7fbde25994 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -28,12 +28,14 @@ */ function wptexturize($text) { global $wp_cockneyreplace; - $next = true; - $has_pre_parent = false; $output = ''; $curl = ''; $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE); $stop = count($textarr); + $no_texturize_tags = apply_filters('no_texturize_tags', array('pre', 'code', 'kbd', 'style', 'script', 'tt')); + $no_texturize_shortcodes = apply_filters('no_texturize_shortcodes', array('code')); + $no_texturize_tags_stack = array(); + $no_texturize_shortcodes_stack = array(); // if a plugin has provided an autocorrect array, use it if ( isset($wp_cockneyreplace) ) { @@ -53,19 +55,15 @@ function wptexturize($text) { for ( $i = 0; $i < $stop; $i++ ) { $curl = $textarr[$i]; - if ( !empty($curl) && '<' != $curl{0} && '[' != $curl{0} && $next && !$has_pre_parent) { // If it's not a tag + if ( !empty($curl) && '<' != $curl{0} && '[' != $curl{0} + && empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack)) { // If it's not a tag // static strings $curl = str_replace($static_characters, $static_replacements, $curl); // regular expressions $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl); - } elseif (strpos($curl, '') !== false) { - $has_pre_parent = false; } else { - $next = true; + wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>'); + wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']'); } $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&$1', $curl); @@ -75,6 +73,21 @@ function wptexturize($text) { return $output; } +function wptexturize_pushpop_element($text, &$stack, $disabled_elements, $opening = '<', $closing = '>') { + $o = preg_quote($opening); + $c = preg_quote($closing); + foreach($disabled_elements as $element) { + if (preg_match('/^'.$o.$element.'\b/', $text)) array_push($stack, $element); + if (preg_match('/^'.$o.'\/'.$element.$c.'/', $text)) { + $last = array_pop($stack); + // disable texturize until we find a closing tag of our type (e.g.
)
+			// even if there was invalid nesting before that
+			// Example: in the case 
sadsadasd"baba"
"baba" won't be texturized + if ($last != $element) array_push($stack, $last); + } + } +} + /** * Accepts matches array from preg_replace_callback in wpautop() or a string. *