/** * @module Template * A lightweight DOM-based template engine inspired by Mustache/Handlebars but with enhanced * functionality for direct DOM manipulation without virtual DOM overhead. * * Design philosophy: * - Keep DOM references intact during updates instead of replacing elements * - Enable fine-grained binding at the attribute and text node level * - Support component-like patterns with function calls and pipes * - Provide conditional rendering and iteration with minimal syntax */ /** * Typed representation of arguments in template function calls * Used during parsing to distinguish between literal values and variable references * before they're resolved against the actual data context */ export type CurlyTokenType = { type: 'string'; value: string; } | { type: 'number'; value: number; } | { type: 'variable'; name: string; }; /** * Internal AST (Abstract Syntax Tree) nodes for template parsing * Each token represents a distinct part of the template with its own rendering logic */ export type CurlyToken = { type: 'text'; content: string; } | { type: 'variable'; name: string; pipes: string[]; } | { type: 'function'; signature: string; pipes: string[]; }; /** * Tokenizes a template string into a sequence of text and expression tokens * Functions as the lexical analyzer in the template compilation process * @param templateString - Raw template string with embedded expressions * @returns AST nodes representing the template structure */ export declare function parseCurly(templateString: string): CurlyToken[]; /** * Orchestrates the DOM traversal and binding process * Acts as a registry for all data and initialization callbacks * that will be triggered when template is rendered */ export interface ParserContext { currentNode: Node; nodePath: number[]; getNodePath(node: Node): number[]; traverseNode(node: Node): void; traverseChildren(node: Node): void; /** * Binds initialization callbacks to be executed after node structure is created * @param callback - Function to run during initialization phase */ bindInit(callback: TemplateCallback): void; /** * Binds data update callbacks to refresh nodes when data changes * @param callback - Function to run when data is updated */ bindData(callback: TemplateCallback): void; } /** * Control flow enum for DOM traversal * Allows processors to guide how traversal continues after their execution, * enabling efficient skipping of branches or preventing double-processing */ export declare enum NodeProcessorResult { /** * Stop processing the current node, as it was fully handled */ Stop = 0, /** * Process attributes but skip child nodes */ VisitAttributesOnly = 1, /** * Continue normal processing (process this node and its children) */ Continue = 2 } /** * Node processors form the plugin architecture of the template system * Each processor examines a node for specific binding patterns and registers * appropriate callbacks when matches are found * @param node - Current DOM node in traversal * @param context - Context for registering bindings * @returns Flow control directive for traversal */ export type NodeProcessor = (node: Node, context: ParserContext) => NodeProcessorResult; /** * Two-tiered data context for template rendering * Separates component-level data (methods, properties) from * template-specific data (like loop variables and conditionals) */ export interface TemplateContext { node: Node; component: object; data: object; getNode(path: number[]): Node; } /** * Main entry point and API facade for the template engine * Name is a playful reference to "cutting the mustard" - a progressive enhancement technique * Processes templates through all registered binders in sequence * @param htmlOrNode - Raw template input (string or DOM) * @returns Compiled template ready for data binding */ export declare function beTheMustard(htmlOrNode: string | Node): Template; /** * Compiles a template string into an executable rendering function * Processes nested expressions and builds a composite renderer from individual part handlers * Represents the core template compilation process * @param text - Raw template string with expressions * @returns Compiled rendering function */ export declare function createCallbackFromMustard(text: string): TemplateCallback; /** * Processes element attributes for data binding expressions * Handles both function calls and curly syntax in attributes * @param node - DOM node to process * @param parserContext - Parser context for callback registration * @returns Processing directive for traversal control */ export declare function dataAttributeBinder(node: Node, parserContext: ParserContext): NodeProcessorResult; /** * Function type for template rendering callbacks * Takes a context (data + component) and produces output */ export type TemplateCallback = (ctx: TemplateContext) => any; /** * Main template class that manages binding and rendering * Acts as the primary interface for template usage */ export declare class Template { element: HTMLElement; private functionBinders; private dataAssigners; /** * Creates a new template instance * @param element - Root DOM element for this template * @param context - Either a ParserContext from parsing or another Template for cloning */ constructor(element: HTMLElement, context: ParserContext | Template); /** * Binds data to the template, triggering all data update callbacks * @param data - Data context containing component and data objects */ bind(data: TemplateContext): void; /** * Creates a clone of this template with a new root element * Preserves all bindings and callbacks * @param clonedElement - New element to use as root for the clone * @returns New template instance with same bindings */ clone(clonedElement: HTMLElement): Template; }