diff options
author | Albin Larsson <mail@albinlarsson.com> | 2018-06-11 03:21:18 +0200 |
---|---|---|
committer | Albin Larsson <mail@albinlarsson.com> | 2018-06-11 08:23:08 +0200 |
commit | 4c1337b4c5e86e22c47dac1d74e3b3298bbc01cb (patch) | |
tree | b226cb2b92d7957f0e2c022f4a5ade037e020585 /src | |
parent | 1ad76800b07f4e9fb14450a14433ef18151fe260 (diff) | |
download | plyr-4c1337b4c5e86e22c47dac1d74e3b3298bbc01cb.tar.lz plyr-4c1337b4c5e86e22c47dac1d74e3b3298bbc01cb.tar.xz plyr-4c1337b4c5e86e22c47dac1d74e3b3298bbc01cb.zip |
Assure type safety in getSources() and getQualityOptions() (always return arrays), and remove external conditions and type conversion no longer needed
Diffstat (limited to 'src')
-rw-r--r-- | src/js/html5.js | 53 |
1 files changed, 11 insertions, 42 deletions
diff --git a/src/js/html5.js b/src/js/html5.js index 63596cfc..a7ff0bd9 100644 --- a/src/js/html5.js +++ b/src/js/html5.js @@ -8,35 +8,21 @@ import utils from './utils'; const html5 = { getSources() { if (!this.isHTML5) { - return null; + return []; } - return this.media.querySelectorAll('source'); + return Array.from(this.media.querySelectorAll('source')); }, // Get quality levels getQualityOptions() { - if (!this.isHTML5) { - return null; - } - - // Get sources - const sources = html5.getSources.call(this); - - if (utils.is.empty(sources)) { - return null; - } - - // Get <source> with size attribute - const sizes = Array.from(sources).filter(source => !utils.is.empty(source.getAttribute('size'))); - - // If none, bail - if (utils.is.empty(sizes)) { - return null; - } + // Get sizes from <source> elements + const sizes = html5.getSources.call(this) + .map(source => Number(source.getAttribute('size'))) + .filter(Boolean); // Reduce to unique list - return utils.dedupe(sizes.map(source => Number(source.getAttribute('size')))); + return utils.dedupe(sizes); }, extend() { @@ -51,34 +37,17 @@ const html5 = { get() { // Get sources const sources = html5.getSources.call(player); + const [source] = sources.filter(source => source.getAttribute('src') === player.source); - if (utils.is.empty(sources)) { - return null; - } - - const matches = Array.from(sources).filter(source => source.getAttribute('src') === player.source); - - if (utils.is.empty(matches)) { - return null; - } - - return Number(matches[0].getAttribute('size')); + // Return size, if match is found + return source && Number(source.getAttribute('size')); }, set(input) { // Get sources const sources = html5.getSources.call(player); - if (utils.is.empty(sources)) { - return; - } - // Get matches for requested size - const matches = Array.from(sources).filter(source => Number(source.getAttribute('size')) === input); - - // No matches for requested size - if (utils.is.empty(matches)) { - return; - } + const matches = sources.filter(source => Number(source.getAttribute('size')) === input); // Get supported sources const supported = matches.filter(source => support.mime.call(player, source.getAttribute('type'))); |