Changeset 289867 in webkit


Ignore:
Timestamp:
Feb 15, 2022, 5:34:23 PM (4 years ago)
Author:
Jon Davis
Message:

Fix WordPress wpautop to account for the Dialog element.
https://bugs.webkit.org/show_bug.cgi?id=236320

Reviewed by Darin Adler.

  • wp-content/themes/webkit/functions.php:
Location:
trunk/Websites/webkit.org
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Websites/webkit.org/ChangeLog

    r289230 r289867  
     12022-02-15  Jon Davis  <jond@apple.com>
     2
     3        Fix WordPress wpautop to account for the Dialog element.
     4        https://bugs.webkit.org/show_bug.cgi?id=236320
     5
     6        Reviewed by Darin Adler.
     7
     8        * wp-content/themes/webkit/functions.php:
     9
    1102022-02-07  Tim Nguyen  <ntim@apple.com>
    211
  • trunk/Websites/webkit.org/wp-content/themes/webkit/functions.php

    r254639 r289867  
    672672
    673673}
     674
     675/** Workaround to support <dialog> tags by customizing the built-in wpautop() function **/
     676remove_filter('the_content', 'wpautop');
     677add_filter('the_content', function ($pee, $br = true) {
     678/**
     679 * Replaces double line breaks with paragraph elements.
     680 *
     681 * A group of regex replaces used to identify text formatted with newlines and
     682 * replace double line breaks with HTML paragraph tags. The remaining line breaks
     683 * after conversion become <<br />> tags, unless $br is set to '0' or 'false'.
     684 *
     685 * @since 0.71
     686 *
     687 * @param string $pee The text which has to be formatted.
     688 * @param bool   $br  Optional. If set, this will convert all remaining line breaks
     689 *                    after paragraphing. Line breaks within `<script>`, `<style>`,
     690 *                    and `<svg>` tags are not affected. Default true.
     691 * @return string Text which has been converted into correct paragraph tags.
     692 */
     693    $pre_tags = array();
     694
     695    if ( trim( $pee ) === '' ) {
     696        return '';
     697    }
     698
     699    // Just to make things a little easier, pad the end.
     700    $pee = $pee . "\n";
     701
     702    /*
     703     * Pre tags shouldn't be touched by autop.
     704     * Replace pre tags with placeholders and bring them back after autop.
     705     */
     706    if ( strpos( $pee, '<pre' ) !== false ) {
     707        $pee_parts = explode( '</pre>', $pee );
     708        $last_pee  = array_pop( $pee_parts );
     709        $pee       = '';
     710        $i         = 0;
     711
     712        foreach ( $pee_parts as $pee_part ) {
     713            $start = strpos( $pee_part, '<pre' );
     714
     715            // Malformed HTML?
     716            if ( false === $start ) {
     717                $pee .= $pee_part;
     718                continue;
     719            }
     720
     721            $name              = "<pre wp-pre-tag-$i></pre>";
     722            $pre_tags[ $name ] = substr( $pee_part, $start ) . '</pre>';
     723
     724            $pee .= substr( $pee_part, 0, $start ) . $name;
     725            $i++;
     726        }
     727
     728        $pee .= $last_pee;
     729    }
     730    // Change multiple <br>'s into two line breaks, which will turn into paragraphs.
     731    $pee = preg_replace( '|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee );
     732
     733    $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|dialog)';
     734
     735    // Add a double line break above block-level opening tags.
     736    $pee = preg_replace( '!(<' . $allblocks . '[\s/>])!', "\n\n$1", $pee );
     737
     738    // Add a double line break below block-level closing tags.
     739    $pee = preg_replace( '!(</' . $allblocks . '>)!', "$1\n\n", $pee );
     740
     741    // Add a double line break after hr tags, which are self closing.
     742    $pee = preg_replace( '!(<hr\s*?/?>)!', "$1\n\n", $pee );
     743
     744    // Standardize newline characters to "\n".
     745    $pee = str_replace( array( "\r\n", "\r" ), "\n", $pee );
     746
     747    // Find newlines in all elements and add placeholders.
     748    $pee = wp_replace_in_html_tags( $pee, array( "\n" => ' <!-- wpnl --> ' ) );
     749
     750    // Collapse line breaks before and after <option> elements so they don't get autop'd.
     751    if ( strpos( $pee, '<option' ) !== false ) {
     752        $pee = preg_replace( '|\s*<option|', '<option', $pee );
     753        $pee = preg_replace( '|</option>\s*|', '</option>', $pee );
     754    }
     755
     756    /*
     757     * Collapse line breaks inside <object> elements, before <param> and <embed> elements
     758     * so they don't get autop'd.
     759     */
     760    if ( strpos( $pee, '</object>' ) !== false ) {
     761        $pee = preg_replace( '|(<object[^>]*>)\s*|', '$1', $pee );
     762        $pee = preg_replace( '|\s*</object>|', '</object>', $pee );
     763        $pee = preg_replace( '%\s*(</?(?:param|embed)[^>]*>)\s*%', '$1', $pee );
     764    }
     765
     766    /*
     767     * Collapse line breaks inside <audio> and <video> elements,
     768     * before and after <source> and <track> elements.
     769     */
     770    if ( strpos( $pee, '<source' ) !== false || strpos( $pee, '<track' ) !== false ) {
     771        $pee = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $pee );
     772        $pee = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $pee );
     773        $pee = preg_replace( '%\s*(<(?:source|track)[^>]*>)\s*%', '$1', $pee );
     774    }
     775
     776    // Collapse line breaks before and after <figcaption> elements.
     777    if ( strpos( $pee, '<figcaption' ) !== false ) {
     778        $pee = preg_replace( '|\s*(<figcaption[^>]*>)|', '$1', $pee );
     779        $pee = preg_replace( '|</figcaption>\s*|', '</figcaption>', $pee );
     780    }
     781
     782    // Remove more than two contiguous line breaks.
     783    $pee = preg_replace( "/\n\n+/", "\n\n", $pee );
     784
     785    // Split up the contents into an array of strings, separated by double line breaks.
     786    $pees = preg_split( '/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY );
     787
     788    // Reset $pee prior to rebuilding.
     789    $pee = '';
     790
     791    // Rebuild the content as a string, wrapping every bit with a <p>.
     792    foreach ( $pees as $tinkle ) {
     793        $pee .= '<p>' . trim( $tinkle, "\n" ) . "</p>\n";
     794    }
     795
     796    // Under certain strange conditions it could create a P of entirely whitespace.
     797    $pee = preg_replace( '|<p>\s*</p>|', '', $pee );
     798
     799    // Add a closing <p> inside <div>, <address>, or <form> tag if missing.
     800    $pee = preg_replace( '!<p>([^<]+)</(div|address|form)>!', '<p>$1</p></$2>', $pee );
     801
     802    // If an opening or closing block element tag is wrapped in a <p>, unwrap it.
     803    $pee = preg_replace( '!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', '$1', $pee );
     804
     805    // In some cases <li> may get wrapped in <p>, fix them.
     806    $pee = preg_replace( '|<p>(<li.+?)</p>|', '$1', $pee );
     807
     808    // If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.
     809    $pee = preg_replace( '|<p><blockquote([^>]*)>|i', '<blockquote$1><p>', $pee );
     810    $pee = str_replace( '</blockquote></p>', '</p></blockquote>', $pee );
     811
     812    // If an opening or closing block element tag is preceded by an opening <p> tag, remove it.
     813    $pee = preg_replace( '!<p>\s*(</?' . $allblocks . '[^>]*>)!', '$1', $pee );
     814
     815    // If an opening or closing block element tag is followed by a closing <p> tag, remove it.
     816    $pee = preg_replace( '!(</?' . $allblocks . '[^>]*>)\s*</p>!', '$1', $pee );
     817
     818    // Optionally insert line breaks.
     819    if ( $br ) {
     820        // Replace newlines that shouldn't be touched with a placeholder.
     821        $pee = preg_replace_callback( '/<(script|style|svg).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee );
     822
     823        // Normalize <br>
     824        $pee = str_replace( array( '<br>', '<br/>' ), '<br />', $pee );
     825
     826        // Replace any new line characters that aren't preceded by a <br /> with a <br />.
     827        $pee = preg_replace( '|(?<!<br />)\s*\n|', "<br />\n", $pee );
     828
     829        // Replace newline placeholders with newlines.
     830        $pee = str_replace( '<WPPreserveNewline />', "\n", $pee );
     831    }
     832
     833    // If a <br /> tag is after an opening or closing block tag, remove it.
     834    $pee = preg_replace( '!(</?' . $allblocks . '[^>]*>)\s*<br />!', '$1', $pee );
     835
     836    // If a <br /> tag is before a subset of opening or closing block tags, remove it.
     837    $pee = preg_replace( '!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee );
     838    $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
     839
     840    // Replace placeholder <pre> tags with their original content.
     841    if ( ! empty( $pre_tags ) ) {
     842        $pee = str_replace( array_keys( $pre_tags ), array_values( $pre_tags ), $pee );
     843    }
     844
     845    // Restore newlines in all elements.
     846    if ( false !== strpos( $pee, '<!-- wpnl -->' ) ) {
     847        $pee = str_replace( array( ' <!-- wpnl --> ', '<!-- wpnl -->' ), "\n", $pee );
     848    }
     849
     850    return $pee;
     851});
Note: See TracChangeset for help on using the changeset viewer.