Make WordPress Core

Ticket #31041: 31041.diff

File 31041.diff, 5.9 KB (added by ericlewis, 10 years ago)
  • src/wp-includes/formatting.php

     
    378378
    379379        $pee = $pee . "\n"; // just to make things a little easier, pad the end
    380380
     381        /*
     382         * Pre tags shouldn't be touched by autop.
     383         * Replace pre tags with placeholders and bring them back after autop.
     384         */
    381385        if ( strpos($pee, '<pre') !== false ) {
    382386                $pee_parts = explode( '</pre>', $pee );
    383387                $last_pee = array_pop($pee_parts);
     
    403407                $pee .= $last_pee;
    404408        }
    405409
     410        // Change double <br>s into double breaks, which will turn into paragraphs.
    406411        $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
    407         // Space things out a little
     412
    408413        $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)';
     414        // Add a single line break above block-level opening tags.
    409415        $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
     416        // Add a double line break below block-level closing tags.
    410417        $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
    411         $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
     418        // Standardize newline characters to "\n".
     419        $pee = str_replace(array("\r\n", "\r"), "\n", $pee);
    412420
     421        // Collapse line breaks before and after <option> elements so they don't get autop'd.
    413422        if ( strpos( $pee, '<option' ) !== false ) {
    414                 // no P/BR around option
    415423                $pee = preg_replace( '|\s*<option|', '<option', $pee );
    416424                $pee = preg_replace( '|</option>\s*|', '</option>', $pee );
    417425        }
    418426
     427        /*
     428         * Collapse line breaks inside <object> elements, and before <param> and <embed> elements
     429         * so they don't get autop'd.
     430         */
    419431        if ( strpos( $pee, '</object>' ) !== false ) {
    420                 // no P/BR around param and embed
    421432                $pee = preg_replace( '|(<object[^>]*>)\s*|', '$1', $pee );
    422433                $pee = preg_replace( '|\s*</object>|', '</object>', $pee );
    423434                $pee = preg_replace( '%\s*(</?(?:param|embed)[^>]*>)\s*%', '$1', $pee );
    424435        }
    425436
     437        /*
     438         * Collapse line breaks inside <audio> and <video> elements.
     439         * Collapse line breaks before and after <source> and <track> elements.
     440         */
    426441        if ( strpos( $pee, '<source' ) !== false || strpos( $pee, '<track' ) !== false ) {
    427                 // no P/BR around source and track
     442
    428443                $pee = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $pee );
    429444                $pee = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $pee );
    430445                $pee = preg_replace( '%\s*(<(?:source|track)[^>]*>)\s*%', '$1', $pee );
    431446        }
    432447
    433         $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
    434         // make paragraphs, including one at the end
     448        // Remove duplicate, contiguous double-line breaks.
     449        $pee = preg_replace("/\n\n+/", "\n\n", $pee);
     450
     451        // Split up the contents into an array of bits, separated by double line breaks.
    435452        $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
     453
    436454        $pee = '';
    437 
     455        // Rebuild the content as a string, wrapping every bit with a <p>.
    438456        foreach ( $pees as $tinkle ) {
    439457                $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
    440458        }
     459        // Under certain strange conditions it could create a P of entirely whitespace.
     460        $pee = preg_replace('|<p>\s*</p>|', '', $pee);
    441461
    442         $pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
     462        /*
     463         * If a closing <div>, <address>, or <form> tag is on a line with preceding
     464         * text that doesn't include another tag, wrap the preceding text with a <p>.
     465         */
    443466        $pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
    444         $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
    445         $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
     467
     468        // If an opening or closing block element tag is wrapped in a <p>, unwrap it.
     469        $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
     470
     471        // Problem with nested lists, which is probably taken care of the preceding preg_replace().
     472        $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee);
     473
     474        // If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.
    446475        $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
    447476        $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
     477
     478        // If an opening or closing block element tag is preceded by an opening <p> tag, remove it.
    448479        $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
     480
     481        // If an opening or closing block element tag is followed by a closing <p> tag, remove it.
    449482        $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
    450483
     484        // Optionally insert line breaks.
    451485        if ( $br ) {
     486                // Replace newlines that shouldn't be touched with a placeholder.
    452487                $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);
    453                 $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
     488                // Replace any new line characters that aren't preceded by a <br /> with a <br />.
     489                $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee);
     490                // Replace newline placeholders with newlines.
    454491                $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
    455492        }
    456493
     494        // If a <br /> tag is after an opening or closing block tag, remove it.
    457495        $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
     496        // If a <br /> tag is before a subset of opening or closing block tags, remove it.
    458497        $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
    459498        $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
    460499
     500        // Replace placeholder <pre> tags with their original content.
    461501        if ( !empty($pre_tags) )
    462502                $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee);
    463503