/* eslint-disable @typescript-eslint/consistent-type-imports */ import * as Chunk from "effect/Chunk" import type { IToken } from "html5parser" import { tokenize } from "html5parser" import * as Template from "../Template.js" import { convertCharacterEntities } from "./character-entities.js" import { PART_REGEX, PART_STRING } from "./chunks.js" import { PathStack, templateHash, unsafeParsePartIndex } from "./parser.js" import { keyToPartType } from "./utils.js" export { templateHash } from "./parser.js" // Unfortunately these are compiled as `const enum` and cannot be exported enum TokenKind { Literal = 0 as import("html5parser").TokenKind.Literal, OpenTag = 1 as import("html5parser").TokenKind.OpenTag, OpenTagEnd = 2 as import("html5parser").TokenKind.OpenTagEnd, CloseTag = 3 as import("html5parser").TokenKind.CloseTag, Whitespace = 4 as import("html5parser").TokenKind.Whitespace, AttrValueEq = 5 as import("html5parser").TokenKind.AttrValueEq, AttrValueNq = 6 as import("html5parser").TokenKind.AttrValueNq, AttrValueSq = 7 as import("html5parser").TokenKind.AttrValueSq, AttrValueDq = 8 as import("html5parser").TokenKind.AttrValueDq } /** * @since 1.0.0 */ export const TEXT_ONLY_NODES_REGEX = new Set([ "textarea", "script", "style", "title", "plaintext", "xmp" ]) /** * @since 1.0.0 */ export const SELF_CLOSING_TAGS = new Set([ "area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr" ]) class Parser { protected html!: string protected tokens!: Array protected index: number = 0 protected parts!: Array]> protected path!: PathStack parse(templateStrings: ReadonlyArray): Template.Template { this.init(templateStrings) return new Template.Template(this.parseNodes(), templateHash(templateStrings), this.parts) } private init(templateStrings: ReadonlyArray) { this.html = templateWithParts(templateStrings) this.tokens = tokenize(this.html) this.index = 0 this.parts = [] this.path = new PathStack() } private peek(): IToken | undefined { return this.tokens[this.index] } private consumeNextTokenOfKind(kind: TokenKind) { const token = this.tokens[this.index] // @ts-expect-error if (token.type !== kind) { throw new Error(`Expected ${kind} but got ${token.type}`) } this.index++ return token } private consumeWhitespace() { // @ts-expect-error while (this.tokens[this.index]?.type === TokenKind.Whitespace) { this.index++ } } private consumeNextTokenOfKinds(...kinds: Array) { const token = this.tokens[this.index] if (!kinds.includes(token.type as any)) { throw new Error(`Expected ${kinds.join(" or ")} but got ${token.type}`) } this.index++ return token } private parseNodes(): Array { const nodes: Array = [] while (this.index < this.tokens.length) { const token = this.peek() if (token === undefined) { break } // @ts-expect-error if (token.type === TokenKind.Literal) { nodes.push(...this.parseNodeParts()) // @ts-expect-error } else if (token.type === TokenKind.OpenTag) { nodes.push(this.parseOpenTag()) this.path.inc() // @ts-expect-error } else if (token.type === TokenKind.CloseTag) { this.index++ this.consumeWhitespace() break // @ts-expect-error } else if (token.type === TokenKind.Whitespace) { if (nodes.length > 0) { this.path.inc() nodes.push(new Template.TextNode(token.value)) } this.index++ } else { throw new Error(`Unexpected token ${token.type}`) } } return nodes } private parseNodeParts(): Array { const token = this.consumeNextTokenOfKind(TokenKind.Literal) const parts = parseTextAndParts( token.value, (index) => new Template.NodePart(index) ) return parts.map((p) => { if (p._tag === "text") { this.path.inc() return p } else { return this.addPartWithPrevious(p) } }) } private parseOpenTag(): Template.Node { const { value: name } = this.consumeNextTokenOfKind(TokenKind.OpenTag) // Comments if (name === "!--") { const node = this.parseCommentNode() this.path.inc() return node } // Doctype if (name === "!doctype") { this.consumeWhitespace() const next = this.peek() // @ts-expect-error if (next && next.type === TokenKind.AttrValueNq) { this.index++ this.consumeWhitespace() this.consumeNextTokenOfKind(TokenKind.OpenTagEnd) return new Template.DocType(next.value) } this.consumeNextTokenOfKind(TokenKind.OpenTagEnd) return new Template.DocType("html") } // Self-closing tags if (SELF_CLOSING_TAGS.has(name)) { return this.parseSelfClosingElementNode(name) } // Text-only nodes, e.g.