Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vide.js : Keep video extension, to preserve params .mp4?ts=12345678 #209

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 35 additions & 9 deletions dist/jquery.vide.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,32 @@
$('<img src="' + path + '.png">').on('load', onLoad);
}

/**
* Add extension if not in path
* @private
* @param {String} path
* @param {String} extension '.mp4'
* @returns {String}
*/
function addExtensionIfNotInPath(path, extension) {

if (path.indexOf(extension) === -1) {
path = path + extension;
}

return path;
}

/**
* Remove extension in path
* @private
* @param {String} path
* @returns {String}
*/
function removeExtension(path) {
return path.replace(/\.\w*$/, '');
}

/**
* Vide constructor
* @param {HTMLElement} element
Expand All @@ -202,11 +228,11 @@

// Remove an extension
if (typeof path === 'string') {
path = path.replace(/\.\w*$/, '');
path = removeExtension(path);
} else if (typeof path === 'object') {
for (var i in path) {
if (path.hasOwnProperty(i)) {
path[i] = path[i].replace(/\.\w*$/, '');
if (path.hasOwnProperty(i) && i === 'poster') {
path[i] = removeExtension(path[i]);
}
}
}
Expand Down Expand Up @@ -266,11 +292,11 @@
poster = path.poster;
} else {
if (path.mp4) {
poster = path.mp4;
poster = removeExtension(path.mp4);
} else if (path.webm) {
poster = path.webm;
poster = removeExtension(path.webm);
} else if (path.ogv) {
poster = path.ogv;
poster = removeExtension(path.ogv);
}
}
}
Expand All @@ -293,15 +319,15 @@

if (typeof path === 'object') {
if (path.mp4) {
sources += '<source src="' + path.mp4 + '.mp4" type="video/mp4">';
sources += '<source src="' + addExtensionIfNotInPath(path.mp4, '.mp4') + '" type="video/mp4">';
}

if (path.webm) {
sources += '<source src="' + path.webm + '.webm" type="video/webm">';
sources += '<source src="' + addExtensionIfNotInPath(path.webm, '.webm') + '" type="video/webm">';
}

if (path.ogv) {
sources += '<source src="' + path.ogv + '.ogv" type="video/ogg">';
sources += '<source src="' + addExtensionIfNotInPath(path.ogv, '.ogv') + '" type="video/ogg">';
}

$video = vide.$video = $('<video>' + sources + '</video>');
Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.vide.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 35 additions & 9 deletions src/jquery.vide.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,32 @@
$('<img src="' + path + '.png">').on('load', onLoad);
}

/**
* Add extension if not in path
* @private
* @param {String} path
* @param {String} extension '.mp4'
* @returns {String}
*/
function addExtensionIfNotInPath(path, extension) {

if (path.indexOf(extension) === -1) {
path = path + extension;
}

return path;
}

/**
* Remove extension in path
* @private
* @param {String} path
* @returns {String}
*/
function removeExtension(path) {
return path.replace(/\.\w*$/, '');
}

