all files / htmlcs/lib/rules/ css-in-head.js

100% Statements 14/14
100% Branches 14/14
100% Functions 4/4
100% Lines 14/14
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                      103×   103× 42× 30×     12×           39× 38×                  
/**
 * @file rule: css-in-head
 * @author nighca<nighca@live.cn>
 */
 
module.exports = {
 
    name: 'css-in-head',
 
    desc: 'All css contents are recommended to be imported in <head>.',
 
    lint: function (getCfg, document, reporter) {
        var head = document.querySelector('head');
 
        document.querySelectorAll('link[rel="stylesheet"], style').forEach(function (css) {
            if (!getCfg(css)) {
                return;
            }
 
            if (!(head && head.contains(css))) {
                reporter.warn(css.startIndex, '008', 'CSS contents are recommended to be imported in <head>.');
            }
        });
    },
 
    format: function (getCfg, document, options) {
        if (!getCfg()) {
            return;
        }
 
        var head = document.querySelector('head');
        head && document.querySelectorAll('link[rel="stylesheet"], style').forEach(function (css) {
            if (!getCfg(css) || head.contains(css)) {
                return;
            }
 
            head.appendChild(css);
        });
    }
 
};