html.js 1011 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. var toText = require('../utils').toText,
  2. slice = [].slice
  3. module.exports = {
  4. bind: function () {
  5. // a comment node means this is a binding for
  6. // {{{ inline unescaped html }}}
  7. if (this.el.nodeType === 8) {
  8. // hold nodes
  9. this.holder = document.createElement('div')
  10. this.nodes = []
  11. }
  12. },
  13. update: function (value) {
  14. value = toText(value)
  15. if (this.holder) {
  16. this.swap(value)
  17. } else {
  18. this.el.innerHTML = value
  19. }
  20. },
  21. swap: function (value) {
  22. var parent = this.el.parentNode,
  23. holder = this.holder,
  24. nodes = this.nodes,
  25. i = nodes.length, l
  26. while (i--) {
  27. parent.removeChild(nodes[i])
  28. }
  29. holder.innerHTML = value
  30. nodes = this.nodes = slice.call(holder.childNodes)
  31. for (i = 0, l = nodes.length; i < l; i++) {
  32. parent.insertBefore(nodes[i], this.el)
  33. }
  34. }
  35. }