/* Template caching + the HTML/dependency parser. */ import { ATTR, ATTRS, DYNAMIC_TAG_MARKER, NODE, TAG, WC_ATTRS_ATTR_PREFIX, WC_ATTRS_MARKER, WC_MARKER, isAttrRegex, isDynamicTagRegex, isInsideTextTag, onlyTextChildrenElementsRegex, selfClosingRegex, treeWalker, } from './constants.js'; import { Dynamics, DynamicAttribute, DynamicAttributes, DynamicNode, DynamicTag, } from './dynamics.js'; import type { Dependency, RenderHtml } from './types.js'; /** * The CachedTemplate class is used to efficiently render components. The template HTML element is * stored here and only cloned when a new component is instantiated. */ export class CachedTemplate { public template: HTMLTemplateElement; public dependencies: Dependency[]; /** Indices where the template has an element with no walker-visible children (no element/comment * children — text-only or empty). During `adopt()` we use these to detect when a nested custom * element has populated itself between SSR and our walk (a non-island wompo component upgrades * synchronously when its `customElements.define` runs, and runs its own template render before * the enclosing island gets a chance to call `_$hydrate`). Without this, the adopt walker * descends into the unexpected children and throws a "hydration mismatch" — even though the * shape we *care about* matches. Recording leaf positions lets us treat such elements as the * leaves the template said they were and jump past their SSR'd subtree. */ public leafElementIndices: Set; /** Maps the node index of each dynamic-tag placeholder (`wc-wc`) to the node index immediately * after its template subtree. When a dynamic tag resolves to a component, that component re-homes * the tag's children into its own SSR output (behind a `