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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
function Compile(el, selfVue) { this.el = document.querySelector(el) this.selfVue = selfVue this.fragment = null this.init() }
Compile.prototype = {
init() { if (this.el) { this.fragment = this.nodeToFragment(this.el) this.compileElement(this.fragment) this.el.appendChild(this.fragment) }else { console.log('Dom元素不存在'); } },
nodeToFragment(el) { const fragment = document.createDocumentFragment(); let child = el.firstChild while (child) { console.log(el, child); fragment.appendChild(child) child = el.firstChild } return fragment },
compileElement(el) { const childNodes = el.childNodes; [...childNodes].slice().forEach((node) => { const reg = /\{\{(.*)\}\}/; const text = node.textContent if (this.isTextNode(node) && reg.test(text)) { this.compileText(node, reg.exec(text)[1]) }else if (this.isElementNode(node)) { this.complie(node) } if (node.childNodes && node.childNodes.length) { this.compileElement(node) } }) },
complie(node){ let nodeAttrs = node.attributes; [...nodeAttrs].forEach(attr =>{ let attrName = attr.name if (this.isDirective(attrName)) { let prop = attr.value let dir = attrName.substring(2) if (this.isEventDirective(dir)) { this.complieEvent(node, this.selfVue, prop, dir) }else { this.complieModel(node, this.selfVue, prop, dir) } node.removeAttribute(attrName) } }) },
compileText(node, prop) { let initText = this.selfVue[prop] this.updateText(node, initText) new Watcher(this.selfVue, prop, (value) => this.updateText(node, value)) },
complieEvent(node, selfVue, prop, dir) { let eventType = dir.split(':')[1] let cb = selfVue?.methods?.[prop] if (eventType && cb) { node.addEventListener(eventType, cb.bind(selfVue), false) }else{ throw new Error(`事件类型不可以为${eventType}或${prop}不可以为${cb}`) } },
complieModel(node, selfVue, prop, dir) { let val = selfVue[prop] this.modelUpdater(node, val) new Watcher(selfVue, prop, value => this.modelUpdater(node, value)) node.addEventListener('input', (e) => { let newValue = e.target.value if(val == newValue) return selfVue[prop] = newValue val = newValue }) },
updateText(node, value) { node.textContent = typeof value == "undefined" ? '' : value },
modelUpdater(node, value, oldValue) { node.value = typeof value == "undefined" ? '' : value },
isDirective(attr) { return attr.indexOf('v-') == 0 },
isEventDirective(dir) { return dir.indexOf('on:') == 0 },
isElementNode(node) { return node.nodeType == 1 },
isTextNode(node) { return node.nodeType == 3 }
}
|