Skip to content

Commit

Permalink
Add a polyfill for CustomEvent.
Browse files Browse the repository at this point in the history
Change-Id: I92ac840976794e62c6ffc18a2065d4ebc4dfb33c
  • Loading branch information
joeyparrish committed Apr 21, 2015
1 parent a61c544 commit 9ebd522
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/polyfill/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

goog.provide('shaka.polyfill.installAll');

goog.require('shaka.polyfill.CustomEvent');
goog.require('shaka.polyfill.Fullscreen');
goog.require('shaka.polyfill.MediaKeys');
goog.require('shaka.polyfill.VideoPlaybackQuality');
Expand All @@ -36,6 +37,7 @@ goog.require('shaka.polyfill.VideoPlaybackQuality');
* @export
*/
shaka.polyfill.installAll = function() {
shaka.polyfill.CustomEvent.install();
shaka.polyfill.Fullscreen.install();
shaka.polyfill.MediaKeys.install();
shaka.polyfill.VideoPlaybackQuality.install();
Expand Down
68 changes: 68 additions & 0 deletions lib/polyfill/customevent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @fileoverview A polyfill to implement the CustomEvent constructor.
*
* @see http://enwp.org/polyfill
*/

goog.provide('shaka.polyfill.CustomEvent');


/**
* @namespace shaka.polyfill.CustomEvent
* @export
*
* @summary A polyfill to implement the CustomEvent constructor on browsers
* which don't have one or don't allow its direct use.
*/


/**
* Install the polyfill if needed.
* @export
*/
shaka.polyfill.CustomEvent.install = function() {
var present = 'CustomEvent' in window;

if (present) {
try {
new CustomEvent('');
} catch (exception) {
present = false;
}
}

if (!present) {
window['CustomEvent'] = shaka.polyfill.CustomEvent.ctor_;
}
};



/**
* @this {CustomEvent}
* @constructor
* @param {string} type
* @param {CustomEventInit=} opt_init
* @private
*/
shaka.polyfill.CustomEvent.ctor_ = function(type, opt_init) {
var event = /** @type {!CustomEvent} */(document.createEvent('CustomEvent'));
var init = opt_init || { bubbles: false, cancelable: false, detail: null };
event.initCustomEvent(type, !!init.bubbles, !!init.cancelable, init.detail);
return event;
};

32 changes: 27 additions & 5 deletions support.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ var found = {};
var async = [];

// Find an entry in the report by name, then change its status to "bad".
function markAsBad(name) {
function markAsBad(name, opt_newValue) {
for (var i = 0; i < report.length; ++i) {
if (report[i][0] == name) {
report[i][1] = kBad;
if (opt_newValue !== undefined) {
report[i][2] = opt_newValue;
}
return;
}
}
Expand Down Expand Up @@ -96,21 +99,38 @@ function testFor(parent, name, required, boolValue, prefixes, prefixFn) {
function testForClass(parent, name, required) {
testFor(parent, name, required, false, classPrefixes,
function(prefix, name) {
return prefix + name;
return prefix + name;
});
}

function testForClassUsable(parent, name, required, args) {
testForClass(parent, name, required);
var ctor = found[name];
if (ctor) {
try {
// Some native DOM object constructors cannot be called without new.
// So in order to apply args, they must be bound.
var bindArgs = [ null ].concat(args);
var boundCtor = ctor.bind.apply(ctor, bindArgs);
new boundCtor();
} catch (exception) {
found[name] = false;
markAsBad(name, '(not usable)');
}
}
}

function testForMethod(parent, name, required) {
testFor(parent, name, required, false, propertyPrefixes,
function(prefix, name) {
return prefix + name.charAt(0).toUpperCase() + name.slice(1);
return prefix + name.charAt(0).toUpperCase() + name.slice(1);
});
}

function testForProperty(parent, name, required) {
testFor(parent, name, required, true, propertyPrefixes,
function(prefix, name) {
return prefix + name.charAt(0).toUpperCase() + name.slice(1);
return prefix + name.charAt(0).toUpperCase() + name.slice(1);
});
}

Expand Down Expand Up @@ -216,7 +236,9 @@ testForProperty(document, 'children', true);
testForClass(window, 'VTTCue', false);
testForProperty(document, 'fullscreenElement', false);
testForProperty(document, 'fullScreenElement', false);
testForClassUsable(window, 'CustomEvent', true, ['']);

// Codecs:
testForMimeType(vp8Type);
testForMimeType(vp9Type);
testForMimeType(mp4Type);
Expand Down Expand Up @@ -304,7 +326,7 @@ function onAsyncComplete() {
found[fairPlayId];
var fullscreenApi = found['fullscreenElement'] || found['fullScreenElement'];
var requiresPolyfills = !latestEme || !found['getVideoPlaybackQuality'] ||
!document.fullscreenElement;
!document.fullscreenElement || !found['CustomEvent'];

var emeStatus, emeValue;
if (emeApi && anyKeySystems) {
Expand Down

0 comments on commit 9ebd522

Please sign in to comment.