all files / htmlcs/lib/rules/ self-close.js

100% Statements 16/16
100% Branches 14/14
100% Functions 4/4
100% Lines 16/16
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                                    103× 103×   103× 909×           103× 909× 520×     389× 389×   389×             389×                   39×      
/**
 * @file rule: self-close
 * @author nighca<nighca@live.cn>
 */
 
// http://www.w3.org/TR/html5/syntax.html#elements-0
var voidTags = [
    'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
    'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'
];
 
module.exports = {
 
    name: 'self-close',
 
    desc: 'Should void tags close themeselves with "/".',
 
    target: 'parser',
 
    lint: function (getCfg, parser, reporter) {
        var tokenizer = parser.tokenizer;
        var current;
 
        tokenizer.on('opentagname', function (name) {
            current = {
                pos: this._sectionStart - 1,
                tag: name.toLowerCase()
            };
        });
 
        tokenizer.on('opentagend', function () {
            if (!current || voidTags.indexOf(current.tag) < 0) {
                return;
            }
 
            var cfg = getCfg();
            var selfClosed = this._buffer[this._index - 1] === '/';
 
            if (selfClosed && cfg === 'no-close') {
                reporter.warn(
                    current.pos,
                    '039',
                    'Void tags should not close themeselves with "/".'
                );
            }
 
            if (!selfClosed && cfg === 'close') {
                reporter.warn(
                    current.pos,
                    '040',
                    'Void tags should close themeselves with "/".'
                );
            }
        });
    },
 
    format: function (getCfg, document, options) {
        options['self-close'] = {close: 'close'}[getCfg()] || 'no-close';
    }
};