diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php
index a75af2ee99..fbcde5630b 100644
--- a/wp-includes/formatting.php
+++ b/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('!(' . $allblocks . '>)!', "$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('!(' . $allblocks . '>)!', "$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 ' ) !== false ) { - // no P/BR around param and embed $pee = preg_replace( '|(|', '', $pee ); $pee = preg_replace( '%\s*(?(?:param|embed)[^>]*>)\s*%', '$1', $pee ); } + /* + * Collapse line breaks inside