all files / htmlcs/lib/rules/ unique-id.js

100% Statements 13/13
100% Branches 8/8
100% Functions 4/4
100% Lines 13/13
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×   103× 60× 25×     35× 34×       103× 12×     103× 27× 27×            
/**
 * @file rule: unique-id
 * @author nighca<nighca@live.cn>
 */
 
module.exports = {
 
    name: 'unique-id',
 
    desc: 'Id should be unique in page.',
 
    lint: function (getCfg, document, reporter) {
        var idMap = {};
 
        document.querySelectorAll('[id]').forEach(function (element) {
            if (!getCfg(element)) {
                return;
            }
 
            if (element.id) {
                (idMap[element.id] = idMap[element.id] || []).push(element);
            }
        });
 
        var report = function (element) {
            reporter.warn(element.startIndex, '026', 'Id should be unique in page.');
        };
 
        Object.keys(idMap).forEach(function (id) {
            var elements = idMap[id];
            if (elements.length > 1) {
                elements.forEach(report);
            }
        });
    }
 
};