Make WordPress Core


Ignore:
Timestamp:
05/15/2009 09:37:18 PM (13 years ago)
Author:
ryan
Message:

Handle nested tag in wptexturize(). Props nbachiyski. fixes #7056 see #6969

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/formatting.php

    r11338 r11345  
    2929function wptexturize($text) {
    3030    global $wp_cockneyreplace;
    31     $next = true;
    32     $has_pre_parent = false;
    3331    $output = '';
    3432    $curl = '';
    3533    $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
    3634    $stop = count($textarr);
     35    $no_texturize_tags = apply_filters('no_texturize_tags', array('pre', 'code', 'kbd', 'style', 'script', 'tt'));
     36    $no_texturize_shortcodes = apply_filters('no_texturize_shortcodes', array('code'));
     37    $no_texturize_tags_stack = array();
     38    $no_texturize_shortcodes_stack = array();
    3739
    3840    // if a plugin has provided an autocorrect array, use it
     
    5456        $curl = $textarr[$i];
    5557
    56         if ( !empty($curl) && '<' != $curl{0} && '[' != $curl{0} && $next && !$has_pre_parent) { // If it's not a tag
     58        if ( !empty($curl) && '<' != $curl{0} && '[' != $curl{0}
     59                && empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack)) { // If it's not a tag
    5760            // static strings
    5861            $curl = str_replace($static_characters, $static_replacements, $curl);
    5962            // regular expressions
    6063            $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
    61         } elseif (strpos($curl, '<tt') !== false || strpos($curl, '<code') !== false || strpos($curl, '<kbd') !== false || strpos($curl, '<style') !== false || strpos($curl, '<script') !== false) {
    62             $next = false;
    63         } elseif (strpos($curl, '<pre') !== false) {
    64             $has_pre_parent = true;
    65         } elseif (strpos($curl, '</pre>') !== false) {
    66             $has_pre_parent = false;
    6764        } else {
    68             $next = true;
     65            wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>');
     66            wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']');
    6967        }
    7068
     
    7472
    7573    return $output;
     74}
     75
     76function wptexturize_pushpop_element($text, &$stack, $disabled_elements, $opening = '<', $closing = '>') {
     77    $o = preg_quote($opening);
     78    $c = preg_quote($closing);
     79    foreach($disabled_elements as $element) {
     80        if (preg_match('/^'.$o.$element.'\b/', $text)) array_push($stack, $element);
     81        if (preg_match('/^'.$o.'\/'.$element.$c.'/', $text)) {
     82            $last = array_pop($stack);
     83            // disable texturize until we find a closing tag of our type (e.g. <pre>)
     84            // even if there was invalid nesting before that
     85            // Example: in the case <pre>sadsadasd</code>"baba"</pre> "baba" won't be texturized
     86            if ($last != $element) array_push($stack, $last);
     87        }
     88    }
    7689}
    7790
Note: See TracChangeset for help on using the changeset viewer.