WordPress.org

Make WordPress Core

Ticket #27350: formatting-27350-rev1.diff

File formatting-27350-rev1.diff, 3.4 KB (added by jond, 7 years ago)

Revision for accurate preg_match return values

  • src/wp-includes/formatting.php

     
    147147                $dynamic_characters['apos'] = array_keys( $dynamic );
    148148                $dynamic_replacements['apos'] = array_values( $dynamic );
    149149                $dynamic = array();
    150                
     150
    151151                // Quoted Numbers like "42"
    152152                if ( '"' !== $opening_quote && '"' !== $closing_quote ) {
    153153                        $dynamic[ '/(?<=\A|' . $spaces . ')"(\d[.,\d]*)"/' ] = $opening_quote . '$1' . $closing_quote;
     
    167167                if ( '"' !== $closing_quote ) {
    168168                        $dynamic[ '/"/' ] = $closing_quote;
    169169                }
    170                
     170
    171171                $dynamic_characters['quote'] = array_keys( $dynamic );
    172172                $dynamic_replacements['quote'] = array_values( $dynamic );
    173173                $dynamic = array();
    174                
     174
    175175                // Dashes and spaces
    176176                $dynamic[ '/---/' ] = $em_dash;
    177177                $dynamic[ '/(?<=' . $spaces . ')--(?=' . $spaces . ')/' ] = $em_dash;
     
    387387
    388388        $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
    389389        // Space things out a little
    390         $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|details|menu|summary)';
     390        $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|details|menu|summary)\b';
    391391        $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
    392392        $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
    393393        $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
     
    414414
    415415        $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
    416416        // make paragraphs, including one at the end
    417         $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
     417        $pees = preg_split('/\n\s*\n/', ltrim($pee), -1, PREG_SPLIT_NO_EMPTY);
    418418        $pee = '';
    419419
    420420        foreach ( $pees as $tinkle ) {
    421                 $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
     421                if ( preg_match( '!</?blockquote([^>]*)>!', $tinkle) == 1 ) {
     422                        // pee inside blockquotes (prevent splashing)
     423                        $blockquote = preg_replace('|<blockquote([^>]*)>|i', "<blockquote$1><p>", $tinkle);
     424                        $blockquote = str_replace('</blockquote>', '</p></blockquote>', $blockquote);
     425                        $pee .= $blockquote . "\n";
     426                } elseif ( preg_match( '!</?' . $allblocks . '[^>]*>!', $tinkle ) == 1 ) {
     427                        $pee .= trim($tinkle, "\n") . "\n"; // avoid peeing on block elements in the stream
     428                } else {
     429                        $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
     430                }
    422431        }
    423432
    424433        $pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
    425434        $pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
    426435        $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
    427436        $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
    428         $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
    429         $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
    430437        $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
    431438        $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
    432439