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 174 175 176 177 178 179 180 181
| import { setAttribute, setComponentProps, createComponent } from "./index"
export function diff (dom, vnode, container) { console.log("diff被调用") let ret = diffNode(dom, vnode) if (container) { container.appendChild(ret) }
return ret }
export function diffNode (dom, vnode) { console.log("diffNode被调用") console.log("dom", dom) let out = dom if (vnode === undefined || vnode === null || typeof vnode === 'boolean') vnode = '' if (typeof vnode === 'number') { vnode = String(vnode) } if (typeof vnode === 'string') { if (dom && dom.nodeType === 3) { if (dom.textContent !== vnode) { dom.textContent = vnode } } else { out = document.createTextNode(vnode) if (dom && dom.parentNode) { dom.parentNode.replaceNode(out, dom) } } return out } if (typeof vnode.tag === 'function') { return diffComponent(dom, vnode) }
if (!dom) { out = document.createElement(vnode.tag) console.log('非文本dom节点:out', out) } if (vnode.childrens && vnode.childrens.length > 0 || (out.childNodes && out.childNodes.length > 0)) { console.log("比较子节点") diffChildren(out, vnode.childrens) }
diffAttribute(out, vnode) return out }
function diffComponent (dom, vnode) { console.log("diffComponent被调用") let comp = dom if (comp && comp.constructor === vnode.tag) { setComponentProps(comp, vnode.attrs) dom = comp.base } else { if (comp) { unmountComponent(comp) comp = null }
comp = createComponent(vnode.tag, vnode.attrs) console.log("diffComponent1", comp) setComponentProps(comp, vnode.attrs) dom = comp.base } return dom }
function unmountComponent (comp) { removeNode(comp.base) }
function removeNode (dom) { if (dom && dom.parentNode) { dom.parentNode.removeNode(dom) } }
function diffChildren (dom, vchildren) { const domChildren = dom.childNodes const children = [] const keyed = {}
if (domChildren.length > 0) { [...domChildren].forEach(item => { const key = item.key if (key) { keyed[key] = item } else { children.push(item) } }) } if (vchildren && vchildren.length > 0) { let min = 0 let childrenLen = children.length; [...vchildren].forEach((vchild, i) => { const key = vchild.key let child if (key) { if (keyed[key]) { child = keyed[key] keyed[key] = undefined } } else if (childrenLen > min) { for (let j = min; j < childrenLen; j++) { let c = children[j] if (c) { child = c children[j] = undefined if (j === childrenLen - 1) { childrenLen-- } if (j === min) { min++ } break } } } child = diffNode(child, vchild) const f = domChildren[i]
if (child && child !== dom && child !== f) { if (!f) { dom.appendChild(child) } else if (child === f.nextSibling) { removeNode(f) } else { dom.insertBefore(child, f) } } }) } }
function diffAttribute (dom, vnode) { const oldAttrs = {} const newAttrs = vnode.attrs const domAttrs = dom.attributes ;[...domAttrs].forEach(item => { oldAttrs[item.name] = item.value })
for (let key in oldAttrs) { if (!(key in newAttrs)) { setAttribute(dom, key, undefined) } } for (let key in newAttrs) { if (oldAttrs[key] !== newAttrs[key]) { setAttribute(dom, key, newAttrs[key]) } } }
|