all files / htmlcs/lib/rules/ id-class-ad-disabled.js

100% Statements 13/13
100% Branches 8/8
100% Functions 3/3
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 39 40 41 42 43 44                          103× 852× 852× 830×     22× 22×   22× 10× 10×   10× 15×                            
/**
 * @file rule: id-class-ad-disabled
 * @author nighca<nighca@live.cn>
 */
 
module.exports = {
 
    name: 'id-class-ad-disabled',
 
    desc: 'Id and class can not use ad-relative keyword, it will be blocked by adblock software.',
 
    target: 'parser',
 
    lint: function (getCfg, parser, reporter) {
        parser.tokenizer.on('attribdata', function (value) {
            var cfg = getCfg();
            if (!cfg) {
                return;
            }
 
            var name = parser._attribname;
            var keywords = Array.isArray(cfg) ? cfg : ['ad'];
 
            if (['id', 'class'].indexOf(name) >= 0) {
                var pos = this._sectionStart;
                var parts = value.split(/[\_\-]+/);
 
                keywords.forEach(function (keyword) {
                    if (parts.indexOf(keyword) >= 0) {
                        reporter.warn(
                            pos,
                            '031',
                            'Id and class can not use ad-relative keyword ('
                                + keyword
                                + '), it will be blocked by adblock software.'
                        );
                    }
                });
            }
        });
    }
 
};