github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/vue-1.0.24/src/directives/public/html.js (about)

     1  import { parseTemplate } from '../../parsers/template'
     2  import {
     3    createAnchor,
     4    before,
     5    replace,
     6    remove,
     7    _toString,
     8    toArray
     9  } from '../../util/index'
    10  
    11  export default {
    12  
    13    bind () {
    14      // a comment node means this is a binding for
    15      // {{{ inline unescaped html }}}
    16      if (this.el.nodeType === 8) {
    17        // hold nodes
    18        this.nodes = []
    19        // replace the placeholder with proper anchor
    20        this.anchor = createAnchor('v-html')
    21        replace(this.el, this.anchor)
    22      }
    23    },
    24  
    25    update (value) {
    26      value = _toString(value)
    27      if (this.nodes) {
    28        this.swap(value)
    29      } else {
    30        this.el.innerHTML = value
    31      }
    32    },
    33  
    34    swap (value) {
    35      // remove old nodes
    36      var i = this.nodes.length
    37      while (i--) {
    38        remove(this.nodes[i])
    39      }
    40      // convert new value to a fragment
    41      // do not attempt to retrieve from id selector
    42      var frag = parseTemplate(value, true, true)
    43      // save a reference to these nodes so we can remove later
    44      this.nodes = toArray(frag.childNodes)
    45      before(frag, this.anchor)
    46    }
    47  }