all files / htmlcs/lib/rules/ ie-edge.js

100% Statements 23/23
100% Branches 24/24
100% Functions 3/3
100% Lines 23/23
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60                      103× 103× 95×     18×                     39× 39× 36×                            
/**
 * @file rule: ie-edge
 * @author nighca<nighca@live.cn>
 */
 
module.exports = {
 
    name: 'ie-edge',
 
    desc: '<meta http-equiv="X-UA-Compatible" content="IE=Edge"> recommended.',
 
    lint: function (getCfg, document, reporter) {
        var head = document.querySelector('head');
        if (!head || !getCfg(head)) {
            return;
        }
 
        var hasEdgeMeta = false;
        head.querySelectorAll('meta').forEach(function (meta) {
            if (
                (meta.getAttribute('http-equiv') || '').toLowerCase() === 'x-ua-compatible'
                && (meta.getAttribute('content') || '').toLowerCase() === 'ie=edge'
            ) {
                hasEdgeMeta = true;
            }
        });
 
        if (!hasEdgeMeta) {
            reporter.warn(head.startIndex, '011', '<meta http-equiv="X-UA-Compatible" content="IE=Edge"> recommended.');
        }
    },
 
    format: function (getCfg, document, options) {
        var head = document.querySelector('head');
        if (!head || !getCfg(head)) {
            return;
        }
 
        var edgeMeta = head.querySelector('meta[http-equiv="X-UA-Compatible"]');
 
        if (edgeMeta) {
            edgeMeta.setAttribute('content', 'IE=Edge');
        }
        else {
            edgeMeta = document.createElement('meta');
            edgeMeta.setAttribute('http-equiv', 'X-UA-Compatible');
            edgeMeta.setAttribute('content', 'IE=Edge');
 
            var title = head.querySelector('title');
            if (title && title.nextSibling) {
                head.insertBefore(edgeMeta, title.nextSibling);
            }
            else {
                head.appendChild(edgeMeta);
            }
        }
    }
 
};