all files / renders/ share.js

98.15% Statements 53/54
91.43% Branches 32/35
100% Functions 6/6
98.15% Lines 53/54
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      1365× 1365× 2121× 183×   1938×     1365×   1516× 2360×   2360× 2360× 190×   2170× 2170× 2170×       2360× 1122× 1122× 11× 11× 11×         2360× 2360× 2359×           577× 577× 662× 428×   662×       48× 48×     48× 48× 105× 105× 50× 49× 49× 49× 48× 48× 48×         48× 48×     911×                
import { avalon, createFragment } from '../seed/core'
import { lookupOption,getSelectedValue } from '../directives/duplex/option'
 
function getChildren(arr) {
    var count = 0
    for (var i = 0, el; el = arr[i++];) {
        if (el.nodeName === '#document-fragment') {
            count += getChildren(el.children)
        } else {
            count += 1
        }
    }
    return count
}
export function groupTree(parent, children) {
    children && children.forEach(function(vdom) {
        Iif (!vdom)
            return
        var vlength = vdom.children && getChildren(vdom.children)
        if (vdom.nodeName === '#document-fragment') {
            var dom = createFragment()
        } else {
            dom = avalon.vdom(vdom, 'toDOM')
            var domlength = dom.childNodes && dom.childNodes.length
            if (domlength && vlength && domlength > vlength) {
                Eif (!appendChildMayThrowError[dom.nodeName]) {
                    avalon.clearHTML(dom)
                }
            }
        }
        if (vlength) {
            groupTree(dom, vdom.children)
            if(vdom.nodeName === 'select'){
                var values = []
                getSelectedValue(vdom, values)
                lookupOption(vdom, values)
            }
        }
        //高级版本可以尝试 querySelectorAll
 
        try {
            if (!appendChildMayThrowError[parent.nodeName]) {
                parent.appendChild(dom)
            }
        } catch (e) {}
    })
}
 
export function dumpTree(elem) {
    var firstChild
    while (firstChild = elem.firstChild) {
        if (firstChild.nodeType === 1) {
            dumpTree(firstChild)
        }
        elem.removeChild(firstChild)
    }
}
 
export function getRange(childNodes, node) {
    var i = childNodes.indexOf(node) + 1
    var deep = 1,
        nodes = [],
        end
    nodes.start = i
    while (node = childNodes[i++]) {
        nodes.push(node)
        if (node.nodeName === '#comment') {
            if (startWith(node.nodeValue, 'ms-for:')) {
                deep++
            } else Eif (node.nodeValue === 'ms-for-end:') {
                deep--
                if (deep === 0) {
                    end = node
                    nodes.pop()
                    break
                }
            }
        }
    }
    nodes.end = end
    return nodes
}
 
export function startWith(long, short) {
    return long.indexOf(short) === 0
}
 
var appendChildMayThrowError = {
    '#text': 1,
    '#comment': 1,
    script: 1,
    style: 1,
    noscript: 1
}