WordPress.org

Make WordPress Core

Changeset 27094


Ignore:
Timestamp:
02/05/2014 12:45:53 AM (7 years ago)
Author:
azaozz
Message:

Consolidate handling of <object>, <audio> and <video> in wpautop() and add unit tests for them. Part props wonderboymusic, see #26864

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r27042 r27094  
    239239    $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
    240240    $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    241     if ( strpos($pee, '<object') !== false ) {
    242         $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
    243         $pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
    244     }
     241
     242    if ( strpos( $pee, '</object>' ) !== false ) {
     243        // no P/BR around param and embed
     244        $pee = preg_replace( '|(<object[^>]*>)\s*|', '$1', $pee );
     245        $pee = preg_replace( '|\s*</object>|', '</object>', $pee );
     246        $pee = preg_replace( '%\s*(</?(?:param|embed)[^>]*>)\s*%', '$1', $pee );
     247    }
     248
     249    if ( strpos( $pee, '<source' ) !== false || strpos( $pee, '<track' ) !== false ) {
     250        // no P/BR around source and track
     251        $pee = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $pee );
     252        $pee = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $pee );
     253        $pee = preg_replace( '%\s*(<(?:source|track)[^>]*>)\s*%', '$1', $pee );
     254    }
     255
    245256    $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
    246257    // make paragraphs, including one at the end
    247258    $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY);
    248259    $pee = '';
    249     foreach ( $pees as $tinkle )
     260
     261    foreach ( $pees as $tinkle ) {
    250262        $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
     263    }
     264
    251265    $pee = preg_replace('|<p>\s*</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
    252266    $pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee);
     
    257271    $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
    258272    $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
     273
    259274    if ( $br ) {
    260275        $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);
     
    262277        $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
    263278    }
     279
    264280    $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
    265281    $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
  • trunk/tests/phpunit/tests/formatting/Autop.php

    r25002 r27094  
    9999        $this->assertEquals( "<p>$str</p>", trim( wpautop( $str ) ) );
    100100    }
     101
     102    /**
     103     * wpautop() Should not add <p> and <br/> around <source> and <track>
     104     *
     105     * @ticket 26864
     106     */
     107    public function test_source_track_elements() {
     108        $content = "Paragraph one.\n\n" .
     109            '<video class="wp-video-shortcode" id="video-0-1" width="640" height="360" preload="metadata" controls="controls">
     110                <source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4" />
     111                <!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
     112                <source type="video/webm" src="myvideo.webm" />
     113                <!-- Ogg/Vorbis for older Firefox and Opera versions -->
     114                <source type="video/ogg" src="myvideo.ogv" />
     115                <!-- Optional: Add subtitles for each language -->
     116                <track kind="subtitles" src="subtitles.srt" srclang="en" />
     117                <!-- Optional: Add chapters -->
     118                <track kind="chapters" src="chapters.srt" srclang="en" />
     119                <a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a>
     120            </video>' .
     121            "\n\nParagraph two.";
     122
     123        $content2 = "Paragraph one.\n\n" .
     124            '<video class="wp-video-shortcode" id="video-0-1" width="640" height="360" preload="metadata" controls="controls">
     125
     126            <source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4" />
     127
     128            <!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
     129            <source type="video/webm" src="myvideo.webm" />
     130
     131            <!-- Ogg/Vorbis for older Firefox and Opera versions -->
     132            <source type="video/ogg" src="myvideo.ogv" />
     133
     134            <!-- Optional: Add subtitles for each language -->
     135            <track kind="subtitles" src="subtitles.srt" srclang="en" />
     136
     137            <!-- Optional: Add chapters -->
     138            <track kind="chapters" src="chapters.srt" srclang="en" />
     139
     140            <a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a>
     141
     142            </video>' .
     143            "\n\nParagraph two.";
     144
     145        $expected = "<p>Paragraph one.</p>\n" . // line breaks only after <p>
     146            '<p><video class="wp-video-shortcode" id="video-0-1" width="640" height="360" preload="metadata" controls="controls">' .
     147            '<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4" />' .
     148            '<!-- WebM/VP8 for Firefox4, Opera, and Chrome -->' .
     149            '<source type="video/webm" src="myvideo.webm" />' .
     150            '<!-- Ogg/Vorbis for older Firefox and Opera versions -->' .
     151            '<source type="video/ogg" src="myvideo.ogv" />' .
     152            '<!-- Optional: Add subtitles for each language -->' .
     153            '<track kind="subtitles" src="subtitles.srt" srclang="en" />' .
     154            '<!-- Optional: Add chapters -->' .
     155            '<track kind="chapters" src="chapters.srt" srclang="en" />' .
     156            '<a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">' .
     157            "http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a></video></p>\n" .
     158            '<p>Paragraph two.</p>';
     159
     160        // When running the content through wpautop() from wp_richedit_pre()
     161        $shortcode_content = "Paragraph one.\n\n" .
     162            '[video width="720" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]
     163            <!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
     164            <source type="video/webm" src="myvideo.webm" />
     165            <!-- Ogg/Vorbis for older Firefox and Opera versions -->
     166            <source type="video/ogg" src="myvideo.ogv" />
     167            <!-- Optional: Add subtitles for each language -->
     168            <track kind="subtitles" src="subtitles.srt" srclang="en" />
     169            <!-- Optional: Add chapters -->
     170            <track kind="chapters" src="chapters.srt" srclang="en" />
     171            [/video]' .
     172            "\n\nParagraph two.";
     173
     174        $shortcode_expected = "<p>Paragraph one.</p>\n" . // line breaks only after <p>
     175            '<p>[video width="720" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]' .
     176            '<!-- WebM/VP8 for Firefox4, Opera, and Chrome --><source type="video/webm" src="myvideo.webm" />' .
     177            '<!-- Ogg/Vorbis for older Firefox and Opera versions --><source type="video/ogg" src="myvideo.ogv" />' .
     178            '<!-- Optional: Add subtitles for each language --><track kind="subtitles" src="subtitles.srt" srclang="en" />' .
     179            '<!-- Optional: Add chapters --><track kind="chapters" src="chapters.srt" srclang="en" />' .
     180            "[/video]</p>\n" .
     181            '<p>Paragraph two.</p>';
     182
     183        $this->assertEquals( $expected, trim( wpautop( $content ) ) );
     184        $this->assertEquals( $expected, trim( wpautop( $content2 ) ) );
     185        $this->assertEquals( $shortcode_expected, trim( wpautop( $shortcode_content ) ) );
     186    }
     187
     188    /**
     189     * wpautop() Should not add <p> and <br/> around <param> and <embed>
     190     *
     191     * @ticket 26864
     192     */
     193    public function test_param_embed_elements() {
     194        $content1 = '
     195Paragraph one.
     196
     197<object width="400" height="224" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
     198    <param name="src" value="http://domain.tld/wp-content/uploads/2013/12/xyz.swf" />
     199    <param name="allowfullscreen" value="true" />
     200    <param name="allowscriptaccess" value="always" />
     201    <param name="overstretch" value="true" />
     202    <param name="flashvars" value="isDynamicSeeking=true" />
     203
     204    <embed width="400" height="224" type="application/x-shockwave-flash" src="http://domain.tld/wp-content/uploads/2013/12/xyz.swf" wmode="direct" seamlesstabbing="true" allowfullscreen="true" overstretch="true" flashvars="isDynamicSeeking=true" />
     205</object>
     206
     207Paragraph two.';
     208
     209        $expected1 = "<p>Paragraph one.</p>\n" . // line breaks only after <p>
     210            '<p><object width="400" height="224" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">' .
     211            '<param name="src" value="http://domain.tld/wp-content/uploads/2013/12/xyz.swf" />' .
     212            '<param name="allowfullscreen" value="true" />' .
     213            '<param name="allowscriptaccess" value="always" />' .
     214            '<param name="overstretch" value="true" />' .
     215            '<param name="flashvars" value="isDynamicSeeking=true" />' .
     216            '<embed width="400" height="224" type="application/x-shockwave-flash" src="http://domain.tld/wp-content/uploads/2013/12/xyz.swf" wmode="direct" seamlesstabbing="true" allowfullscreen="true" overstretch="true" flashvars="isDynamicSeeking=true" />' .
     217            "</object></p>\n" .
     218            '<p>Paragraph two.</p>';
     219
     220        $content2 = '
     221Paragraph one.
     222
     223<div class="video-player" id="x-video-0">
     224<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="640" height="360" id="video-0" standby="Standby text">
     225  <param name="movie" value="http://domain.tld/wp-content/uploads/2013/12/xyz.swf" />
     226  <param name="quality" value="best" />
     227
     228  <param name="seamlesstabbing" value="true" />
     229  <param name="allowfullscreen" value="true" />
     230  <param name="allowscriptaccess" value="always" />
     231  <param name="overstretch" value="true" />
     232
     233  <!--[if !IE]--><object type="application/x-shockwave-flash" data="http://domain.tld/wp-content/uploads/2013/12/xyz.swf" width="640" height="360" standby="Standby text">
     234    <param name="quality" value="best" />
     235
     236    <param name="seamlesstabbing" value="true" />
     237    <param name="allowfullscreen" value="true" />
     238    <param name="allowscriptaccess" value="always" />
     239    <param name="overstretch" value="true" />
     240  </object><!--<![endif]-->
     241</object></div>
     242
     243Paragraph two.';
     244
     245        $expected2 = "<p>Paragraph one.</p>\n" . // line breaks only after block tags
     246            '<div class="video-player" id="x-video-0">' . "\n" .
     247            '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="640" height="360" id="video-0" standby="Standby text">' .
     248            '<param name="movie" value="http://domain.tld/wp-content/uploads/2013/12/xyz.swf" />' .
     249            '<param name="quality" value="best" />' .
     250            '<param name="seamlesstabbing" value="true" />' .
     251            '<param name="allowfullscreen" value="true" />' .
     252            '<param name="allowscriptaccess" value="always" />' .
     253            '<param name="overstretch" value="true" />' .
     254            '<!--[if !IE]--><object type="application/x-shockwave-flash" data="http://domain.tld/wp-content/uploads/2013/12/xyz.swf" width="640" height="360" standby="Standby text">' .
     255            '<param name="quality" value="best" />' .
     256            '<param name="seamlesstabbing" value="true" />' .
     257            '<param name="allowfullscreen" value="true" />' .
     258            '<param name="allowscriptaccess" value="always" />' .
     259            '<param name="overstretch" value="true" /></object><!--<![endif]-->' .
     260            "</object></div>\n" .
     261            '<p>Paragraph two.</p>';
     262
     263        $this->assertEquals( $expected1, trim( wpautop( $content1 ) ) );
     264        $this->assertEquals( $expected2, trim( wpautop( $content2 ) ) );
     265    }
    101266}
  • trunk/tests/phpunit/tests/media.php

    r26978 r27094  
    365365        $this->assertEquals( $contents, $matches );
    366366    }
     367
     368    /**
     369     * Test [video] shortcode processing
     370     *
     371     * @ticket 26864
     372     */
     373    function test_video_shortcode_body() {
     374        $width = 720;
     375        $height = 480;
     376
     377        $video =<<<VIDEO
     378[video width="720" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]
     379<!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
     380<source type="video/webm" src="myvideo.webm" />
     381<!-- Ogg/Vorbis for older Firefox and Opera versions -->
     382<source type="video/ogg" src="myvideo.ogv" />
     383<!-- Optional: Add subtitles for each language -->
     384<track kind="subtitles" src="subtitles.srt" srclang="en" />
     385<!-- Optional: Add chapters -->
     386<track kind="chapters" src="chapters.srt" srclang="en" />
     387[/video]
     388VIDEO;
     389
     390        $w = $GLOBALS['content_width'];
     391        $h = ceil( ( $height * $w ) / $width );
     392
     393        $content = apply_filters( 'the_content', $video );
     394
     395        $expected = '<div style="width: ' . $w . 'px; max-width: 100%;" class="wp-video">' .
     396            "<!--[if lt IE 9]><script>document.createElement(\'video\');</script><![endif]-->\n" .
     397            '<video class="wp-video-shortcode" id="video-0-1" width="' . $w . '" height="' . $h . '" preload="metadata" controls="controls">' .
     398            '<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4" />' .
     399            '<!-- WebM/VP8 for Firefox4, Opera, and Chrome --><source type="video/webm" src="myvideo.webm" />' .
     400            '<!-- Ogg/Vorbis for older Firefox and Opera versions --><source type="video/ogg" src="myvideo.ogv" />' .
     401            '<!-- Optional: Add subtitles for each language --><track kind="subtitles" src="subtitles.srt" srclang="en" />' .
     402            '<!-- Optional: Add chapters --><track kind="chapters" src="chapters.srt" srclang="en" />' .
     403            '<a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">' .
     404            "http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a></video></div>\n";
     405
     406        $this->assertEquals( $expected, $content );
     407    }
    367408}
Note: See TracChangeset for help on using the changeset viewer.