/**
* Server HTML: the tree, the head tags and the `` / `
` attributes
* as escaped strings.
*/
import type { ServerSink } from "../../internal/services.ts"
import * as View from "../../View.ts"
import * as Head from "../HtmlHead.ts"
import * as elements from "./elements.ts"
import * as headAttributes from "./headAttributes.ts"
import * as markers from "./markers.ts"
import * as url from "./url.ts"
// ---------------------------------------------------------------------------
// HTML
// ---------------------------------------------------------------------------
const voidTags: ReadonlySet = new Set([
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
"input",
"link",
"meta",
"source",
"track",
"wbr"
])
/**
* Elements whose content the HTML parser reads as text: escaped text
* (`textarea`, `title`) or raw text. Markers or tags inside them would become
* part of the text, so the server writes only their text.
*
* @internal
*/
export const textOnlyTags: ReadonlySet = new Set([
"textarea",
"title",
"script",
"style",
"xmp",
"iframe",
"noembed",
"noframes",
"noscript"
])
const escapedTextTags: ReadonlySet = new Set(["textarea", "title"])
/** Elements whose first newline the HTML parser drops. */
const newlineTags: ReadonlySet = new Set(["pre", "textarea", "listing"])
// Most text and values need no escaping, and are returned as they are without a replace.
const textSpecial = /[&<>]/
const attributeSpecial = /[&"<>]/
const escapeText = (text: string): string =>
textSpecial.test(text)
? text.replace(/[&<>]/g, (char) => char === "&" ? "&" : char === "<" ? "<" : ">")
: text
const escapeAttribute = (value: string): string =>
attributeSpecial.test(value)
? value.replace(
/[&"<>]/g,
(char) =>
char === "&"
? "&"
: char === "\""
? """
: char === "<"
? "<"
: ">"
)
: value
/**
* An element's attributes, with its controlled properties written as the
* attributes that give the same initial state. Other properties are set when
* the client adopts the element.
*/
const elementAttributes = (
tag: string,
attrs: Readonly>,
props: Readonly>
): Map => {
const out = new Map()
const svg = elements.svgTagSet.has(tag)
for (const [name, value] of Object.entries(attrs)) {
const normalized = elements.attributeName(name, svg)
if (normalized === undefined) continue
if (url.blockedAttribute(tag, normalized, value)) out.delete(normalized)
else out.set(normalized, value)
}
if (elements.isCustomTag(tag)) return out
for (const [name, value] of Object.entries(props)) {
switch (name) {
case "value":
if (tag !== "textarea" && tag !== "select" && value !== undefined) {
out.set("value", String(value))
}
break
case "checked":
case "selected":
case "open":
// Only `true`: a modal dialog's `"modal"` has no attribute, so it renders closed until hydration.
if (value === true) out.set(name, "")
else out.delete(name)
break
default:
if (url.blockedProperty(tag, name, value)) {
out.delete(name.toLowerCase())
}
}
}
return out
}
const written = (attributes: Map): string => {
let out = ""
for (const [name, value] of attributes) {
out += value === "" ? ` ${name}` : ` ${name}="${escapeAttribute(value)}"`
}
return out
}
/** An option's implicit value is its text with ASCII whitespace stripped and collapsed. */
const optionValue = (text: string): string => text.replace(/[\t\n\f\r ]+/g, " ").replace(/^ | $/g, "")
/**
* The text of a text-only element. A textarea with no children shows its
* value. Raw text that could end the element early is left out; the client
* builds the children anyway.
*/
const textContent = (tag: string, text: string): string => {
if (escapedTextTags.has(tag)) return escapeText(text)
return new RegExp(`${tag}|