@cossackframework/renderer 0.8.3 → 0.9.1

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.
Files changed (43) hide show
  1. package/README.md +171 -29
  2. package/agent-docs.json +121 -0
  3. package/dist/attributes.d.ts +824 -0
  4. package/dist/attributes.js +1 -0
  5. package/dist/browser-ssr.d.ts +5 -0
  6. package/dist/browser-ssr.js +7 -0
  7. package/dist/browser.d.ts +2 -0
  8. package/dist/browser.js +11 -0
  9. package/dist/component-tags.d.ts +69 -0
  10. package/dist/component-tags.js +106 -0
  11. package/dist/cossack-element.d.ts +36 -2
  12. package/dist/cossack-element.js +26 -28
  13. package/dist/cossack-html.d.ts +23 -5
  14. package/dist/cossack-html.js +1059 -551
  15. package/dist/css-result.d.ts +9 -0
  16. package/dist/css-result.js +14 -0
  17. package/dist/css-scoping.d.ts +4 -0
  18. package/dist/css-scoping.js +95 -0
  19. package/dist/css.d.ts +3 -16
  20. package/dist/css.js +13 -111
  21. package/dist/diagnostics.d.ts +85 -0
  22. package/dist/diagnostics.js +465 -0
  23. package/dist/directives.d.ts +13 -7
  24. package/dist/directives.js +7 -7
  25. package/dist/index.d.ts +2 -0
  26. package/dist/index.js +9 -7
  27. package/dist/minify-html.d.ts +22 -0
  28. package/dist/minify-html.js +123 -18
  29. package/dist/server.d.ts +3 -1
  30. package/dist/server.js +6 -4
  31. package/dist/ssr-form-values.d.ts +2 -0
  32. package/dist/ssr-form-values.js +72 -0
  33. package/dist/style-registry.d.ts +42 -0
  34. package/dist/style-registry.js +73 -0
  35. package/dist/style-runtime.d.ts +47 -0
  36. package/dist/style-runtime.js +43 -0
  37. package/dist/template-analysis.d.ts +224 -0
  38. package/dist/template-analysis.js +321 -0
  39. package/dist/template-parse.d.ts +157 -0
  40. package/dist/template-parse.js +526 -0
  41. package/dist/template.d.ts +2 -0
  42. package/dist/template.js +3 -0
  43. package/package.json +26 -6
@@ -1,9 +1,14 @@
1
1
  // @ts-self-types="./cossack-html.d.ts"
2
- import { isComponentResult as e } from "./component.js";
3
- import { CossackElement as t, popCurrentInstance as n, pushCurrentInstance as r } from "./cossack-element.js";
4
- import { BindResult as i, CacheResult as a, GuardResult as o, IfDefinedResult as s, KeyResult as c, LiveResult as l, PreventDefaultResult as u, RepeatResult as d, resolveField as f, setField as p } from "./directives.js";
2
+ import { serializeFormValues as e } from "./ssr-form-values.js";
3
+ import { RAW_TEXT_TAGS as t, getTemplateAnalysis as n, tokenizeHtml as r } from "./template-analysis.js";
4
+ import { recordStyleScope as i, stylesForScope as a } from "./style-runtime.js";
5
+ import { acquireStyle as o, flushStyleAcquisitions as s, isManagedStyle as c, releaseStyle as l, updateStyleRoot as u } from "./style-registry.js";
6
+ import { isComponentResult as d } from "./component.js";
7
+ import { compileComponentTags as f, mayContainComponentTags as ee } from "./component-tags.js";
8
+ import { CossackElement as p, getComponentRange as te, popCurrentInstance as ne, pushCurrentInstance as re, setComponentRange as m } from "./cossack-element.js";
9
+ import { BindResult as h, CacheResult as g, GuardResult as _, IfDefinedResult as v, KeyResult as y, LiveResult as b, PreventDefaultResult as ie, RepeatResult as x, resolveField as S, setField as ae } from "./directives.js";
5
10
  //#region src/cossack-html.ts
