Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Replacing HtmlHelper::video() with HtmlHelper::media() which can gene…

…rate either audio or video element
  • Loading branch information...
commit c57275636cd8ce98fe4397671d8690560a07b240 1 parent 2f7f00d
@ADmad ADmad authored
View
46 lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
@@ -1492,30 +1492,33 @@ public function testPara() {
}
/**
- * testVideo method
+ * testMedia method
*
* @return void
*/
- public function testVideo() {
- $result = $this->Html->video('video.webm');
- $expected = array('video' => array('src' => 'files/video.webm'));
+ public function testMedia() {
+ $result = $this->Html->media('video.webm');
+ $expected = array('video' => array('src' => 'files/video.webm'), '/video');
$this->assertTags($result, $expected);
- $result = $this->Html->video('video.webm', array(
+ $result = $this->Html->media('video.webm', array(
'text' => 'Your browser does not support the HTML5 Video element.'
));
$expected = array('video' => array('src' => 'files/video.webm'), 'Your browser does not support the HTML5 Video element.', '/video');
$this->assertTags($result, $expected);
- $result = $this->Html->video('video.webm', array('autoload', 'muted' => 'muted'));
- $expected = array('video' => array(
- 'src' => 'files/video.webm',
- 'autoload' => 'autoload',
- 'muted' => 'muted'
- ));
+ $result = $this->Html->media('video.webm', array('autoload', 'muted' => 'muted'));
+ $expected = array(
+ 'video' => array(
+ 'src' => 'files/video.webm',
+ 'autoload' => 'autoload',
+ 'muted' => 'muted'
+ ),
+ '/video'
+ );
$this->assertTags($result, $expected);
- $result = $this->Html->video(
+ $result = $this->Html->media(
array('video.webm', array('src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'")),
array('pathPrefix' => 'videos/', 'poster' => 'poster.jpg', 'text' => 'Your browser does not support the HTML5 Video element.')
);
@@ -1527,6 +1530,25 @@ public function testVideo() {
'/video'
);
$this->assertTags($result, $expected);
+
+ $result = $this->Html->media('video.ogv', array('type' => 'video'));
+ $expected = array('video' => array('src' => 'files/video.ogv'), '/video');
+ $this->assertTags($result, $expected);
+
+ $result = $this->Html->media('audio.mp3');
+ $expected = array('audio' => array('src' => 'files/audio.mp3'), '/audio');
+ $this->assertTags($result, $expected);
+
+ $result = $this->Html->media(
+ array(array('src' => 'video.mov', 'type' => 'video/mp4'), 'video.webm')
+ );
+ $expected = array(
+ '<video',
+ array('source' => array('src' => 'files/video.mov', 'type' => 'video/mp4')),
+ array('source' => array('src' => 'files/video.webm', 'type' => 'video/webm')),
+ '/video'
+ );
+ $this->assertTags($result, $expected);
}
/**
View
65 lib/Cake/View/Helper/HtmlHelper.php
@@ -968,24 +968,32 @@ public function para($class, $text, $options = array()) {
}
/**
- * Returns a VIDEO element
+ * Returns an audio/video element
*
* ### Usage
*
- * Using single video file:
+ * Using an audio file:
*
- * `echo $this->Html->video('video.mp4', array('fullBase' => true, 'text' => 'Fallback text'));`
+ * `echo $this->Html->media('audio.mp3', array('fullBase' => true));`
*
* Outputs:
*
- * `<video src="http://www.somehost.com/files/video.mp4">Fallback text</video>`
+ * `<video src="http://www.somehost.com/files/audio.mp3">Fallback text</video>`
+ *
+ * Using a video file:
+ *
+ * `echo $this->Html->media('video.mp4', array('text' => 'Fallback text'));`
+ *
+ * Outputs:
+ *
+ * `<video src="/files/video.mp4">Fallback text</video>`
*
* Using multiple video files:
*
* {{{
* echo $this->Html->video(
- * array('video.mp4', array('src' => 'video.ogg', 'type' => "video/ogg; codecs='theora, vorbis'")),
- * array('autoplay')
+ * array('video.mp4', array('src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'")),
+ * array('type' => 'video', 'autoplay')
* );
* }}}
*
@@ -994,13 +1002,15 @@ public function para($class, $text, $options = array()) {
* {{{
* <video autoplay="autoplay">
* <source src="/files/video.mp4" type="video/mp4"/>
- * <source src="/files/video.ogg" type="video/ogg; codecs='theora, vorbis'"/>
+ * <source src="/files/video.ogv" type="video/ogv; codecs='theora, vorbis'"/>
* </video>
* }}}
*
* ### Options
*
- * - `text` Text to include inside the video tag
+ * - `type` Type of media element to generate, valid values are "audio" or "video".
+ * If type is not provided media type is guessed based on file's mime type.
+ * - `text` Text to include inside the audio/video tag
* - `pathPrefix` Path prefix to use for relative urls, defaults to 'files/'
* - `fullBase` If provided the src attribute will get a full address including domain name
*
@@ -1009,8 +1019,14 @@ public function para($class, $text, $options = array()) {
* @param array $options Array of HTML attributes, and special options above.
* @return string Generated video tag
*/
- public function video($path, $options = array()) {
- $options += array('pathPrefix' => 'files/', 'text' => '');
+ public function media($path, $options = array()) {
+ $options += array('type' => null, 'pathPrefix' => 'files/', 'text' => '');
+
+ if (!empty($options['type'])) {
+ $type = $options['type'];
+ } else {
+ $type = null;
+ }
if (is_array($path)) {
$response = null;
@@ -1025,7 +1041,15 @@ public function video($path, $options = array()) {
if ($response === null) {
$response = new CakeResponse();
}
- $source['type'] = $response->getMimeType(pathinfo($source['src'], PATHINFO_EXTENSION));
+ $ext = pathinfo($source['src'], PATHINFO_EXTENSION);
+ $source['type'] = $response->getMimeType($ext);
+ }
+ if ($type === null) {
+ if (preg_match('#^video/#', $source['type'])) {
+ $type = 'video';
+ } else {
+ $type = 'audio';
+ }
}
$source['src'] = $this->assetUrl($source['src'], $options);
$sourceTags .= $this->useTag('tagselfclosing', 'source', $source);
@@ -1033,6 +1057,15 @@ public function video($path, $options = array()) {
$options['text'] = $sourceTags . $options['text'];
unset($options['fullBase']);
} else {
+ if ($type === null) {
+ $response = new CakeResponse();
+ $mimeType = $response->getMimeType(pathinfo($path, PATHINFO_EXTENSION));
+ if (preg_match('#^video/#', $mimeType)) {
+ $type = 'video';
+ } else {
+ $type = 'audio';
+ }
+ }
$path = $this->assetUrl($path, $options);
$options['src'] = $path;
}
@@ -1040,11 +1073,15 @@ public function video($path, $options = array()) {
if (isset($options['poster'])) {
$options['poster'] = $this->assetUrl($options['poster'], array('pathPrefix' => IMAGES_URL) + $options);
}
-
$text = $options['text'];
- $options = array_diff_key($options, array('fullBase' => '', 'pathPrefix' => '', 'text' => ''));
- return $this->tag('video', $text, $options);
+ $options = array_diff_key($options, array(
+ 'type' => '',
+ 'fullBase' => '',
+ 'pathPrefix' => '',
+ 'text' => ''
+ ));
+ return $this->tag($type, $text, $options);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.