2023-12-06 14:59:11 +08:00
|
|
|
import {
|
|
|
|
isArray,
|
|
|
|
normalizeClass,
|
|
|
|
normalizeStyle,
|
|
|
|
toDisplayString,
|
|
|
|
} from '@vue/shared'
|
|
|
|
import type { Block, ParentBlock } from './render'
|
|
|
|
|
|
|
|
export function insert(
|
|
|
|
block: Block,
|
|
|
|
parent: ParentNode,
|
|
|
|
anchor: Node | null = null,
|
|
|
|
) {
|
|
|
|
// if (!isHydrating) {
|
|
|
|
if (block instanceof Node) {
|
|
|
|
parent.insertBefore(block, anchor)
|
|
|
|
} else if (isArray(block)) {
|
|
|
|
for (const child of block) insert(child, parent, anchor)
|
|
|
|
} else {
|
|
|
|
insert(block.nodes, parent, anchor)
|
|
|
|
parent.insertBefore(block.anchor, anchor)
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
export function prepend(parent: ParentBlock, ...nodes: Node[]) {
|
|
|
|
if (parent instanceof Node) {
|
|
|
|
// TODO use insertBefore for better performance https://jsbench.me/rolpg250hh/1
|
|
|
|
parent.prepend(...nodes)
|
|
|
|
} else if (isArray(parent)) {
|
|
|
|
parent.unshift(...nodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function append(parent: ParentBlock, ...nodes: Node[]) {
|
|
|
|
if (parent instanceof Node) {
|
|
|
|
// TODO use insertBefore for better performance
|
|
|
|
parent.append(...nodes)
|
|
|
|
} else if (isArray(parent)) {
|
|
|
|
parent.push(...nodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function remove(block: Block, parent: ParentNode) {
|
2023-12-15 01:23:17 +08:00
|
|
|
if (block instanceof DocumentFragment) {
|
|
|
|
remove(Array.from(block.childNodes), parent)
|
|
|
|
} else if (block instanceof Node) {
|
2023-12-06 14:59:11 +08:00
|
|
|
parent.removeChild(block)
|
|
|
|
} else if (isArray(block)) {
|
|
|
|
for (const child of block) remove(child, parent)
|
|
|
|
} else {
|
|
|
|
remove(block.nodes, parent)
|
|
|
|
block.anchor && parent.removeChild(block.anchor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 01:23:17 +08:00
|
|
|
export function setText(el: Node, oldVal: any, newVal: any) {
|
2023-12-06 14:59:11 +08:00
|
|
|
if ((newVal = toDisplayString(newVal)) !== oldVal) {
|
|
|
|
el.textContent = newVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setHtml(el: Element, oldVal: any, newVal: any) {
|
|
|
|
if (newVal !== oldVal) {
|
|
|
|
el.innerHTML = newVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setClass(el: Element, oldVal: any, newVal: any) {
|
|
|
|
if ((newVal = normalizeClass(newVal)) !== oldVal && (newVal || oldVal)) {
|
|
|
|
el.className = newVal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setStyle(el: HTMLElement, oldVal: any, newVal: any) {
|
|
|
|
if ((newVal = normalizeStyle(newVal)) !== oldVal && (newVal || oldVal)) {
|
|
|
|
if (typeof newVal === 'string') {
|
|
|
|
el.style.cssText = newVal
|
|
|
|
} else {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setAttr(el: Element, key: string, oldVal: any, newVal: any) {
|
|
|
|
if (newVal !== oldVal) {
|
|
|
|
if (newVal != null) {
|
|
|
|
el.setAttribute(key, newVal)
|
|
|
|
} else {
|
|
|
|
el.removeAttribute(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setDynamicProp(el: Element, key: string, val: any) {
|
|
|
|
if (key === 'class') {
|
|
|
|
setClass(el, void 0, val)
|
|
|
|
} else if (key === 'style') {
|
|
|
|
setStyle(el as HTMLElement, void 0, val)
|
|
|
|
} else if (key in el) {
|
|
|
|
;(el as any)[key] = val
|
|
|
|
} else {
|
|
|
|
// TODO special checks
|
|
|
|
setAttr(el, key, void 0, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Children = Record<number, [ChildNode, Children]>
|
2023-12-15 01:23:17 +08:00
|
|
|
export function children(n: Node): Children {
|
2023-12-29 22:05:33 +08:00
|
|
|
const result: Children = {}
|
|
|
|
const array = Array.from(n.childNodes)
|
|
|
|
for (let i = 0; i < array.length; i++) {
|
|
|
|
const n = array[i]
|
|
|
|
result[i] = [n, children(n)]
|
|
|
|
}
|
|
|
|
return result
|
2023-12-06 14:59:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function createTextNode(val: unknown): Text {
|
2023-12-29 22:05:33 +08:00
|
|
|
// eslint-disable-next-line no-restricted-globals
|
2023-12-06 14:59:11 +08:00
|
|
|
return document.createTextNode(toDisplayString(val))
|
|
|
|
}
|