all files / htmlcs/lib/rules/ script-content.js

100% Statements 15/15
100% Branches 8/8
100% Functions 3/3
100% Lines 15/15
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                        103× 52× 44×                                    
/**
 * @file rule: script-content
 * @author nighca<nighca@live.cn>
 */
 
var util = require('../util');
 
module.exports = {
 
    name: 'script-content',
 
    desc: 'Content of <script> must meet standard.',
 
    lint: function (getCfg, document, reporter, code) {
        document.querySelectorAll('script:not([type]), script[type="text/javascript"]').forEach(function (script) {
            if (!getCfg(script)) {
                return;
            }
 
            var linters = getCfg(script, 'linters');
            var linter = linters && linters.script;
            if (typeof linter !== 'function') {
                return;
            }
 
            if (!script.childNodes.length) {
                return;
            }
 
            var textNode = script.childNodes[0];
            var indent = (function (content) {
                return content.slice(content.lastIndexOf('\n') + 1);
            })(code.slice(0, script.startIndex));
 
            linter(
                textNode.textContent,
                util.getPosition(code, textNode.startIndex),
                script,
                indent
            );
        });
    }
 
};