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

git-svn-id: https://develop.svn.wordpress.org/trunk@27094 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz 2014-02-05 00:45:53 +00:00
parent b33135c08d
commit 6a0251847b
3 changed files with 226 additions and 4 deletions

View File

@ -238,16 +238,30 @@ function wpautop($pee, $br = true) {
$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);
if ( strpos( $pee, '</object>' ) !== false ) {
// no P/BR around param and embed
$pee = preg_replace( '|(<object[^>]*>)\s*|', '$1', $pee );
$pee = preg_replace( '|\s*</object>|', '</object>', $pee );
$pee = preg_replace( '%\s*(</?(?:param|embed)[^>]*>)\s*%', '$1', $pee );
}
if ( strpos( $pee, '<source' ) !== false || strpos( $pee, '<track' ) !== false ) {
// no P/BR around source and track
$pee = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $pee );
$pee = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $pee );
$pee = preg_replace( '%\s*(<(?:source|track)[^>]*>)\s*%', '$1', $pee );
}
$pee = preg_replace("/\n\n+/", "\n\n", $pee); // 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 )
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
@ -256,11 +270,13 @@ function wpautop($pee, $br = true) {
$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
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);
}
$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 );

View File

@ -98,4 +98,169 @@ PS. Not yet subscribed for update notifications? <a href="%1$s" title="Subscri
$str = 'Username: <input type="text" id="username" name="username" /><br />Password: <input type="password" id="password1" name="password1" />';
$this->assertEquals( "<p>$str</p>", trim( wpautop( $str ) ) );
}
/**
* wpautop() Should not add <p> and <br/> around <source> and <track>
*
* @ticket 26864
*/
public function test_source_track_elements() {
$content = "Paragraph one.\n\n" .
'<video class="wp-video-shortcode" id="video-0-1" width="640" height="360" preload="metadata" controls="controls">
<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4" />
<!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
<source type="video/webm" src="myvideo.webm" />
<!-- Ogg/Vorbis for older Firefox and Opera versions -->
<source type="video/ogg" src="myvideo.ogv" />
<!-- Optional: Add subtitles for each language -->
<track kind="subtitles" src="subtitles.srt" srclang="en" />
<!-- Optional: Add chapters -->
<track kind="chapters" src="chapters.srt" srclang="en" />
<a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a>
</video>' .
"\n\nParagraph two.";
$content2 = "Paragraph one.\n\n" .
'<video class="wp-video-shortcode" id="video-0-1" width="640" height="360" preload="metadata" controls="controls">
<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4" />
<!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
<source type="video/webm" src="myvideo.webm" />
<!-- Ogg/Vorbis for older Firefox and Opera versions -->
<source type="video/ogg" src="myvideo.ogv" />
<!-- Optional: Add subtitles for each language -->
<track kind="subtitles" src="subtitles.srt" srclang="en" />
<!-- Optional: Add chapters -->
<track kind="chapters" src="chapters.srt" srclang="en" />
<a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a>
</video>' .
"\n\nParagraph two.";
$expected = "<p>Paragraph one.</p>\n" . // line breaks only after <p>
'<p><video class="wp-video-shortcode" id="video-0-1" width="640" height="360" preload="metadata" controls="controls">' .
'<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4" />' .
'<!-- WebM/VP8 for Firefox4, Opera, and Chrome -->' .
'<source type="video/webm" src="myvideo.webm" />' .
'<!-- Ogg/Vorbis for older Firefox and Opera versions -->' .
'<source type="video/ogg" src="myvideo.ogv" />' .
'<!-- Optional: Add subtitles for each language -->' .
'<track kind="subtitles" src="subtitles.srt" srclang="en" />' .
'<!-- Optional: Add chapters -->' .
'<track kind="chapters" src="chapters.srt" srclang="en" />' .
'<a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">' .
"http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a></video></p>\n" .
'<p>Paragraph two.</p>';
// When running the content through wpautop() from wp_richedit_pre()
$shortcode_content = "Paragraph one.\n\n" .
'[video width="720" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]
<!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
<source type="video/webm" src="myvideo.webm" />
<!-- Ogg/Vorbis for older Firefox and Opera versions -->
<source type="video/ogg" src="myvideo.ogv" />
<!-- Optional: Add subtitles for each language -->
<track kind="subtitles" src="subtitles.srt" srclang="en" />
<!-- Optional: Add chapters -->
<track kind="chapters" src="chapters.srt" srclang="en" />
[/video]' .
"\n\nParagraph two.";
$shortcode_expected = "<p>Paragraph one.</p>\n" . // line breaks only after <p>
'<p>[video width="720" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]' .
'<!-- WebM/VP8 for Firefox4, Opera, and Chrome --><source type="video/webm" src="myvideo.webm" />' .
'<!-- Ogg/Vorbis for older Firefox and Opera versions --><source type="video/ogg" src="myvideo.ogv" />' .
'<!-- Optional: Add subtitles for each language --><track kind="subtitles" src="subtitles.srt" srclang="en" />' .
'<!-- Optional: Add chapters --><track kind="chapters" src="chapters.srt" srclang="en" />' .
"[/video]</p>\n" .
'<p>Paragraph two.</p>';
$this->assertEquals( $expected, trim( wpautop( $content ) ) );
$this->assertEquals( $expected, trim( wpautop( $content2 ) ) );
$this->assertEquals( $shortcode_expected, trim( wpautop( $shortcode_content ) ) );
}
/**
* wpautop() Should not add <p> and <br/> around <param> and <embed>
*
* @ticket 26864
*/
public function test_param_embed_elements() {
$content1 = '
Paragraph one.
<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">
<param name="src" value="http://domain.tld/wp-content/uploads/2013/12/xyz.swf" />
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="overstretch" value="true" />
<param name="flashvars" value="isDynamicSeeking=true" />
<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" />
</object>
Paragraph two.';
$expected1 = "<p>Paragraph one.</p>\n" . // line breaks only after <p>
'<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">' .
'<param name="src" value="http://domain.tld/wp-content/uploads/2013/12/xyz.swf" />' .
'<param name="allowfullscreen" value="true" />' .
'<param name="allowscriptaccess" value="always" />' .
'<param name="overstretch" value="true" />' .
'<param name="flashvars" value="isDynamicSeeking=true" />' .
'<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" />' .
"</object></p>\n" .
'<p>Paragraph two.</p>';
$content2 = '
Paragraph one.
<div class="video-player" id="x-video-0">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="640" height="360" id="video-0" standby="Standby text">
<param name="movie" value="http://domain.tld/wp-content/uploads/2013/12/xyz.swf" />
<param name="quality" value="best" />
<param name="seamlesstabbing" value="true" />
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="overstretch" value="true" />
<!--[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">
<param name="quality" value="best" />
<param name="seamlesstabbing" value="true" />
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="overstretch" value="true" />
</object><!--<![endif]-->
</object></div>
Paragraph two.';
$expected2 = "<p>Paragraph one.</p>\n" . // line breaks only after block tags
'<div class="video-player" id="x-video-0">' . "\n" .
'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="640" height="360" id="video-0" standby="Standby text">' .
'<param name="movie" value="http://domain.tld/wp-content/uploads/2013/12/xyz.swf" />' .
'<param name="quality" value="best" />' .
'<param name="seamlesstabbing" value="true" />' .
'<param name="allowfullscreen" value="true" />' .
'<param name="allowscriptaccess" value="always" />' .
'<param name="overstretch" value="true" />' .
'<!--[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">' .
'<param name="quality" value="best" />' .
'<param name="seamlesstabbing" value="true" />' .
'<param name="allowfullscreen" value="true" />' .
'<param name="allowscriptaccess" value="always" />' .
'<param name="overstretch" value="true" /></object><!--<![endif]-->' .
"</object></div>\n" .
'<p>Paragraph two.</p>';
$this->assertEquals( $expected1, trim( wpautop( $content1 ) ) );
$this->assertEquals( $expected2, trim( wpautop( $content2 ) ) );
}
}