6
- var m = class {
11
+ var oe = class {
7
12
  strings;
8
13
  values;
9
14
  _cossack_template_result = !0;
@@ -13,42 +18,123 @@ var m = class {
13
18
  }
14
19
  __cossackOwner;
15
20
  __cossackScope;
16
- }, h = (e, ...n) => {
17
- let r = new m(e, n, 1), i = t.currentRenderingInstance;
21
+ }, C = (e, t, n) => {
22
+ let r = new oe(e, t, n), i = p.currentRenderingInstance;
18
23
  return i && (r.__cossackOwner = i, r.__cossackScope = i._getStyleScopeId()), r;
19
- }, g = (e, ...n) => {
20
- let r = new m(e, n, 2), i = t.currentRenderingInstance;
21
- return i && (r.__cossackOwner = i, r.__cossackScope = i._getStyleScopeId()), r;
22
- }, _ = Symbol.for("cossack-nothing"), v = (e, n, r) => h`${{
23
- _type: "COMPONENT",
24
- clazz: e,
25
- props: n ?? {},
26
- children: r,
27
- parent: t.currentRenderingInstance
28
- }}`, y = (e) => typeof e == "object" && !!e && e._cossack_template_result === !0, b = Symbol.for("@cossackframework/renderer/unsafe-html"), x = class {
24
+ }, se = /* @__PURE__ */ new WeakMap(), ce = (e, t) => {
25
+ let n = se.get(e);
26
+ return n === void 0 && (n = ee(e) ? f(e, { svg: t }) : null, se.set(e, n)), n;
27
+ }, le = (e) => e === null ? "null" : typeof e == "function" ? `function ${e.name || "(anonymous)"}` : typeof e, ue = (e, t, n) => {
28
+ let r = t[e.binding];
29
+ if (typeof r != "function") throw TypeError(`[cossack/renderer] A component tag <\${...}> must bind a component class, not ${le(r)}.`);
30
+ if (e.closeBinding !== null && t[e.closeBinding] !== r) throw Error(`[cossack/renderer] The closing tag of <\${${r.name || "Component"}}> binds ${le(t[e.closeBinding])}. Close a component tag with the same class.`);
31
+ let i = {};
32
+ for (let n of e.props) if (n.kind === "static") i[n.name] = n.value;
33
+ else if (n.kind === "value") i[n.name] = t[n.binding];
34
+ else if (n.kind === "spread") {
35
+ let e = t[n.binding];
36
+ typeof e == "object" && e && Object.assign(i, e);
37
+ } else if (ze(n.bindings, t)) delete i[n.name];
38
+ else {
39
+ let e = n.segments[0];
40
+ for (let r = 0; r < n.bindings.length; r++) e += M(t[n.bindings[r]]) + n.segments[r + 1];
41
+ i[n.name] = e;
42
+ }
43
+ let a = e.children;
44
+ return pe(r, i, a.kind === "none" ? void 0 : a.kind === "text" ? a.text : a.kind === "slot" ? de(a.slot, t, n) : w(a.plan, t, n));
45
+ }, de = (e, t, n) => typeof e == "number" ? t[e] : ue(e, t, n), w = (e, t, n) => C(e.strings, e.slots.map((e) => de(e, t, n)), n), T = (e, ...t) => {
46
+ let n = ce(e, !1);
47
+ return n ? w(n, t, 1) : C(e, t, 1);
48
+ }, fe = (e, ...t) => {
49
+ let n = ce(e, !0);
50
+ return n ? w(n, t, 2) : C(e, t, 2);
51
+ }, E = Symbol.for("cossack-nothing");
52
+ function pe(e, t, n) {
53
+ return T`${{
54
+ _type: "COMPONENT",
55
+ clazz: e,
56
+ props: t ?? {},
57
+ children: n,
58
+ parent: p.currentRenderingInstance
59
+ }}`;
60
+ }
61
+ var D = (e) => typeof e == "object" && !!e && e._cossack_template_result === !0, me = Symbol.for("@cossackframework/renderer/unsafe-html"), he = class {
29
62
  value;
30
- [b] = !0;
63
+ [me] = !0;
31
64
  constructor(e) {
32
65
  this.value = e;
33
66
  }
34
- }, S = (e) => new x(e), C = (e) => typeof e == "object" && !!e && e[b] === !0, w = (e) => String(e).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;"), T = (s, u = {}) => {
35
- if (s === _ || s === "" || s == null || s === !1) return "";
36
- if (Array.isArray(s)) return u.hydrate ? s.map((e) => `<!--CSA-S-->${T(e, u)}<!--CSA-E-->`).join("") : s.map((e) => T(e, u)).join("");
37
- if (y(s)) return F(s, u);
38
- if (s instanceof l) return T(s.value, u);
39
- if (s instanceof d) return u.hydrate ? s.items.map((e, t) => `<!--CSA-S-->${T(s.templateFn(e, t), u)}<!--CSA-E-->`).join("") : s.items.map((e, t) => T(s.templateFn(e, t), u)).join("");
40
- if (s instanceof c) return T(s.template, u);
41
- if (s instanceof o) return T(s.factory(), u);
42
- if (s instanceof a) return T(s.value, u);
43
- if (s instanceof i) return T(f(s.component, s.fieldName), u);
44
- if (e(s)) {
45
- let e = new s.clazz();
46
- Object.assign(e, s.props), "props" in e && (e.props = s.props), e.children = s.children, e.__parent = s.parent || t.currentRenderingInstance, s.serviceScope && typeof e._setServiceScope == "function" && e._setServiceScope(s.serviceScope), e.__parent && (e._id = `${e.__parent._id}:${e.__parent._childCounter++}`), e.connectedCallback(), r(e), e.willUpdate(/* @__PURE__ */ new Map());
47
- let i = e._finalizeRenderOutput(e.render()), a = "";
48
- return i && (a = y(i) ? F(i, u) : F(h`${i}`, u)), n(), a;
49
- }
50
- return C(s) ? s.value : typeof s == "function" || typeof s == "object" && s && "value" in s ? "" : w(s);
51
- }, E = /* @__PURE__ */ new Set([
67
+ }, ge = (e) => new he(e), O = (e) => typeof e == "object" && !!e && e[me] === !0, _e = {
68
+ "&": "&amp;",
69
+ "<": "&lt;",
70
+ ">": "&gt;",
71
+ "\"": "&quot;",
72
+ "'": "&#039;"
73
+ }, ve = /[&<>"']/, ye = /[&<>"']/g, k = (e) => {
74
+ let t = String(e);
75
+ return ve.test(t) ? t.replace(ye, (e) => _e[e]) : t;
76
+ }, A = (e) => typeof e == "function" ? "" : String(e), j = (e) => {
77
+ for (;;) if (e instanceof b || e instanceof v) e = e.value;
78
+ else if (e instanceof h) e = S(e.component, e.fieldName);
79
+ else return e;
80
+ }, M = (e) => (e = j(e), e === E || e == null || typeof e == "function" ? "" : String(e)), be = (e) => !e.whole && !/^[.?@]/.test(e.name) && e.name !== "ref" && e.name !== "...", xe = (e) => d(e) || Array.isArray(e) || e instanceof x || e instanceof y || e instanceof _ || e instanceof g, Se = (e) => {
81
+ let t = e.__parent;
82
+ if (!t) return;
83
+ let n;
84
+ do
85
+ n = `${t._id}:${t._childCounter++}`;
86
+ while (t._liveChildIds.has(n));
87
+ e._id = n, t._liveChildIds.set(n, e), P(t);
88
+ }, Ce = (e) => {
89
+ e.__parent && (e.__parent._childCounter++, P(e.__parent));
90
+ }, N = null, P = (e, t = 1) => {
91
+ if (!N) return;
92
+ let n = N.slots ??= /* @__PURE__ */ new Map();
93
+ n.set(e, (n.get(e) ?? 0) + t);
94
+ }, we = (e) => {
95
+ let t = N, n = { slots: null };
96
+ N = n;
97
+ try {
98
+ e();
99
+ } finally {
100
+ if (N = t, n.slots) for (let [e, t] of n.slots) P(e, t);
101
+ }
102
+ return n.slots;
103
+ }, Te = (e) => {
104
+ if (e) for (let [t, n] of e) t._childCounter += n, P(t, n);
105
+ }, Ee = (e) => {
106
+ let t = e.__parent?._liveChildIds;
107
+ t?.get(e._id) === e && t.delete(e._id);
108
+ }, De = (e, t) => {
109
+ e.willUpdate(/* @__PURE__ */ new Map());
110
+ let n = e._getWrappedTemplate, r = typeof n == "function" ? n.call(e) : e._finalizeRenderOutput(e.render());
111
+ return r ? We(D(r) ? r : T`${r}`, t) : "";
112
+ }, F = (e, t = {}) => {
113
+ if (e === E || e === "" || e == null || e === !1) return "";
114
+ if (Array.isArray(e)) return t.hydrate ? e.map((e) => `<!--CSA-S-->${F(e, t)}<!--CSA-E-->`).join("") : e.map((e) => F(e, t)).join("");
115
+ if (D(e)) return We(e, t);
116
+ if (e instanceof b || e instanceof v) return F(e.value, t);
117
+ if (e instanceof x) return t.hydrate ? e.items.map((n, r) => `<!--CSA-S-->${F(e.templateFn(n, r), t)}<!--CSA-E-->`).join("") : e.items.map((n, r) => F(e.templateFn(n, r), t)).join("");
118
+ if (e instanceof y) return F(e.template, t);
119
+ if (e instanceof _) return F(e.factory(), t);
120
+ if (e instanceof g) return F(e.value, t);
121
+ if (e instanceof h) return F(S(e.component, e.fieldName), t);
122
+ if (d(e)) {
123
+ let n = new e.clazz();
124
+ Object.assign(n, e.props), "props" in n && (n.props = e.props), n.children = e.children, n.__parent = e.parent || p.currentRenderingInstance, e.serviceScope && typeof n._setServiceScope == "function" && n._setServiceScope(e.serviceScope), n.__parent && (n._id = `${n.__parent._id}:${n.__parent._childCounter++}`), n.connectedCallback(), re(n);
125
+ try {
126
+ return De(n, t);
127
+ } finally {
128
+ ne();
129
+ try {
130
+ n.disconnectedCallback();
131
+ } catch (e) {
132
+ console.error("[cossack] SSR component disconnection failed:", e);
133
+ }
134
+ }
135
+ }
136
+ return O(e) ? e.value : k(A(e));
137
+ }, I = /* @__PURE__ */ new Set([
52
138
  "allowfullscreen",
53
139
  "async",
54
140
  "autofocus",
@@ -74,185 +160,300 @@ var m = class {
74
160
  "required",
75
161
  "reversed",
76
162
  "selected"
77
- ]), D = (e, t) => E.has(e.toLowerCase()) ? t ? "" : null : String(t), O = (e, t) => {
78
- if (!(e === _ || typeof e != "object" || !e)) for (let [n, r] of Object.entries(e)) {
79
- if (n.startsWith("@")) continue;
80
- let e = n, a = r;
81
- if (n.startsWith("?")) {
82
- r !== _ && r && (t.result += ` ${n.slice(1)}`);
83
- continue;
84
- }
85
- if (n.startsWith(".")) {
86
- if (e = n.slice(1), a instanceof i ? a = f(a.component, a.fieldName) : a instanceof l && (a = a.value), a === _) continue;
87
- if (E.has(e)) {
88
- a && (t.result += ` ${e}`);
89
- continue;
90
- }
91
- }
92
- if ((e === "class" || e === "className") && typeof a == "string" && a) {
93
- let e = k(t.result, a);
94
- if (e !== null) {
95
- t.result = e;
96
- continue;
97
- }
98
- }
99
- if (a !== _) if (a instanceof s) a.value !== void 0 && (t.result += ` ${e}="${w(String(a.value))}"`);
100
- else if (typeof a == "boolean") {
101
- let n = D(e, a);
102
- n === "" ? t.result += ` ${e}` : n !== null && (t.result += ` ${e}="${n}"`);
103
- } else typeof a == "function" || a != null && (t.result += ` ${e}="${w(a)}"`);
104
- }
105
- }, k = (e, t) => {
106
- let n = e.lastIndexOf("<"), r = n >= 0 ? e.slice(n) : e, i = r.match(/(^|\s)class="([^"]*)"(\s|\/?>|$)/);
107
- if (!i) return null;
108
- let a = i[1], o = i[2], s = i[3], c = t.trim();
109
- if (!c) return e;
110
- let l = o ? `${o} ${c}` : c, u = r.replace(i[0], `${a}class="${l}"${s}`);
111
- return e.slice(0, n) + u;
112
- }, A = (e) => {
113
- let t = [], n = !1, r = null, i = /(\.\.\.|[.@?]?[a-zA-Z0-9_:-]+)=["']?$/;
114
- for (let a = 0; a < e.length - 1; a++) {
115
- let o = e[a];
116
- for (let e = 0; e < o.length; e++) r ? o[e] === r && (r = null) : n && (o[e] === "\"" || o[e] === "'") ? e > 0 && o[e - 1] === "=" && (r = o[e]) : o[e] === "<" && o[e + 1] !== "!" && o[e + 1] !== "/" ? n = !0 : o[e] === ">" && (n = !1);
117
- let s = o.match(i);
118
- t[a] = !(n && (s || r));
163
+ ]), L = (e, t) => I.has(e.toLowerCase()) ? t ? "" : null : String(t), Oe = (e) => (e instanceof v && (e = e.value === void 0 ? void 0 : String(e.value)), e === E || e == null || e === !1 || typeof e == "function" ? "" : String(e)), ke = (e) => e.split(/[ \t\n\f\r]+/).filter(Boolean), Ae = /^[?.@]?(?![?.@])[^\s"'<>/=\u0000-\u001f\u007f-\u009f]+$/u, je = (e) => {
164
+ let t = {
165
+ entries: /* @__PURE__ */ new Map(),
166
+ classes: [],
167
+ events: /* @__PURE__ */ new Map()
168
+ };
169
+ for (let n = 0; n < e.length; n++) {
170
+ let r = e[n];
171
+ if (!(typeof r != "object" || !r)) for (let [e, i] of Object.entries(r)) Ae.test(e) && (e === "class" || e === "className" ? t.classes.push(...ke(Oe(i))) : e.startsWith("@") ? t.events.set(`${n}:${e}`, {
172
+ key: e,
173
+ value: i
174
+ }) : (t.entries.delete(e), t.entries.set(e, i)));
119
175
  }
120
176
  return t;
121
- }, j = (e) => {
122
- let t = 0, n;
123
- do
124
- n = `\uE000cossack:${t++}:`;
125
- while (e.some((e) => e.includes(n)));
126
- let r = ":";
127
- return {
128
- marker: (e) => `${n}${e}${r}`,
129
- pattern: RegExp(`${n}(\\d+)${r}`, "g")
177
+ }, Me = (e, t, n) => {
178
+ if (t instanceof h && (t = S(t.component, t.fieldName)), t instanceof b && (t = t.value), t === E) return "";
179
+ if (e === "value") return ` ${n ? "value" : ".value"}="${k(String(t ?? ""))}"`;
180
+ let r = e.toLowerCase();
181
+ return I.has(r) && t ? ` ${r}` : "";
182
+ }, Ne = (e, t) => {
183
+ if (t === E || t == null || typeof t == "function") return null;
184
+ if (t instanceof v) return t.value === void 0 ? null : ` ${e}="${k(String(t.value))}"`;
185
+ if (typeof t == "boolean") {
186
+ let n = L(e, t);
187
+ return n === null ? null : n === "" ? ` ${e}` : ` ${e}="${n}"`;
188
+ }
189
+ return ` ${e}="${k(t)}"`;
190
+ }, Pe = (e, t) => {
191
+ let n = /* @__PURE__ */ new Map(), r = (e, t) => {
192
+ let r = e.toLowerCase();
193
+ n.delete(r), n.set(r, t);
130
194
  };
131
- }, M = (e, t) => {
132
- if (!t) return e;
133
- let n = j(e), r = "";
134
- for (let t = 0; t < e.length; t++) r += e[t], t < e.length - 1 && (r += n.marker(t));
135
- let i = "", a = !1, o = null, s = "", c = "";
136
- for (let e = 0; e < r.length;) {
137
- if (c) {
138
- let t = `</${c}`;
139
- if (r.slice(e, e + t.length).toLowerCase() === t) c = "";
140
- else {
141
- i += r[e++];
142
- continue;
143
- }
144
- }
145
- if (!a && r.startsWith("<!--", e)) {
146
- let t = r.indexOf("-->", e + 4), n = t < 0 ? r.length : t + 3;
147
- i += r.slice(e, n), e = n;
148
- continue;
149
- }
150
- if (!a && r[e] === "<" && /[A-Za-z]/.test(r[e + 1] ?? "")) {
151
- let n = e + 2;
152
- for (; n < r.length && !/[\s/>]/.test(r[n]);) n++;
153
- s = r.slice(e + 1, n).toLowerCase(), i += r.slice(e, n), i += ` data-cossack-scope="${t}"`, e = n, a = !0;
154
- continue;
155
- }
156
- let n = r[e++];
157
- i += n, a && (o ? n === o && (o = null) : n === "\"" || n === "'" ? o = n : n === ">" && (a = !1, /^(script|style|textarea|title)$/.test(s) && (c = s), s = ""));
158
- }
159
- let l = [], u = 0, d;
160
- for (; d = n.pattern.exec(i);) l.push(i.slice(u, d.index)), u = d.index + d[0].length;
161
- return l.push(i.slice(u)), l;
162
- }, N = (e, t) => {
163
- let n = j(e), r = "";
164
- for (let t = 0; t < e.length; t++) r += e[t], t < e.length - 1 && (r += n.marker(t));
165
- let i = /* @__PURE__ */ new Map(), a = /<(?![!/])(?:[^>"']|"[^"]*"|'[^']*')*>/g, o;
166
- for (; o = a.exec(r);) {
167
- let e = /\s+([^\s=/>]+)\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]*)/g, r;
168
- for (; r = e.exec(o[0]);) {
169
- let e = r[0], a = [...e.matchAll(n.pattern)];
170
- if (a.length === 0) continue;
171
- let o = a.map((e) => Number(e[1]));
172
- if (!o.some((e) => t[e] === _)) continue;
173
- let s = a[0], c = a[a.length - 1];
174
- i.set(o[0], {
175
- first: o[0],
176
- last: o[o.length - 1],
177
- prefix: e.slice(0, s.index),
178
- suffix: e.slice(c.index + c[0].length)
179
- });
180
- }
195
+ for (let [n, i] of e) if (n.startsWith("?")) r(n.slice(1), i !== E && i ? ` ${n.slice(1)}` : null);
196
+ else if (n.startsWith(".")) {
197
+ let e = n.slice(1), a = e === "value" ? t ? "value" : ".value" : I.has(e.toLowerCase()) ? e : void 0;
198
+ a && r(a, Me(e, i, t) || null);
199
+ } else r(n, Ne(n, i));
200
+ return n;
201
+ }, Fe = (e, t, n) => {
202
+ let i = r(e.slice(t))[0];
203
+ if (i?.type !== "start-tag") return e;
204
+ let a = i.attributes.find((e) => e.name.toLowerCase() === "class"), o = a && a.valueStart >= 0 ? ke(e.slice(t + a.valueStart, t + a.valueEnd)) : [], s = [.../* @__PURE__ */ new Set([...o, ...n.map((e) => k(e))])].join(" ");
205
+ if (!a) {
206
+ let n = t + (i.closed ? i.end - (i.selfClosing ? 2 : 1) : i.end);
207
+ return `${e.slice(0, n)} class="${s}"${e.slice(n)}`;
208
+ }
209
+ let c = t + (a.valueStart >= 0 ? a.valueStart : a.nameStart), l = t + (a.valueStart >= 0 ? a.valueEnd : a.nameEnd);
210
+ if (a.valueStart >= 0 && a.quote) return e.slice(0, c) + s + e.slice(l);
211
+ let u = `"${s.replace(/"/g, "&quot;")}"`;
212
+ return e.slice(0, c) + (a.valueStart >= 0 ? u : `class=${u}`) + e.slice(l);
213
+ }, Ie = (e, t, n, i, a) => {
214
+ let o = (e.slice(t, n) + e.slice(i)).toLowerCase(), s = !1;
215
+ for (let e of a.keys()) if (o.includes(e)) {
216
+ s = !0;
217
+ break;
181
218
  }
182
- return i;
183
- }, P = class {
219
+ if (!s) return e;
220
+ let c = r(e.slice(t))[0];
221
+ if (c?.type !== "start-tag") return e;
222
+ for (let r = c.attributes.length - 1; r >= 0; r--) {
223
+ let o = c.attributes[r], s = t + o.nameStart;
224
+ if (s >= n && s < i || !a.has(o.name.toLowerCase())) continue;
225
+ let l = t + (o.valueStart >= 0 ? o.valueEnd + +!!o.quote : o.nameEnd), u = s;
226
+ for (; u > t && /[ \t\n\f\r]/.test(e[u - 1]);) u--;
227
+ e = e.slice(0, u) + e.slice(l);
228
+ }
229
+ return e;
230
+ }, Le = (e, t) => {
231
+ let { entries: n, classes: r } = je(t.values), i = Pe(n, t.directValue), a = "";
232
+ for (let e of i.values()) e && (a += e);
233
+ let { tagStart: o, insertAt: s } = t;
234
+ return e = e.slice(0, s) + a + e.slice(s), i.size > 0 && (e = Ie(e, o, s, s + a.length, i)), r.length > 0 ? Fe(e, o, r) : e;
235
+ }, Re = (e) => n(e.strings, e.__cossackScope), ze = (e, t) => {
236
+ for (let n = 0; n < e.length; n++) if (t[e[n]] === E) return !0;
237
+ return !1;
238
+ }, Be = (e) => (e = j(e), e === E || e == null || e === !1 ? "" : Array.isArray(e) ? e.map(Be).join("") : O(e) ? e.value : A(e)), Ve = (e, t) => {
239
+ let n = Be(t);
240
+ return e === "script" ? n.replace(/<\/(script)/gi, "<\\/$1").replace(/<!--/g, "\\u003c!--") : e === "style" ? n.replace(/<\/(style)/gi, "<\\/$1") : n;
241
+ }, He = (e, t, n) => {
242
+ let r = Ve(e, t);
243
+ if (e === "script" || e === "style") return r;
244
+ let i = k(r);
245
+ return e === "textarea" && n && i.startsWith("\n") ? `\n${i}` : i;
246
+ }, Ue = class {
184
247
  resultObj;
185
248
  opts;
186
249
  result = "";
187
250
  stringIdx = 0;
188
251
  charIdxForNext = 0;
189
- isNodePositions;
190
- strings;
191
- nothingAttributeGroups;
252
+ analysis;
253
+ stringStarts = [];
254
+ pendingSpreads = null;
192
255
  constructor(e, t = {}) {
193
- this.resultObj = e, this.opts = t, this.strings = M(e.strings, e.__cossackScope), this.isNodePositions = A(this.strings), this.nothingAttributeGroups = N(this.strings, e.values);
256
+ this.resultObj = e, this.opts = t, this.analysis = Re(e), e.__cossackScope !== void 0 && i(e.__cossackScope);
194
257
  }
195
258
  scan() {
196
- let { values: e } = this.resultObj, t = this.strings;
259
+ let { values: e } = this.resultObj, { strings: t, bindings: n, attributeGroups: r } = this.analysis;
197
260
  for (; this.stringIdx < t.length;) {
198
- let n = t[this.stringIdx].substring(this.charIdxForNext || 0), r = n.match(/(\.\.\.|[.@?]?[a-zA-Z0-9_:-]+)=["']?$/);
199
- if (this.result += n, this.charIdxForNext = 0, this.stringIdx < t.length - 1) {
200
- let n = e[this.stringIdx], a = this.nothingAttributeGroups.get(this.stringIdx);
201
- if (a) {
202
- this.result.endsWith(a.prefix) && (this.result = this.result.slice(0, -a.prefix.length)), this.stringIdx = a.last, this.charIdxForNext = a.suffix.length, this.stringIdx++;
261
+ let i = t[this.stringIdx], a = this.pendingSpreads;
262
+ if (a && a.endString === this.stringIdx) {
263
+ let e = a.endOffset + 1;
264
+ this.result += i.substring(this.charIdxForNext, e), this.result = Le(this.result, a), this.pendingSpreads = null, this.stringStarts[this.stringIdx] = this.result.length - e, this.result += i.substring(e);
265
+ } else this.stringStarts[this.stringIdx] = this.result.length - this.charIdxForNext, this.result += i.substring(this.charIdxForNext);
266
+ if (this.charIdxForNext = 0, this.stringIdx < t.length - 1) {
267
+ let a = n[this.stringIdx], o = e[this.stringIdx];
268
+ a.kind === "attribute" && o instanceof b && (o = o.value);
269
+ let s = r[this.stringIdx];
270
+ if (s && !(a.kind === "attribute" && a.name === "...") && ze(s.indices, e)) {
271
+ this.result.endsWith(s.prefix) && (this.result = this.result.slice(0, -s.prefix.length)), this.stringIdx = s.indices[s.indices.length - 1], this.charIdxForNext = s.suffixLength, this.stringIdx++;
203
272
  continue;
204
273
  }
205
- if (r && !this.isNodePositions[this.stringIdx]) {
206
- let e = r[0], a = r[1];
274
+ if (a.kind === "attribute" && a.start && !be(a)) {
275
+ let e = i.slice(a.prefixStart), n = a.name;
207
276
  this.result = this.result.substring(0, this.result.length - e.length);
208
- let o = e.endsWith("\"") ? "\"" : e.endsWith("'") ? "'" : "", c = !0;
209
- if (a === "...") {
277
+ let r = a.quote, s = !0;
278
+ if (n === "...") {
210
279
  this.result = this.result.trimEnd();
211
- let e = { result: this.result };
212
- O(n, e), this.result = e.result;
213
- } else if (a === "ref") this.result = this.result.trimEnd();
214
- else if (a.startsWith("@")) this.result = this.result.trimEnd();
215
- else if (a.startsWith("?")) n ? (this.result = this.result.trimEnd(), this.result += ` ${a.slice(1)}`) : this.result = this.result.trimEnd();
216
- else if (a.startsWith(".")) {
217
- let e, t = n instanceof i;
218
- e = t ? f(n.component, n.fieldName) : n;
219
- let r = a.slice(1);
220
- t && E.has(r) ? (this.result = this.result.trimEnd(), e && (this.result += ` ${r}`)) : e != null && e !== !1 ? (this.result = this.result.trimEnd(), this.result += ` ${r}="${T(e, this.opts)}"`) : this.result = this.result.trimEnd();
221
- } else if (n instanceof s) n.value === void 0 ? this.result = this.result.trimEnd() : (this.result = this.result.trimEnd(), this.result += ` ${a}="${w(String(n.value))}"`);
222
- else if (n == null || n === _) this.result = this.result.trimEnd();
223
- else if (typeof n == "boolean") {
280
+ let e = this.stringStarts[a.tagString] + a.tagOffset, t = this.pendingSpreads;
281
+ t?.tagStart === e ? (t.values.push(o), t.insertAt = this.result.length) : this.pendingSpreads = {
282
+ tagStart: e,
283
+ insertAt: this.result.length,
284
+ values: [o],
285
+ directValue: a.directValue,
286
+ endString: a.tagEndString,
287
+ endOffset: a.tagEndOffset
288
+ };
289
+ } else if (n === "ref") this.result = this.result.trimEnd();
290
+ else if (n.startsWith("@")) this.result = this.result.trimEnd();
291
+ else if (n.startsWith("?")) o ? (this.result = this.result.trimEnd(), this.result += ` ${n.slice(1)}`) : this.result = this.result.trimEnd();
292
+ else if (n.startsWith(".")) this.result = this.result.trimEnd(), this.result += Me(n.slice(1), o, a.directValue);
293
+ else if (o instanceof v) o.value === void 0 ? this.result = this.result.trimEnd() : (this.result = this.result.trimEnd(), this.result += ` ${n}="${k(String(o.value))}"`);
294
+ else if (o == null || o === E) this.result = this.result.trimEnd();
295
+ else if (typeof o == "boolean") {
224
296
  this.result = this.result.trimEnd();
225
- let e = D(a, n);
226
- e === "" ? this.result += ` ${a}` : e !== null && (this.result += ` ${a}="${e}"`);
297
+ let e = L(n, o);
298
+ e === "" ? this.result += ` ${n}` : e !== null && (this.result += ` ${n}="${e}"`);
227
299
  } else {
228
300
  this.result += e;
229
- let t = T(n, this.opts);
230
- o ? (this.result += t, c = !1) : this.result += `"${t}"`;
301
+ let t = k(M(o));
302
+ r ? (this.result += t, s = !1) : this.result += `"${t}"`;
231
303
  }
232
- if (o && c) {
304
+ if (r && s) {
233
305
  let e = t[this.stringIdx + 1];
234
- e && e.startsWith(o) && (this.charIdxForNext = 1);
306
+ e && e.startsWith(r) && (this.charIdxForNext = 1);
235
307
  }
236
- } else this.opts.hydrate && this.isNodePositions[this.stringIdx] ? (this.result += `<!--CRP_${this.stringIdx}-->`, this.result += T(n, this.opts), this.result += "<!--/CRP-->") : this.result += T(n, this.opts);
308
+ } else a.kind === "raw-text" ? this.result += He(a.tag, o, a.first) : a.kind === "attribute" ? this.result += k(M(o)) : a.kind === "comment" || a.kind === "element" || (a.kind === "node" && this.opts.hydrate ? (this.result += `<!--CRP_${this.stringIdx}-->`, this.result += F(o, this.opts), this.result += "<!--/CRP-->") : this.result += F(o, this.opts));
237
309
  }
238
310
  this.stringIdx++;
239
311
  }
240
- return this.result;
312
+ return this.pendingSpreads && (this.result = Le(this.result, this.pendingSpreads)), this.result;
241
313
  }
242
- }, F = (e, t = {}) => new P(e, t).scan(), I = /* @__PURE__ */ new WeakMap(), L = (e) => {
243
- let t = I.get(e.strings);
244
- t || (t = /* @__PURE__ */ new Map(), I.set(e.strings, t));
314
+ }, We = (e, t) => new Ue(e, t).scan(), Ge = (t, n = {}) => e(We(t, n)), R = !!import.meta.env?.DEV, Ke = R, qe = (e, t, n) => {
315
+ for (let r = 0, i = 0; r < e.length; r++) if (Object.is(e[r], t) && i++ === n) return r;
316
+ return -1;
317
+ }, z = R ? /* @__PURE__ */ new WeakMap() : void 0, Je = (e, t) => {
318
+ if (!(!R || !t)) for (let n = 0; n < e.length; n++) e[n] && z.set(e[n], {
319
+ ...t,
320
+ fields: void 0,
321
+ statePath: t.fields?.[n] ?? t.statePath
322
+ });
323
+ }, B = (e, t) => ({
324
+ identity: e.identity,
325
+ parts: e.parts.map((n) => {
326
+ if (!n) return;
327
+ let r = n.component;
328
+ if (r) {
329
+ let e = r.token ? r.fork?.(r.token) : void 0;
330
+ if (r.token && !e) return;
331
+ r = {
332
+ ...r,
333
+ token: e,
334
+ instance: t.owners.get(r.instance) ?? r.instance,
335
+ parent: r.parent ? t.owners.get(r.parent) ?? r.parent : r.parent
336
+ };
337
+ }
338
+ return {
339
+ ...n,
340
+ component: r,
341
+ template: n.template ? B(n.template, t) : void 0,
342
+ cached: new Map([...n.cached].map(([e, n]) => [e, B(n, t)])),
343
+ children: B({
344
+ identity: e.identity,
345
+ parts: n.children
346
+ }, t).parts,
347
+ keys: [...n.keys]
348
+ };
349
+ })
350
+ }), V = /* @__PURE__ */ new WeakMap(), H = (e) => e.map((e) => e instanceof K ? e.captureDraft() : void 0), U = (e, t, n) => {
351
+ !t || t.tree.identity !== n || !t.context.active || !t.context.current() || e.forEach((e, n) => {
352
+ e instanceof K && e.restoreDraft(t.tree.parts[n], t.context);
353
+ });
354
+ }, Ye = (e) => {
355
+ let t = q.get(e), n = t ? {
356
+ identity: t.identity,
357
+ parts: H(t.parts)
358
+ } : void 0, r;
359
+ return {
360
+ restore(t, i = []) {
361
+ !n || r || (r = {
362
+ active: !0,
363
+ current: t,
364
+ owners: new Map(i)
365
+ }, V.set(e, {
366
+ tree: n,
367
+ context: r
368
+ }), n = void 0);
369
+ },
370
+ discard() {
371
+ n = void 0, r && (r.active = !1, r.owners.clear(), V.get(e)?.context === r && V.delete(e));
372
+ }
373
+ };
374
+ }, W = (e) => {
375
+ for (let t of e) try {
376
+ t?.dispose?.();
377
+ } catch (e) {
378
+ console.error("[cossack] Failed render cleanup:", e);
379
+ }
380
+ }, Xe = /* @__PURE__ */ new WeakMap(), G = (e) => {
381
+ let t = Xe.get(e.strings);
382
+ t || (t = /* @__PURE__ */ new Map(), Xe.set(e.strings, t));
245
383
  let n = `${e._$litType$}:${e.__cossackScope ?? ""}`, r = t.get(n);
246
384
  return r || (r = {}, t.set(n, r)), r;
247
- }, R = class n {
385
+ }, K = class e {
248
386
  startNode;
249
387
  endNode;
388
+ _draft;
389
+ _componentDraft;
390
+ _cacheDrafts = /* @__PURE__ */ new Map();
391
+ captureDraft() {
392
+ let e = this.componentInstance, t = /* @__PURE__ */ new Map();
393
+ for (let [e, n] of this._cacheDrafts) n.context.active && n.context.current() && t.set(e, B(n.tree, n.context));
394
+ for (let [e, n] of this._cacheMap) e !== this._cachedTemplateIdentity && t.set(e, {
395
+ identity: e,
396
+ parts: H(n.parts)
397
+ });
398
+ return {
399
+ component: e ? {
400
+ instance: e,
401
+ clazz: e.constructor,
402
+ parent: e.__parent,
403
+ scope: e._getServiceScope?.(),
404
+ token: e._frameworkCaptureClientDraft?.(),
405
+ fork: e.constructor._frameworkForkClientDraft?.bind(e.constructor)
406
+ } : void 0,
407
+ template: this._cachedParts && this._cachedTemplateIdentity ? {
408
+ identity: this._cachedTemplateIdentity,
409
+ parts: H(this._cachedParts)
410
+ } : void 0,
411
+ cached: t,
412
+ children: this._childParts.map((e) => e.captureDraft()),
413
+ keys: [...this._partKeys],
414
+ keySet: this._keySet,
415
+ key: this._key
416
+ };
417
+ }
418
+ restoreDraft(e, t) {
419
+ this._draft = e && t.active && t.current() ? {
420
+ snapshot: e,
421
+ context: t
422
+ } : void 0;
423
+ }
424
+ restoreComponentDraft(e) {
425
+ let t = this._draft;
426
+ if (this._componentDraft = void 0, !t || !t.context.active || !t.context.current()) return;
427
+ let n = t.snapshot.component;
428
+ !n || n.clazz !== e.constructor || (n.parent && (t.context.owners.get(n.parent) ?? n.parent)) !== e.__parent || n.scope !== e._getServiceScope?.() || e !== n.instance && n.token && !e._frameworkRestoreClientDraft?.(n.token) || (t.context.owners.set(n.instance, e), t.snapshot.template && (this._componentDraft = {
429
+ tree: t.snapshot.template,
430
+ context: t.context
431
+ }));
432
+ }
433
+ updateComponentTemplate(e) {
434
+ let t = this._componentDraft;
435
+ this._componentDraft = void 0, this.updateNode(e, t ?? null);
436
+ }
250
437
  componentInstance = null;
438
+ range = (() => {
439
+ let e = this;
440
+ return {
441
+ get start() {
442
+ return e.startNode;
443
+ },
444
+ get end() {
445
+ return e.endNode;
446
+ }
447
+ };
448
+ })();
251
449
  renderListener = null;
252
450
  _childParts = [];
253
451
  _partKeys = [];
254
452
  _cachedTemplateIdentity = null;
255
453
  _cachedParts = null;
454
+ _unsafeHTML = void 0;
455
+ _committed = void 0;
456
+ _committedSlots = null;
256
457
  _key = void 0;
257
458
  _keySet = !1;
258
459
  _guardDeps = null;
@@ -267,25 +468,54 @@ var m = class {
267
468
  _beginHydration() {
268
469
  this._hydrating = !0;
269
470
  }
270
- update(t) {
271
- if (this._hydrating) {
272
- this._hydrating = !1, this._hydrateValue(t);
273
- return;
274
- }
275
- t instanceof o && (t = this._resolveGuard(t));
276
- let n = !1;
277
- if (t instanceof a) {
278
- let e = t.value;
279
- if (y(e)) {
280
- this.componentInstance && this.disposeComponent(), this._childParts.length > 0 && this.clearChildParts(), this.updateCache(e);
471
+ update(e) {
472
+ e = j(e);
473
+ let t = this._committed;
474
+ this._committed = void 0;
475
+ try {
476
+ if (!(e instanceof g) && !(e instanceof _) && this._cacheDrafts.clear(), this._hydrating) {
477
+ if (this._hydrating = !1, D(e)) {
478
+ let t = e;
479
+ this._committedSlots = we(() => this._hydrateValue(t)), this._committed = t;
480
+ } else this._hydrateValue(e);
281
481
  return;
282
482
  }
283
- t = e, n = !0;
284
- } else if (this._cacheMap.size > 0 || this._cacheCurrent) {
285
- for (let e of this._cacheMap.values()) for (let t of e.parts) t && typeof t.dispose == "function" && t.dispose();
286
- this._cacheMap.clear(), this._cacheCurrent = null;
483
+ e instanceof _ ? (e = this._resolveGuard(e), e instanceof g || this._cacheDrafts.clear()) : this._guardHasDeps && (this._guardHasDeps = !1, this._guardDeps = null, this._guardValue = void 0), O(e) || (this._unsafeHTML = void 0);
484
+ let n = !1;
485
+ if (e instanceof g) {
486
+ this.prepareCacheDrafts();
487
+ let t = e.value;
488
+ if (D(t)) {
489
+ this.componentInstance && this.disposeComponent(), this._childParts.length > 0 && this.clearChildParts(), this.updateCache(t);
490
+ return;
491
+ }
492
+ e = t, n = !0;
493
+ } else if (this._cacheMap.size > 0 || this._cacheCurrent) {
494
+ for (let e of this._cacheMap.values()) for (let t of e.parts) t && typeof t.dispose == "function" && t.dispose();
495
+ this._cacheMap.clear(), this._cacheCurrent = null;
496
+ }
497
+ if (n && this._cacheCurrent) {
498
+ if (this._stashCurrent(), this._draft) {
499
+ for (let e of this._cacheMap.values()) W(e.parts);
500
+ this._cacheMap.clear();
501
+ }
502
+ this._cacheCurrent = null;
503
+ }
504
+ if (this.componentInstance && (!d(e) || e.clazz !== this.componentInstance.constructor) && this.disposeComponent(), !(e instanceof x) && !Array.isArray(e) && this._childParts.length > 0 && this.clearChildParts(), !(e instanceof y) && this._draft?.snapshot.keySet && (this._draft = void 0), d(e)) this.updateComponent(e);
505
+ else if (e instanceof x) this.updateRepeat(e);
506
+ else if (e instanceof y) this.updateKey(e);
507
+ else if (Array.isArray(e)) this.updateArray(e);
508
+ else if (D(e)) {
509
+ if (e === t && !n && this._draft === void 0 && this._cachedParts && this._cachedTemplateIdentity === G(e) && this.startNode.parentNode) {
510
+ this._committed = e, Te(this._committedSlots);
511
+ return;
512
+ }
513
+ let r = e;
514
+ this._committedSlots = we(() => this.updateNode(r)), this._committed = r;
515
+ } else this.updateNode(e);
516
+ } finally {
517
+ this._draft = void 0;
287
518
  }
288
- n && this._cacheCurrent && (this._stashCurrent(), this._cacheCurrent = null), this.componentInstance && (!e(t) || t.clazz !== this.componentInstance.constructor) && this.disposeComponent(), !(t instanceof d) && !Array.isArray(t) && this._childParts.length > 0 && this.clearChildParts(), e(t) ? this.updateComponent(t) : t instanceof d ? this.updateRepeat(t) : t instanceof c ? this.updateKey(t) : Array.isArray(t) ? this.updateArray(t) : this.updateNode(t);
289
519
  }
290
520
  _resolveGuard(e) {
291
521
  let t = Array.isArray(e.deps) ? [...e.deps] : [e.deps];
@@ -296,95 +526,123 @@ var m = class {
296
526
  for (let n = 0; n < e.length; n++) if (e[n] !== t[n]) return !1;
297
527
  return !0;
298
528
  }
299
- _hydrateValue(t) {
300
- if (e(t)) {
301
- this._adoptComponent(t);
529
+ _hydrateValue(e) {
530
+ if (e = j(e), d(e)) {
531
+ this._adoptComponent(e);
302
532
  return;
303
533
  }
304
- if (y(t)) {
305
- this._adoptTemplate(t);
534
+ if (D(e)) {
535
+ this._adoptTemplate(e);
306
536
  return;
307
537
  }
308
- if (t instanceof d) {
309
- this._adoptSequence(t.items.map((e, n) => t.templateFn(e, n)), t.items.map((e, n) => t.keyFn(e, n)));
538
+ if (e instanceof x) {
539
+ this._adoptSequence(e.items.map((t, n) => e.templateFn(t, n)), e.items.map((t, n) => e.keyFn(t, n)));
310
540
  return;
311
541
  }
312
- if (Array.isArray(t)) {
313
- this._adoptSequence(t);
542
+ if (Array.isArray(e)) {
543
+ this._adoptSequence(e);
314
544
  return;
315
545
  }
316
- if (t instanceof c) {
317
- if (y(t.template)) {
318
- this._keySet = !0, this._key = t.value, this._adoptTemplate(t.template);
546
+ if (e instanceof y) {
547
+ if (D(e.template)) {
548
+ this._keySet = !0, this._key = e.value, this._adoptTemplate(e.template);
319
549
  return;
320
550
  }
321
- this.clear(), this._clearTemplateCache(), this.updateNode(t.template);
551
+ this.clear(), this._clearTemplateCache(), this.updateNode(e.template);
322
552
  return;
323
553
  }
324
- if (t instanceof o) {
325
- let e = this._resolveGuard(t);
326
- this._hydrateValue(e);
554
+ if (e instanceof _) {
555
+ let t = this._resolveGuard(e);
556
+ this._hydrateValue(t);
327
557
  return;
328
558
  }
329
- if (t instanceof a) {
330
- let e = t.value;
331
- if (y(e)) {
332
- let t = L(e);
333
- this._cacheMap.has(t) ? this.updateCache(e) : (this._adoptTemplate(e), this._cacheCurrent = t);
334
- } else this._hydrateValue(e);
559
+ if (e instanceof g) {
560
+ let t = e.value;
561
+ if (D(t)) {
562
+ let e = G(t);
563
+ this._cacheMap.has(e) ? this.updateCache(t) : (this._adoptTemplate(t), this._cacheCurrent = e);
564
+ } else this._hydrateValue(t);
335
565
  return;
336
566
  }
337
- if (t === _ || t === "" || t == null || t === !1) {
567
+ if (e === E || e === "" || e == null || e === !1) {
338
568
  this.clear();
339
569
  return;
340
570
  }
341
- if (!(t instanceof Node)) {
342
- if (C(t)) {
343
- if (this.startNode.nextSibling !== this.endNode) return;
344
- this.clear(), this._clearTemplateCache(), this.updateNode(t);
571
+ if (e instanceof Node) return;
572
+ if (O(e)) {
573
+ if (this.startNode.nextSibling !== this.endNode) {
574
+ this._unsafeHTML = e.value;
345
575
  return;
346
576
  }
347
- if (typeof t != "object") {
348
- let e = String(t), n = this.startNode.nextSibling;
349
- n && n.nodeType === Node.TEXT_NODE && n.nextSibling === this.endNode ? n.nodeValue !== e && (n.nodeValue = e) : (this.clear(), this.startNode.parentNode.insertBefore(document.createTextNode(e), this.endNode));
350
- return;
351
- }
352
- this.clear(), this._clearTemplateCache(), this.updateNode(t);
577
+ this.clear(), this._clearTemplateCache(), this.updateNode(e);
578
+ return;
579
+ }
580
+ let t = A(e);
581
+ if (t === "") {
582
+ this.clear();
583
+ return;
353
584
  }
585
+ let n = this.startNode.nextSibling;
586
+ n && n.nodeType === Node.TEXT_NODE && n.nextSibling === this.endNode ? n.nodeValue !== t && (R && Q(new X("text-content", ft(n), "text", "text"), z.get(this)?.boundary ?? "root", void 0, z.get(this), "text-reconcile"), n.nodeValue = t) : (this.clear(), this.startNode.parentNode.insertBefore(document.createTextNode(t), this.endNode));
354
587
  }
355
588
  _adoptComponent(e) {
356
- let n = e.parent || t.currentRenderingInstance;
357
- if (this.componentInstance && (this.componentInstance.constructor !== e.clazz || this.componentInstance.__parent !== n) && this.disposeComponent(), !this.componentInstance) {
358
- this.componentInstance = new e.clazz(), this.componentInstance.__parent = n, e.serviceScope && typeof this.componentInstance._setServiceScope == "function" && this.componentInstance._setServiceScope(e.serviceScope), this.componentInstance.__parent && (this.componentInstance._id = `${this.componentInstance.__parent._id}:${this.componentInstance.__parent._childCounter++}`);
359
- let t = !0;
360
- this.renderListener = (e) => {
361
- if (t) if (t = !1, y(e)) try {
362
- this._adoptTemplate(e);
363
- } catch (t) {
364
- if (t instanceof K) this.clear(), this._clearTemplateCache(), this.updateNode(e);
365
- else throw t;
589
+ let t = e.parent || p.currentRenderingInstance;
590
+ if (this.componentInstance && (this.componentInstance.constructor !== e.clazz || this.componentInstance.__parent !== t) && this.disposeComponent(), this.componentInstance) Ce(this.componentInstance), m(this.componentInstance, this.range);
591
+ else {
592
+ if (this.componentInstance = new e.clazz(), this.componentInstance.__parent = t, e.serviceScope && typeof this.componentInstance._setServiceScope == "function" && this.componentInstance._setServiceScope(e.serviceScope), Se(this.componentInstance), m(this.componentInstance, this.range), R) {
593
+ let t = this.componentInstance.hydrationMetadata, n = z.get(this);
594
+ z.set(this, {
595
+ ...n,
596
+ ...t,
597
+ component: t?.component ?? e.clazz.name ?? n?.component,
598
+ statePath: t?.statePath,
599
+ fields: t?.fields,
600
+ boundary: "component"
601
+ });
602
+ }
603
+ let n = !0;
604
+ this.renderListener = (t) => {
605
+ if (n) if (n = !1, D(t)) try {
606
+ this._adoptTemplate(t);
607
+ } catch (n) {
608
+ if (n instanceof X) R && Q(n, "component", e.clazz.name || "anonymous", z.get(this)), this.clear(), this._clearTemplateCache(), this.updateComponentTemplate(t);
609
+ else throw n;
366
610
  }
367
- else e == null ? this.clear() : this.updateNode(e);
368
- else this.updateNode(e);
369
- }, this.componentInstance.addRenderListener(this.renderListener), Object.assign(this.componentInstance, e.props), "props" in this.componentInstance && (this.componentInstance.props = e.props), this.componentInstance.children = e.children, this.componentInstance.connectedCallback();
611
+ else t == null ? this.clear() : this.updateComponentTemplate(t);
612
+ else this.updateComponentTemplate(t);
613
+ }, this.componentInstance.addRenderListener(this.renderListener), Object.assign(this.componentInstance, e.props), "props" in this.componentInstance && (this.componentInstance.props = e.props), this.componentInstance.children = e.children;
614
+ try {
615
+ this.componentInstance._frameworkRendererAttached?.(), this.restoreComponentDraft(this.componentInstance), this.componentInstance.connectedCallback();
616
+ } catch (e) {
617
+ throw this.disposeComponent(), e;
618
+ }
370
619
  }
371
- let r = this.componentInstance;
372
- Object.assign(r, e.props), "props" in r && (r.props = e.props), r.children = e.children, r.requestUpdate();
620
+ let n = this.componentInstance;
621
+ this._draft?.snapshot.component?.instance === n && this.restoreComponentDraft(n), Object.assign(n, e.props), "props" in n && (n.props = e.props), n.children = e.children, n.requestUpdate();
373
622
  }
374
- _adoptSequence(e, t) {
375
- let r = Y(this._nodesBetween());
376
- if (r.length !== e.length) throw new K(`sequence length mismatch: SSR has ${r.length} items, client has ${e.length}`);
377
- this._childParts = [], this._partKeys = t ? [] : this._partKeys;
378
- for (let i = 0; i < e.length; i++) {
379
- let [a, o] = r[i], s = new n(a, o);
380
- s._beginHydration(), this._childParts.push(s), t && this._partKeys.push(t[i]), s.update(e[i]);
623
+ _adoptSequence(t, n) {
624
+ let r = dt(this._nodesBetween());
625
+ if (r.length !== t.length) throw new X("sequence-length", "sequence", `${t.length} client items`, `${r.length} SSR items`);
626
+ this._childParts = [], this._partKeys = n ? [] : this._partKeys;
627
+ for (let i = 0; i < t.length; i++) {
628
+ let [a, o] = r[i], s = new e(a, o);
629
+ if (R) {
630
+ let e = z.get(this);
631
+ e && z.set(s, e);
632
+ }
633
+ s._beginHydration(), this._childParts.push(s), n && this._partKeys.push(n[i]), s.update(t[i]);
381
634
  }
382
635
  }
383
636
  _adoptTemplate(e) {
384
- let { fragment: t, parts: n } = W(e), r = this._nodesBetween(), i = /* @__PURE__ */ new Map();
385
- X(t.childNodes, r, i), Z(n, i);
386
- for (let t = 0; t < e.values.length; t++) n[t] && n[t].update(e.values[t]);
387
- this._cachedTemplateIdentity = L(e), this._cachedParts = n;
637
+ let { fragment: t, parts: n } = J(e);
638
+ try {
639
+ let r = this._nodesBetween(), i = /* @__PURE__ */ new Map();
640
+ pt(t.childNodes, r, i, "root", R ? z.get(this) : void 0), mt(n, i), Je(n, R ? z.get(this) : void 0);
641
+ for (let t = 0; t < e.values.length; t++) n[t] && n[t].update(e.values[t]);
642
+ this._cachedTemplateIdentity = G(e), this._cachedParts = n, this.startNode.isConnected && s();
643
+ } catch (e) {
644
+ throw W(n), e;
645
+ }
388
646
  }
389
647
  _nodesBetween() {
390
648
  let e = [], t = this.startNode.nextSibling;
@@ -392,57 +650,104 @@ var m = class {
392
650
  return e;
393
651
  }
394
652
  teardownComponent(e) {
395
- if (this.renderListener &&= (e.removeRenderListener(this.renderListener), null), e.disconnectedCallback(), typeof e.destroy == "function") try {
396
- e.destroy();
397
- } catch {}
653
+ this.renderListener &&= (e.removeRenderListener(this.renderListener), null);
654
+ try {
655
+ e.disconnectedCallback();
656
+ } catch (e) {
657
+ console.error("[cossack] Component disconnection failed:", e);
658
+ } finally {
659
+ try {
660
+ typeof e.destroy == "function" && e.destroy();
661
+ } catch {} finally {
662
+ te(e) === this.range && m(e, void 0), Ee(e), e._frameworkRendererDetached?.();
663
+ }
664
+ }
398
665
  }
399
666
  disposeComponent() {
400
667
  this.componentInstance && (this.teardownComponent(this.componentInstance), this.componentInstance = null, this._clearTemplateCache());
401
668
  }
402
669
  updateComponent(e) {
403
- let n = e.parent || t.currentRenderingInstance;
404
- this.componentInstance && this.componentInstance.__parent !== n && this.disposeComponent(), this.componentInstance || (this.componentInstance = new e.clazz(), this.componentInstance.__parent = n, e.serviceScope && typeof this.componentInstance._setServiceScope == "function" && this.componentInstance._setServiceScope(e.serviceScope), this.componentInstance.__parent && (this.componentInstance._id = `${this.componentInstance.__parent._id}:${this.componentInstance.__parent._childCounter++}`), this.renderListener = (e) => {
405
- this.updateNode(e);
406
- }, this.componentInstance.addRenderListener(this.renderListener), Object.assign(this.componentInstance, e.props), "props" in this.componentInstance && (this.componentInstance.props = e.props), this.componentInstance.children = e.children, this.componentInstance.connectedCallback());
407
- let r = this.componentInstance;
408
- Object.assign(r, e.props), "props" in r && (r.props = e.props), r.children = e.children, r.requestUpdate();
670
+ let t = e.parent || p.currentRenderingInstance;
671
+ if (this.componentInstance && this.componentInstance.__parent !== t && this.disposeComponent(), this.componentInstance) Ce(this.componentInstance), m(this.componentInstance, this.range);
672
+ else {
673
+ this.componentInstance = new e.clazz(), this.componentInstance.__parent = t, e.serviceScope && typeof this.componentInstance._setServiceScope == "function" && this.componentInstance._setServiceScope(e.serviceScope), Se(this.componentInstance), m(this.componentInstance, this.range), this.renderListener = (e) => {
674
+ this.updateComponentTemplate(e);
675
+ }, this.componentInstance.addRenderListener(this.renderListener), Object.assign(this.componentInstance, e.props), "props" in this.componentInstance && (this.componentInstance.props = e.props), this.componentInstance.children = e.children;
676
+ try {
677
+ this.componentInstance._frameworkRendererAttached?.(), this.restoreComponentDraft(this.componentInstance), this.componentInstance.connectedCallback();
678
+ } catch (e) {
679
+ throw this.disposeComponent(), e;
680
+ }
681
+ }
682
+ let n = this.componentInstance;
683
+ this._draft?.snapshot.component?.instance === n && this.restoreComponentDraft(n), Object.assign(n, e.props), "props" in n && (n.props = e.props), n.children = e.children, n.requestUpdate();
409
684
  }
410
685
  updateRepeat(e) {
411
- let { items: t, keyFn: n, templateFn: r } = e, i = /* @__PURE__ */ new Map();
686
+ let { items: t, keyFn: n, templateFn: r } = e, i = /* @__PURE__ */ new Map(), a = [];
412
687
  this._childParts.forEach((e, t) => {
413
- let n = this._partKeys[t];
414
- n !== void 0 && i.set(n, e);
688
+ if (t >= this._partKeys.length) {
689
+ a.push(e);
690
+ return;
691
+ }
692
+ let n = this._partKeys[t], r = i.get(n);
693
+ r ? r.push(e) : i.set(n, [e]);
415
694
  });
416
- let a = [], o = [], s = this.startNode;
417
- t.forEach((e, t) => {
418
- let c = n(e, t);
419
- o.push(c);
420
- let l = i.get(c);
421
- l ? (i.delete(c), l.startNode.previousSibling !== s && this.movePart(l, s), l.update(r(e, t))) : (l = this.createChildPart(s), l.update(r(e, t))), a.push(l), s = l.endNode;
422
- }), i.forEach((e) => e.dispose()), this._childParts = a, this._partKeys = o;
695
+ let o = [], s = [], c = /* @__PURE__ */ new Map(), l = !1, u = this.startNode;
696
+ try {
697
+ t.forEach((e, t) => {
698
+ let a = n(e, t);
699
+ s.push(a);
700
+ let d = c.get(a) ?? 0;
701
+ c.set(a, d + 1), d > 0 && (l = !0);
702
+ let f = i.get(a)?.shift();
703
+ if (f ? f.startNode.previousSibling !== u && this.movePart(f, u) : f = this.createChildPart(u), o.push(f), this._draft) {
704
+ let e = qe(this._draft.snapshot.keys, a, d);
705
+ e !== -1 && f.restoreDraft(this._draft.snapshot.children[e], this._draft.context);
706
+ }
707
+ f.update(r(e, t)), u = f.endNode;
708
+ }), l && Ke && console.warn("[cossack/renderer] repeat() received a duplicate key. Keys must be unique; duplicates are matched by position and lose identity when the list reorders."), i.forEach((e) => e.forEach((e) => e.dispose())), a.forEach((e) => e.dispose()), this._childParts = o, this._partKeys = s;
709
+ } catch (e) {
710
+ throw W([.../* @__PURE__ */ new Set([...this._childParts, ...o])]), this._childParts = [], this._partKeys = [], e;
711
+ }
423
712
  }
424
713
  updateKey(e) {
714
+ this._draft && (!this._draft.snapshot.keySet || !Object.is(this._draft.snapshot.key, e.value)) && (this._draft = void 0);
425
715
  let t = !this._keySet || !Object.is(this._key, e.value);
426
716
  this._keySet = !0, this._key = e.value, t && (this.componentInstance && this.disposeComponent(), this._clearTemplateCache(), this.clear()), this.updateNode(e.template);
427
717
  }
718
+ prepareCacheDrafts() {
719
+ for (let [e, t] of this._cacheDrafts) (!t.context.active || !t.context.current()) && this._cacheDrafts.delete(e);
720
+ if (this._draft && this._draft.context.active && this._draft.context.current()) {
721
+ let { snapshot: e, context: t } = this._draft;
722
+ for (let [e, t] of this._cacheMap) e !== this._cachedTemplateIdentity && (W(t.parts), this._cacheMap.delete(e));
723
+ for (let [n, r] of e.cached) this._cacheDrafts.set(n, {
724
+ tree: r,
725
+ context: t
726
+ });
727
+ e.template && this._cacheDrafts.set(e.template.identity, {
728
+ tree: e.template,
729
+ context: t
730
+ });
731
+ }
732
+ }
428
733
  updateCache(e) {
429
- let t = L(e);
430
- if (this._cacheCurrent === t && this._cachedParts) {
431
- this._cachedParts.forEach((t, n) => {
734
+ let t = G(e), n = this._cacheDrafts.get(t);
735
+ if (this._cacheDrafts.delete(t), this._cacheCurrent === t && this._cachedParts) {
736
+ U(this._cachedParts, n, t), this._cachedParts.forEach((t, n) => {
432
737
  t.update(e.values[n]);
433
738
  });
434
739
  return;
435
740
  }
436
- (this._cacheCurrent || this._cachedParts) && this._stashCurrent();
437
- let n = this._cacheMap.get(t);
438
- if (n) {
741
+ if ((this._cacheCurrent || this._cachedParts) && (this._stashCurrent(), this._draft)) for (let [e, n] of this._cacheMap) e !== t && (W(n.parts), this._cacheMap.delete(e));
742
+ let r = this._cacheMap.get(t);
743
+ if (r) {
439
744
  this.clear();
440
- let r = this.startNode.parentNode;
441
- if (!r) return;
442
- for (let e of n.nodes) r.insertBefore(e, this.endNode);
443
- this._cachedTemplateIdentity = t, this._cachedParts = n.parts;
444
- for (let t = 0; t < e.values.length; t++) n.parts[t] && n.parts[t].update(e.values[t]);
445
- } else this.clear(), this.updateNode(e);
745
+ let i = this.startNode.parentNode;
746
+ if (!i) return;
747
+ for (let e of r.nodes) i.insertBefore(e, this.endNode);
748
+ this._cachedTemplateIdentity = t, this._cachedParts = r.parts, U(r.parts, n, t);
749
+ for (let t = 0; t < e.values.length; t++) r.parts[t] && r.parts[t].update(e.values[t]);
750
+ } else this.clear(), this.updateNode(e, n ?? null);
446
751
  this._cacheCurrent = t;
447
752
  }
448
753
  _stashCurrent() {
@@ -462,19 +767,19 @@ var m = class {
462
767
  updateArray(e) {
463
768
  this._partKeys = [];
464
769
  let t = Math.min(e.length, this._childParts.length);
465
- for (let n = 0; n < t; n++) this._childParts[n].update(e[n]);
770
+ for (let n = 0; n < t; n++) this._draft && this._draft.snapshot.keys.length === 0 && this._childParts[n].restoreDraft(this._draft.snapshot.children[n], this._draft.context), this._childParts[n].update(e[n]);
466
771
  if (e.length > this._childParts.length) {
467
772
  let n = this._childParts.length > 0 ? this._childParts[this._childParts.length - 1].endNode : this.startNode;
468
773
  for (let r = t; r < e.length; r++) {
469
774
  let t = this.createChildPart(n);
470
- this._childParts.push(t), t.update(e[r]), n = t.endNode;
775
+ this._childParts.push(t), this._draft && this._draft.snapshot.keys.length === 0 && t.restoreDraft(this._draft.snapshot.children[r], this._draft.context), t.update(e[r]), n = t.endNode;
471
776
  }
472
777
  }
473
778
  if (e.length < this._childParts.length) for (let t = this._childParts.length - 1; t >= e.length; t--) this._childParts[t].dispose(), this._childParts.pop();
474
779
  }
475
- createChildPart(e) {
476
- let t = document.createComment("CRP-Item"), r = document.createComment("/CRP-Item"), i = e.nextSibling;
477
- return e.parentNode.insertBefore(t, i), e.parentNode.insertBefore(r, i), new n(t, r);
780
+ createChildPart(t) {
781
+ let n = document.createComment("CRP-Item"), r = document.createComment("/CRP-Item"), i = t.nextSibling;
782
+ return t.parentNode.insertBefore(n, i), t.parentNode.insertBefore(r, i), new e(n, r);
478
783
  }
479
784
  movePart(e, t) {
480
785
  let n = t.parentNode, r = [], i = e.startNode;
@@ -487,40 +792,58 @@ var m = class {
487
792
  this._childParts.forEach((e) => e.dispose()), this._childParts = [], this._partKeys = [];
488
793
  }
489
794
  dispose() {
490
- if (this.clear(), this._clearTemplateCache(), this._cacheMap.size > 0) {
795
+ if (this._draft = void 0, this._componentDraft = void 0, this._cacheDrafts.clear(), this.clear(), this._clearTemplateCache(), this._cacheMap.size > 0) {
491
796
  for (let e of this._cacheMap.values()) for (let t of e.parts) t && typeof t.dispose == "function" && t.dispose();
492
797
  this._cacheMap.clear();
493
798
  }
494
799
  this._cacheCurrent = null, this.clearChildParts(), this.startNode.parentNode && this.startNode.parentNode.removeChild(this.startNode), this.endNode.parentNode && this.endNode.parentNode.removeChild(this.endNode), this.componentInstance &&= (this.teardownComponent(this.componentInstance), null);
495
800
  }
496
- updateNode(t) {
497
- if (this.startNode.parentNode) if (y(t)) if (this._cachedTemplateIdentity === L(t) && this._cachedParts) this._cachedParts.forEach((e, n) => {
498
- e.update(t.values[n]);
801
+ updateNode(e, t) {
802
+ let n = t === void 0 ? this._draft?.snapshot.template ? {
803
+ tree: this._draft.snapshot.template,
804
+ context: this._draft.context
805
+ } : void 0 : t ?? void 0;
806
+ if (this.startNode.parentNode) if (e = j(e), O(e) || (this._unsafeHTML = void 0), D(e)) if (this._cachedTemplateIdentity === G(e) && this._cachedParts) U(this._cachedParts, n, G(e)), this._cachedParts.forEach((t, n) => {
807
+ t.update(e.values[n]);
499
808
  });
500
809
  else {
501
810
  this._clearTemplateCache(), this.clear();
502
- let e = document.createDocumentFragment();
503
- G(t, e), this.startNode.parentNode.insertBefore(e, this.endNode);
504
- let n = U.get(e.firstChild?.childNodes[0] || e);
505
- n && (this._cachedTemplateIdentity = n.identity, this._cachedParts = n.parts);
811
+ let t = document.createDocumentFragment();
812
+ n && V.set(t, n), Y(e, t), this.startNode.parentNode.insertBefore(t, this.endNode), this.startNode.isConnected && s();
813
+ let r = q.get(t.firstChild?.childNodes[0] || t);
814
+ r && (this._cachedTemplateIdentity = r.identity, this._cachedParts = r.parts);
506
815
  }
507
- else if (C(t)) {
508
- this._clearTemplateCache(), this.clear();
509
- let e = document.createElement("template");
510
- e.innerHTML = t.value, this.startNode.parentNode.insertBefore(e.content, this.endNode);
511
- } else if (e(t) || typeof t == "object" && t && !(t instanceof Node) && !Array.isArray(t)) {
816
+ else if (O(e)) {
817
+ if (this._unsafeHTML === e.value && this.startNode.nextSibling !== this.endNode) return;
818
+ this._clearTemplateCache(), this.clear(), this._unsafeHTML = e.value;
819
+ let t = this.startNode.parentNode, n = this.startNode.ownerDocument;
820
+ if (t instanceof Element && t.namespaceURI === "http://www.w3.org/2000/svg" && ![
821
+ "foreignObject",
822
+ "desc",
823
+ "title"
824
+ ].includes(t.localName)) {
825
+ let r = n.createElementNS(t.namespaceURI, t.localName);
826
+ r.innerHTML = e.value;
827
+ let i = n.createDocumentFragment();
828
+ for (; r.firstChild;) i.appendChild(r.firstChild);
829
+ t.insertBefore(i, this.endNode);
830
+ } else {
831
+ let r = n.createElement("template");
832
+ r.innerHTML = e.value, t.insertBefore(r.content, this.endNode);
833
+ }
834
+ } else if (xe(e)) {
512
835
  this._clearTemplateCache(), this.clear();
513
- let e = document.createDocumentFragment();
514
- G(h`${t}`, e), this.startNode.parentNode.insertBefore(e, this.endNode);
515
- } else if (t instanceof Node) this._clearTemplateCache(), this.clear(), this.startNode.parentNode.insertBefore(t, this.endNode);
516
- else if (t === _ || t === "" || t == null || t === !1) this._clearTemplateCache(), this.clear();
836
+ let t = document.createDocumentFragment();
837
+ Y(T`${e}`, t), this.startNode.parentNode.insertBefore(t, this.endNode), this.startNode.isConnected && s();
838
+ } else if (e instanceof Node) this._clearTemplateCache(), this.clear(), this.startNode.parentNode.insertBefore(e, this.endNode);
839
+ else if (e === E || e === "" || e == null || e === !1 || typeof e == "function") this._clearTemplateCache(), this.clear();
517
840
  else {
518
- let e = String(t), n = this.startNode.nextSibling;
519
- if (n && n.nodeType === Node.TEXT_NODE && n.nextSibling === this.endNode) n.nodeValue !== e && (n.nodeValue = e);
841
+ let t = A(e), n = this.startNode.nextSibling;
842
+ if (n && n.nodeType === Node.TEXT_NODE && n.nextSibling === this.endNode) n.nodeValue !== t && (n.nodeValue = t), this._clearTemplateCache();
520
843
  else {
521
844
  this._clearTemplateCache(), this.clear();
522
- let t = document.createTextNode(e);
523
- this.startNode.parentNode.insertBefore(t, this.endNode);
845
+ let e = document.createTextNode(t);
846
+ this.startNode.parentNode.insertBefore(e, this.endNode);
524
847
  }
525
848
  }
526
849
  }
@@ -535,74 +858,121 @@ var m = class {
535
858
  e.parentNode.removeChild(e), e = t;
536
859
  }
537
860
  }
538
- }, z = class {
861
+ }, Ze = class {
862
+ use;
863
+ constructor(e, t) {
864
+ this.use = o(t, e);
865
+ }
866
+ update() {
867
+ u(this.use);
868
+ }
869
+ dispose() {
870
+ l(this.use);
871
+ }
872
+ }, Qe = /* @__PURE__ */ new WeakMap(), $e = (e) => {
873
+ let t = Qe.get(e);
874
+ if (t) for (let e of t) e.reapplyClasses();
875
+ }, et = class {
876
+ group;
877
+ spread;
878
+ constructor(e, t) {
879
+ this.group = e, this.spread = t;
880
+ }
881
+ update(e) {
882
+ this.group.update(this.spread, e);
883
+ }
884
+ }, tt = class {
539
885
  element;
540
- previousValues = {};
886
+ values = [];
887
+ selectCommit = new ot();
888
+ previousEntries = /* @__PURE__ */ new Map();
541
889
  bindStates = /* @__PURE__ */ new Map();
542
890
  spreadClasses = [];
891
+ eventHandlers = /* @__PURE__ */ new Map();
892
+ templateAttributes = /* @__PURE__ */ new Map();
543
893
  constructor(e) {
544
894
  this.element = e;
895
+ for (let t of Array.from(e.attributes)) this.templateAttributes.set(t.name.toLowerCase(), t.value.includes("__CRP_") ? null : t.value);
545
896
  }
546
- update(e) {
547
- if (e !== _ && (typeof e != "object" || !e)) return;
548
- let t = e === _ ? {} : e;
549
- for (let e of Object.keys(this.previousValues)) if (!(e in t)) if (e.startsWith("@")) {
550
- let t = e.slice(1), n = `__crp_handler_${t}`, r = this.element[n];
551
- r && this.element.removeEventListener(t, r);
552
- } else if (e.startsWith(".")) {
553
- let t = this.bindStates.get(e);
554
- t && (this.element.removeEventListener(t.listener.__eventName, t.listener), this.bindStates.delete(e)), this.element[e.slice(1)] = void 0;
555
- } else if (e.startsWith("?")) this.element.removeAttribute(e.slice(1));
556
- else if (e === "class" || e === "className") {
557
- for (let e of this.spreadClasses) this.element.classList.remove(e);
558
- this.spreadClasses = [];
559
- } else this.element.removeAttribute(e);
560
- for (let [e, n] of Object.entries(t)) if (e.startsWith("@")) {
561
- let t = e.slice(1), r = `__crp_handler_${t}`, i = this.element[r];
562
- if (i && this.element.removeEventListener(t, i), n === _ || typeof n != "function" && !(n instanceof u)) {
563
- this.element[r] = void 0;
564
- continue;
565
- }
566
- let a;
567
- if (n instanceof u) {
568
- let e = n.handler, t = this.element instanceof HTMLFormElement ? this.element : this.element.closest("form");
569
- t instanceof HTMLFormElement && (n.novalidate ? t.setAttribute("novalidate", "") : t.removeAttribute("novalidate")), a = (t) => {
570
- t.preventDefault(), e.call(t.currentTarget, t);
571
- };
572
- } else a = n;
573
- this.element[r] = a, this.element.addEventListener(t, a);
574
- } else if (e.startsWith(".")) {
575
- let t = e.slice(1), r = this.bindStates.get(e);
576
- if (r && !(n instanceof i) && (this.element.removeEventListener(r.listener.__eventName, r.listener), this.bindStates.delete(e)), n === _) this.element[t] = void 0;
577
- else if (n instanceof i && (t === "value" || t === "checked")) this.applySpreadBind(e, t, n);
578
- else if (n instanceof l && (t === "value" || t === "checked")) {
579
- let e = this.element;
580
- if (t === "checked") {
581
- let t = !!n.value;
582
- e.checked !== t && (e.checked = t);
897
+ add() {
898
+ return this.values.push(void 0) - 1;
899
+ }
900
+ update(e, t) {
901
+ this.values[e] = t, e === this.values.length - 1 && this.commit();
902
+ }
903
+ reapplyClasses() {
904
+ for (let e of this.spreadClasses) this.element.classList.add(e);
905
+ }
906
+ commit() {
907
+ let { entries: e, classes: t, events: n } = je(this.values), r = this.selectCommit.cancel();
908
+ for (let [e, t] of this.eventHandlers) n.has(e) || (this.element.removeEventListener(t.eventName, t.listener), this.eventHandlers.delete(e));
909
+ for (let t of this.previousEntries.keys()) if (!e.has(t)) if (t.startsWith(".")) {
910
+ let e = this.bindStates.get(t);
911
+ e && (this.element.removeEventListener(e.listener.__eventName, e.listener), this.bindStates.delete(t)), this.element[t.slice(1)] = void 0;
912
+ } else this.resetAttribute(t.startsWith("?") ? t.slice(1) : t);
913
+ for (let [e, { key: t, value: r }] of n) this.applyEvent(e, t.slice(1), r);
914
+ for (let [t, n] of e) if (t.startsWith(".")) {
915
+ let e = t.slice(1), i = this.bindStates.get(t);
916
+ if (i && !(n instanceof h) && (this.element.removeEventListener(i.listener.__eventName, i.listener), this.bindStates.delete(t)), n === E) this.element[e] = void 0;
917
+ else if (n instanceof h && (e === "value" || e === "checked")) this.applySpreadBind(t, e, n);
918
+ else if (e === "value" && (this.element instanceof HTMLSelectElement || this.element instanceof HTMLTextAreaElement || this.element instanceof HTMLInputElement)) {
919
+ let e = n instanceof b, i = e ? n.value : n, a = i == null ? "" : String(i), o = this.previousEntries.get(t), s = o instanceof b ? o.value : o, c = !this.previousEntries.has(t) || o instanceof h || o === E || String(s ?? "") !== a;
920
+ (e ? this.element.value !== a : c) && (this.element.value = a), this.element instanceof HTMLSelectElement && (e || c || r) && this.selectCommit.schedule(this.element, a);
921
+ } else if (n instanceof b && (e === "value" || e === "checked")) {
922
+ let t = this.element;
923
+ if (e === "checked") {
924
+ let e = !!n.value;
925
+ t.checked !== e && (t.checked = e);
583
926
  } else {
584
- let t = n.value == null ? "" : String(n.value);
585
- e.value !== t && (e.value = t);
927
+ let e = n.value == null ? "" : String(n.value);
928
+ t.value !== e && (t.value = e);
586
929
  }
587
- } else n instanceof i ? this.element[t] = f(n.component, n.fieldName) : n instanceof l ? this.element[t] = n.value : this.element[t] = n;
588
- } else if (e.startsWith("?")) n !== _ && n ? this.element.setAttribute(e.slice(1), "") : this.element.removeAttribute(e.slice(1));
589
- else if (n === _) e === "class" || e === "className" ? this.applySpreadClass("") : this.element.removeAttribute(e);
590
- else if (n instanceof s) n.value === void 0 ? this.element.removeAttribute(e) : this.element.setAttribute(e, String(n.value));
591
- else if (n == null) this.element.removeAttribute(e);
592
- else if (typeof n == "boolean") {
593
- let t = D(e, n);
594
- t === null ? this.element.removeAttribute(e) : this.element.setAttribute(e, t);
595
- } else (e === "class" || e === "className") && typeof n == "string" ? this.applySpreadClass(n) : this.element.setAttribute(e, String(n));
596
- this.previousValues = { ...t };
930
+ } else n instanceof h ? this.element[e] = S(n.component, n.fieldName) : n instanceof b ? this.element[e] = n.value : this.element[e] = n;
931
+ } else t.startsWith("?") ? this.writeAttribute(t.slice(1), n !== E && n ? "" : null) : n === E || n == null || typeof n == "function" ? this.writeAttribute(t, null) : n instanceof v ? this.writeAttribute(t, n.value === void 0 ? null : String(n.value)) : typeof n == "boolean" ? this.writeAttribute(t, L(t, n)) : this.writeAttribute(t, String(n));
932
+ this.applySpreadClass(t), this.previousEntries = e;
933
+ }
934
+ writeAttribute(e, t) {
935
+ this.element.getAttribute(e) !== t && (t === null ? this.element.removeAttribute(e) : this.element.setAttribute(e, t));
936
+ }
937
+ resetAttribute(e) {
938
+ let t = this.templateAttributes.get(e.toLowerCase());
939
+ typeof t == "string" ? this.element.setAttribute(e, t) : this.element.removeAttribute(e);
940
+ }
941
+ applyEvent(e, t, n) {
942
+ let r = this.eventHandlers.get(e);
943
+ if (n === E || typeof n != "function" && !(n instanceof ie)) {
944
+ r && (r.handler = void 0);
945
+ return;
946
+ }
947
+ let i;
948
+ if (n instanceof ie) {
949
+ let e = n.handler, t = this.element instanceof HTMLFormElement ? this.element : this.element.closest("form");
950
+ t instanceof HTMLFormElement && (n.novalidate ? t.setAttribute("novalidate", "") : t.removeAttribute("novalidate")), i = (t) => {
951
+ t.preventDefault(), e.call(t.currentTarget, t);
952
+ };
953
+ } else i = n;
954
+ if (!r) {
955
+ let n = {
956
+ eventName: t,
957
+ handler: void 0,
958
+ listener: (e) => n.handler?.call(e.currentTarget, e)
959
+ };
960
+ r = n, this.eventHandlers.set(e, r), this.element.addEventListener(t, r.listener);
961
+ }
962
+ r.handler = i;
597
963
  }
598
964
  applySpreadClass(e) {
599
- for (let e of this.spreadClasses) this.element.classList.remove(e);
600
- let t = e.split(/\s+/).filter(Boolean);
601
- for (let e of t) this.element.classList.add(e);
602
- this.spreadClasses = t;
965
+ if (!(e.length === this.spreadClasses.length && e.every((e, t) => e === this.spreadClasses[t]))) {
966
+ for (let e of this.spreadClasses) this.element.classList.remove(e);
967
+ for (let t of e) this.element.classList.add(t);
968
+ if (e.length === 0 && !this.templateAttributes.has("class") && this.element.getAttribute("class") === "" && this.element.removeAttribute("class"), this.spreadClasses = e, e.length > 0) {
969
+ let e = Qe.get(this.element);
970
+ e || Qe.set(this.element, e = /* @__PURE__ */ new Set()), e.add(this);
971
+ }
972
+ }
603
973
  }
604
974
  applySpreadBind(e, t, n) {
605
- let r = this.element, i = n.component, a = f(i, n.fieldName), o = this.bindStates.get(e);
975
+ let r = this.element, i = n.component, a = S(i, n.fieldName), o = this.bindStates.get(e);
606
976
  if (t === "checked") {
607
977
  let e = !!a;
608
978
  (!o || o.lastKind !== "checked" || o.lastValue !== e) && (r.checked = e);
@@ -610,12 +980,12 @@ var m = class {
610
980
  let e = a == null ? "" : String(a);
611
981
  (!o || o.lastKind !== "value" || String(o.lastValue) !== e || this.element instanceof HTMLSelectElement && r.value !== e) && (r.value = e);
612
982
  }
613
- let s = H(this.element, t), c = !!o && (o.boundComponent !== i || o.boundField !== n.fieldName || o.listener.__eventName !== s);
983
+ let s = ct(this.element, t), c = !!o && (o.boundComponent !== i || o.boundField !== n.fieldName || o.listener.__eventName !== s);
614
984
  if (o && c && (this.element.removeEventListener(o.listener.__eventName, o.listener), this.bindStates.delete(e), o = void 0), o) o.lastKind = t, o.lastValue = t === "checked" ? !!a : a == null ? "" : String(a);
615
985
  else {
616
986
  let r = (e) => {
617
987
  let r = e.currentTarget, a = t === "checked" ? !!r[t] : r[t];
618
- i && p(i, n.fieldName, a);
988
+ i && ae(i, n.fieldName, a);
619
989
  };
620
990
  r.__eventName = s, this.element.addEventListener(s, r), o = {
621
991
  listener: r,
@@ -623,18 +993,52 @@ var m = class {
623
993
  lastValue: t === "checked" ? !!a : a == null ? "" : String(a),
624
994
  boundComponent: i,
625
995
  boundField: n.fieldName
626
- }, this.bindStates.set(e, o), t === "value" && V(this.element, () => this.bindStates.get(e) === o, () => f(o.boundComponent, o.boundField));
996
+ }, this.bindStates.set(e, o), t === "value" && st(this.element, () => this.bindStates.get(e) === o, () => S(o.boundComponent, o.boundField));
627
997
  }
628
998
  }
629
- }, B = class {
999
+ }, nt = class {
630
1000
  element;
631
1001
  name;
1002
+ domName;
632
1003
  segments;
633
- isMulti;
634
- multiState;
1004
+ values;
1005
+ written = void 0;
1006
+ constructor(e, t, n, r) {
1007
+ this.element = e, this.name = t, this.domName = n, this.segments = r, this.values = Array.from({ length: r.length - 1 });
1008
+ }
1009
+ update(e, t) {
1010
+ this.values[e] = t, e === this.values.length - 1 && this.commit();
1011
+ }
1012
+ commit() {
1013
+ let e = null;
1014
+ if (!this.values.includes(E)) {
1015
+ e = this.segments[0];
1016
+ for (let t = 0; t < this.values.length; t++) e += M(this.values[t]) + this.segments[t + 1];
1017
+ }
1018
+ let t = this.written === void 0 ? this.element.getAttribute(this.domName) : this.written;
1019
+ this.written = e, e !== t && (e === null ? this.element.removeAttribute(this.domName) : this.element.setAttribute(this.name, e), this.name === "class" && $e(this.element));
1020
+ }
1021
+ }, rt = class {
1022
+ attribute;
1023
+ position;
1024
+ constructor(e, t) {
1025
+ this.attribute = e, this.position = t;
1026
+ }
1027
+ update(e) {
1028
+ this.attribute.update(this.position, e);
1029
+ }
1030
+ }, it = class {
1031
+ element;
1032
+ name;
1033
+ domName;
1034
+ selectCommit = new ot();
635
1035
  lastFormValue = void 0;
636
1036
  lastFormKind = null;
1037
+ writtenAttribute = void 0;
637
1038
  bindListener = null;
1039
+ eventHandler = void 0;
1040
+ eventListener = null;
1041
+ eventListenerTarget = null;
638
1042
  boundPropName = null;
639
1043
  boundComponent = null;
640
1044
  boundFieldName = null;
@@ -643,97 +1047,76 @@ var m = class {
643
1047
  let e = this.bindListener;
644
1048
  this.element.removeEventListener(e.__eventName, e), this.bindListener = null, this.boundPropName = null, this.boundComponent = null, this.boundFieldName = null;
645
1049
  }
646
- constructor(e, t, n, r) {
647
- this.element = e, this.name = t;
648
- let i = [...n.matchAll(/__CRP_(\d+)__/g)];
649
- this.segments = [];
650
- let a = 0;
651
- for (let e of i) {
652
- let t = e.index;
653
- this.segments.push(n.substring(a, t)), a = t + e[0].length;
654
- }
655
- this.segments.push(n.substring(a)), this.isMulti = i.length > 1, this.multiState = r ?? null;
1050
+ constructor(e, t, n = t) {
1051
+ this.element = e, this.name = t, this.domName = n;
656
1052
  }
657
1053
  update(e) {
658
- if (e === _) {
659
- if (this.bindListener && this._detachBindListener(), this.isMulti && this.multiState) {
660
- this.multiState.currentValues[this.multiState.position] = _, this.element.removeAttribute(this.name);
661
- return;
662
- }
663
- if (this.name.startsWith("@")) this.element[`__crp_handler_${this.name.slice(1)}`] = void 0, this.element.removeAttribute(this.name);
1054
+ this.commit(e);
1055
+ }
1056
+ writeAttribute(e, t, n) {
1057
+ n !== this.writtenAttribute && (this.writtenAttribute = n, n === null ? (this.element.removeAttribute(e), t !== e && this.element.hasAttribute(t) && this.element.removeAttribute(t)) : this.element.setAttribute(e, n), this.name === "class" && $e(this.element));
1058
+ }
1059
+ commit(e) {
1060
+ let t = this.selectCommit.cancel();
1061
+ if (e === E) {
1062
+ if (this.bindListener && this._detachBindListener(), this.name.startsWith("@")) this.eventHandler = void 0, this.element.removeAttribute(this.domName);
664
1063
  else if (this.name.startsWith(".")) {
665
1064
  let e = this.name.slice(1);
666
- this.element[e] = void 0, this.lastFormKind = null, this.lastFormValue = void 0, this.element.removeAttribute(this.name);
667
- } else this.name.startsWith("?") && this.element.removeAttribute(this.name.slice(1)), this.element.removeAttribute(this.name);
1065
+ this.element[e] = void 0, this.lastFormKind = null, this.lastFormValue = void 0, this.element.removeAttribute(this.domName);
1066
+ } else this.name.startsWith("?") ? (this.writeAttribute(this.name.slice(1), this.name.slice(1), null), this.element.hasAttribute(this.domName) && this.element.removeAttribute(this.domName)) : this.writeAttribute(this.name, this.domName, null);
668
1067
  return;
669
1068
  }
670
- let t = !1;
671
- e instanceof l && (t = !0, e = e.value);
672
1069
  let n = !1;
673
- if (e instanceof s && (n = !0, e = e.value), e instanceof i) {
1070
+ e instanceof b && (n = !0, e = e.value);
1071
+ let r = !1;
1072
+ if (e instanceof v && (r = !0, e = e.value), e instanceof h) {
674
1073
  this.updateBind(e);
675
1074
  return;
676
1075
  }
677
1076
  if (this.bindListener && this._detachBindListener(), this.name === "ref") {
678
- typeof e == "function" ? e(this.element) : e && typeof e == "object" && "value" in e && (e.value = this.element), this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
679
- return;
680
- }
681
- if (this.isMulti && this.multiState) {
682
- let { currentValues: t, position: n } = this.multiState;
683
- if (t[n] = e, t.includes(_)) {
684
- this.element.removeAttribute(this.name);
685
- return;
686
- }
687
- let r = this.segments[0];
688
- for (let e = 0; e < t.length; e++) r += String(t[e]) + this.segments[e + 1];
689
- this.element.setAttribute(this.name, r);
1077
+ typeof e == "function" ? e(this.element) : e && typeof e == "object" && "value" in e && (e.value = this.element), this.element.hasAttribute(this.domName) && this.element.removeAttribute(this.domName);
690
1078
  return;
691
1079
  }
692
1080
  if (this.name.startsWith("@")) {
693
- let t = this.name.slice(1), n = `__crp_handler_${t}`, r = `__crp_wrapper_${t}`;
694
- if (!this.element[r]) {
1081
+ let t = this.name.slice(1);
1082
+ if (!this.eventListener || this.eventListenerTarget !== this.element) {
695
1083
  let e = (e) => {
696
- let t = e.currentTarget?.[n];
1084
+ let t = this.eventHandler;
697
1085
  typeof t == "function" && t.call(e.currentTarget, e);
698
1086
  };
699
- this.element[r] = e, this.element.addEventListener(t, e);
1087
+ this.eventListener = e, this.eventListenerTarget = this.element, this.element.addEventListener(t, e);
700
1088
  }
701
- let i = e;
702
- if (i instanceof u) {
703
- let e = i.handler, t = this.element instanceof HTMLFormElement ? this.element : this.element.closest("form");
704
- t instanceof HTMLFormElement && (i.novalidate ? t.setAttribute("novalidate", "") : t.removeAttribute("novalidate")), i = (t) => {
1089
+ let n = e;
1090
+ if (n instanceof ie) {
1091
+ let e = n.handler, t = this.element instanceof HTMLFormElement ? this.element : this.element.closest("form");
1092
+ t instanceof HTMLFormElement && (n.novalidate ? t.setAttribute("novalidate", "") : t.removeAttribute("novalidate")), n = (t) => {
705
1093
  t.preventDefault(), e.call(t.currentTarget, t);
706
1094
  };
707
1095
  }
708
- this.element[n] = typeof i == "function" ? i : void 0, this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
1096
+ this.eventHandler = typeof n == "function" ? n : void 0, this.element.hasAttribute(this.domName) && this.element.removeAttribute(this.domName);
709
1097
  } else if (this.name.startsWith(".")) {
710
- let n = this.name.slice(1);
711
- if ((n === "value" || n === "checked") && (this.element instanceof HTMLInputElement || this.element instanceof HTMLTextAreaElement || this.element instanceof HTMLSelectElement)) if (n === "checked") {
712
- let n = !!e;
713
- (t ? this.element.checked !== n : this.lastFormKind !== "checked" || this.lastFormValue !== n) && (this.element.checked = n, this.lastFormValue = n, this.lastFormKind = "checked");
1098
+ let r = this.name.slice(1);
1099
+ if ((r === "value" || r === "checked") && (this.element instanceof HTMLInputElement || this.element instanceof HTMLTextAreaElement || this.element instanceof HTMLSelectElement)) if (r === "checked") {
1100
+ let t = !!e;
1101
+ (n ? this.element.checked !== t : this.lastFormKind !== "checked" || this.lastFormValue !== t) && (this.element.checked = t, this.lastFormValue = t, this.lastFormKind = "checked");
714
1102
  } else {
715
- let n = String(e);
716
- (t ? this.element.value !== n : this.lastFormKind !== "value" || String(this.lastFormValue) !== n) && (this.element.value = n, this.lastFormValue = e, this.lastFormKind = "value");
1103
+ let r = e == null ? "" : String(e), i = n ? this.element.value !== r : this.lastFormKind !== "value" || String(this.lastFormValue) !== r;
1104
+ i && (this.element.value = r, this.lastFormValue = r, this.lastFormKind = "value"), this.element instanceof HTMLSelectElement && (n || i || t) && this.selectCommit.schedule(this.element, r);
717
1105
  }
718
- else this.element[n] = e;
719
- this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
1106
+ else this.element[r] = e;
1107
+ this.element.hasAttribute(this.domName) && this.element.removeAttribute(this.domName);
720
1108
  } else if (this.name.startsWith("?")) {
721
1109
  let t = this.name.slice(1);
722
- e ? this.element.setAttribute(t, "") : this.element.removeAttribute(t), this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
723
- } else if (n) e === void 0 ? this.element.removeAttribute(this.name) : this.element.setAttribute(this.name, this.segments[0] + String(e) + this.segments[1]);
724
- else if (e == null) this.element.removeAttribute(this.name);
725
- else if (typeof e == "boolean") {
726
- let t = D(this.name, e);
727
- t === null ? this.element.removeAttribute(this.name) : this.element.setAttribute(this.name, t);
728
- } else this.element.setAttribute(this.name, this.segments[0] + String(e) + this.segments[1]);
1110
+ this.writeAttribute(t, t, e ? "" : null), this.element.hasAttribute(this.domName) && this.element.removeAttribute(this.domName);
1111
+ } else r ? this.writeAttribute(this.name, this.domName, e === void 0 ? null : String(e)) : e == null ? this.writeAttribute(this.name, this.domName, null) : typeof e == "boolean" ? this.writeAttribute(this.name, this.domName, L(this.name, e)) : this.writeAttribute(this.name, this.domName, M(e));
729
1112
  }
730
1113
  updateBind(e) {
731
1114
  let t = this.name.startsWith(".") ? this.name.slice(1) : this.name;
732
1115
  if (t !== "value" && t !== "checked") {
733
- this.element[t] = f(e.component, e.fieldName), this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name), this.bindListener && this._detachBindListener();
1116
+ this.element[t] = S(e.component, e.fieldName), this.element.hasAttribute(this.domName) && this.element.removeAttribute(this.domName), this.bindListener && this._detachBindListener();
734
1117
  return;
735
1118
  }
736
- let n = this.element, r = e.component, i = f(r, e.fieldName);
1119
+ let n = this.element, r = e.component, i = S(r, e.fieldName);
737
1120
  if (t === "checked") {
738
1121
  let e = !!i;
739
1122
  (this.lastFormKind !== "checked" || this.lastFormValue !== e) && (n.checked = e, this.lastFormValue = e, this.lastFormKind = "checked");
@@ -741,26 +1124,52 @@ var m = class {
741
1124
  let e = i == null ? "" : String(i);
742
1125
  (this.lastFormKind !== "value" || String(this.lastFormValue) !== e || this.element instanceof HTMLSelectElement && n.value !== e) && (n.value = e, this.lastFormValue = e, this.lastFormKind = "value");
743
1126
  }
744
- let a = H(this.element, t);
1127
+ let a = ct(this.element, t);
745
1128
  if (this.bindListener && (this.boundPropName !== t || this.boundComponent !== r || this.boundFieldName !== e.fieldName || this.bindListener.__eventName !== a) && this._detachBindListener(), !this.bindListener) {
746
1129
  let n = (n) => {
747
1130
  let i = n.currentTarget, a = t === "checked" ? !!i[t] : i[t];
748
- r && p(r, e.fieldName, a);
1131
+ r && ae(r, e.fieldName, a);
749
1132
  };
750
1133
  if (n.__eventName = a, this.bindListener = n, this.boundPropName = t, this.boundComponent = r, this.boundFieldName = e.fieldName, this.element.addEventListener(a, n), t === "value") {
751
1134
  let e = n;
752
- V(this.element, () => this.bindListener === e, () => f(this.boundComponent, this.boundFieldName));
1135
+ st(this.element, () => this.bindListener === e, () => S(this.boundComponent, this.boundFieldName));
753
1136
  }
754
1137
  }
755
- this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
1138
+ this.element.hasAttribute(this.domName) && this.element.removeAttribute(this.domName);
756
1139
  }
757
- }, V = (e, t, n) => {
1140
+ }, at = class {
1141
+ element;
1142
+ state;
1143
+ position;
1144
+ constructor(e, t, n) {
1145
+ this.element = e, this.state = t, this.position = n;
1146
+ }
1147
+ update(e) {
1148
+ let { tag: t, segments: n, values: r } = this.state;
1149
+ r[this.position] = e;
1150
+ let i = n[0];
1151
+ for (let e = 0; e < r.length; e++) i += Ve(t, r[e]) + n[e + 1];
1152
+ this.element.textContent !== i && (this.element.textContent = i);
1153
+ }
1154
+ }, ot = class {
1155
+ pending = null;
1156
+ cancel() {
1157
+ let e = this.pending !== null;
1158
+ return this.pending = null, e;
1159
+ }
1160
+ schedule(e, t) {
1161
+ let n = {};
1162
+ this.pending = n, queueMicrotask(() => {
1163
+ this.pending === n && (this.pending = null, e.value !== t && (e.value = t));
1164
+ });
1165
+ }
1166
+ }, st = (e, t, n) => {
758
1167
  e instanceof HTMLSelectElement && queueMicrotask(() => {
759
1168
  if (!t()) return;
760
1169
  let r = n(), i = r == null ? "" : String(r);
761
1170
  e.value !== i && (e.value = i);
762
1171
  });
763
- }, H = (e, t) => {
1172
+ }, ct = (e, t) => {
764
1173
  if (e instanceof HTMLSelectElement) return "change";
765
1174
  if (e instanceof HTMLTextAreaElement) return "input";
766
1175
  if (e instanceof HTMLInputElement) {
@@ -768,100 +1177,171 @@ var m = class {
768
1177
  return t === "checked" || n === "checkbox" || n === "radio" || n === "range" ? "change" : "input";
769
1178
  }
770
1179
  return "input";
771
- }, U = /* @__PURE__ */ new WeakMap(), W = (e) => {
772
- let t = [], n = M(e.strings, e.__cossackScope), r = "", i = !1, a = null, o = /(\.\.\.|[.@?]?[a-zA-Z0-9_:-]+)=["']?$/;
773
- for (let e = 0; e < n.length - 1; e++) {
774
- let t = n[e];
775
- for (let e = 0; e < t.length; e++) a ? t[e] === a && (a = null) : i && (t[e] === "\"" || t[e] === "'") ? e > 0 && t[e - 1] === "=" && (a = t[e]) : t[e] === "<" && t[e + 1] !== "!" && t[e + 1] !== "/" ? i = !0 : t[e] === ">" && (i = !1);
776
- r += t;
777
- let s = t.match(o);
778
- if (i && (s || a)) {
779
- if (r += `__CRP_${e}__`, s && !a) {
780
- let e = s[0].endsWith("\"") ? "\"" : s[0].endsWith("'") ? "'" : null;
781
- e && (a = e);
782
- }
783
- } else r += `<!--CRP_${e}-->`;
1180
+ }, q = /* @__PURE__ */ new WeakMap(), lt = /* @__PURE__ */ new WeakMap(), ut = (e, { strings: t, bindings: n }) => {
1181
+ let r = G(e), i = lt.get(r);
1182
+ if (i && i.ownerDocument === document) return i;
1183
+ let a = "";
1184
+ for (let e = 0; e < t.length - 1; e++) {
1185
+ let r = n[e];
1186
+ if (r.kind === "attribute" && r.start && r.name === "...") {
1187
+ let n = t[e];
1188
+ a += `${n.slice(0, r.prefixStart)}crp-spread-${e}${n.slice(r.prefixStart + 3)}`;
1189
+ } else a += t[e];
1190
+ let i = r.kind;
1191
+ i === "node" ? a += `<!--CRP_${e}-->` : (i === "attribute" || i === "raw-text") && (a += `__CRP_${e}__`);
784
1192
  }
785
- let s = n[n.length - 1];
786
- for (let e = 0; e < s.length; e++) a ? s[e] === a && (a = null) : s[e] === "<" && s[e + 1] !== "!" && s[e + 1] !== "/" ? i = !0 : s[e] === ">" && (i = !1);
787
- r += s;
788
- let c = document.createElement("template");
1193
+ a += t[t.length - 1];
1194
+ let o = document.createElement("template");
789
1195
  if (e._$litType$ === 2) {
790
- c.innerHTML = `<svg>${r}</svg>`;
791
- let e = c.content.firstElementChild, t = document.createDocumentFragment();
1196
+ o.innerHTML = `<svg>${a}</svg>`;
1197
+ let e = o.content.firstElementChild, t = document.createDocumentFragment();
792
1198
  if (e) for (; e.firstChild;) t.appendChild(e.firstChild);
793
- c.content.replaceChildren(t);
794
- } else c.innerHTML = r;
795
- let l = c.content.cloneNode(!0), u = document.createTreeWalker(l, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT), d, f = [];
1199
+ o.content.replaceChildren(t);
1200
+ } else o.innerHTML = a;
1201
+ return lt.set(r, o), o;
1202
+ }, J = (e) => {
1203
+ let n = [], r = Re(e), { bindings: i } = r, o = (e, t) => {
1204
+ let n = i[e];
1205
+ return n?.kind === "attribute" && /^[.@?]/.test(n.name) ? n.name : t;
1206
+ }, s = (e) => {
1207
+ let t = i[e];
1208
+ return t?.kind === "attribute" && t.name === "...";
1209
+ }, c = /* @__PURE__ */ new Map(), l = ut(e, r).content.cloneNode(!0), u = document.createTreeWalker(l, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT), d, f = [];
796
1210
  for (; d = u.nextNode();) f.push(d);
797
- return f.forEach((e) => {
1211
+ f.forEach((e) => {
798
1212
  if (e.nodeType === Node.COMMENT_NODE) {
799
- let n = e.nodeValue?.match(/CRP_(\d+)/);
800
- if (n) {
801
- let r = parseInt(n[1]), i = document.createComment("/CRP");
1213
+ let t = e.nodeValue?.match(/^CRP_(\d+)$/);
1214
+ if (t) {
1215
+ let r = parseInt(t[1]), i = document.createComment("/CRP");
802
1216
  e.parentNode.insertBefore(i, e.nextSibling);
803
- let a = new R(e, i);
804
- t[r] = a;
1217
+ let a = new K(e, i);
1218
+ n[r] = a;
805
1219
  }
806
1220
  } else if (e.nodeType === Node.ELEMENT_NODE) {
807
- let n = e;
808
- Array.from(n.attributes).forEach((e) => {
809
- let r = [...e.value.matchAll(/__CRP_(\d+)__/g)];
810
- if (r.length === 0) return;
811
- if (r.length === 1) {
812
- let i = parseInt(r[0][1]);
813
- if (!t[i]) if (e.name === "...") {
814
- let e = new z(n);
815
- t[i] = e, n.removeAttribute("...");
816
- } else {
817
- let r = new B(n, e.name, e.value);
818
- t[i] = r;
1221
+ let r = e;
1222
+ if (t.has(r.localName)) {
1223
+ let e = r.textContent ?? "", t = [...e.matchAll(/__CRP_(\d+)__/g)].filter((e) => i[Number(e[1])]?.kind === "raw-text");
1224
+ if (t.length > 0) {
1225
+ let i = [], a = 0;
1226
+ for (let n of t) i.push(e.slice(a, n.index)), a = n.index + n[0].length;
1227
+ i.push(e.slice(a));
1228
+ let o = {
1229
+ tag: r.localName,
1230
+ segments: i,
1231
+ values: Array(t.length).fill(void 0)
1232
+ };
1233
+ t.forEach((e, t) => {
1234
+ n[Number(e[1])] = new at(r, o, t);
1235
+ }), r.textContent = i.join("");
1236
+ }
1237
+ }
1238
+ Array.from(r.attributes).forEach((e) => {
1239
+ let t = [...e.value.matchAll(/__CRP_(\d+)__/g)];
1240
+ if (t.length === 0) return;
1241
+ let a = t.map((e) => parseInt(e[1]));
1242
+ if (a.some((e) => n[e])) return;
1243
+ let l = i[a[0]];
1244
+ if (s(a[0])) {
1245
+ if (a.length === 1) {
1246
+ let e = c.get(r);
1247
+ e || c.set(r, e = new tt(r)), n[a[0]] = new et(e, e.add());
819
1248
  }
1249
+ r.removeAttribute(e.name);
820
1250
  return;
821
1251
  }
822
- if (e.name === "...") return;
823
- let i = r.map((e) => parseInt(e[1])), a = Array(r.length).fill(void 0);
824
- for (let r = 0; r < i.length; r++) {
825
- let o = i[r];
826
- if (!t[o]) {
827
- let i = new B(n, e.name, e.value, {
828
- currentValues: a,
829
- position: r
830
- });
831
- t[o] = i;
832
- }
1252
+ if (a.length === 1 && !(l?.kind === "attribute" && be(l))) {
1253
+ n[a[0]] = new it(r, o(a[0], e.name), e.name);
1254
+ return;
833
1255
  }
1256
+ let u = [], d = 0;
1257
+ for (let n of t) u.push(e.value.slice(d, n.index)), d = n.index + n[0].length;
1258
+ u.push(e.value.slice(d));
1259
+ let f = new nt(r, o(a[0], e.name), e.name, u);
1260
+ a.forEach((e, t) => {
1261
+ n[e] = new rt(f, t);
1262
+ });
834
1263
  });
835
1264
  }
836
- }), {
1265
+ });
1266
+ let ee = e.__cossackScope === void 0 ? void 0 : a(e.__cossackScope);
1267
+ if (ee) {
1268
+ let t = l.firstChild;
1269
+ for (; t && $(t);) t = t.nextSibling;
1270
+ t && (n[e.strings.length - 1] = new Ze(t, ee));
1271
+ }
1272
+ return {
837
1273
  fragment: l,
838
- parts: t
1274
+ parts: n
839
1275
  };
840
- }, G = (e, t) => {
841
- let n = U.get(t), r = L(e);
842
- if (n && n.identity === r) {
843
- n.parts.forEach((t, n) => {
1276
+ }, Y = (e, t) => {
1277
+ let n = V.get(t);
1278
+ V.delete(t);
1279
+ let r = q.get(t), i = G(e);
1280
+ if (r && r.identity === i) {
1281
+ U(r.parts, n, i), r.parts.forEach((t, n) => {
844
1282
  t.update(e.values[n]);
845
1283
  });
846
1284
  return;
847
1285
  }
848
- if (n) for (let e of n.parts) e && typeof e.dispose == "function" && e.dispose();
849
- let { fragment: i, parts: a } = W(e), o = e.values;
850
- for (let e = 0; e < o.length; e++) a[e] && a[e].update(o[e]);
851
- if (typeof HTMLElement < "u" && t instanceof HTMLElement || typeof ShadowRoot < "u" && t instanceof ShadowRoot) t.innerHTML = "";
852
- else for (; t.firstChild;) t.removeChild(t.firstChild);
853
- t.appendChild(i), U.set(t, {
854
- identity: r,
855
- parts: a
1286
+ if (r) {
1287
+ q.delete(t);
1288
+ for (let e of r.parts) e && typeof e.dispose == "function" && e.dispose();
1289
+ }
1290
+ let { fragment: a, parts: o } = J(e);
1291
+ U(o, n, i);
1292
+ let l = e.values;
1293
+ try {
1294
+ for (let e = 0; e < l.length; e++) o[e] && o[e].update(l[e]);
1295
+ if (typeof ShadowRoot < "u" && t instanceof ShadowRoot) for (let e = t.firstChild; e;) {
1296
+ let n = e.nextSibling;
1297
+ c(e) || t.removeChild(e), e = n;
1298
+ }
1299
+ else if (typeof HTMLElement < "u" && t instanceof HTMLElement) t.innerHTML = "";
1300
+ else for (; t.firstChild;) t.removeChild(t.firstChild);
1301
+ t.appendChild(a), q.set(t, {
1302
+ identity: i,
1303
+ parts: o
1304
+ });
1305
+ } catch (e) {
1306
+ throw W(o), e;
1307
+ }
1308
+ t.isConnected && s();
1309
+ }, X = class extends Error {
1310
+ category;
1311
+ path;
1312
+ expected;
1313
+ actual;
1314
+ constructor(e, t, n, r) {
1315
+ super(`${e} at ${t}: expected ${n}; found ${r}`), this.category = e, this.path = t, this.expected = n, this.actual = r;
1316
+ }
1317
+ }, Z = (e) => e ? e.nodeType === Node.ELEMENT_NODE ? `<${e.nodeName.toLowerCase()}>` : e.nodeType === Node.COMMENT_NODE ? "comment" : e.nodeType === Node.TEXT_NODE ? "text" : `nodeType:${e.nodeType}` : "<none>", Q = (e, t, n, r, i = "full-render-fallback") => {
1318
+ if (!R || r?.expectedClientOnly) return;
1319
+ n ??= r?.component;
1320
+ let a = r?.statePath && /^[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*|\[\d+\])*$/.test(r.statePath) ? r.statePath.slice(0, 160) : void 0, o = r?.source?.file, s = typeof o == "string" && o.length <= 240 && !o.startsWith("/") && !o.includes("\\") && !o.split("/").includes("..") && !/^[a-zA-Z]+:/.test(o) ? {
1321
+ file: o,
1322
+ ...Number.isSafeInteger(r?.source?.line) && r.source.line > 0 ? { line: r.source.line } : {},
1323
+ ...Number.isSafeInteger(r?.source?.column) && r.source.column > 0 ? { column: r.source.column } : {}
1324
+ } : void 0, c = n?.replace(/[^a-zA-Z0-9_$]/g, "_").slice(0, 80);
1325
+ console.warn({
1326
+ event: "cossack.hydration.mismatch",
1327
+ boundary: t,
1328
+ ...c ? { component: c } : {},
1329
+ ...s ? { source: s } : {},
1330
+ ...a ? { statePath: a } : {},
1331
+ category: e.category,
1332
+ path: e.path,
1333
+ expected: e.expected,
1334
+ actual: e.actual,
1335
+ action: i
856
1336
  });
857
- }, K = class extends Error {}, q = (e) => e ? e.nodeType === Node.ELEMENT_NODE ? `<${e.nodeName.toLowerCase()}>` : e.nodeType === Node.COMMENT_NODE ? `<!--${e.nodeValue}-->` : e.nodeType === Node.TEXT_NODE ? `text"${(e.nodeValue || "").slice(0, 30)}"` : `nodeType:${e.nodeType}` : "<none>", J = (e) => {
1337
+ }, $ = (e) => {
858
1338
  if (e.nodeType === Node.TEXT_NODE) return (e.nodeValue || "").trim() === "";
859
1339
  if (e.nodeType === Node.COMMENT_NODE) {
860
1340
  let t = e.nodeValue || "";
861
1341
  return !/^CRP_\d+$/.test(t) && t !== "/CRP" && t !== "CSA-S" && t !== "CSA-E";
862
1342
  }
863
1343
  return !1;
864
- }, Y = (e) => {
1344
+ }, dt = (e) => {
865
1345
  let t = [], n = [];
866
1346
  for (let r of e) {
867
1347
  if (r.nodeType !== Node.COMMENT_NODE) continue;
@@ -873,70 +1353,98 @@ var m = class {
873
1353
  }
874
1354
  }
875
1355
  return t;
876
- }, X = (e, t, n, r = "root") => {
877
- let i = 0;
878
- for (let a = 0; a < e.length; a++) {
879
- let o = e[a];
880
- if (J(o)) continue;
881
- for (; i < t.length && J(t[i]);) i++;
882
- let s = t[i];
883
- if (!s) throw new K(`existing shorter than blueprint at ${r} (expected ${q(o)})`);
884
- if (o.nodeType === Node.ELEMENT_NODE) {
885
- if (s.nodeType !== Node.ELEMENT_NODE || o.nodeName !== s.nodeName || o.namespaceURI !== s.namespaceURI) throw new K(`tag mismatch: blueprint ${q(o)} vs existing ${q(s)}`);
886
- n.set(o, s), X(o.childNodes, Array.from(s.childNodes), n, `${r}/${o.nodeName.toLowerCase()}[${a}]`);
1356
+ }, ft = (e) => {
1357
+ let t = [];
1358
+ for (let n = e; n?.parentNode && t.length < 12; n = n.parentNode) {
1359
+ let e = Array.prototype.indexOf.call(n.parentNode.childNodes, n);
1360
+ t.unshift(`${n.nodeType === Node.ELEMENT_NODE ? n.nodeName.toLowerCase() : "node"}[${e}]`);
1361
+ }
1362
+ return `root/${t.join("/")}`;
1363
+ }, pt = (e, n, r, i = "root", a) => {
1364
+ let o = 0;
1365
+ for (let s = 0; s < e.length; s++) {
1366
+ let c = e[s];
1367
+ if ($(c)) continue;
1368
+ for (; o < n.length && $(n[o]);) o++;
1369
+ let l = n[o], u = `${i}/${c.nodeType === Node.ELEMENT_NODE ? c.nodeName.toLowerCase() : "node"}[${s}]`;
1370
+ if (!l) throw new X("missing-node", u, Z(c), "<none>");
1371
+ if (c.nodeType === Node.ELEMENT_NODE) {
1372
+ if (l.nodeType !== Node.ELEMENT_NODE || c.nodeName !== l.nodeName) throw new X("element-tag", u, Z(c), Z(l));
1373
+ if (c.namespaceURI !== l.namespaceURI) throw new X("element-namespace", u, Z(c), Z(l));
1374
+ if (r.set(c, l), t.has(c.localName)) {
1375
+ o++;
1376
+ continue;
1377
+ }
1378
+ pt(c.childNodes, Array.from(l.childNodes), r, `${i}/${c.nodeName.toLowerCase()}[${s}]`, a);
887
1379
  } else {
888
- if (o.nodeType !== s.nodeType) throw new K(`type mismatch: blueprint ${q(o)} vs existing ${q(s)}`);
889
- n.set(o, s);
1380
+ if (c.nodeType !== l.nodeType) throw new X("node-type", u, Z(c), Z(l));
1381
+ c.nodeType === Node.TEXT_NODE && c.nodeValue !== l.nodeValue && (R && Q(new X("text-content", u, "text", "text"), a?.boundary ?? "root", void 0, a, "text-reconcile"), l.nodeValue = c.nodeValue), r.set(c, l);
890
1382
  }
891
- if (i++, o.nodeType === Node.COMMENT_NODE && /^CRP_\d+$/.test(o.nodeValue || "")) {
1383
+ if (o++, c.nodeType === Node.COMMENT_NODE && /^CRP_\d+$/.test(c.nodeValue || "")) {
892
1384
  let e = 1;
893
- for (; i < t.length && e > 0;) {
894
- let n = t[i];
895
- if (n.nodeType === Node.COMMENT_NODE) {
896
- if (/^CRP_\d+$/.test(n.nodeValue || "")) e++;
897
- else if (n.nodeValue === "/CRP" && (e--, e === 0)) break;
1385
+ for (; o < n.length && e > 0;) {
1386
+ let t = n[o];
1387
+ if (t.nodeType === Node.COMMENT_NODE) {
1388
+ if (/^CRP_\d+$/.test(t.nodeValue || "")) e++;
1389
+ else if (t.nodeValue === "/CRP" && (e--, e === 0)) break;
898
1390
  }
899
- i++;
1391
+ o++;
900
1392
  }
901
1393
  }
902
1394
  }
903
- for (; i < t.length && J(t[i]);) i++;
904
- if (i < t.length) throw new K(`existing longer than blueprint (unexpected ${q(t[i])})`);
905
- }, Z = (e, t) => {
1395
+ for (; o < n.length && $(n[o]);) o++;
1396
+ if (o < n.length) throw new X("unexpected-node", `${i}/node[${o}]`, "end of template", Z(n[o]));
1397
+ }, mt = (e, t) => {
906
1398
  for (let n of e) if (n) {
907
- if (n instanceof R) {
1399
+ if (n instanceof K) {
908
1400
  let e = t.get(n.startNode), r = t.get(n.endNode);
909
- if (!e || !r) throw new K("node part anchors not found");
1401
+ if (!e || !r) throw new X("missing-part-anchor", "part", "start and end anchors", "missing anchor");
910
1402
  n.startNode = e, n.endNode = r, n._beginHydration();
911
- } else if (n instanceof B || n instanceof z) {
1403
+ } else if (n instanceof it || n instanceof at) {
912
1404
  let e = t.get(n.element);
913
- if (!e) throw new K("attribute part element not found");
1405
+ if (!e) throw new X("missing-part-element", "part", "attribute element", "missing element");
914
1406
  n.element = e;
1407
+ } else if (n instanceof et) {
1408
+ if (n.spread !== 0) continue;
1409
+ let e = t.get(n.group.element);
1410
+ if (!e) throw new X("missing-part-element", "part", "attribute element", "missing element");
1411
+ n.group.element = e;
1412
+ } else if (n instanceof rt) {
1413
+ if (n.position !== 0) continue;
1414
+ let e = t.get(n.attribute.element);
1415
+ if (!e) throw new X("missing-part-element", "part", "attribute element", "missing element");
1416
+ n.attribute.element = e;
1417
+ } else if (n instanceof Ze) {
1418
+ let e = t.get(n.use.anchor);
1419
+ e && (n.use.anchor = e);
915
1420
  }
916
1421
  }
917
- }, Q = (e, t) => {
918
- if (!t.hasChildNodes()) return G(e, t);
919
- let n = U.get(t), r = L(e);
920
- if (n && n.identity === r) {
921
- n.parts.forEach((t, n) => t.update(e.values[n]));
1422
+ }, ht = (e) => Array.from(e.childNodes).filter((e) => !c(e)), gt = (e, t, n) => {
1423
+ if (ht(t).length === 0) return Y(e, t);
1424
+ let r = q.get(t), i = G(e);
1425
+ if (r && r.identity === i) {
1426
+ r.parts.forEach((t, n) => t.update(e.values[n]));
922
1427
  return;
923
1428
  }
924
- if (n) for (let e of n.parts) e && typeof e.dispose == "function" && e.dispose();
925
- let i;
1429
+ if (r) {
1430
+ q.delete(t);
1431
+ for (let e of r.parts) e && typeof e.dispose == "function" && e.dispose();
1432
+ }
1433
+ let a = [];
926
1434
  try {
927
- let { fragment: n, parts: r } = W(e);
928
- i = r;
929
- let a = /* @__PURE__ */ new Map();
930
- X(n.childNodes, Array.from(t.childNodes), a), Z(i, a);
931
- for (let t = 0; t < e.values.length; t++) i[t] && i[t].update(e.values[t]);
932
- } catch (n) {
933
- if (n instanceof K) return console.warn("[cossack] hydration mismatch, falling back to render:", n.message), G(e, t);
934
- throw n;
935
- }
936
- U.set(t, {
937
- identity: r,
938
- parts: i
939
- });
1435
+ let { fragment: r, parts: i } = J(e);
1436
+ a = i;
1437
+ let o = /* @__PURE__ */ new Map();
1438
+ pt(r.childNodes, ht(t), o, "root", R ? n : void 0), mt(a, o), Je(a, n);
1439
+ for (let t = 0; t < e.values.length; t++) a[t] && a[t].update(e.values[t]);
1440
+ } catch (r) {
1441
+ if (W(a), r instanceof X) return R && Q(r, "root", void 0, n), Y(e, t);
1442
+ throw r;
1443
+ }
1444
+ q.set(t, {
1445
+ identity: i,
1446
+ parts: a
1447
+ }), t.isConnected && s();
940
1448
  };
941
1449
  //#endregion
942
- export { m as TemplateResult, x as UnsafeHTMLResult, v as component, w as escapeHtml, h as html, Q as hydrate, y as isTemplateResult, C as isUnsafeHTML, _ as nothing, G as render, F as renderToString, g as svg, S as unsafeHTML };
1450
+ export { oe as TemplateResult, he as UnsafeHTMLResult, Ye as captureRenderDrafts, pe as component, k as escapeHtml, T as html, gt as hydrate, D as isTemplateResult, O as isUnsafeHTML, E as nothing, Y as render, Ge as renderToString, fe as svg, ge as unsafeHTML };