import type { AttributeObject, BindingConfiguration, RMLTemplateExpression, RMLTemplateExpressions, SinkBindingConfiguration, SourceBindingConfiguration } from "../types/internal"; import type { CSSWritableProperty } from "../types/style"; import type { Future, MaybeFuture } from "../types/futures"; import type { Collapser } from "../utils/chronolyze"; import type { KVP } from "../types/key-value-pair"; import type { Sink } from "../types/sink"; import type { HTMLString, RMLEventAttributeName, RMLEventName } from "../types/dom"; import { isFutureSinkAttributeValue, isSinkBindingConfiguration, isSourceBindingConfiguration } from "../types/internal"; import { currentValue } from "../lib/current-value"; import { state, waitingElementHandlers } from "../internal-state"; import { BOOLEAN_ATTRIBUTES } from "../definitions/boolean-attributes"; import { INTERACTIVE_NODE_START, INTERACTIVE_NODE_END, REF_TAG, RESOLVE_ATTRIBUTE, RML_DEBUG } from "../constants"; import { PreSink } from "../sinks/index"; import { sinkByAttributeName } from '../parser/sink-map'; import { DOMAttributePreSink, FixedAttributePreSink, WritableElementAttribute } from "../sinks/attribute-sink"; import { Mixin } from "../sinks/mixin-sink"; import { InnerHTML } from "../sinks/inner-html-sink"; import { TextContent } from "../sinks/text-content-sink"; import { StyleObjectSink, StylePreSink, STYLE_OBJECT_SINK_TAG } from "../sinks/style-sink"; import { isFuture } from "../types/futures" import { isRMLEventListener } from "../types/event-listener"; import { DatasetItemPreSink } from "../sinks/dataset-sink"; import { ClassObjectSink } from "../sinks/class-sink"; import { chronolyze } from "../utils/chronolyze"; import { ENABLE_RML_DEBUGGER } from "../debug"; import { arrayse } from "../lib/lifts/arrayse"; /** * Store some data binding information to be used at mount time */ export const addRef = (ref: string, ...data: BindingConfiguration[]) => { waitingElementHandlers.get(ref)?.push(...data) ?? waitingElementHandlers.set(ref, data); }; const getEventName = (eventAttributeString: RMLEventAttributeName): [RMLEventName, RMLEventAttributeName] | [] => { const x = /\s+(?(?rml:)?on(?\w+))=['"]?$/.exec(eventAttributeString)?.groups; return x ? [`${x.prefix??''}${x?.event}`, x.attr] : [] }; /** * rml — the main entry point for Rimmel.js * * rml is a tag function. You call it by tagging it with an ES6 template literal * of HTML text interleaved with references to any JavaScript entity that's in scope. * * Any JavaScript expression inside the template literal will be evaluated and the * resulting value will be inserted into the HTML template literal. Async expressions * such as Promises and Observables will be rendered as they resolve/emit. * * ## Example * * ```ts * import { rml } from 'rimmel'; * * const Component = () => { * const content = 'hello world'; * * return rml` *
${content}
* `; * }; * ``` * * ## Example * * ```ts * import { rml } from 'rimmel'; * * const Component = () => { * const num = 123; * * return rml` * * `; * }; * ``` * * ## Example * * ```ts * import { rml } from 'rimmel'; * * const Component = () => { * const data = fetch('/api').then(res => res.text()); * * return rml` *
${data}
* `; * }; * ``` **/ export function rml(_strings: TemplateStringsArray, ..._expressions: RMLTemplateExpression[]): HTMLString { const strings = [..._strings]; const expressions = [..._expressions]; let acc = ''; let strlen = strings.length -1; for(let i=0; i\s*$/.test(string) && /^\s*<\s*/.test(nextString) ? 'child/subtree' // : /(?[a-z0-9\-_]+)\=(?['"]?)(?[^"]*)$/.exec(resultPlusString) ? 'attribute' // We treat and render any null or undefined values as empty strings // as a graceful handling of non-values (should this be configurable for a better QA?) // const printableExpressionType = typeof expression ?? 'undefined'; const printableExpressionType = typeof (expression ?? ''); if(ENABLE_RML_DEBUGGER) { if(string.includes(RML_DEBUG)) { const nl = (str: string) => str.replace(/\t/g, ' '); const reducer = (str: string[], next: string, j: number) => str.concat( (j>0?'}':'') +nl(j==i ? next.replace(RML_DEBUG, `%c${RML_DEBUG}%c`) : next), j==i && next.includes(RML_DEBUG) ? ['color: red', ''] : [], (j<=strlen-1?'${':'') +(j[]); console.log(...currentTemplate); /* Stopped parsing a RML template */ // The current template being processed, except its parameters template; debugger; } } // if(['string', 'number', 'boolean'].includes(printableExpressionType)) { if(printableExpressionType == 'string' || printableExpressionType == 'number' || printableExpressionType == 'boolean') { // Static expressions, no data binding. Just concatenate and move on acc = accPlusString +(expression ?? ''); } else { const lastTag = accPlusString.lastIndexOf('<'); const [ eventName, eventAttributeName ] = getEventName(string as RMLEventAttributeName); const r = (accPlusString).match(/<\w[\w-]*\s+[^>]*resolve="(?[^"]+)"\s*[^>]*(?:>\s*[^<]*|[^>]*)$/); const existingRef = r?.groups?.existingRef; const ref = existingRef ?? `${REF_TAG}${state.refCount++}`; if(eventName) { // Event Source // so feed it to an Rx Subject | Observer | Handler Function | POJO | Array // TODO: support EventListenerObject? // Use Cases: // // // TODO: do we want the following? // will feed it the .value of the input field // will feed it the .value of the input field // TODO: Shall we support arrays of streams to feed multiple subscriptions at once? (may conflict with the array syntax above? //