@cossackframework/renderer 0.9.0 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ // @ts-self-types="./attributes.d.ts"
package/dist/browser.js CHANGED
@@ -1,9 +1,11 @@
1
1
  // @ts-self-types="./browser.d.ts"
2
- import { isComponentResult as e } from "./component.js";
3
- import { CSSResult as t, css as n, unsafeCSS as r } from "./css.js";
4
- import { CossackElement as i, instanceStack as a, popCurrentInstance as o, pushCurrentInstance as s } from "./cossack-element.js";
5
- import { BindResult as c, CacheResult as l, GuardResult as u, IfDefinedResult as d, KeyResult as f, LiveResult as p, PreventDefaultResult as m, RepeatResult as h, bind as g, cache as _, choose as v, classMap as y, guard as b, ifDefined as x, join as S, key as C, live as w, map as T, preventDefault as E, range as D, repeat as O, resolveField as k, setField as A, styleMap as j, when as M } from "./directives.js";
6
- import { TemplateResult as N, UnsafeHTMLResult as P, captureRenderDrafts as F, component as I, escapeHtml as L, html as R, hydrate as z, isTemplateResult as B, isUnsafeHTML as V, nothing as H, render as U, svg as W, unsafeHTML as G } from "./cossack-html.js";
7
- import { createContext as K } from "./context.js";
8
- import { renderToString as q } from "./browser-ssr.js";
9
- export { c as BindResult, t as CSSResult, l as CacheResult, i as CossackElement, u as GuardResult, d as IfDefinedResult, f as KeyResult, p as LiveResult, m as PreventDefaultResult, h as RepeatResult, N as TemplateResult, P as UnsafeHTMLResult, g as bind, _ as cache, F as captureRenderDrafts, v as choose, y as classMap, I as component, K as createContext, n as css, L as escapeHtml, b as guard, R as html, z as hydrate, x as ifDefined, a as instanceStack, e as isComponentResult, B as isTemplateResult, V as isUnsafeHTML, S as join, C as key, w as live, T as map, H as nothing, o as popCurrentInstance, E as preventDefault, s as pushCurrentInstance, D as range, U as render, q as renderToString, O as repeat, k as resolveField, A as setField, j as styleMap, W as svg, r as unsafeCSS, G as unsafeHTML, M as when };
2
+ import { collectComponentStyles as e, renderComponentStyles as t } from "./style-runtime.js";
3
+ import { isComponentResult as n } from "./component.js";
4
+ import { COMPONENT_RANGE as r, CossackElement as i, getComponentRange as a, instanceStack as o, popCurrentInstance as s, pushCurrentInstance as c, setComponentRange as l } from "./cossack-element.js";
5
+ import { BindResult as u, CacheResult as d, GuardResult as f, IfDefinedResult as p, KeyResult as m, LiveResult as h, PreventDefaultResult as g, RepeatResult as _, bind as v, cache as y, choose as b, classMap as x, guard as S, ifDefined as C, join as w, key as T, live as E, map as D, preventDefault as O, range as k, repeat as A, resolveField as j, setField as M, styleMap as N, when as P } from "./directives.js";
6
+ import { TemplateResult as F, UnsafeHTMLResult as I, captureRenderDrafts as L, component as R, escapeHtml as z, html as B, hydrate as V, isTemplateResult as H, isUnsafeHTML as U, nothing as W, render as G, svg as K, unsafeHTML as q } from "./cossack-html.js";
7
+ import { createContext as J } from "./context.js";
8
+ import { CSSResult as Y } from "./css-result.js";
9
+ import { css as X, unsafeCSS as Z } from "./css.js";
10
+ import { renderToString as Q } from "./browser-ssr.js";
11
+ export { u as BindResult, r as COMPONENT_RANGE, Y as CSSResult, d as CacheResult, i as CossackElement, f as GuardResult, p as IfDefinedResult, m as KeyResult, h as LiveResult, g as PreventDefaultResult, _ as RepeatResult, F as TemplateResult, I as UnsafeHTMLResult, v as bind, y as cache, L as captureRenderDrafts, b as choose, x as classMap, e as collectComponentStyles, R as component, J as createContext, X as css, z as escapeHtml, a as getComponentRange, S as guard, B as html, V as hydrate, C as ifDefined, o as instanceStack, n as isComponentResult, H as isTemplateResult, U as isUnsafeHTML, w as join, T as key, E as live, D as map, W as nothing, s as popCurrentInstance, O as preventDefault, c as pushCurrentInstance, k as range, G as render, t as renderComponentStyles, Q as renderToString, A as repeat, j as resolveField, l as setComponentRange, M as setField, N as styleMap, K as svg, Z as unsafeCSS, q as unsafeHTML, P as when };
@@ -0,0 +1,69 @@
1
+ /** One prop, in source order: later props and spreads override earlier ones. */
2
+ export type ComponentPropPlan =
3
+ /** `name="text"` (entities decoded) or a bare `name` (true). */
4
+ {
5
+ readonly kind: 'static';
6
+ readonly name: string;
7
+ readonly value: string | true;
8
+ }
9
+ /** `name=${value}`: the value as is. */
10
+ | {
11
+ readonly kind: 'value';
12
+ readonly name: string;
13
+ readonly binding: number;
14
+ }
15
+ /** `name="a ${b}"`: text, the same as an interpolated attribute. */
16
+ | {
17
+ readonly kind: 'text';
18
+ readonly name: string;
19
+ readonly segments: readonly string[];
20
+ readonly bindings: readonly number[];
21
+ }
22
+ /** `...=${props}`. */
23
+ | {
24
+ readonly kind: 'spread';
25
+ readonly binding: number;
26
+ };
27
+ export type ComponentChildrenPlan = {
28
+ readonly kind: 'none';
29
+ }
30
+ /** Static text without markup or entities. */
31
+ | {
32
+ readonly kind: 'text';
33
+ readonly text: string;
34
+ }
35
+ /** One binding or component tag with only whitespace around it. */
36
+ | {
37
+ readonly kind: 'slot';
38
+ readonly slot: TemplateSlot;
39
+ } | {
40
+ readonly kind: 'template';
41
+ readonly plan: TemplatePlan;
42
+ };
43
+ export interface ComponentPlan {
44
+ /** The binding that holds the component class. */
45
+ readonly binding: number;
46
+ /** The binding of the closing tag, which must hold the same class. */
47
+ readonly closeBinding: number | null;
48
+ readonly props: readonly ComponentPropPlan[];
49
+ readonly children: ComponentChildrenPlan;
50
+ }
51
+ /** A value of the compiled template: an original binding, or a component. */
52
+ export type TemplateSlot = number | ComponentPlan;
53
+ export interface TemplatePlan {
54
+ /** Identity-stable strings, so the compiled template is cached like any other. */
55
+ readonly strings: TemplateStringsArray;
56
+ readonly slots: readonly TemplateSlot[];
57
+ }
58
+ /** Decode the character references a static attribute value commonly uses. */
59
+ export declare const decodeEntities: (text: string) => string;
60
+ /** True when a template might contain component tags; the parse decides. */
61
+ export declare const mayContainComponentTags: (strings: readonly string[]) => boolean;
62
+ /**
63
+ * Compile the component tags of a template. Returns null when it has none.
64
+ * Throws for a component tag that is never closed or a closing tag without
65
+ * an opening one: the template has no meaning then.
66
+ */
67
+ export declare const compileComponentTags: (input: readonly string[], options?: {
68
+ svg?: boolean;
69
+ }) => TemplatePlan | null;
@@ -0,0 +1,106 @@
1
+ // @ts-self-types="./component-tags.d.ts"
2
+ import { parseTemplate as e } from "./template-parse.js";
3
+ //#region src/component-tags.ts
4
+ var t = {
5
+ amp: "&",
6
+ lt: "<",
7
+ gt: ">",
8
+ quot: "\"",
9
+ apos: "'",
10
+ nbsp: "\xA0"
11
+ }, n = (e) => e.includes("&") ? e.replace(/&(#x[0-9a-f]+|#[0-9]+|[a-z]+);/gi, (e, n) => {
12
+ if (n[0] !== "#") return t[n.toLowerCase()] ?? e;
13
+ let r = n[1] === "x" || n[1] === "X" ? parseInt(n.slice(2), 16) : parseInt(n.slice(1), 10);
14
+ return r > 0 && r <= 1114111 ? String.fromCodePoint(r) : e;
15
+ }) : e, r = (e) => Object.freeze(Object.assign(e, { raw: Object.freeze([...e]) })), i = (e) => {
16
+ for (let t = 0; t < e.length - 1; t++) {
17
+ let n = e[t];
18
+ if (n.charCodeAt(n.length - 1) === 60 || n.endsWith("</")) return !0;
19
+ }
20
+ return !1;
21
+ }, a = (e, t = []) => {
22
+ for (let n of e) n.type === "component" ? t.push(n) : n.type === "element" && a(n.children, t);
23
+ return t;
24
+ }, o = (e) => e.trim() === "", s = (t, i = {}) => {
25
+ let s = e(t, i), c = s.problems.find((e) => e.code === "COSSACK_TEMPLATE_UNCLOSED_COMPONENT" || e.code === "COSSACK_TEMPLATE_UNEXPECTED_COMPONENT_END");
26
+ if (c) throw Error(`[cossack/renderer] ${c.message}`);
27
+ if (a(s.nodes).length === 0) return null;
28
+ let l = (e, n, i) => {
29
+ let o = a(i), s = [], c = [], l = "", d = e.string, f = e.offset, p = 0;
30
+ for (;;) {
31
+ let e = o[p];
32
+ if (e && e.startTag.start.string === d) {
33
+ s.push(l + t[d].slice(f, e.startTag.start.offset)), l = "", c.push(u(e)), d = e.range.end.string, f = e.range.end.offset, p++;
34
+ continue;
35
+ }
36
+ if (d === n.string) {
37
+ s.push(l + t[d].slice(f, n.offset));
38
+ break;
39
+ }
40
+ s.push(l + t[d].slice(f)), l = "", c.push(d), d++, f = 0;
41
+ }
42
+ return {
43
+ strings: r(s),
44
+ slots: c
45
+ };
46
+ }, u = (e) => {
47
+ let t = [];
48
+ for (let r of e.attributes) {
49
+ let { name: e, bindings: i, segments: a } = r;
50
+ e === "..." ? i.length === 1 && t.push({
51
+ kind: "spread",
52
+ binding: i[0]
53
+ }) : r.hasValue ? i.length === 0 ? t.push({
54
+ kind: "static",
55
+ name: e,
56
+ value: n(a[0])
57
+ }) : i.length === 1 && a[0] === "" && a[1] === "" ? t.push({
58
+ kind: "value",
59
+ name: e,
60
+ binding: i[0]
61
+ }) : t.push({
62
+ kind: "text",
63
+ name: e,
64
+ segments: a.map(n),
65
+ bindings: i
66
+ }) : t.push({
67
+ kind: "static",
68
+ name: e,
69
+ value: !0
70
+ });
71
+ }
72
+ return {
73
+ binding: e.binding,
74
+ closeBinding: e.closeBinding,
75
+ props: t,
76
+ children: d(e)
77
+ };
78
+ }, d = (e) => {
79
+ if (e.closeStart === null) return { kind: "none" };
80
+ let t = l(e.startTag.end, e.closeStart, e.children), { strings: n, slots: r } = t;
81
+ if (r.length === 0) {
82
+ let e = n[0];
83
+ if (o(e)) return { kind: "none" };
84
+ if (!e.includes("<") && !e.includes("&")) return {
85
+ kind: "text",
86
+ text: e
87
+ };
88
+ } else if (r.length === 1 && o(n[0]) && o(n[1])) return {
89
+ kind: "slot",
90
+ slot: r[0]
91
+ };
92
+ return {
93
+ kind: "template",
94
+ plan: t
95
+ };
96
+ }, f = t.length - 1;
97
+ return l({
98
+ string: 0,
99
+ offset: 0
100
+ }, {
101
+ string: f,
102
+ offset: t[f].length
103
+ }, s.nodes);
104
+ };
105
+ //#endregion
106
+ export { s as compileComponentTags, n as decodeEntities, i as mayContainComponentTags };
@@ -1,6 +1,6 @@
1
1
  import { TemplateResult, type HydrationDiagnosticMetadata } from './cossack-html.js';
