all files / htmlcs/lib/rules/ rel-stylesheet.js

100% Statements 11/11
100% Branches 8/8
100% Functions 4/4
100% Lines 11/11
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                      103× 26× 14×     12×             39×                
/**
 * @file rule: rel-stylesheet
 * @author nighca<nighca@live.cn>
 */
 
module.exports = {
 
    name: 'rel-stylesheet',
 
    desc: 'Attribute "rel" of <link> should be set as "stylesheet".',
 
    lint: function (getCfg, document, reporter) {
        document.querySelectorAll('link').forEach(function (element) {
            if (!getCfg(element)) {
                return;
            }
 
            if (!element.getAttribute('rel')) {
                reporter.warn(element.startIndex, '022', 'Attribute "rel" of <link> should be set as "stylesheet".');
            }
        });
 
    },
 
    format: function (getCfg, document, options) {
        document.querySelectorAll('link').forEach(function (element) {
            if (!getCfg(element)) {
                return;
            }
 
            if (!element.getAttribute('rel')) {
                element.setAttribute('rel', 'stylesheet');
            }
        });
    }
 
};