/* 实现 */ class Element { constructor(type, props, children) { this.type = type; this.props = props; this.children = children; } } function createElement(type, props, children) { return new Element(type, props, children) } let virtualDom = createElement("ul", { class: "list" }, [ createElement("li", { class: "item" }, ["a"]), createElement("li", { class: "item" }, ["b"]), createElement("li", { class: "item" }, ["c"]) ]);
做用:将虚拟dom转化为真实Domjavascript
/* 将虚拟Dom属性应用到真实Dom */ function setAttr(node, key, value) { switch (key) { case "value": if (node.tagName.toUpperCase() === "INPUT" || node.tagName.toUpperCase() === "TEXTAREA") { node.value = value; } break; case "style": node.style.cssText = value; break; default: node.setAttribute(key, value); } } /* 渲染虚拟dom成真实的dom */ function render(vDom) {/* "li", { class: "item" }, ["1"] */ /* 建立结点 */ let el = document.createElement(vDom.type); /* 增长属性 */ for (let key in vDom.props) { setAttr(el, key, vDom.props[key]); } /* 插入子元素 */ vDom.children.forEach(child => { /* 如果Element继续递归render */ child = (child instanceof Element) ? render(child) : document.createTextNode(child); el.appendChild(child); }); return el; }
做用:将真实dom渲染到对应位置css
/* 将真实Dom放到target容器 */ function renderDom(el, target) { target.appendChild(el); }
经过js层的计算,返回一个patch对象,解析patch从新渲染更改部分java
{type:'ATTRS',attrs:{class:'list2'}}
{type:'REMOVE',index:xxx}
{type:'REPLACE',newNode:newNode}
{type:'TEXT',text:"aaa"}
let allPatches;/* 补丁 */ let Index;/* 树的结点编号 */ /* 顶层 */ function diff(oldTree, newTree) { /* 初始化 */ allPatches = {}; Index = 0; /* 遍历结点 */ walk(oldTree, newTree, Index); /* 返回补丁 */ return allPatches; }
/* 判断是否为文本 */ function isString(node) { return Object.prototype.toString.call(node) === "[object String]"; } /* 比对两个结点 */ function walk(oldNode, newNode, currentIndex) { /* 当前补丁 */ let currentPatches = []; /* 结点被删除 */ if (!newNode) { currentPatches.push({ type: "REMOVE", index: currentIndex }) } /* 文本结点 */ else if (isString(oldNode) && isString(newNode)) { if (oldNode !== newNode) currentPatches.push({ type: "TEXT", text: newNode }) } /* 属性改变 */ else if (oldNode.type === newNode.type) { /* 找出差别属性 */ let attrs = diffAttr(oldNode.props, newNode.props); if (Object.keys(attrs).length) { currentPatches.push({ type: "ATTR", attrs }); } /* 遍历子元素 */ diffChildren(oldNode.children, newNode.children); } /* 结点被替换 */ else { currentPatches.push({ type: "REPLACE", node: newNode }) } /* 将补丁合并到总补丁 */ if (currentPatches.length) allPatches[currentIndex] = currentPatches; }
/* 比对属性 */ function diffAttr(oldAttr, newAttr) { let attrs = {}; /* 比对变化 */ for (let key in oldAttr) { if (oldAttr[key] !== newAttr[key]) { attrs[key] = newAttr[key]; } } /* 比对新增属性 */ for (let key in newAttr) { if (!oldAttr.hasOwnProperty(key)) { attrs[key] = newAttr[key]; } } return attrs; }
/* 比对子元素 */ function diffChildren(oldChild, newChild) { oldChild.forEach((child, idx) => { /* 递归比对 Index是子元素的索引 */ walk(child, newChild[idx], ++Index) }) }
let allPatches = {}; let Index = 0; function patch(el, patches) { allPatches = patches; Index = 0; walk(el); } /* 按照索引的顺序遍历孩子 */ function walk(el) { let currentPatches = allPatches[Index++]; let childNodes = el.childNodes; /* 深度优先遍历 */ childNodes.forEach(child => walk(child)); /* 自下而上更新 */ if (currentPatches) { doPatch(el, currentPatches); } }
/* 将补丁应用到el上 */ function doPatch(el, patches) { patches.forEach(patch => { switch (patch.type) {/* {type:'TEXT',text:"aaa"} */ case "TEXT": el.textContent = patch.text; break; case "ATTR":/* {type:'ATTRS',attrs:{class:'list2'}} */ for (let key in patch.attrs) { let value = patch.attrs[key]; /* 若值为undefine直接删除属性 */ if (value) setAttr(el, key, value); else el.removeAttribute(key); } break; case "REPLACE":/* {type:'REPLACE',newNode:newNode} */ /* 文本结点特殊处理 */ let newNode = (patch.newNode instanceof Element) ? render(patch.newNode) : document.createTextNode(patch.newNode); el.parentNode.replaceChild(newNode, el); break; case "REMOVE":/* {type:'REMOVE',index:xxx} */ el.parentNode.removeChild(el); break; } }) }