all files / htmlcs/lib/rules/ bool-attribute-value.js

100% Statements 9/9
100% Branches 6/6
100% Functions 4/4
100% Lines 9/9
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                                      103× 909× 802×     107× 2782× 10×                     39×        
/**
 * @file rule: bool-attribute-value
 * @author nighca<nighca@live.cn>
 */
 
var booleanAttributes = [
    'allowfullscreen', 'async', 'autofocus', 'autoplay',
    'checked', 'controls', 'default', 'defer',
    'disabled', 'formnovalidate', 'hidden', 'ismap',
    'itemscope', 'loop', 'multiple', 'muted', 'novalidate',
    'open', 'readonly', 'required', 'reversed',
    'scoped', 'seamless', 'selected', 'sortable', 'typemustmatch'
];
 
module.exports = {
 
    name: 'bool-attribute-value',
 
    desc: 'Value of boolean attributes should not be set.',
 
    lint: function (getCfg, document, reporter) {
        document.querySelectorAll('*').forEach(function (element) {
            if (!getCfg(element)) {
                return;
            }
 
            booleanAttributes.forEach(function (attribute) {
                if (element.getAttribute(attribute)) {
                    reporter.warn(
                        element.startIndex,
                        '003',
                        'Value of boolean attribute "' + attribute + '" should not be set.'
                    );
                }
            });
        });
    },
 
    format: function (getCfg, document, options) {
        options['bool-attribute-value'] = getCfg() ? 'remove' : 'preserve';
    }
 
};