all files / js/ Preload.class.js

100% Statements 14/14
66.67% Branches 4/6
100% Functions 2/2
100% Lines 13/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                10×                                
import Support from './Support.class';
/**
 * @fileOverview Preload Class.
 * @author Simon Gattner <npm@0x38.de>
 * @license MIT
 * @version 1.0.0
 * @module {es6} Preload
 * @requires Support
 */
export default class Preload extends Support {
    /**
     * Check and upgrade preload support.
     * @class Preload
     * @classdesc Class to check and upgrade preload onload support.
     */
    constructor() {
        super(document.createElement('link').relList, 'preload');
    }
 
    /**
     * Update <link> rel=preload elements and execute onload attr.
     * @function Preload.update
     * @return {boolean}.updateSupport
     */
    update() {
        const links = document.getElementsByTagName('link');
        for (let i = 0; i < links.length; i++) {
            const link = links[i];
            Eif (link.rel === 'preload') {
                const as = link.getAttribute('as');
                const onload = (typeof link.onload === 'function');
                Eif (onload) link.onload();
                switch (true) {
                    case as === 'style':
                        link.setAttribute('rel', 'stylesheet');
                        break;
                    default:
                        break;
                }
            }
        }
        return true;
    }
}