lib/service/BranchedHTMLStore.js
function createBranchedHTML(service, branchStatements, targetNode) {
    let template = service.template
    const stripedRanges = []

    /**
     * Strip template  of range.
     * @param  {number} start The start index of range.
     * @param  {number} end The end index of range.
     * @returns {void}
     */
    function strip(start, end) {
        const before = template.slice(0, start)
        const target = template.slice(start, end)
        const after = template.slice(end)
        template = before + target.replace(/\S/gu, " ") + after
        stripedRanges.push([start, end])
    }

    const visitor = {
        IfStatement(node) {
            if (
                node.alternate.range[0] <= targetNode.range[0] &&
                targetNode.range[0] < node.alternate.range[1]
            ) {
                strip(node.consequent.range[0], node.consequent.range[1])
            } else {
                strip(node.alternate.range[0], node.alternate.range[1])
            }
        },
        SwitchStatement(node) {
            let nodeCaseIndex = node.cases.findIndex((cur, index) => {
                const next = node.cases[index + 1]
                const endIndex = next ? next.range[0] : node.range[1]
                return (
                    cur.range[0] <= targetNode.range[0] &&
                    targetNode.range[0] < endIndex
                )
            })
            if (nodeCaseIndex < 0) {
                nodeCaseIndex = 0
            }
            node.cases.forEach((cur, index) => {
                if (index === nodeCaseIndex) {
                    return
                }
                const next = node.cases[index + 1]
                const endIndex = next ? next.range[0] : node.range[1]
                strip(cur.range[0], endIndex)
            })
        },
    }
    for (const node of branchStatements) {
        visitor[node.type](node)
    }

    return new BranchedHTML(
        template,
        service.parseHtml(template),
        stripedRanges
    )
}
function createBranchedHTML(service, branchStatements, targetNode) {
    let template = service.template
    const stripedRanges = []

    /**
     * Strip template  of range.
     * @param  {number} start The start index of range.
     * @param  {number} end The end index of range.
     * @returns {void}
     */
    function strip(start, end) {
        const before = template.slice(0, start)
        const target = template.slice(start, end)
        const after = template.slice(end)
        template = before + target.replace(/\S/gu, " ") + after
        stripedRanges.push([start, end])
    }

    const visitor = {
        IfStatement(node) {
            if (
                node.alternate.range[0] <= targetNode.range[0] &&
                targetNode.range[0] < node.alternate.range[1]
            ) {
                strip(node.consequent.range[0], node.consequent.range[1])
            } else {
                strip(node.alternate.range[0], node.alternate.range[1])
            }
        },
        SwitchStatement(node) {
            let nodeCaseIndex = node.cases.findIndex((cur, index) => {
                const next = node.cases[index + 1]
                const endIndex = next ? next.range[0] : node.range[1]
                return (
                    cur.range[0] <= targetNode.range[0] &&
                    targetNode.range[0] < endIndex
                )
            })
            if (nodeCaseIndex < 0) {
                nodeCaseIndex = 0
            }
            node.cases.forEach((cur, index) => {
                if (index === nodeCaseIndex) {
                    return
                }
                const next = node.cases[index + 1]
                const endIndex = next ? next.range[0] : node.range[1]
                strip(cur.range[0], endIndex)
            })
        },
    }
    for (const node of branchStatements) {
        visitor[node.type](node)
    }

    return new BranchedHTML(
        template,
        service.parseHtml(template),
        stripedRanges
    )
}
function createBranchedHTML ( service , branchStatements , targetNode ) { let template = service . template const stripedRanges = [ ] /**
     * Strip template  of range.
     * @param  {number} start The start index of range.
     * @param  {number} end The end index of range.
     * @returns {void}
     */ function strip ( start , end ) { const before = template . slice ( 0 , start ) const target = template . slice ( start , end ) const after = template . slice ( end ) template = before + target . replace ( / \S / gu , " " ) + after stripedRanges . push ( [ start , end ] ) } const visitor = { IfStatement ( node ) { if ( node . alternate . range [ 0 ] <= targetNode . range [ 0 ] && targetNode . range [ 0 ] < node . alternate . range [ 1 ] ) { strip ( node . consequent . range [ 0 ] , node . consequent . range [ 1 ] ) } else { strip ( node . alternate . range [ 0 ] , node . alternate . range [ 1 ] ) } } , SwitchStatement ( node ) { let nodeCaseIndex = node . cases . findIndex ( ( cur , index ) => { const next = node . cases [ index + 1 ] const endIndex = next ? next . range [ 0 ] : node . range [ 1 ] return ( cur . range [ 0 ] <= targetNode . range [ 0 ] && targetNode . range [ 0 ] < endIndex ) } ) if ( nodeCaseIndex < 0 ) { nodeCaseIndex = 0 } node . cases . forEach ( ( cur , index ) => { if ( index === nodeCaseIndex ) { return } const next = node . cases [ index + 1 ] const endIndex = next ? next . range [ 0 ] : node . range [ 1 ] strip ( cur . range [ 0 ] , endIndex ) } ) } , } for ( const node of branchStatements ) { visitor [ node . type ] ( node ) } return new BranchedHTML ( template , service . parseHtml ( template ) , stripedRanges ) }
Get HTML with alternate statements striped.
@param {MicroTemplateService} service The MicroTemplateService
@param {Array} branchStatements The branch statements
@param {ASTNode} targetNode The target node.
@returns {BranchedHTML} Branch-processed HTML
Get HTML with alternate statements striped .