Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 12x 12x 24x 24x 21x 3x 1x 1x 2x 2x 2x 3x 3x 3x 3x | import { VirtualNode } from "../interfaces";
import createNode from "./createNode";
/**
* Ensures the DOM node is in sync with its virtual node
* @param vnode
* @param node
* @returns
*/
export default function patchNode(vnode: VirtualNode, container: Node = undefined): Node {
// if (typeof vnode === 'string') {
// return null;
// }
let node = vnode.$node;
if (node === undefined) {
return vnode.$node = node = createNode(vnode);
}
if (vnode.tag === null) { // Fragment
// The container is needed to patch the children
patchChildren(container, vnode.children);
return container;
}
Eif (vnode.tag.toUpperCase() === (node as HTMLElement).tagName) { // Check if the attributes and children changed
//patchAttributes(element as HTMLElement, vnode.attributes || {}, oldVNode.attributes || {});
patchChildren(node, vnode.children);
return node;
}
else { // Different tags, replace the element
return vnode.$node = node = createNode(vnode);
}
}
// function patchAttributes(element: HTMLElement, newAttributes: Record<string, any> = {}, oldAttributes: Record<string, any> = {}) {
// for (const [key, value] of Object.entries(newAttributes)) {
// setAttribute(element, key, value);
// }
// }
function patchChildren(element: Node, children: VirtualNode[] = []) {
children.forEach((child, i) => {
const node = patchNode(child, element);
Eif (node !== null) { // Not a text node
element.appendChild(node);
}
});
// const newChildrenLength = newChildren.length;
// // Remove the remaining children if any
// for (let i = children.length - 1; i >= newChildrenLength; --i) {
// removeNode(oldChildren[i]);
// }
}
// function setAttribute(element: HTMLElement, key: string, value: any) {
// element.setAttribute(key, value);
// }
|