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

89.26% Statements 108/121
82.73% Branches 115/139
100% Functions 13/13
89.92% Lines 107/119

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
    20837x

    37x
    37x
    37x
    37x

    37x

    957x
    1746x

    115x

    111x
    53x
    53x
    85x
    53x
    58x
    16x
    16x

    1677x

    1677x
    634x
    634x
    634x

    113x

    634x
    113x
    81x
    101x
    101x

    101x
    160x
    121x

    101x

    32x
    5x

    27x

    521x
    521x

    521x

    602x

    1043x
    41x

    1002x
    141x

    141x
    141x
    141x
    141x
    141x

    141x
    141x
    861x

    861x
    177x

    177x
    119x

    58x

    684x

    129x
    129x
    165x

    293x
    293x
    61x
    232x
    169x

    293x
    55x
    55x

    55x

    129x
    74x

    55x
    55x
    91x
    55x

    36x
    36x
    51x
    20x

    31x

    55x
    55x

    116x
    116x
    116x
    116x
    116x

    225x
    225x
    225x
    225x
    184x
    184x

    28x
    28x

    116x
    9x

    10x
    10x
    7x

    116x
    116x

    902x
    902x
    902x
    902x

    37xconst { 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;
                    Eif (full[prop]) {
                        extra.value = full[prop];
                    } else {
                        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;
        partial.forEach((p, i) => {
            for (const key in p) {
                // 属性中包含$$$
                let value = null;
                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 (value && value.match && value.match(new RegExp(Expando.slice(0, -1) + '\\$3'))) {
                    key$$$ = value.replace(new RegExp(Expando.slice(0, -1) + '\\$3'), '') || '$'
                    index$$$ = i;

                    break;
                }
            }
        })
        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' ? [{
                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 != '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 };