Skip to content

Commit

Permalink
Fixed type support checking for an empty src string
Browse files Browse the repository at this point in the history
  • Loading branch information
heff committed Jan 12, 2015
1 parent a3b12d9 commit dd4fb07
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/js/media/html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,13 @@ vjs.Html5.nativeSourceHandler = {};
* @return {String} 'probably', 'maybe', or '' (empty string)
*/
vjs.Html5.nativeSourceHandler.canHandleSource = function(source){
var ext;
var match, ext;

function canPlayType(type){
// IE9 on Windows 7 without MediaPlayer throws an error here
// https://github.com/videojs/video.js/issues/519
try {
return !!vjs.TEST_VID.canPlayType(type);
return vjs.TEST_VID.canPlayType(type);
} catch(e) {
return '';
}
Expand All @@ -322,11 +322,15 @@ vjs.Html5.nativeSourceHandler.canHandleSource = function(source){
// If a type was provided we should rely on that
if (source.type) {
return canPlayType(source.type);
} else {
} else if (source.src) {
// If no type, fall back to checking 'video/[EXTENSION]'
ext = source.src.match(/\.([^\/\?]+)(\?[^\/]+)?$/i)[1];
match = source.src.match(/\.([^\/\?]+)(\?[^\/]+)?$/i);
ext = match && match[1];

return canPlayType('video/'+ext);
}

return '';
};

/**
Expand Down
26 changes: 26 additions & 0 deletions test/unit/media.html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,29 @@ test('error events may not set the errors property', function() {
test('should have the source handler interface', function() {
ok(vjs.Html5.registerSourceHandler, 'has the registerSourceHandler function');
});

test('native source handler canHandleSource', function(){
var result;

// Stub the test video canPlayType (used in canHandleSource) to control results
var origCPT = vjs.TEST_VID.canPlayType;
vjs.TEST_VID.canPlayType = function(type){
if (type === 'video/mp4') {
return 'maybe';
}
return '';
};

var canHandleSource = vjs.Html5.nativeSourceHandler.canHandleSource;

equal(canHandleSource({ type: 'video/mp4', src: 'video.flv' }), 'maybe', 'Native source handler reported type support');
equal(canHandleSource({ src: 'http://www.example.com/video.mp4' }), 'maybe', 'Native source handler reported extension support');
// Test for issue videojs/video.js#1785 and other potential failures
equal(canHandleSource({ src: '' }), '', 'Native source handler handled empty src');
equal(canHandleSource({}), '', 'Native source handler handled empty object');
equal(canHandleSource({ src: 'foo' }), '', 'Native source handler handled bad src');
equal(canHandleSource({ type: 'foo' }), '', 'Native source handler handled bad type');

// Reset test video canPlayType
vjs.TEST_VID.canPlayType = origCPT;
});

0 comments on commit dd4fb07

Please sign in to comment.