All files / gogocode-core/src/html-core find.js

90% Statements 117/130
84% Branches 126/150
100% Functions 12/12
90.62% Lines 116/128

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 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 22737x   37x 37x 37x 37x   37x     1138x 2040x   123x       117x 53x 53x 85x 53x 64x 18x 18x       1969x   1969x 734x 734x 734x   127x   734x 127x 94x 120x 120x     120x 193x 143x     120x     33x 6x   27x       607x 607x               607x           701x   1235x 52x   1183x 165x     165x 165x 165x 165x 165x         165x 165x 1018x     1018x 201x     201x 136x   65x     817x                 145x 145x 145x 188x 188x   334x 334x 70x 264x 190x   334x 140x 140x 1x 1x                 1x     334x 64x 64x   64x     188x   145x 81x   64x 64x 107x 64x   43x 43x 69x 27x   42x       64x 64x       135x 135x 135x 135x 135x       270x 270x 270x 270x 198x 198x                 28x 28x       135x 6x   6x 6x 4x           135x 135x       923x 923x 923x 923x         37x
const { isObject, hasOwn, escapeRegExp } = require('../util')
// 通过简单ast结构查找ast节点
const filterProps = require('./filter-prop.js');
const traverse = require('./html-traverse')
const NodePath = require('../NodePath');
const generate = require('./serialize-node');
 
let Expando = 'g123o456g789o';
 
function checkIsMatch(full, partial, extraData, strictSequence) {
    return Object.keys(partial).every(prop => {
        if (prop == 'children') {
            // 匹配一段代码
            if (full.children && partial.children.length == 1
                && partial.children[0].nodeType == 'text'
                && (partial.children[0].content.value.content.match 
                )) {
                if (partial.children[0].content.value.content.match(Expando)) {
                    const expandoKey = partial.children[0].content.value.content.replace(Expando, '') || '0';
                    extraData[expandoKey] = extraData[expandoKey] || [];
                    extraData[expandoKey].push({ node: full.children, value: full.children.map(c => generate(c)).join('\n') })
                    return true;
                } else if (partial.children[0].content.value.content.match(new RegExp(Expando.slice(0, -1) + '\\$3'))) {
                    find$$$(partial[prop], full[prop], extraData, strictSequence);
                    return true;
                }
            }
        }
        Iif (!full || !partial) {
            return false;
        } else if (isObject(partial[prop])) {
            let res = false;
            let has$$$ = false;
            if (Array.isArray(partial[prop])) {
                // 处理$$$这种情况
                has$$$ = find$$$(partial[prop], full[prop], extraData, strictSequence);
            }
            if (Array.isArray(partial[prop]) && !strictSequence) {
                if (hasOwn(full, prop)) {
                    res = partial[prop].every(p => {
                        let a = false;
                        Iif (!full[prop].length && partial[prop].length == 1 && has$$$) {
                            return true
                        }
                        full[prop] && full[prop].forEach(f => {
                            if (checkIsMatch(f, p, extraData, strictSequence)) {
                                a = true;
                            }
                        });
                        return a;
                    });
                } else {
                    if (partial[prop].length == 1 && has$$$) {
                        return true
                    }
                    return false
                }
            } else {
                // todo
                try {
                    Iif (partial[prop].type == 'token:attribute-value' && !full[prop]) {
                        if (partial[prop].content.match && partial[prop].content.match(Expando)) {
                            const expandoKey = partial[prop].content.replace(Expando, '') || '0';
                            extraData[expandoKey] = extraData[expandoKey] || [];
                            extraData[expandoKey].push({ node: null, value: null })
                            return true;
                        }
                    }
                    res = hasOwn(full, prop) && checkIsMatch(full[prop], partial[prop], extraData, strictSequence);
                } catch (e) {
                    console.log(e)
                }
 
            }
            return res;
        } else {
            if (partial[prop].match && partial[prop].match(new RegExp(Expando.slice(0, -1) + '\\$3'))) {
                return true;
            }
            if (partial[prop] == Expando || (partial[prop].match && partial[prop].match(Expando))) {
                let extra = {
                    node: full
                };
                const expandoKey = partial[prop].replace(Expando, '') || '0';
                extraData[expandoKey] = extraData[expandoKey] || [];
                Iif (!full) return;
                if (full[prop] !== undefined) {
                    extra.value = full[prop] || '';
                } else E{
                    extra.value = {};
                    filterProps(full, extra.value);
                }
                extraData[expandoKey].push(extra);
                return true;
            } else Eif (partial[prop]) {
                // const reg = /^(?:\$\[).*(?=\]\$)/;
            }
            if (prop == 'content') {
                Iif (partial[prop].trim().match(new RegExp(Expando))) {
                    return true; 
                } else {
                    if (partial.type == 'token:attribute-key') {
                        return full ? full[prop] == partial[prop].trim() : false;
                    }
                    return full ? !!full[prop].match(escapeRegExp(partial[prop].trim())) : false;
                }
            } else {
                return full ? full[prop] == partial[prop] : false;
            }
        }
    });
}
 
