import type { BrowserEntry } from "../../Entry.js" import type { RenderContext } from "../../RenderContext.js" import type * as Template from "../../Template.js" import { parse } from "../parser2.js" export function getBrowserEntry( document: Document, ctx: RenderContext, templateStrings: TemplateStringsArray ): BrowserEntry { const cached = ctx.templateCache.get(templateStrings) if (cached === undefined || cached._tag === "Server") { const template = parse(templateStrings) const content = buildTemplate(document, template) const entry: BrowserEntry = { _tag: "Browser", template, content } ctx.templateCache.set(templateStrings, entry) return entry } else { return cached } } export function buildTemplate(document: Document, { nodes }: Template.Template): DocumentFragment { const fragment = document.createDocumentFragment() for (let i = 0; i < nodes.length; ++i) { fragment.append(buildNode(document, nodes[i], false)) } return fragment } function buildNode(document: Document, node: Template.Node, isSvgContext: boolean): globalThis.Node { switch (node._tag) { case "element": case "self-closing-element": case "text-only-element": return buildElement(document, node, isSvgContext) case "text": return document.createTextNode(node.value) case "comment": return document.createComment(node.value) case "sparse-comment": return document.createComment(`hole${node.nodes.map((n) => n._tag === "text" ? "" : n.index).join("")}`) // Create placeholders for these elements case "comment-part": case "node": return document.createComment(`hole${node.index}`) case "doctype": return document.implementation.createDocumentType( node.name, docTypeNameToPublicId(node.name), docTypeNameToSystemId(node.name) ) } } function docTypeNameToPublicId(name: string): string { switch (name) { case "html": return "-//W3C//DTD HTML 4.01//EN" case "svg": return "-//W3C//DTD SVG 1.1//EN" case "math": return "-//W3C//DTD MathML 2.0//EN" default: return "" } } function docTypeNameToSystemId(name: string): string { switch (name) { // HTML5 case "html": return "http://www.w3.org/TR/html4/strict.dtd" case "svg": return "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" case "math": return "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd" default: return "" } } const SVG_NAMESPACE = "http://www.w3.org/2000/svg" function buildElement( document: Document, node: Template.ElementNode | Template.SelfClosingElementNode | Template.TextOnlyElement, isSvgContext: boolean ): Element { const { _tag, attributes, tagName } = node const isSvg = isSvgContext ? tagName !== "foreignObject" : tagName === "svg" const element = isSvg ? document.createElementNS(SVG_NAMESPACE, tagName) : document.createElement(tagName) for (let i = 0; i < attributes.length; ++i) { const attr = attributes[i] // We only handle static attributes here, parts are handled elsewhere if (attr._tag === "attribute") { element.setAttribute(attr.name, attr.value) } else if (attr._tag === "boolean") { element.toggleAttribute(attr.name, true) } } if (_tag === "element") { element.append(...node.children.map((child) => buildNode(document, child, isSvg))) } else if (_tag === "text-only-element") { element.append(...node.children.map((child) => buildTextChild(document, child))) } return element } function buildTextChild(document: Document, node: Template.Text): globalThis.Node { if (node._tag === "text") { return document.createTextNode(node.value) } return document.createComment(`hole${node.index}`) }