All files / gogocode-core/src/html-core get-selector.js

100% Statements 15/15
75% Branches 6/8
100% Functions 1/1
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  37x 37x     135x 135x   2x 2x 2x 2x   133x         133x 133x 133x 133x   133x     37x
// 把简单的api转换成ast
const parse = require('./parse');
const filterProps = require('./filter-prop.js');
 
function getSelector(selectorCode, parseOptions, expando = 'g123o456g789o') {
    const selector = { nodeType: '', structure: {} };
    if (typeof selectorCode != 'string') {
        // 如果是通过builders造出来的ast结构,比如return语句
        selector.nodeType = selectorCode.nodeType;
        filterProps(selectorCode, selector.structure);
        selector.type = selectorCode.nodeType; // 兼容只用type匹配的选择器
        return selector;
    } else {
        selectorCode = selectorCode
            .trim()
            .replace(/\$_\$/g, expando)
            .replace(/\$\$\$/g, expando.slice(0, -1) + '$3')
    }
    let selectorAst = parse(selectorCode, parseOptions);
    Eif (selectorAst.content && selectorAst.content.children && selectorAst.content.children[0]) {
        filterProps(selectorAst.content.children[0], selector.structure);
        selector.nodeType = selectorAst.content.children[0].nodeType;
    }
    return selector;
}
 
module.exports = getSelector;