function find$$$(partial, full, extraData, strictSequence) {
    // 先考虑strctSequence = false的情况
    let key$$$;
    let index$$$ = -1;
    let i = 0;
    while(partial[i]) {
        const p = partial[i];
        for (const key in p) {
            // 属性中包含$$$
            let value = '';
            if (p[key] && p[key].value && p[key].value.content) {
                value = p[key].value.content
            } else if (p[key] && p[key].content) {
                value = p[key].content
            }
            if (p.nodeType == 'text') {
                const start = value.indexOf(Expando.slice(0, -1) + '$3')
                if (start > -1 && value.slice(0, start).trim()) {
                    p.content.value.content = value.slice(0, start)
                    partial[i + 1] = {
                        nodeType: 'text',
                        content: {
                            value: {
                                content: value.slice(start),
                                type:'token:text'
                            }
                        }
                    }
                    value = p.content.value.content
                }
            }
            if (value && value.match && value.match(new RegExp(Expando.slice(0, -1) + '\\$3'))) {
                key$$$ = value.match(new RegExp(`(?<=${Expando.slice(0, -1)}\\$3)([0-9]|[a-z]|[A-Z])*`))[0] || '$'
                index$$$ = i;
 
                break;
            }
        }
        i++
    }
    if (!key$$$) {
        return false;
    }
    const extraNodeList = full ? full.slice(0) : [];
    partial.forEach((p, i) => {
        if (i == index$$$) {
            return;
        }
        let fi = 0;
        while(extraNodeList[fi]) {
            if (checkIsMatch(extraNodeList[fi], p, {}, strictSequence)) {
                extraNodeList.splice(fi, 1);
            } else {
                fi++;
            }
        }
    })
    extraData[`$$$${key$$$}`] = (extraData[`$$$${key$$$}`] || []).concat(extraNodeList);
    return true;
}
 
function find(nodeType, structure, strictSequence, deep, expando = 'g123o456g789o') {
    const nodePathList = [];
    const matchWildCardList = [];
    let isMatch = false;
    Expando = expando
    const traverseMap = {
        tag: (nodeType == 'tag' || nodeType == 'script') ? [{
            value: (structure.content.name || '').match(Expando) ? '' : structure.content.name,
            handle(tagContent, { attrMap, parentRef, nodeRef } = {}) {
                tagContent, attrMap, parentRef
                const matchWildCard = {};
                isMatch = checkIsMatch(nodeRef, structure, matchWildCard, strictSequence);
                if (isMatch) {
                    nodePathList.push(linkParentPath(nodeRef));
                    matchWildCardList.push(matchWildCard)
                }
            }
        }] : [],
        attr: [],
        text: nodeType == 'text' ? [{
            value: [(structure.content.trim && structure.content.trim() == Expando || structure.content.value.content.trim() == Expando) ? '' : structure.content.value.content.trim()],
            type: 'containOne',
            handle(node) {
                nodePathList.push(linkParentPath(node));
                matchWildCardList.push(node.content.value.content);
            }
        }]: []
    }
    if (nodeType != 'tag' && nodeType != 'script' && nodeType != 'text') {
        traverseMap[nodeType] = [{
            handle(node) {
                nodePathList.push(linkParentPath(node));
                if (node.content.value) {
                    matchWildCardList.push(node.content.value.content);
                }
                
            }
        }]
    }
    traverse(this, traverseMap)
    return { nodePathList, matchWildCardList };
}
 
function linkParentPath(node) {
    while(node) {
        const pPath = node.parentRef ? linkParentPath(node.parentRef) : null;
        const path = new NodePath(node, pPath, pPath);
        return path;
    }
    return null
}
 
module.exports = { find };