dompro
Version:
36 lines (35 loc) • 1.08 kB
JavaScript
import { createElement } from './createElement.js';
import { hasChanged } from './hasChanged.js';
import { updateProps } from './propHandler.js';
export function updateDOM($parent, newNode, oldNode, index = 0) {
if (!oldNode) {
$parent.appendChild(
createElement(newNode)
);
} else if (!newNode) {
$parent.removeChild(
$parent.childNodes[index]
);
} else if (hasChanged(newNode, oldNode)) {
$parent.replaceChild(
createElement(newNode),
$parent.childNodes[index]
);
} else if (newNode.type) {
updateProps(
$parent.childNodes[index],
newNode.props,
oldNode.props
);
const newLength = newNode.children.length;
const oldLength = oldNode.children.length;
for (let i = 0; i < newLength || i < oldLength; i++) {
updateDOM(
$parent.childNodes[index],
newNode.children[i],
oldNode.children[i],
i
);
}
}
}