This repository has been archived on 2023-08-29. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
anot.js
Archived
1
0
Fork 0
anot.js/src/23-:html.js

50 lines
1.6 KiB
JavaScript

Anot.directive('html', {
update: function(val) {
var binding = this
var elem = this.element
var isHtmlFilter = elem.nodeType !== 1
var _parent = isHtmlFilter ? elem.parentNode : elem
if (!_parent) return
val = val == null ? '' : val
if (elem.nodeType === 3) {
var signature = generateID('html')
_parent.insertBefore(DOC.createComment(signature), elem)
binding.element = DOC.createComment(signature + ':end')
_parent.replaceChild(binding.element, elem)
elem = binding.element
}
if (typeof val !== 'object') {
//string, number, boolean
var fragment = Anot.parseHTML(String(val))
} else if (val.nodeType === 11) {
//将val转换为文档碎片
fragment = val
} else if (val.nodeType === 1 || val.item) {
var nodes = val.nodeType === 1 ? val.childNodes : val.item
fragment = anotFragment.cloneNode(true)
while (nodes[0]) {
fragment.appendChild(nodes[0])
}
}
nodes = Anot.slice(fragment.childNodes)
//插入占位符, 如果是过滤器,需要有节制地移除指定的数量,如果是html指令,直接清空
if (isHtmlFilter) {
var endValue = elem.nodeValue.slice(0, -4)
while (true) {
var node = elem.previousSibling
if (!node || (node.nodeType === 8 && node.nodeValue === endValue)) {
break
} else {
_parent.removeChild(node)
}
}
_parent.insertBefore(fragment, elem)
} else {
Anot.clearHTML(elem).appendChild(fragment)
}
scanNodeArray(nodes, binding.vmodels)
}
})
Anot 是Anot not only templateEngine的缩写。 它是一款迷你,易用、高性能的前端MVVM框架, fork于avalon。进行了大量的重构,精简部分冗余的API, 同时针对组件拓展进行了优化。
JavaScript 100%