import { diffable, isComment } from "@typed/wire" import udomdiff from "udomdiff" import { isRenderEvent } from "../RenderEvent.js" import type { RenderQueue } from "../RenderQueue.js" import { convertCharacterEntities } from "./character-entities.js" import { NodePartImpl } from "./server-parts.js" import { findHoleComment, isCommentWithValue } from "./utils.js" export function makeRenderNodePart( index: number, parent: HTMLElement | SVGElement, queue: RenderQueue, document: Document, isHydrating: boolean ) { const comment = findHoleComment(parent, index) let text: Text | null = isHydrating ? getPreviousTextSibling(comment) : null let nodes = isHydrating ? findPreviousNodes(comment, index) : [] return new NodePartImpl(index, ({ part, value }, priority) => { return queue.add( part, () => { matchValue( value, (content) => { if (text === null) { text = document.createTextNode("") } text.textContent = convertCharacterEntities(content) nodes = diffChildren(comment, nodes, [text], document) }, (updatedNodes) => { nodes = diffChildren(comment, nodes, updatedNodes, document) } ) }, priority ) }, nodes) } export function getPreviousTextSibling(node: Node | null) { if (!node) return null if (node && node.nodeType === node.TEXT_NODE) { // During hydration there should be a comment to separate these values if ( node.previousSibling && isComment(node.previousSibling) && isCommentWithValue(node.previousSibling, "text") ) { return node as Text } } return null } export function notIsEmptyTextNode(node: Node) { if (node.nodeType === node.TEXT_NODE) { return node.nodeValue?.trim() === "" } return true } export function findPreviousNodes(comment: Comment, index: number) { const previousIndex = `hole${index - 1}` const nodes: Array = [] let node = comment.previousSibling while (node && !isCommentWithValue(node, previousIndex) && !isCommentWithValue(node, "text")) { nodes.unshift(node) node = node.previousSibling } return nodes } export function diffChildren( comment: Comment, currentNodes: Array, nextNodes: Array, document: Document ) { return udomdiff( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion comment.parentNode!, // Document Fragments cannot be removed, so we filter them out currentNodes.filter((x) => x.nodeType !== x.DOCUMENT_FRAGMENT_NODE), nextNodes, diffable(document), comment ) } function matchValue(value: unknown, onText: (text: string) => A, onNodes: (nodes: Array) => B): A | B { switch (typeof value) { // primitives are handled as text content case "string": case "symbol": case "number": case "bigint": case "boolean": return onText(String(value)) case "undefined": case "object": { if (!value) { return onNodes([]) } else if (Array.isArray(value)) { // arrays can be used to cleanup, if empty if (value.length === 0) return onNodes([]) // or diffed, if these contains nodes or "wires" else if (value.some((x) => typeof x === "object")) { return onNodes(value.flatMap(renderEventToArray)) } // in all other cases the content is stringified as is else return onText(String(value)) } else { return onNodes(renderEventToArray(value)) } } case "function": return onNodes([]) } } function renderEventToArray(x: unknown): Array { if (x === null || x === undefined) return [] if (isRenderEvent(x)) { const value = x.valueOf() return Array.isArray(value) ? value : [value] } return [x as Node] }