/**
* Vide constructor
* @param {HTMLElement} element
Expand All @@ -194,11 +220,11 @@

// Remove an extension
if (typeof path === 'string') {
path = path.replace(/\.\w*$/, '');
path = removeExtension(path);
} else if (typeof path === 'object') {
for (var i in path) {
if (path.hasOwnProperty(i)) {
path[i] = path[i].replace(/\.\w*$/, '');
if (path.hasOwnProperty(i) && i === 'poster') {
path[i] = removeExtension(path[i]);
}
}
}
Expand Down Expand Up @@ -258,11 +284,11 @@
poster = path.poster;
} else {
if (path.mp4) {
poster = path.mp4;
poster = removeExtension(path.mp4);
} else if (path.webm) {
poster = path.webm;
poster = removeExtension(path.webm);
} else if (path.ogv) {
poster = path.ogv;
poster = removeExtension(path.ogv);
}
}
}
Expand All @@ -285,15 +311,15 @@

if (typeof path === 'object') {
if (path.mp4) {
sources += '<source src="' + path.mp4 + '.mp4" type="video/mp4">';
sources += '<source src="' + addExtensionIfNotInPath(path.mp4, '.mp4') + '" type="video/mp4">';
}

if (path.webm) {
sources += '<source src="' + path.webm + '.webm" type="video/webm">';
sources += '<source src="' + addExtensionIfNotInPath(path.webm, '.webm') + '" type="video/webm">';
}

if (path.ogv) {
sources += '<source src="' + path.ogv + '.ogv" type="video/ogg">';
sources += '<source src="' + addExtensionIfNotInPath(path.ogv, '.ogv') + '" type="video/ogg">';
}

$video = vide.$video = $('<video>' + sources + '</video>');
Expand Down
1 change: 1 addition & 0 deletions test/vide.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
<div id="block5"></div>
<div id="block6" data-vide-bg="http://vodkabears.github.io:80/vide/video/ocean"></div>
</div>
<div id="block7" data-vide-bg='{"mp4": "video/ocean.mp4?ts=12345678", "poster": "video/ocean.jpg"}'></div>
</body>
</html>
34 changes: 29 additions & 5 deletions test/vide_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
var $block4;
var $block5;
var $block6;
var $block7;

QUnit.begin(function() {
$block1 = $('#block1');
Expand All @@ -34,6 +35,7 @@
$block4 = $('#block4');
$block5 = $('#block5');
$block6 = $('#block6');
$block7 = $('#block7');
});

QUnit.test('Initialization', function() {
Expand All @@ -53,6 +55,7 @@
ok($block4.data('vide'));
ok($block5.data('vide'));
ok($block6.data('vide'));
ok($block7.data('vide'));
});

QUnit.test('Default settings', function() {
Expand All @@ -77,7 +80,7 @@
QUnit.test('Parsing of the path with multiple names', function() {
deepEqual($block3.data('vide').path, {
mp4: 'http://vodkabears.github.io/vide/video/ocean',
webm: 'video/ocean',
webm: 'video/ocean.webm',
ogv: 'http://vodkabears.github.io:80/vide/video/ocean',
poster: 'video/ocean'
});
Expand Down Expand Up @@ -106,8 +109,8 @@

deepEqual(inst.path, {
mp4: 'http://vodkabears.github.io/vide/video/ocean',
webm: 'video/ocean',
ogv: 'http://vodkabears.github.io:80/vide/video/ocean',
webm: 'video/ocean.webm',
ogv: 'http://vodkabears.github.io:80/vide/video/ocean.ogv',
poster: 'video/ocean'
});

Expand All @@ -120,7 +123,7 @@

deepEqual(inst.path, {
mp4: 'video/ocean',
webm: 'video/ocean',
webm: 'video/ocean.webm',
ogv: 'video/ocean',
poster: 'video/ocean'
});
Expand Down Expand Up @@ -160,6 +163,23 @@
strictEqual($wrapper.css('background-position'), video.style.left + ' ' + video.style.top);
});

QUnit.test('Path extension detection', function() {
var inst4 = $block4.data('vide');
var $wrapper4 = inst4.$wrapper;
var inst7 = $block7.data('vide');
var $wrapper7 = inst7.$wrapper;

// Test without extention
strictEqual(inst4.path.mp4, 'http://vodkabears.github.io/vide/video/ocean');
strictEqual($wrapper4.find('source').filter(function() {
return $(this).attr('type') === 'video/mp4';
}).attr('src'), inst4.path.mp4 + '.mp4');

// Test with extention
strictEqual(inst7.path.mp4, 'video/ocean.mp4?ts=12345678');
strictEqual($wrapper7.find('source').attr('src'), inst7.path.mp4);
});

QUnit.test('Re-initialization', function() {
$block1.vide('video/ocean');
$block2.vide('video/ocean');
Expand All @@ -177,7 +197,8 @@
ok($block4.data('vide'));
ok($block5.data('vide'));
ok($block6.data('vide'));
equal(count, 6);
ok($block7.data('vide'));
equal(count, 7);
});

QUnit.test('getVideoObject() method', function() {
Expand All @@ -187,6 +208,7 @@
ok($block4.data('vide').getVideoObject());
ok($block5.data('vide').getVideoObject());
ok($block6.data('vide').getVideoObject());
ok($block7.data('vide').getVideoObject());
});

QUnit.test('resize() method', function() {
Expand Down Expand Up @@ -217,6 +239,7 @@
$block4.data('vide').destroy();
$block5.data('vide').destroy();
$block6.data('vide').destroy();
$block7.data('vide').destroy();

var count = $.vide.lookup.filter(function(value) {
return value !== undefined;
Expand All @@ -229,6 +252,7 @@
strictEqual($block4.find('video').length, 0);
strictEqual($block5.find('video').length, 0);
strictEqual($block6.find('video').length, 0);
strictEqual($block7.find('video').length, 0);
});

}(window.jQuery));