From dd4fb07e6f9402e294387a0acad926dda4b17da1 Mon Sep 17 00:00:00 2001 From: heff Date: Mon, 12 Jan 2015 15:18:35 -0800 Subject: [PATCH] Fixed type support checking for an empty src string fixes #1785 --- src/js/media/html5.js | 12 ++++++++---- test/unit/media.html5.js | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/js/media/html5.js b/src/js/media/html5.js index 9f1ffe35ee..7c41ac884f 100644 --- a/src/js/media/html5.js +++ b/src/js/media/html5.js @@ -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 ''; } @@ -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 ''; }; /** diff --git a/test/unit/media.html5.js b/test/unit/media.html5.js index a2e1758221..f99520b036 100644 --- a/test/unit/media.html5.js +++ b/test/unit/media.html5.js @@ -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; +});