aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2018-06-11 16:54:20 +1000
committerSam Potts <sam@potts.es>2018-06-11 16:54:20 +1000
commit3fad6ed42cfa77740fbe17ca61a5c5a106a73578 (patch)
tree2479d6d174ab910fc0475ce9b3723e3d6634f65a /src/js/utils.js
parent38f10d4cc67b3109189699f7e65189a852064236 (diff)
parent38f954ef179d559095eeca5cbd5fd08fae211d34 (diff)
downloadplyr-3fad6ed42cfa77740fbe17ca61a5c5a106a73578.tar.lz
plyr-3fad6ed42cfa77740fbe17ca61a5c5a106a73578.tar.xz
plyr-3fad6ed42cfa77740fbe17ca61a5c5a106a73578.zip
Merge branch 'develop' into a11y-improvements
# Conflicts: # demo/dist/demo.css # dist/plyr.css # dist/plyr.js.map # dist/plyr.min.js # dist/plyr.min.js.map # dist/plyr.polyfilled.js.map # dist/plyr.polyfilled.min.js # dist/plyr.polyfilled.min.js.map # src/js/captions.js
Diffstat (limited to 'src/js/utils.js')
-rw-r--r--src/js/utils.js60
1 files changed, 34 insertions, 26 deletions
diff --git a/src/js/utils.js b/src/js/utils.js
index 216cd341..d2a37706 100644
--- a/src/js/utils.js
+++ b/src/js/utils.js
@@ -11,63 +11,64 @@ const utils = {
// Check variable types
is: {
object(input) {
- return this.getConstructor(input) === Object;
+ return utils.getConstructor(input) === Object;
},
number(input) {
- return this.getConstructor(input) === Number && !Number.isNaN(input);
+ return utils.getConstructor(input) === Number && !Number.isNaN(input);
},
string(input) {
- return this.getConstructor(input) === String;
+ return utils.getConstructor(input) === String;
},
boolean(input) {
- return this.getConstructor(input) === Boolean;
+ return utils.getConstructor(input) === Boolean;
},
function(input) {
- return this.getConstructor(input) === Function;
+ return utils.getConstructor(input) === Function;
},
array(input) {
- return !this.nullOrUndefined(input) && Array.isArray(input);
+ return !utils.is.nullOrUndefined(input) && Array.isArray(input);
},
weakMap(input) {
- return this.instanceof(input, WeakMap);
+ return utils.is.instanceof(input, WeakMap);
},
nodeList(input) {
- return this.instanceof(input, NodeList);
+ return utils.is.instanceof(input, NodeList);
},
element(input) {
- return this.instanceof(input, Element);
+ return utils.is.instanceof(input, Element);
},
textNode(input) {
- return this.getConstructor(input) === Text;
+ return utils.getConstructor(input) === Text;
},
event(input) {
- return this.instanceof(input, Event);
+ return utils.is.instanceof(input, Event);
},
cue(input) {
- return this.instanceof(input, window.TextTrackCue) || this.instanceof(input, window.VTTCue);
+ return utils.is.instanceof(input, window.TextTrackCue) || utils.is.instanceof(input, window.VTTCue);
},
track(input) {
- return this.instanceof(input, TextTrack) || (!this.nullOrUndefined(input) && this.string(input.kind));
+ return utils.is.instanceof(input, TextTrack) || (!utils.is.nullOrUndefined(input) && utils.is.string(input.kind));
},
url(input) {
- return !this.nullOrUndefined(input) && /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/.test(input);
+ return !utils.is.nullOrUndefined(input) && /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/.test(input);
},
nullOrUndefined(input) {
return input === null || typeof input === 'undefined';
},
empty(input) {
return (
- this.nullOrUndefined(input) ||
- ((this.string(input) || this.array(input) || this.nodeList(input)) && !input.length) ||
- (this.object(input) && !Object.keys(input).length)
+ utils.is.nullOrUndefined(input) ||
+ ((utils.is.string(input) || utils.is.array(input) || utils.is.nodeList(input)) && !input.length) ||
+ (utils.is.object(input) && !Object.keys(input).length)
);
},
instanceof(input, constructor) {
return Boolean(input && constructor && input instanceof constructor);
},
- getConstructor(input) {
- return !this.nullOrUndefined(input) ? input.constructor : null;
- },
+ },
+
+ getConstructor(input) {
+ return !utils.is.nullOrUndefined(input) ? input.constructor : null;
},
// Unfortunately, due to mixed support, UA sniffing is required
@@ -603,16 +604,16 @@ const utils = {
formatTime(time = 0, displayHours = false, inverted = false) {
// Bail if the value isn't a number
if (!utils.is.number(time)) {
- return this.formatTime(null, displayHours, inverted);
+ return utils.formatTime(null, displayHours, inverted);
}
// Format time component to add leading zero
const format = value => `0${value}`.slice(-2);
// Breakdown to hours, mins, secs
- let hours = this.getHours(time);
- const mins = this.getMinutes(time);
- const secs = this.getSeconds(time);
+ let hours = utils.getHours(time);
+ const mins = utils.getMinutes(time);
+ const secs = utils.getSeconds(time);
// Do we need to display hours?
if (displayHours || hours > 0) {
@@ -770,10 +771,10 @@ const utils = {
// Parse URL if needed
if (input.startsWith('http://') || input.startsWith('https://')) {
- ({ search } = this.parseUrl(input));
+ ({ search } = utils.parseUrl(input));
}
- if (this.is.empty(search)) {
+ if (utils.is.empty(search)) {
return null;
}
@@ -809,6 +810,13 @@ const utils = {
return fragment.firstChild.innerText;
},
+ // Like outerHTML, but also works for DocumentFragment
+ getHTML(element) {
+ const wrapper = document.createElement('div');
+ wrapper.appendChild(element);
+ return wrapper.innerHTML;
+ },
+
// Get aspect ratio for dimensions
getAspectRatio(width, height) {
const getRatio = (w, h) => (h === 0 ? w : getRatio(h, w % h));