All files / gogocode-core/src/html-core serialize-node.js

92.3% Statements 48/52
84.74% Branches 50/59
100% Functions 10/10
92.3% Lines 48/52

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                37x     14x   14x 14x     14x       20x       425x   425x 203x   425x 425x 103x     322x                     425x 332x   332x 332x     332x   313x 313x 310x   3x   313x 310x   3x   313x       313x       313x         313x     332x         14x 14x   14x       14x         805x       1380x 364x 1119x     1380x   96x       14x       386x                   805x       20x       21x                   18x                   20x             37x
const {
    NODE_DOCUMENT,
    NODE_DOCTYPE,
    NODE_TAG,
    NODE_TEXT,
    NODE_COMMENT,
    NODE_SCRIPT,
    NODE_STYLE
} = require('hyntax-yx/lib/constants/ast-nodes')
  
function serializeDoctypeNode (node) {
    let attributes = serializeDoctypeAttributes(node.content.attributes)
  
    Eif (attributes !== '') {
        attributes = ` ${ attributes }`
    }
  
    return `<!doctype${ attributes }>`
}
  
function serializeCommentNode (node) {
    return `<!--${ node.content.value.content }-->`
}
  
function serializeTagNode (nodeName, attributes, serializedChildren, selfClosing, node) {
    let serializedAttributes = serializeTagAttributes(attributes, node)
  
    if (serializedAttributes !== '') {
        serializedAttributes = ` ${ serializedAttributes }`
    }
    selfClosing = (selfClosing === false ? false : node.content.selfClosing);
    if (selfClosing) {
        return `<${ nodeName }${ serializedAttributes }/>`
    }
  
    return (
        `<${ nodeName }${ serializedAttributes }>` +
      serializedChildren +
      `</${ nodeName }>`
    )
}
  
function serializeTagAttributes (attributes = []) {
    // if (node.content.openEnd.content.replace(/\s/g, '').match('/if')) {
      
    // }
    return attributes.map((item) => {
        let serialized = ''
  
        Eif (item.key !== undefined) {
            serialized += item.key.content
        }
  
        if (item.value !== undefined) {
        // 处理属性中的引号
            let quota = ['"', '"'];
            if (item.startWrapper && item.startWrapper.content) {
                quota[0] = item.startWrapper.content
            } else {
                quota[0] = ''
            }
            if (item.endWrapper && item.endWrapper.content) {
                quota[1] = item.endWrapper.content
            } else {
                quota[1] = ''
            }
            Iif (item.value.content && item.value.content.match && item.value.content.match(quota)) {
                quota = ["'", "'"];
            }
 
            Iif (item.value.content && item.value.content.trim && item.value.content.trim()[0] == '=') {
                // 处理属性中的if语句{{#if(xx == bb)}}xxx{{/if(xx == bb)}}
                quota = ['', '']
                // serialized += `=${ item.value.content }`
            } else Iif (item.value.content && item.value.content.match && item.value.content.match(/\(/) && item.value.content.match(/\)/) && item.key.content.match(/\{\{/)) {
                // 处理属性中的function语句{{ = body_updateState(this,crowd) }}
                quota = ['', '']
                // serialized += `=${ item.value.content }`
            }
            serialized += `=${quota[0]}${ item.value.content }${quota[1]}`
        }
  
        return serialized
    }).join(' ')
}
  
function serializeDoctypeAttributes (attributes = []) {
    return attributes.map((item) => {
        let wrapper = ''
  
        Iif (item.startWrapper !== undefined) {
            wrapper = item.startWrapper
        }
  
        return `${ wrapper }${ item.value.content }${ wrapper }`
    }).join(' ')
}
  
function serializeTextNode (node) {
    return node.content.value.content
}
  
function serializeNode (node, serializedChildren = '') {
    if (node.content && node.content.children && node.content.children.length > 0) {
        serializedChildren = node.content.children.map(child => {
            return serializeNode(child, '');
        }).join('');
    }
    switch (node.nodeType) {
    case NODE_DOCUMENT: {
        return serializedChildren
    }
  
    case NODE_DOCTYPE: {
        return serializeDoctypeNode(node)
    }
  
    case NODE_TAG: {
        return serializeTagNode(
            node.content.name,
            node.content.attributes,
            serializedChildren,
            undefined,
            node
        )
    }
  
    case NODE_TEXT: {
        return serializeTextNode(node)
    }
  
    case NODE_COMMENT: {
        return serializeCommentNode(node)
    }
  
    case NODE_SCRIPT: {
        return serializeTagNode(
            'script',
            node.content.attributes,
            node.content.value ? node.content.value.content : '',
            false,
            node
        )
    }
  
    case NODE_STYLE: {
        return serializeTagNode(
            'style',
            node.content.attributes,
            node.content.value ? node.content.value.content : '',
            false,
            node
        )
    }
  
    default: {
        throw new Error(
            `generate failed! Unexpected node type for serialization: ${ node.nodeType }`
        )
    }
    }
}
  
module.exports = serializeNode