all files / htmlcs/lib/rules/ spec-char-escape.js

100% Statements 12/12
100% Branches 6/6
100% Functions 2/2
100% Lines 12/12
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 45 46                          103×     103×   103× 1371× 1346×     25×     25×       21× 84×                      
/**
 * @file rule: spec-char-escape
 * @author nighca<nighca@live.cn>
 */
 
module.exports = {
 
    name: 'spec-char-escape',
 
    desc: 'Special characters must be escaped..',
 
    target: 'parser',
 
    lint: function (getCfg, parser, reporter) {
        var specialChars = ['<', '>'];
 
        // exclude-tags
        var excludeTags = ['script', 'style'];
 
        parser.on('text', function (data) {
            if (!getCfg()) {
                return;
            }
 
            var currentTag = this._stack[this._stack.length - 1];
 
            // if should be excluded
            if (excludeTags.indexOf(currentTag) >= 0) {
                return;
            }
 
            // check for special chars
            for (var i = 0, l = data.length; i < l; i++) {
                if (specialChars.indexOf(data[i]) >= 0) {
                    reporter.warn(
                        this.startIndex + i,
                        '033',
                        'Special characters must be escaped.'
                    );
                }
            }
        });
    }
 
};