all files / htmlcs/lib/rules/ label-for-input.js

100% Statements 8/8
100% Branches 9/9
100% Functions 2/2
100% Lines 8/8
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                        103× 63×       59×                    
/**
 * @file rule: label-for-input
 * @author chris<wfsr@foxmail.com>
 */
 
module.exports = {
 
    name: 'label-for-input',
 
    desc: 'Most <input> should be associate with <label>.',
 
    lint: function (getCfg, document, reporter) {
 
        document.querySelectorAll('input').forEach(function (input) {
            if (!getCfg(input)
                || /^(?:reset|submit|button|image)$/i.test(input.getAttribute('type'))
                || input.matches('label input')
            ) {
                return;
            }
 
            var id = input.getAttribute('id');
            var label = id && document.querySelector('label[for=' + id + ']');
 
            if (!label) {
                reporter.warn(input.startIndex, '044', '<input> should be associate with <label>.');
            }
 
        });
    }
 
};