all files / dom/shim/ compact.js

100% Statements 30/30
93.75% Branches 15/16
100% Functions 8/8
100% Lines 30/30
15 statements, 5 functions, 8 branches Ignored     
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                                                     
import { avalon, document, window, inBrowser, msie, root } from '../../seed/core'
import { fixClone } from './fixClone'
import { fixContains } from './fixContains'
export { avalon }
avalon.contains = fixContains
 
avalon.cloneNode = function(a) {
    return a.cloneNode(true)
}
 
//IE6-11的文档对象没有contains
/* istanbul ignore next */
function shimHack() {
    Iif (msie < 10) {
        avalon.cloneNode = fixClone
    }
    Iif (!document.contains) {
        document.contains = function(b) {
            return fixContains(document, b)
        }
    }
    Eif (avalon.modern) {
        Iif (!document.createTextNode('x').contains) {
            Node.prototype.contains = function(child) { //IE6-8没有Node对象
                return fixContains(this, child)
            }
        }
    }
    //firefox 到11时才有outerHTML
    function fixFF(prop, cb) {
        Iif (!(prop in root) && HTMLElement.prototype.__defineGetter__) {
            HTMLElement.prototype.__defineGetter__(prop, cb)
        }
    }
    fixFF('outerHTML', function() {
        var div = document.createElement('div')
        div.appendChild(this)
        return div.innerHTML
    })
    fixFF('children', function() {
        var children = []
        for (var i = 0, el; el = this.childNodes[i++];) {
            if (el.nodeType === 1) {
                children.push(el)
            }
        }
        return children
    })
    fixFF('innerText', function() { //firefox45+, chrome4+ http://caniuse.com/#feat=innertext
        return this.textContent
    })
 
}
 
Eif (inBrowser) {
    shimHack()
}