# all files / src/ createElement.js

77.78% Statements 56/72
72.55% Branches 37/51
50% Functions 3/6
77.78% Lines 56/72

    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
    1×
    1×

    1×

    23×
    23×
    23×
    23×
    23×

    23×
    35×

    23×

    6×
    4×
    4×

    1×
    1×

    2×
    2×

    2×

    1×
    1×

    23×

    23×
    2×

    2×

    21×

    23×

    23×
    23×

    39×
    2×

    37×
    4×

    33×
    33×
    33×
    20×
    20×

    13×

    13×

    13×
    13×

    23×

    23×
    23×
    23×

    23×
    1×

    23×

    23×
    21×

    23×
    23×

    23×

    23×

    1×

     import {extend,__push} from './util'
    const stack = [];
    const EMPTY_CHILDREN = [];

    export var CurrentOwner = {
        cur: null
    }
    /**
     * 创建虚拟DOM
     *
     * @param {string} type
     * @param {object} props
     * @param {array} ...children
     * @returns
     */

    export function createElement(type, configs) {
        var props = {},
            key = null,
            ref = null,
            vtype = 1,
            checkProps = 0

        for (let i = 2, n = arguments.length; i < n; i++) {
            stack.push(arguments[i])
        }
        if (configs) {
            // eslint-disable-next-line
            for (let i in configs) {
                var val = configs[i]
                switch (i) {
                    case 'key':
                        key = val
                        break
                    case 'ref':
                        ref = val
                        break
                    case 'children':
                        Eif (!stack.length && val && val.length) {
                            __push.apply(stack, val)

                        }
                        break
                    default:
                        checkProps = 1
                        props[i] = val
                }
            }
        }

        var children = flattenChildren(stack)

        if (typeof type === 'function') {
            vtype = type.prototype && type.prototype.render
                ? 2
                : 4
            Iif (children.length)
                props.children = children
        } else {
            props.children = children
        }

        return new Vnode(type, props, key, ref, vtype, checkProps, CurrentOwner.cur)
    }

    function flattenChildren(stack) {
        var lastText,
            child,
            children = EMPTY_CHILDREN
        while (stack.length) {
            //比较巧妙地判定是否为子数组
            if ((child = stack.pop()) && child.pop !== undefined) {
                for (let i = 0; i < child.length; i++) {
                    stack[stack.length] = child[i]
                }
            } else {
                // eslint-disable-next-line
                if (child === null || child === void 666 || child === false || child === true) {
                    continue
                }
                var childType = typeof child
                Eif (childType !== 'object') { //不是对象就是字符串或数字
                    if (lastText) {
                        lastText.text = child + lastText.text
                        continue
                    }
                    child = {
                        type: '#text',
                        text: child + ''
                    }
                    lastText = child
                } else {
                    lastText = null
                }
                Eif (children === EMPTY_CHILDREN) {
                    children = [child];
                } else {
                    children.unshift(child);
                }

            }
        }
        return children
    }

    //fix 0.14对此方法的改动，之前refs里面保存的是虚拟DOM
    function getDOMNode() {
        return this
    }

    function Vnode(type, props, key, ref, vtype, checkProps, owner) {
        this.type = type
        this.props = props
        this.vtype = vtype

        if (key) {
            this.key = key
        }
        Iif (owner) {
            this._owner = owner
        }
        if (vtype === 1) {
            this.checkProps = checkProps
        }
        var refType = typeof ref
        Iif (refType === 'string') {
            this._refKey = ref
        } else Iif (refType === 'function') {
            this.__ref = ref
        } else {
            this.__ref = null
        }

        /*
        this._hostNode = null
        this._instance = null
        this._hostParent = null
      */

    }

    Vnode.prototype = {
        getDOMNode: function () {
            return this._hostNode || null
        },
        __ref: function (dom) {
            var instance = this._owner
            if (dom) {
                dom.getDOMNode = getDOMNode
            }
            if (instance) {
                instance.refs[this._refKey] = dom
            }
        },

        $$typeof: 1
    }