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

93.48% Statements 43/46
84.31% Branches 43/51
100% Functions 10/10
93.48% Lines 43/46

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

    37x

    14x

    14x
    14x

    14x

    20x

    398x

    398x
    192x

    398x
    398x
    103x

    295x

    398x
    317x

    317x
    317x

    317x

    298x
    298x
    2x

    298x

    298x

    298x

    317x

    14x
    14x

    14x

    14x

    756x

    1284x
    324x
    1057x

    1284x

    78x

    14x

    362x

    756x

    20x

    18x

    18x

    18x

    37xconst {
        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.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)}}
                    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) }}
                    serialized += `=${ item.value.content }`
                } else {
                    serialized += `=${quota}${ item.value.content }${quota}`
                }
            }

            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