From 6d78c95bb456d8c9dd6749c3f916048615eafe0e Mon Sep 17 00:00:00 2001 From: shmulik Date: Thu, 12 Jun 2014 15:24:46 -0700 Subject: [PATCH] Added cross-browser isArray for cross-frame support. fixes #1195. closes #1218 --- CHANGELOG.md | 2 +- src/js/component.js | 2 +- src/js/lib.js | 11 +++++++++++ src/js/player.js | 2 +- test/unit/lib.js | 8 ++++++++ 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ace40a1d..6eedcd451 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ CHANGELOG ========= ## HEAD (Unreleased) -_(none)_ +* Added cross-browser isArray for cross-frame support. fixes #1195 ([view](https://github.com/videojs/video.js/pull/1218)) -------------------- diff --git a/src/js/component.js b/src/js/component.js index b421bdc28..b6ff71bd8 100644 --- a/src/js/component.js +++ b/src/js/component.js @@ -471,7 +471,7 @@ vjs.Component.prototype.initChildren = function(){ if (children) { // Allow for an array of children details to passed in the options - if (children instanceof Array) { + if (vjs.obj.isArray(children)) { for (var i = 0; i < children.length; i++) { child = children[i]; diff --git a/src/js/lib.js b/src/js/lib.js index c2cd2ae42..bbcd90914 100644 --- a/src/js/lib.js +++ b/src/js/lib.js @@ -160,6 +160,17 @@ vjs.obj.isPlain = function(obj){ && obj.constructor === Object; }; +/** + * Check if an object is Array +* Since instanceof Array will not work on arrays created in another frame we need to use Array.isArray, but since IE8 does not support Array.isArray we need this shim + * @param {Object} obj Object to check + * @return {Boolean} True if plain, false otherwise + * @private + */ +vjs.obj.isArray = Array.isArray || function(arr) { + return Object.prototype.toString.call(arr) === '[object Array]'; +}; + /** * Bind (a.k.a proxy or Context). A simple method for changing the context of a function It also stores a unique id on the function so it can be easily removed from events diff --git a/src/js/player.js b/src/js/player.js index b39f77a8a..cb6798943 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -1103,7 +1103,7 @@ vjs.Player.prototype.src = function(source){ } // Case: Array of source objects to choose from and pick the best to play - if (source instanceof Array) { + if (vjs.obj.isArray(source)) { var sourceTech = this.selectSource(source), techName; diff --git a/test/unit/lib.js b/test/unit/lib.js index d75433828..5ec602003 100644 --- a/test/unit/lib.js +++ b/test/unit/lib.js @@ -41,6 +41,14 @@ test('should copy an object', function(){ deepEqual(asdf,fdsa); }); +test('should check if an object is an Array', function(){ + var arr = ['a', 'b', 'c']; + ok(vjs.obj.isArray(arr) === true, 'Arr object is an Array'); + + var obj = {}; + ok(vjs.obj.isArray(obj) === false, 'Obj is not an Array'); +}); + test('should check if an object is plain', function(){ var empty = {}; ok(vjs.obj.isPlain(empty) === true, 'Empty object is plain');