Index: formatting.php
===================================================================
--- formatting.php	(revision 26022)
+++ formatting.php	(working copy)
@@ -172,89 +172,133 @@
 }
 
 /**
- * Replaces double line-breaks with paragraph elements.
+ * Formats content to contain proper blocking of elements. The core logic 
+ * is a group of regex replaces to identify block elements and place paragraph
+ * tags around non-blocked content.
  *
- * 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 <<br />> tags, unless $br is set to '0'
- * or 'false'.
+ * Shortcode tags are are treated as normal text. (wpatup is unaware)
  *
+ * Details ::
+ *   object, video, audio and pre tags are excluded from formatting
+ *   script, style, math, select and svg tags are excluded from line-break conversions
+ *   Double line breaks are replaced with HTML paragraph tags.
+ *   Line-breaks after conversion become <<br />> tags, unless $br is set to '0'
+ *   or 'false'.
+ *
  * @since 0.71
  *
- * @param string $pee The text which has to be formatted.
+ * @param string $content The text which has to be formatted.
  * @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) {
-	$pre_tags = array();
-
-	if ( trim($pee) === '' )
+function wpautop($content, $br = true) {
+	
+	if ( trim($content) === '' )
 		return '';
 
-	$pee = $pee . "\n"; // just to make things a little easier, pad the end
+	$save_tags = array();
+	$no_format_tags = array( 'pre', 'audio', 'video', 'object');
+	foreach( $no_format_tags as $no_format_tag ) {
+	
+		// replace <pre|audio|video> tags to ignore formatting
+		if ( strpos($content, '<' . $no_format_tag) !== false ) {
+			$content_parts = explode( '</' . $no_format_tag . '>', $content );
+			$last_content = array_pop($content_parts);
+			$content = '';
+			$i = 0;
 
-	if ( strpos($pee, '<pre') !== false ) {
-		$pee_parts = explode( '</pre>', $pee );
-		$last_pee = array_pop($pee_parts);
-		$pee = '';
-		$i = 0;
+			foreach ( $content_parts as $content_part ) {
+				$start = strpos( $content_part, '<' . $no_format_tag );
 
-		foreach ( $pee_parts as $pee_part ) {
-			$start = strpos($pee_part, '<pre');
+				// Malformed html?
+				if ( $start === false ) {
+					$content .= $content_part;
+					continue;
+				}
 
-			// Malformed html?
-			if ( $start === false ) {
-				$pee .= $pee_part;
-				continue;
+				$name = "<" . $no_format_tag ." wp-no-format-tag-$i></" . $no_format_tag . ">";
+				$save_tags[$name] = substr( $content_part, $start ) . '</' . $no_format_tag . '>';
+
+				$content .= substr( $content_part, 0, $start ) . $name;
+				$i++;
 			}
 
-			$name = "<pre wp-pre-tag-$i></pre>";
-			$pre_tags[$name] = substr( $pee_part, $start ) . '</pre>';
-
-			$pee .= substr( $pee_part, 0, $start ) . $name;
-			$i++;
+			$content .= $last_content;
 		}
-
-		$pee .= $last_pee;
 	}
 
-	$pee = preg_replace('|<br />\s*<br />|', "\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|select|option|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|noscript|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
-	if ( strpos($pee, '<object') !== false ) {
-		$pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
-		$pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
+	$content = preg_replace('|<br />\s*<br />|', "\n\n", $content);
+	
+	// skip formatting of predefined tags
+	$content = preg_replace_callback('/<(script|style|math|select|svg).*?<\/\\1>/si', '_autop_newline_preservation_helper', $content);
+	
+	// skip new lines of comments
+	$content = preg_replace_callback('/<!--.*?-->\s*/', '_autop_newline_preservation_helper', $content);
+	
+	// List of Blocking elements (minus p)
+	$blocklist = "table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|noscript|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary";
+	
+	$allblocks = '(?:' . $blocklist . '|p)';
+	$allblocks_p = '(?:' . $blocklist . ')';
+	
+	$content = preg_replace('!(<' . $allblocks . '[^>]*>)!i', "\n\n$1", $content);
+	$content = preg_replace('!(<' . $allblocks . '[^>]*\/>)!i', "$1\n\n", $content);
+	$content = preg_replace('!(</' . $allblocks . '>)!i', "$1\n\n", $content);
+	$content = str_replace(array("\r\n", "\r"), "\n", $content); // cross-platform newlines
+	
+	while ( preg_match("/(<[^>\n]*)\n+/", $content) ) {
+		$content = preg_replace("/(<[^>\n]*)\n+/", "$1 ", $content); // eliminate carriage returns in tags 
 	}