View File

@ -364,4 +364,45 @@ CONTENT;
$matches = get_media_embedded_in_content( $content, $types );
$this->assertEquals( $contents, $matches );
}
/**
* Test [video] shortcode processing
*
* @ticket 26864
*/
function test_video_shortcode_body() {
$width = 720;
$height = 480;
$video =<<<VIDEO
[video width="720" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]
<!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
<source type="video/webm" src="myvideo.webm" />
<!-- Ogg/Vorbis for older Firefox and Opera versions -->
<source type="video/ogg" src="myvideo.ogv" />
<!-- Optional: Add subtitles for each language -->
<track kind="subtitles" src="subtitles.srt" srclang="en" />
<!-- Optional: Add chapters -->
<track kind="chapters" src="chapters.srt" srclang="en" />
[/video]
VIDEO;
$w = $GLOBALS['content_width'];
$h = ceil( ( $height * $w ) / $width );
$content = apply_filters( 'the_content', $video );
$expected = '<div style="width: ' . $w . 'px; max-width: 100%;" class="wp-video">' .
"<!--[if lt IE 9]><script>document.createElement(\'video\');</script><![endif]-->\n" .
'<video class="wp-video-shortcode" id="video-0-1" width="' . $w . '" height="' . $h . '" preload="metadata" controls="controls">' .
'<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4" />' .
'<!-- WebM/VP8 for Firefox4, Opera, and Chrome --><source type="video/webm" src="myvideo.webm" />' .
'<!-- Ogg/Vorbis for older Firefox and Opera versions --><source type="video/ogg" src="myvideo.ogv" />' .
'<!-- Optional: Add subtitles for each language --><track kind="subtitles" src="subtitles.srt" srclang="en" />' .
'<!-- Optional: Add chapters --><track kind="chapters" src="chapters.srt" srclang="en" />' .
'<a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">' .
"http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a></video></div>\n";
$this->assertEquals( $expected, $content );
}
}