Make WordPress Core

Changeset 20307


Ignore:
Timestamp:
03/28/2012 03:43:31 PM (12 years ago)
Author:
azaozz
Message:

Do not process <pre> tags with wpautop, replace them with placeholders, process the rest of the content and then put them back. Part props kurtpayne, see #19855

Location:
trunk/wp-includes
Files:
2 edited

Legend:

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

    r20281 r20307  
    29842984
    29852985/**
     2986 * Accepts matches array from preg_replace_callback in wpautop() or a string.
     2987 *
     2988 * Ensures that the contents of a <<pre>>...<</pre>> HTML block are not
     2989 * converted into paragraphs or line-breaks.
     2990 *
     2991 * @since 1.2.0
     2992 * @deprecated 3.4.0
     2993 *
     2994 * @param array|string $matches The array or string
     2995 * @return string The pre block without paragraph/line-break conversion.
     2996 */
     2997function clean_pre($matches) {
     2998    _deprecated_function( __FUNCTION__, '3.4' );
     2999   
     3000    if ( is_array($matches) )
     3001        $text = $matches[1] . $matches[2] . "</pre>";
     3002    else
     3003        $text = $matches;
     3004
     3005    $text = str_replace('<br />', '', $text);
     3006    $text = str_replace('<p>', "\n", $text);
     3007    $text = str_replace('</p>', '', $text);
     3008
     3009    return $text;
     3010}
     3011
     3012
     3013/**
    29863014 * Add callbacks for image header display.
    29873015 *
  • trunk/wp-includes/formatting.php

    r20078 r20307  
    174174
    175175/**
    176  * Accepts matches array from preg_replace_callback in wpautop() or a string.
    177  *
    178  * Ensures that the contents of a <<pre>>...<</pre>> HTML block are not
    179  * converted into paragraphs or line-breaks.
    180  *
    181  * @since 1.2.0
    182  *
    183  * @param array|string $matches The array or string
    184  * @return string The pre block without paragraph/line-break conversion.
    185  */
    186 function clean_pre($matches) {
    187     if ( is_array($matches) )
    188         $text = $matches[1] . $matches[2] . "</pre>";
    189     else
    190         $text = $matches;
    191 
    192     $text = str_replace('<br />', '', $text);
    193     $text = str_replace('<p>', "\n", $text);
    194     $text = str_replace('</p>', '', $text);
    195 
    196     return $text;
    197 }
    198 
    199 /**
    200176 * Replaces double line-breaks with paragraph elements.
    201177 *
     
    208184 *
    209185 * @param string $pee The text which has to be formatted.
    210  * @param int|bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true.
     186 * @param bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true.
    211187 * @return string Text which has been converted into correct paragraph tags.
    212188 */
    213 function wpautop($pee, $br = 1) {
     189function wpautop($pee, $br = true) {
     190    $pre_tags = array();
    214191
    215192    if ( trim($pee) === '' )
    216193        return '';
     194
    217195    $pee = $pee . "\n"; // just to make things a little easier, pad the end
     196
     197    if ( strpos($pee, '<pre') !== false ) {
     198        $pee_parts = explode( '</pre>', $pee );
     199        $last_pee = array_pop($pee_parts);
     200        $pee = '';
     201        $i = 0;
     202
     203        foreach ( $pee_parts as $pee_part ) {
     204            $start = strpos($pee_part, '<pre');
     205
     206            // Malformed html?
     207            if ( $start === false ) {
     208                $pee .= $pee_part;
     209                continue;
     210            }
     211
     212            $name = "<pre wp-pre-tag-$i></pre>";
     213            $pre_tags[$name] = substr( $pee_part, $start ) . '</pre>';
     214
     215            $pee .= substr( $pee_part, 0, $start ) . $name;
     216            $i++;
     217        }
     218
     219        $pee .= $last_pee;
     220    }
     221
    218222    $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
    219223    // Space things out a little
     
    240244    $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
    241245    $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
    242     if ($br) {
     246    if ( $br ) {
    243247        $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);
    244248        $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
     
    247251    $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
    248252    $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
    249     if (strpos($pee, '<pre') !== false)
    250         $pee = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee );
    251253    $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
     254
     255    if ( !empty($pre_tags) )
     256        $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
    252257
    253258    return $pee;
Note: See TracChangeset for help on using the changeset viewer.