-	$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
+	
+	$content = preg_replace("/\n\n+/", "\n\n", $content); // take care of duplicates	
+	
 	// make paragraphs, including one at the end
-	$pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
-	$pee = '';
-	foreach ( $pees as $tinkle )
-		$pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
-	$pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
-	$pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
-	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
-	$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
-	$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
-	$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
-	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
-	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
+	$paragraphs = preg_split('/\n\s*\n/', $content, -1, PREG_SPLIT_NO_EMPTY);
+	$content = '';
+	foreach ( $paragraphs as $paragraph ) { 
+		// test if block contains open or closing blocking elements if so add necessary closures
+		
+		// current content segment does not contain an open and closing blocking element
+		if( 0 == preg_match('/<' . $allblocks_p . '[^>]*>.*?<\/' . $allblocks_p . '[^>]*>/si', $paragraph) ) {
+		
+			// current content contains an open blocking element
+			if( 1 == preg_match('/<' . $allblocks_p .'[^>]*>/si', $paragraph) && 0 == preg_match('/<' . $allblocks_p .'[^>]*\/>/si', $paragraph) ) {
+				$content .= preg_replace('/(<' . $allblocks_p . '[^>]*>)/si', "$1\n<p>", trim($paragraph, "\n") );
+				$content .= "</p>\n";
+				
+			// current content contains a closing blocking element
+			} else if ( 1 == preg_match('/(<\/' . $allblocks_p .'[^>]*>)/si', $paragraph) ) {
+				$content .= '<p>' . preg_replace('/(<\/' . $allblocks_p . '[^>]*>)/si', "</p>\n$1\n", trim($paragraph, "\n") );
+			
+			} else {
+			
+				// current content is not wrapped in <p> tag and is not self closing tag
+				if( 0 == preg_match( '/<p[^>]*>.*?<\/p[^>]*>/si', $paragraph ) && 0 == preg_match('/<' . $allblocks .'[^>]*\/>/si', $paragraph ) ) {
+					$content .= '<p>' . trim($paragraph, "\n") . "</p>\n";
+					
+				} else {
+					$content .= trim($paragraph, "\n") . "\n";
+				}
+			}
+			
+		// current content contains an open and closing blocking element
+		} else {
+			$content .= trim($paragraph, "\n") . "\n";
+		}
+	}
+	$content = preg_replace('|<p>\s*</p>|', '', $content); // under certain strange conditions it could create a P of entirely whitespace
 	if ( $br ) {
-		$pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);
-		$pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
-		$pee = str_replace('<WPPreserveNewline />', "\n", $pee);
+		$content = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $content); // optionally make line breaks
 	}
-	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
-	$pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
-	$pee = preg_replace( "|\n</p>$|", '</p>', $pee );
+	$content = str_replace('<WPPreserveNewline />', "\n", $content);
+	$content = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $content);
+	$content = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $content);
+	$content = preg_replace( "|\n</p>$|", '</p>', $content );
 
-	if ( !empty($pre_tags) )
-		$pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
+	if ( !empty($save_tags) )
+		$content = str_replace(array_keys($save_tags), array_values($save_tags), $content);
 
-	return $pee;
+	return trim($content);
 }
 
 /**
@@ -277,14 +321,14 @@
  *
  * @since 2.9.0
  *
- * @param string $pee The content.
+ * @param string $content The content.
  * @return string The filtered content.
  */
-function shortcode_unautop( $pee ) {
+function shortcode_unautop( $content ) {
 	global $shortcode_tags;
 
 	if ( empty( $shortcode_tags ) || !is_array( $shortcode_tags ) ) {
-		return $pee;
+		return $content;
 	}
 
 	$tagregexp = join( '|', array_map( 'preg_quote', array_keys( $shortcode_tags ) ) );
@@ -321,7 +365,7 @@
 		. '<\\/p>'                           // closing paragraph
 		. '/s';
 
-	return preg_replace( $pattern, '$1', $pee );
+	return preg_replace( $pattern, '$1', $content );
 }
 
 /**
