221 | | $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee); |
222 | | // Space things out a little |
223 | | $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)'; |
224 | | $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee); |
225 | | $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee); |
226 | | $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines |
227 | | if ( strpos($pee, '<object') !== false ) { |
228 | | $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed |
229 | | $pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee); |
| 230 | $content = preg_replace('|<br />\s*<br />|', "\n\n", $content); |
| 231 | |
| 232 | // skip formatting of predefined tags |
| 233 | $content = preg_replace_callback('/<(script|style|math|select|svg).*?<\/\\1>/si', '_autop_newline_preservation_helper', $content); |
| 234 | |
| 235 | // skip new lines of comments |
| 236 | $content = preg_replace_callback('/<!--.*?-->\s*/', '_autop_newline_preservation_helper', $content); |
| 237 | |
| 238 | // List of Blocking elements (minus p) |
| 239 | $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"; |
| 240 | |
| 241 | $allblocks = '(?:' . $blocklist . '|p)'; |
| 242 | $allblocks_p = '(?:' . $blocklist . ')'; |
| 243 | |
| 244 | $content = preg_replace('!(<' . $allblocks . '[^>]*>)!i', "\n\n$1", $content); |
| 245 | $content = preg_replace('!(<' . $allblocks . '[^>]*\/>)!i', "$1\n\n", $content); |
| 246 | $content = preg_replace('!(</' . $allblocks . '>)!i', "$1\n\n", $content); |
| 247 | $content = str_replace(array("\r\n", "\r"), "\n", $content); // cross-platform newlines |
| 248 | |
| 249 | while ( preg_match("/(<[^>\n]*)\n+/", $content) ) { |
| 250 | $content = preg_replace("/(<[^>\n]*)\n+/", "$1 ", $content); // eliminate carriage returns in tags |
233 | | $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY); |
234 | | $pee = ''; |
235 | | foreach ( $pees as $tinkle ) |
236 | | $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n"; |
237 | | $pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace |
238 | | $pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee); |
239 | | $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag |
240 | | $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists |
241 | | $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee); |
242 | | $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee); |
243 | | $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee); |
244 | | $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); |
| 256 | $paragraphs = preg_split('/\n\s*\n/', $content, -1, PREG_SPLIT_NO_EMPTY); |
| 257 | $content = ''; |
| 258 | foreach ( $paragraphs as $paragraph ) { |
| 259 | // test if block contains open or closing blocking elements if so add necessary closures |
| 260 | |
| 261 | // current content segment does not contain an open and closing blocking element |
| 262 | if( 0 == preg_match('/<' . $allblocks_p . '[^>]*>.*?<\/' . $allblocks_p . '[^>]*>/si', $paragraph) ) { |
| 263 | |
| 264 | // current content contains an open blocking element |
| 265 | if( 1 == preg_match('/<' . $allblocks_p .'[^>]*>/si', $paragraph) && 0 == preg_match('/<' . $allblocks_p .'[^>]*\/>/si', $paragraph) ) { |
| 266 | $content .= preg_replace('/(<' . $allblocks_p . '[^>]*>)/si', "$1\n<p>", trim($paragraph, "\n") ); |
| 267 | $content .= "</p>\n"; |
| 268 | |
| 269 | // current content contains a closing blocking element |
| 270 | } else if ( 1 == preg_match('/(<\/' . $allblocks_p .'[^>]*>)/si', $paragraph) ) { |
| 271 | $content .= '<p>' . preg_replace('/(<\/' . $allblocks_p . '[^>]*>)/si', "</p>\n$1\n", trim($paragraph, "\n") ); |
| 272 | |
| 273 | } else { |
| 274 | |
| 275 | // current content is not wrapped in <p> tag and is not self closing tag |
| 276 | if( 0 == preg_match( '/<p[^>]*>.*?<\/p[^>]*>/si', $paragraph ) && 0 == preg_match('/<' . $allblocks .'[^>]*\/>/si', $paragraph ) ) { |
| 277 | $content .= '<p>' . trim($paragraph, "\n") . "</p>\n"; |
| 278 | |
| 279 | } else { |
| 280 | $content .= trim($paragraph, "\n") . "\n"; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // current content contains an open and closing blocking element |
| 285 | } else { |
| 286 | $content .= trim($paragraph, "\n") . "\n"; |
| 287 | } |
| 288 | } |
| 289 | $content = preg_replace('|<p>\s*</p>|', '', $content); // under certain strange conditions it could create a P of entirely whitespace |
250 | | $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee); |
251 | | $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee); |
252 | | $pee = preg_replace( "|\n</p>$|", '</p>', $pee ); |
| 293 | $content = str_replace('<WPPreserveNewline />', "\n", $content); |
| 294 | $content = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $content); |
| 295 | $content = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $content); |
| 296 | $content = preg_replace( "|\n</p>$|", '</p>', $content ); |