# all files / src/ browser.js

90.91% Statements 40/44
78.57% Branches 22/28
100% Functions 6/6
90.91% Lines 40/44

    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

    6×
    6×
    6×

    1×

    1×

    13×
    2×

    1×
    1×
    2×

    1×
    1×
    1×
    1×

    1×

    1×

    1×

    1×

    1×

    2×
    2×
    2×

    2×

    2×

    2×
    2×
    1×

    1×

    1×

    1×

    1×
    1×
    1×
    1×

    13×
    8×
    5×
    2×

    3×

    2×

     import {
        oneObject,
        recyclables
    } from './util'

    //用于后端的元素节点
    export function DOMElement(type) {
        this.nodeName = type
        this.style = {}
        this.children = []
    }
    var fn = DOMElement.prototype = {
        contains: Boolean
    }
    String('replaceChild,appendChild,removeAttributeNS,setAttributeNS,removeAttribute,setAttribute' +
        ',getAttribute,insertBefore,removeChild,addEventListener,removeEventListener,attachEvent' +
        ',detachEvent').replace(/\w+/g, function (name) {
        fn[name] = function () {
            console.log('fire ' + name)
        }
    })

    //用于后端的document
    export var fakeDoc = new DOMElement
    fakeDoc.createElement = fakeDoc.createElementNS = function (type) {
        return new DOMElement(type)
    }
    fakeDoc.createTextNode = fakeDoc.createComment = Boolean
    fakeDoc.documentElement = new DOMElement('html')
    fakeDoc.nodeName = '#document'
    export var inBrowser = typeof window === 'object' && window.alert

    export var win = inBrowser ?
        window : {
            document: fakeDoc
        };

    export var document = win.document || fakeDoc

    var versions = {
        objectobject: 7, //IE7-8
        objectundefined: 6, //IE6
        undefinedfunction: NaN, // other modern browsers
        undefinedobject: NaN
    };
    /* istanbul ignore next  */
    export var msie = document.documentMode || versions[typeof document.all + typeof XMLHttpRequest]

    export var modern = /NaN|undefined/.test(msie) || msie > 8

    export function createDOMElement(vnode) {
        var type = vnode.type
        var node = recyclables[type] && recyclables[type].pop()
        Iif (node) {
            node.nodeValue = vnode.text
            return node
        }
        Iif (type === '#text') {
            return document.createTextNode(vnode.text)
        }
        Iif (type === '#comment') {
            return document.createComment(vnode.text)
        }

        try {
            if (vnode.ns) {
                return document.createElementNS(vnode.ns, type)
            }
            //eslint-disable-next-line
        } catch (e) {}
        return document.createElement(type)
    }
    // https://developer.mozilla.org/en-US/docs/Web/MathML/Element/math
    // http://demo.yanue.net/HTML5element/
    var mhtml = {
        meter: 1,
        menu: 1,
        map: 1,
        meta: 1,
        mark: 1
    }
    var svgTags = oneObject('' +
        // structure
        'svg,g,defs,desc,metadata,symbol,use,' +
        // image & shape
        'image,path,rect,circle,line,ellipse,polyline,polygon,' +
        // text
        'text,tspan,tref,textpath,' +
        // other
        'marker,pattern,clippath,mask,filter,cursor,view,animate,' +
        // font
        'font,font-face,glyph,missing-glyph', svgNs)

    var rmathTags = /^m/
    var mathNs = 'http://www.w3.org/1998/Math/MathML'
    var svgNs = 'http://www.w3.org/2000/svg'
    var mathTags = {
        semantics: mathNs
    }

    export function getNs(type) {
        if (svgTags[type]) {
            return svgNs
        } else if (mathTags[type]) {
            return mathNs
        } else {
            if (!mhtml[type] && rmathTags.test(type)) {
                //eslint-disable-next-line
                return mathTags[type] = mathNs
            }
        }
    }