2
2
  import { Context } from './context.js';
3
- import type { CSSResultGroup } from './css.js';
3
+ import type { CSSResultGroup } from './css-result.js';
4
4
  export type PropertyDeclaration = {
5
5
  type?: unknown;
6
6
  reflect?: boolean;
@@ -10,6 +10,30 @@ export interface PropertyDeclarations {
10
10
  [key: string]: PropertyDeclaration;
11
11
  }
12
12
  export type PropertyValues = Map<string | number | symbol, unknown>;
13
+ /**
14
+ * @internal Framework-only. The comment markers that bracket a `component()`
15
+ * child's rendered output. Children have no host element, so this is how the
16
+ * framework scopes DOM work (such as `@On` delegation) to exactly their nodes.
17
+ * `start` and `end` always reflect the renderer's current markers.
18
+ */
19
+ export interface ComponentRange {
20
+ readonly start: Comment;
21
+ readonly end: Comment;
22
+ }
23
+ /**
24
+ * @internal Framework-only. Registered-symbol key under which the client
25
+ * renderer stores a component instance's {@link ComponentRange}. A symbol key
26
+ * (instead of a declared field) keeps DOM types out of the component's
27
+ * public field types, and `Symbol.for` lets core read it without importing.
28
+ */
29
+ export declare const COMPONENT_RANGE: unique symbol;
30
+ /**
31
+ * @internal Framework-only. The DOM range of a client-rendered `component()`
32
+ * child; undefined for root-mounted components and on the server.
33
+ */
34
+ export declare const getComponentRange: (instance: object) => ComponentRange | undefined;
35
+ /** @internal */
36
+ export declare const setComponentRange: (instance: object, range: ComponentRange | undefined) => void;
13
37
  export interface ReactiveController {
14
38
  hostConnected?(): void;
15
39
  hostDisconnected?(): void;
@@ -37,6 +61,12 @@ export declare class CossackElement implements ReactiveControllerHost {
37
61
  __parent: CossackElement | null;
38
62
  _id: string;
39
63
  _childCounter: number;
64
+ /**
65
+ * @internal Live client-rendered children by `_id`. The counter restarts
66
+ * every render while retained children keep their ids, so a newly created
67
+ * child skips any id a live sibling still holds.
68
+ */
69
+ _liveChildIds: Map<string, CossackElement>;
40
70
  private __providedContexts;
41
71
  private __properties;
42
72
  private __updatePromise;
@@ -1,18 +1,10 @@
1
1
  // @ts-self-types="./cossack-element.d.ts"
2
- import { getFinalizedStyles as e } from "./css.js";
3
- import { TemplateResult as t, html as n, hydrate as r, isTemplateResult as i, render as a } from "./cossack-html.js";
2
+ import { getFinalizedStyles as e } from "./style-runtime.js";
3
+ import { TemplateResult as t, html as n, hydrate as r, isTemplateResult as i, nothing as a, render as o } from "./cossack-html.js";
4
4
  //#region src/cossack-element.ts
5
- var o = [], s = (e) => o.push(e), c = () => o.pop(), l = /* @__PURE__ */ new WeakMap(), u = (e) => {
6
- let n = [e];
7
- return n.raw = n, new t(n, []);
8
- }, d = (e, t) => {
9
- let n = l.get(e);
10
- if (!n) {
11
- let r = t.cssText.replace(/<\/style/gi, "<\\/style");
12
- n = u(`<style data-cossack-style="${t.scopeId}">${r}</style>`), l.set(e, n);
13
- }
14
- return n;
15
- }, f = (e, t) => n`${e}${t}`, p = class {
5
+ var s = Symbol.for("@cossackframework/renderer/component-range"), c = (e) => e[s], l = (e, t) => {
6
+ e[s] = t;
7
+ }, u = [], d = (e) => u.push(e), f = () => u.pop(), p = Object.assign(["", ""], { raw: ["", ""] }), m = (e) => e == null || e === !1 || e === "" || e === a, h = class {
16
8
  get hydrationMetadata() {}
17
9
  static properties = {};
18
10
  static _isCossackElement = !0;
@@ -23,6 +15,7 @@ var o = [], s = (e) => o.push(e), c = () => o.pop(), l = /* @__PURE__ */ new Wea
23
15
  __parent = null;
24
16
  _id = "";
25
17
  _childCounter = 0;
18
+ _liveChildIds = /* @__PURE__ */ new Map();
26
19
  __providedContexts = /* @__PURE__ */ new Map();
27
20
  __properties = /* @__PURE__ */ new Map();
28
21
  __updatePromise = null;
@@ -35,7 +28,7 @@ var o = [], s = (e) => o.push(e), c = () => o.pop(), l = /* @__PURE__ */ new Wea
35
28
  this.initialize();
36
29
  }
37
30
  static get currentRenderingInstance() {
38
- return o.length > 0 ? o[o.length - 1] : null;
31
+ return u.length > 0 ? u[u.length - 1] : null;
39
32
  }
40
33
  resetRenderState() {
41
34
  this._childCounter = 0;
@@ -85,12 +78,12 @@ var o = [], s = (e) => o.push(e), c = () => o.pop(), l = /* @__PURE__ */ new Wea
85
78
  let e = !1;
86
79
  try {
87
80
  if (e = this.shouldUpdate(this.__changedProperties), e) {
88
- this.__controllers.forEach((e) => e.hostUpdate && e.hostUpdate()), this.willUpdate(this.__changedProperties), this.resetRenderState(), s(this);
81
+ this.__controllers.forEach((e) => e.hostUpdate && e.hostUpdate()), this.willUpdate(this.__changedProperties), this.resetRenderState(), d(this);
89
82
  let e = this._finalizeRenderOutput(this.render());
90
- this.__notifyListeners(e), c(), this.updated(this.__changedProperties), this.__controllers.forEach((e) => e.hostUpdated && e.hostUpdated());
83
+ this.__notifyListeners(e), f(), this.updated(this.__changedProperties), this.__controllers.forEach((e) => e.hostUpdated && e.hostUpdated());
91
84
  }
92
85
  } catch (e) {
93
- console.error("Error during update:", e), o[o.length - 1] === this && c();
86
+ console.error("Error during update:", e), u[u.length - 1] === this && f();
94
87
  }
95
88
  return this.__changedProperties = /* @__PURE__ */ new Map(), this.__updatePromise = null, e;
96
89
  }
@@ -106,9 +99,11 @@ var o = [], s = (e) => o.push(e), c = () => o.pop(), l = /* @__PURE__ */ new Wea
106
99
  _getStyleScopeId() {
107
100
  return e(this.constructor)?.scopeId;
108
101
  }
109
- _finalizeRenderOutput(t) {
110
- let n = e(this.constructor);
111
- return this._claimTemplateOwnership(t, n?.scopeId), n ? f(d(this.constructor, n), t) : t;
102
+ _finalizeRenderOutput(n) {
103
+ let r = e(this.constructor);
104
+ if (this._claimTemplateOwnership(n, r?.scopeId), !r || r.cssText === "" || m(n) || i(n) && n.__cossackOwner === this) return n;
105
+ let a = new t(p, [n]);
106
+ return a.__cossackOwner = this, a.__cossackScope = r.scopeId, a;
112
107
  }
113
108
  _claimTemplateOwnership(e, t) {
114
109
  if (!(!e || typeof e != "object")) {
@@ -129,11 +124,11 @@ var o = [], s = (e) => o.push(e), c = () => o.pop(), l = /* @__PURE__ */ new Wea
129
124
  mount(e, t = !1) {
130
125
  this._frameworkAttachRenderRoot(e, () => !0, t), this.requestUpdate();
131
126
  }
132
- _frameworkAttachRenderRoot(e, t, o = !1) {
133
- let s = o, c = (o) => {
127
+ _frameworkAttachRenderRoot(e, t, a = !1) {
128
+ let s = a, c = (a) => {
134
129
  if (!t()) return;
135
- let c = i(o) ? o : n`${o}`;
136
- s ? (s = !1, r(c, e, import.meta.env?.DEV ? this.hydrationMetadata : void 0)) : a(c, e);
130
+ let c = i(a) ? a : n`${a}`;
131
+ s ? (s = !1, r(c, e, import.meta.env?.DEV ? this.hydrationMetadata : void 0)) : o(c, e);
137
132
  };
138
133
  return this.addRenderListener(c), () => this.removeRenderListener(c);
139
134
  }
@@ -167,4 +162,4 @@ var o = [], s = (e) => o.push(e), c = () => o.pop(), l = /* @__PURE__ */ new Wea
167
162
  }
168
163
  };
169
164
  //#endregion
170
- export { p as CossackElement, o as instanceStack, c as popCurrentInstance, s as pushCurrentInstance };
165
+ export { s as COMPONENT_RANGE, h as CossackElement, c as getComponentRange, u as instanceStack, f as popCurrentInstance, d as pushCurrentInstance, l as setComponentRange };
@@ -12,12 +12,12 @@ export declare class TemplateResult<T extends TemplateResultType = TemplateResul
12
12
  __cossackScope?: string;
13
13
  }
14
14
  export type SVGTemplateResult = TemplateResult<2>;
15
+ /** The types template diagnostics check bindings against; see attributes.ts. */
16
+ export type { TemplateTypeMap } from './attributes.js';
15
17
  export declare const html: (strings: TemplateStringsArray, ...values: unknown[]) => TemplateResult<1>;
16
18
  export declare const svg: (strings: TemplateStringsArray, ...values: unknown[]) => SVGTemplateResult;
17
19
  /** Lit-compatible sentinel that removes the value in its binding context. */
18
20
  export declare const nothing: unique symbol;
19
- export type ValueSanitizer = (value: unknown) => unknown;
20
- export type SanitizerFactory = (node: Node, name: string, type: 'property' | 'attribute') => ValueSanitizer;
21
21
  export declare function component<T extends CossackElement>(clazz: new () => T, props: T['props'], children?: unknown): TemplateResult<1>;
22
22
  export declare function component<T extends CossackElement>(clazz: new () => T, props?: T['props'], children?: unknown): TemplateResult<1>;
23
23
  export declare const isTemplateResult: (value: unknown) => value is TemplateResult;
@@ -63,4 +63,3 @@ export declare const render: (result: TemplateResult, container: Node) => void;
63
63
  * back to a full `render()` so behaviour is always correct.
64
64
  */
65
65
  export declare const hydrate: (result: TemplateResult, container: Node, metadata?: HydrationDiagnosticMetadata) => void;
66
- export {};