all files / dom/html/ index.js

100% Statements 30/30
87.5% Branches 7/8
100% Functions 4/4
100% Lines 30/30
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                                    244×                     34× 16×   34×      
import {avalon, Cache, document, createFragment} from '../../seed/core'
import {fromString} from '../../vtree/fromString'
export {avalon}
 
var rhtml = /<|&#?\w+;/
var htmlCache = new Cache(128)
var rxhtml = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig
 
avalon.parseHTML = function (html) {
    var fragment = createFragment()
    //处理非字符串
    if (typeof html !== 'string') {
        return fragment
    }
    //处理非HTML字符串
    if (!rhtml.test(html)) {
        return document.createTextNode(html)
    }
 
    html = html.replace(rxhtml, '<$1></$2>').trim()
    var hasCache = htmlCache.get(html)
    if (hasCache) {
        return avalon.cloneNode(hasCache)
    }
    var vnodes = fromString(html)
    for (var i = 0, el; el = vnodes[i++]; ) {
        var child = avalon.vdom(el, 'toDOM')
        fragment.appendChild(child)
    }
    Eif (html.length < 1024) {
        htmlCache.put(html, fragment)
    }
    return fragment
}
 
avalon.innerHTML = function (node, html) {
    var parsed = avalon.parseHTML(html)
    this.clearHTML(node)
    node.appendChild(parsed)
}
 
//https://github.com/karloespiritu/escapehtmlent/blob/master/index.js
avalon.unescapeHTML = function (html) {
    return String(html)
            .replace(/&quot;/g, '"')
            .replace(/&#39;/g, '\'')
            .replace(/&lt;/g, '<')
            .replace(/&gt;/g, '>')
            .replace(/&amp;/g, '&')
}
 
 
 
avalon.clearHTML = function (node) {
    /* istanbul ignore next */
    while (node.lastChild) {
        node.removeChild(node.lastChild)
    }
    return node
}