From b939e06b06acef7deda074b8ffb7fad57a7e4ba4 Mon Sep 17 00:00:00 2001 From: Drew Jaynes Date: Fri, 20 Mar 2015 19:46:41 +0000 Subject: [PATCH] Add more verbose inline documentation inside of `wpautop()` to more clearly explain what's going on. Props ericlewis, johneckman, azaozz. Fixes #31041. git-svn-id: https://develop.svn.wordpress.org/trunk@31852 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/formatting.php | 85 ++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 20 deletions(-) diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index a75af2ee99..fbcde5630b 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -360,14 +360,14 @@ function _wptexturize_pushpop_element($text, &$stack, $disabled_elements) { * Replaces double line-breaks with paragraph elements. * * A group of regex replaces used to identify text formatted with newlines and - * replace double line-breaks with HTML paragraph tags. The remaining - * line-breaks after conversion become <
> tags, unless $br is set to '0' - * or 'false'. + * replace double line-breaks with HTML paragraph tags. The remaining line-breaks + * after conversion become <
> tags, unless $br is set to '0' or 'false'. * * @since 0.71 * * @param string $pee The text which has to be formatted. - * @param bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true. + * @param bool $br Optional. If set, this will convert all remaining line-breaks + * after paragraphing. Default true. * @return string Text which has been converted into correct paragraph tags. */ function wpautop($pee, $br = true) { @@ -376,8 +376,13 @@ function wpautop($pee, $br = true) { if ( trim($pee) === '' ) return ''; - $pee = $pee . "\n"; // just to make things a little easier, pad the end + // Just to make things a little easier, pad the end. + $pee = $pee . "\n"; + /* + * Pre tags shouldn't be touched by autop. + * Replace pre tags with placeholders and bring them back after autop. + */ if ( strpos($pee, '', $pee ); $last_pee = array_pop($pee_parts); @@ -402,62 +407,102 @@ function wpautop($pee, $br = true) { $pee .= $last_pee; } - + // Change multiple
s into two line breaks, which will turn into paragraphs. $pee = preg_replace('|
\s*
|', "\n\n", $pee); - // Space things out a little - $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|figcaption|details|menu|summary)'; - $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee); - $pee = preg_replace('!()!', "$1\n\n", $pee); - $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines + $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|figcaption|details|menu|summary)'; + + // Add a single line break above block-level opening tags. + $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee); + + // Add a double line break below block-level closing tags. + $pee = preg_replace('!()!', "$1\n\n", $pee); + + // Standardize newline characters to "\n". + $pee = str_replace(array("\r\n", "\r"), "\n", $pee); + + // Collapse line breaks before and after ', $pee ); } + /* + * Collapse line breaks inside elements, before and elements + * so they don't get autop'd. + */ if ( strpos( $pee, '' ) !== false ) { - // no P/BR around param and embed $pee = preg_replace( '|(]*>)\s*|', '$1', $pee ); $pee = preg_replace( '|\s*|', '', $pee ); $pee = preg_replace( '%\s*(]*>)\s*%', '$1', $pee ); } + /* + * Collapse line breaks inside