@cossackframework/renderer 0.8.3 → 0.9.0

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.
@@ -1,9 +1,10 @@
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 { isComponentResult as t } from "./component.js";
4
+ import { CossackElement as n, popCurrentInstance as r, pushCurrentInstance as i } from "./cossack-element.js";
5
+ import { BindResult as a, CacheResult as o, GuardResult as s, IfDefinedResult as c, KeyResult as l, LiveResult as u, PreventDefaultResult as d, RepeatResult as f, resolveField as p, setField as m } from "./directives.js";
5
6
  //#region src/cossack-html.ts
6
- var m = class {
7
+ var h = class {
7
8
  strings;
8
9
  values;
9
10
  _cossack_template_result = !0;
@@ -13,42 +14,51 @@ var m = class {
13
14
  }
14
15
  __cossackOwner;
15
16
  __cossackScope;
16
- }, h = (e, ...n) => {
17
- let r = new m(e, n, 1), i = t.currentRenderingInstance;
17
+ }, g = (e, ...t) => {
18
+ let r = new h(e, t, 1), i = n.currentRenderingInstance;
18
19
  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;
20
+ }, ee = (e, ...t) => {
21
+ let r = new h(e, t, 2), i = n.currentRenderingInstance;
21
22
  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 {
23
+ }, _ = Symbol.for("cossack-nothing");
24
+ function te(e, t, r) {
25
+ return g`${{
26
+ _type: "COMPONENT",
27
+ clazz: e,
28
+ props: t ?? {},
29
+ children: r,
30
+ parent: n.currentRenderingInstance
31
+ }}`;
32
+ }
33
+ var v = (e) => typeof e == "object" && !!e && e._cossack_template_result === !0, y = Symbol.for("@cossackframework/renderer/unsafe-html"), b = class {
29
34
  value;
30
- [b] = !0;
35
+ [y] = !0;
31
36
  constructor(e) {
32
37
  this.value = e;
33
38
  }
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([
39
+ }, ne = (e) => new b(e), x = (e) => typeof e == "object" && !!e && e[y] === !0, S = (e) => String(e).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;"), C = (e, c = {}) => {
40
+ if (e === _ || e === "" || e == null || e === !1) return "";
41
+ if (Array.isArray(e)) return c.hydrate ? e.map((e) => `<!--CSA-S-->${C(e, c)}<!--CSA-E-->`).join("") : e.map((e) => C(e, c)).join("");
42
+ if (v(e)) return O(e, c);
43
+ if (e instanceof u) return C(e.value, c);
44
+ if (e instanceof f) return c.hydrate ? e.items.map((t, n) => `<!--CSA-S-->${C(e.templateFn(t, n), c)}<!--CSA-E-->`).join("") : e.items.map((t, n) => C(e.templateFn(t, n), c)).join("");
45
+ if (e instanceof l) return C(e.template, c);
46
+ if (e instanceof s) return C(e.factory(), c);
47
+ if (e instanceof o) return C(e.value, c);
48
+ if (e instanceof a) return C(p(e.component, e.fieldName), c);
49
+ if (t(e)) {
50
+ let t = new e.clazz();
51
+ Object.assign(t, e.props), "props" in t && (t.props = e.props), t.children = e.children, t.__parent = e.parent || n.currentRenderingInstance, e.serviceScope && typeof t._setServiceScope == "function" && t._setServiceScope(e.serviceScope), t.__parent && (t._id = `${t.__parent._id}:${t.__parent._childCounter++}`), t.connectedCallback(), i(t);
52
+ try {
53
+ t.willUpdate(/* @__PURE__ */ new Map());
54
+ let e = t._getWrappedTemplate, n = typeof e == "function" ? e.call(t) : t._finalizeRenderOutput(t.render());
55
+ return n ? O(v(n) ? n : g`${n}`, c) : "";
56
+ } finally {
57
+ r();
58
+ }
59
+ }
60
+ return x(e) ? e.value : typeof e == "function" || typeof e == "object" && e && "value" in e ? "" : S(e);
61
+ }, w = /* @__PURE__ */ new Set([
52
62
  "allowfullscreen",
53
63
  "async",
54
64
  "autofocus",
@@ -74,42 +84,51 @@ var m = class {
74
84
  "required",
75
85
  "reversed",
76
86
  "selected"
77
- ]), D = (e, t) => E.has(e.toLowerCase()) ? t ? "" : null : String(t), O = (e, t) => {
87
+ ]), T = (e, t) => w.has(e.toLowerCase()) ? t ? "" : null : String(t), re = (e, t) => {
78
88
  if (!(e === _ || typeof e != "object" || !e)) for (let [n, r] of Object.entries(e)) {
79
89
  if (n.startsWith("@")) continue;
80
- let e = n, a = r;
90
+ let e = n, i = r;
81
91
  if (n.startsWith("?")) {
82
92
  r !== _ && r && (t.result += ` ${n.slice(1)}`);
83
93
  continue;
84
94
  }
85
95
  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}`);
96
+ if (e = n === ".value" ? ".value" : n.slice(1), i instanceof a ? i = p(i.component, i.fieldName) : i instanceof u && (i = i.value), i === _) continue;
97
+ if (e === ".value") {
98
+ t.result += ` .value="${S(String(i ?? ""))}"`;
99
+ continue;
100
+ }
101
+ if (w.has(e)) {
102
+ i && (t.result += ` ${e}`);
89
103
  continue;
90
104
  }
91
105
  }
92
- if ((e === "class" || e === "className") && typeof a == "string" && a) {
93
- let e = k(t.result, a);
106
+ if ((e === "class" || e === "className") && i instanceof c) {
107
+ if (i.value === void 0) continue;
108
+ i = String(i.value);
109
+ }
110
+ if ((e === "class" || e === "className") && typeof i == "string") {
111
+ let e = ie(t.result, i);
94
112
  if (e !== null) {
95
113
  t.result = e;
96
114
  continue;
97
115
  }
116
+ import.meta.env?.DEV && console.warn("[cossack/renderer] spread class merge failed — falling back to a separate class attribute. If the tag already has a class attribute, check that it uses double quotes.");
98
117
  }
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);
118
+ if (i !== _) if (i instanceof c) i.value !== void 0 && (t.result += ` ${e}="${S(String(i.value))}"`);
119
+ else if (typeof i == "boolean") {
120
+ let n = T(e, i);
102
121
  n === "" ? t.result += ` ${e}` : n !== null && (t.result += ` ${e}="${n}"`);
103
- } else typeof a == "function" || a != null && (t.result += ` ${e}="${w(a)}"`);
122
+ } else typeof i == "function" || i != null && (t.result += ` ${e}="${S(i)}"`);
104
123
  }
105
- }, k = (e, t) => {
124
+ }, ie = (e, t) => {
106
125
  let n = e.lastIndexOf("<"), r = n >= 0 ? e.slice(n) : e, i = r.match(/(^|\s)class="([^"]*)"(\s|\/?>|$)/);
107
126
  if (!i) return null;
108
- let a = i[1], o = i[2], s = i[3], c = t.trim();
127
+ let a = i[1], o = i[2], s = i[3], c = S(t.trim());
109
128
  if (!c) return e;
110
- let l = o ? `${o} ${c}` : c, u = r.replace(i[0], `${a}class="${l}"${s}`);
129
+ let l = o ? `${o} ${c}` : c, u = r.replace(i[0], () => `${a}class="${l}"${s}`);
111
130
  return e.slice(0, n) + u;
112
- }, A = (e) => {
131
+ }, ae = (e) => {
113
132
  let t = [], n = !1, r = null, i = /(\.\.\.|[.@?]?[a-zA-Z0-9_:-]+)=["']?$/;
114
133
  for (let a = 0; a < e.length - 1; a++) {
115
134
  let o = e[a];
@@ -118,7 +137,7 @@ var m = class {
118
137
  t[a] = !(n && (s || r));
119
138
  }
120
139
  return t;
121
- }, j = (e) => {
140
+ }, E = (e) => {
122
141
  let t = 0, n;
123
142
  do
124
143
  n = `\uE000cossack:${t++}:`;
@@ -128,9 +147,9 @@ var m = class {
128
147
  marker: (e) => `${n}${e}${r}`,
129
148
  pattern: RegExp(`${n}(\\d+)${r}`, "g")
130
149
  };
131
- }, M = (e, t) => {
150
+ }, D = (e, t) => {
132
151
  if (!t) return e;
133
- let n = j(e), r = "";
152
+ let n = E(e), r = "";
134
153
  for (let t = 0; t < e.length; t++) r += e[t], t < e.length - 1 && (r += n.marker(t));
135
154
  let i = "", a = !1, o = null, s = "", c = "";
136
155
  for (let e = 0; e < r.length;) {
@@ -159,8 +178,8 @@ var m = class {
159
178
  let l = [], u = 0, d;
160
179
  for (; d = n.pattern.exec(i);) l.push(i.slice(u, d.index)), u = d.index + d[0].length;
161
180
  return l.push(i.slice(u)), l;
162
- }, N = (e, t) => {
163
- let n = j(e), r = "";
181
+ }, oe = (e, t) => {
182
+ let n = E(e), r = "";
164
183
  for (let t = 0; t < e.length; t++) r += e[t], t < e.length - 1 && (r += n.marker(t));
165
184
  let i = /* @__PURE__ */ new Map(), a = /<(?![!/])(?:[^>"']|"[^"]*"|'[^']*')*>/g, o;
166
185
  for (; o = a.exec(r);) {
@@ -180,7 +199,7 @@ var m = class {
180
199
  }
181
200
  }
182
201
  return i;
183
- }, P = class {
202
+ }, se = class {
184
203
  resultObj;
185
204
  opts;
186
205
  result = "";
@@ -190,63 +209,178 @@ var m = class {
190
209
  strings;
191
210
  nothingAttributeGroups;
192
211
  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);
212
+ this.resultObj = e, this.opts = t, this.strings = D(e.strings, e.__cossackScope), this.isNodePositions = ae(this.strings), this.nothingAttributeGroups = oe(this.strings, e.values);
194
213
  }
195
214
  scan() {
196
- let { values: e } = this.resultObj, t = this.strings;
197
- 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++;
215
+ let { values: t } = this.resultObj, n = this.strings;
216
+ for (; this.stringIdx < n.length;) {
217
+ let e = n[this.stringIdx].substring(this.charIdxForNext || 0), r = e.match(/(\.\.\.|[.@?]?[a-zA-Z0-9_:-]+)=["']?$/);
218
+ if (this.result += e, this.charIdxForNext = 0, this.stringIdx < n.length - 1) {
219
+ let e = t[this.stringIdx], i = this.nothingAttributeGroups.get(this.stringIdx);
220
+ if (i) {
221
+ this.result.endsWith(i.prefix) && (this.result = this.result.slice(0, -i.prefix.length)), this.stringIdx = i.last, this.charIdxForNext = i.suffix.length, this.stringIdx++;
203
222
  continue;
204
223
  }
205
224
  if (r && !this.isNodePositions[this.stringIdx]) {
206
- let e = r[0], a = r[1];
207
- this.result = this.result.substring(0, this.result.length - e.length);
208
- let o = e.endsWith("\"") ? "\"" : e.endsWith("'") ? "'" : "", c = !0;
209
- if (a === "...") {
225
+ let t = r[0], i = r[1];
226
+ this.result = this.result.substring(0, this.result.length - t.length);
227
+ let o = t.endsWith("\"") ? "\"" : t.endsWith("'") ? "'" : "", s = !0;
228
+ if (i === "...") {
210
229
  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") {
230
+ let t = { result: this.result };
231
+ re(e, t), this.result = t.result;
232
+ } else if (i === "ref") this.result = this.result.trimEnd();
233
+ else if (i.startsWith("@")) this.result = this.result.trimEnd();
234
+ else if (i.startsWith("?")) e ? (this.result = this.result.trimEnd(), this.result += ` ${i.slice(1)}`) : this.result = this.result.trimEnd();
235
+ else if (i.startsWith(".")) {
236
+ let t, n = e instanceof a;
237
+ t = n ? p(e.component, e.fieldName) : e;
238
+ let r = i === ".value" ? ".value" : i.slice(1), o = n && w.has(r);
239
+ if (r === ".value") {
240
+ let e = t instanceof u ? t.value : t;
241
+ this.result = this.result.trimEnd(), this.result += ` .value="${S(String(e ?? ""))}"`;
242
+ } else o ? (this.result = this.result.trimEnd(), t && (this.result += ` ${r}`)) : t != null && t !== !1 ? (this.result = this.result.trimEnd(), this.result += ` ${r}="${C(t, this.opts)}"`) : this.result = this.result.trimEnd();
243
+ } else if (e instanceof c) e.value === void 0 ? this.result = this.result.trimEnd() : (this.result = this.result.trimEnd(), this.result += ` ${i}="${S(String(e.value))}"`);
244
+ else if (e == null || e === _) this.result = this.result.trimEnd();
245
+ else if (typeof e == "boolean") {
224
246
  this.result = this.result.trimEnd();
225
- let e = D(a, n);
226
- e === "" ? this.result += ` ${a}` : e !== null && (this.result += ` ${a}="${e}"`);
247
+ let t = T(i, e);
248
+ t === "" ? this.result += ` ${i}` : t !== null && (this.result += ` ${i}="${t}"`);
227
249
  } else {
228
- this.result += e;
229
- let t = T(n, this.opts);
230
- o ? (this.result += t, c = !1) : this.result += `"${t}"`;
250
+ this.result += t;
251
+ let n = C(e, this.opts);
252
+ o ? (this.result += n, s = !1) : this.result += `"${n}"`;
231
253
  }
232
- if (o && c) {
233
- let e = t[this.stringIdx + 1];
254
+ if (o && s) {
255
+ let e = n[this.stringIdx + 1];
234
256
  e && e.startsWith(o) && (this.charIdxForNext = 1);
235
257
  }
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);
258
+ } else this.opts.hydrate && this.isNodePositions[this.stringIdx] ? (this.result += `<!--CRP_${this.stringIdx}-->`, this.result += C(e, this.opts), this.result += "<!--/CRP-->") : this.result += C(e, this.opts);
237
259
  }
238
260
  this.stringIdx++;
239
261
  }
240
- return this.result;
262
+ return e(this.result);
263
+ }
264
+ }, O = (e, t = {}) => new se(e, t).scan(), k = !!import.meta.env?.DEV, A = k ? /* @__PURE__ */ new WeakMap() : void 0, j = (e, t) => {
265
+ if (!(!k || !t)) for (let n = 0; n < e.length; n++) e[n] && A.set(e[n], {
266
+ ...t,
267
+ fields: void 0,
268
+ statePath: t.fields?.[n] ?? t.statePath
269
+ });
270
+ }, M = (e, t) => ({
271
+ identity: e.identity,
272
+ parts: e.parts.map((n) => {
273
+ if (!n) return;
274
+ let r = n.component;
275
+ if (r) {
276
+ let e = r.token ? r.fork?.(r.token) : void 0;
277
+ if (r.token && !e) return;
278
+ r = {
279
+ ...r,
280
+ token: e,
281
+ instance: t.owners.get(r.instance) ?? r.instance,
282
+ parent: r.parent ? t.owners.get(r.parent) ?? r.parent : r.parent
283
+ };
284
+ }
285
+ return {
286
+ ...n,
287
+ component: r,
288
+ template: n.template ? M(n.template, t) : void 0,
289
+ cached: new Map([...n.cached].map(([e, n]) => [e, M(n, t)])),
290
+ children: M({
291
+ identity: e.identity,
292
+ parts: n.children
293
+ }, t).parts,
294
+ keys: [...n.keys]
295
+ };
296
+ })
297
+ }), N = /* @__PURE__ */ new WeakMap(), P = (e) => e.map((e) => e instanceof z ? e.captureDraft() : void 0), F = (e, t, n) => {
298
+ !t || t.tree.identity !== n || !t.context.active || !t.context.current() || e.forEach((e, n) => {
299
+ e instanceof z && e.restoreDraft(t.tree.parts[n], t.context);
300
+ });
301
+ }, ce = (e) => {
302
+ let t = G.get(e), n = t ? {
303
+ identity: t.identity,
304
+ parts: P(t.parts)
305
+ } : void 0, r;
306
+ return {
307
+ restore(t, i = []) {
308
+ !n || r || (r = {
309
+ active: !0,
310
+ current: t,
311
+ owners: new Map(i)
312
+ }, N.set(e, {
313
+ tree: n,
314
+ context: r
315
+ }), n = void 0);
316
+ },
317
+ discard() {
318
+ n = void 0, r && (r.active = !1, r.owners.clear(), N.get(e)?.context === r && N.delete(e));
319
+ }
320
+ };
321
+ }, I = (e) => {
322
+ for (let t of e) try {
323
+ t?.dispose?.();
324
+ } catch (e) {
325
+ console.error("[cossack] Failed render cleanup:", e);
241
326
  }
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));
327
+ }, L = /* @__PURE__ */ new WeakMap(), R = (e) => {
328
+ let t = L.get(e.strings);
329
+ t || (t = /* @__PURE__ */ new Map(), L.set(e.strings, t));
245
330
  let n = `${e._$litType$}:${e.__cossackScope ?? ""}`, r = t.get(n);
246
331
  return r || (r = {}, t.set(n, r)), r;
247
- }, R = class n {
332
+ }, z = class e {
248
333
  startNode;
249
334
  endNode;
335
+ _draft;
336
+ _componentDraft;
337
+ _cacheDrafts = /* @__PURE__ */ new Map();
338
+ captureDraft() {
339
+ let e = this.componentInstance, t = /* @__PURE__ */ new Map();
340
+ for (let [e, n] of this._cacheDrafts) n.context.active && n.context.current() && t.set(e, M(n.tree, n.context));
341
+ for (let [e, n] of this._cacheMap) e !== this._cachedTemplateIdentity && t.set(e, {
342
+ identity: e,
343
+ parts: P(n.parts)
344
+ });
345
+ return {
346
+ component: e ? {
347
+ instance: e,
348
+ clazz: e.constructor,
349
+ parent: e.__parent,
350
+ scope: e._getServiceScope?.(),
351
+ token: e._frameworkCaptureClientDraft?.(),
352
+ fork: e.constructor._frameworkForkClientDraft?.bind(e.constructor)
353
+ } : void 0,
354
+ template: this._cachedParts && this._cachedTemplateIdentity ? {
355
+ identity: this._cachedTemplateIdentity,
356
+ parts: P(this._cachedParts)
357
+ } : void 0,
358
+ cached: t,
359
+ children: this._childParts.map((e) => e.captureDraft()),
360
+ keys: [...this._partKeys],
361
+ keySet: this._keySet,
362
+ key: this._key
363
+ };
364
+ }
365
+ restoreDraft(e, t) {
366
+ this._draft = e && t.active && t.current() ? {
367
+ snapshot: e,
368
+ context: t
369
+ } : void 0;
370
+ }
371
+ restoreComponentDraft(e) {
372
+ let t = this._draft;
373
+ if (this._componentDraft = void 0, !t || !t.context.active || !t.context.current()) return;
374
+ let n = t.snapshot.component;
375
+ !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 = {
376
+ tree: t.snapshot.template,
377
+ context: t.context
378
+ }));
379
+ }
380
+ updateComponentTemplate(e) {
381
+ let t = this._componentDraft;
382
+ this._componentDraft = void 0, this.updateNode(e, t ?? null);
383
+ }
250
384
  componentInstance = null;
251
385
  renderListener = null;
252
386
  _childParts = [];
@@ -267,25 +401,37 @@ var m = class {
267
401
  _beginHydration() {
268
402
  this._hydrating = !0;
269
403
  }
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);
404
+ update(e) {
405
+ try {
406
+ if (!(e instanceof o) && !(e instanceof s) && this._cacheDrafts.clear(), this._hydrating) {
407
+ this._hydrating = !1, this._hydrateValue(e);
281
408
  return;
282
409
  }
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;
410
+ e instanceof s && (e = this._resolveGuard(e), e instanceof o || this._cacheDrafts.clear());
411
+ let n = !1;
412
+ if (e instanceof o) {
413
+ this.prepareCacheDrafts();
414
+ let t = e.value;
415
+ if (v(t)) {
416
+ this.componentInstance && this.disposeComponent(), this._childParts.length > 0 && this.clearChildParts(), this.updateCache(t);
417
+ return;
418
+ }
419
+ e = t, n = !0;
420
+ } else if (this._cacheMap.size > 0 || this._cacheCurrent) {
421
+ for (let e of this._cacheMap.values()) for (let t of e.parts) t && typeof t.dispose == "function" && t.dispose();
422
+ this._cacheMap.clear(), this._cacheCurrent = null;
423
+ }
424
+ if (n && this._cacheCurrent) {
425
+ if (this._stashCurrent(), this._draft) {
426
+ for (let e of this._cacheMap.values()) I(e.parts);
427
+ this._cacheMap.clear();
428
+ }
429
+ this._cacheCurrent = null;
430
+ }
431
+ this.componentInstance && (!t(e) || e.clazz !== this.componentInstance.constructor) && this.disposeComponent(), !(e instanceof f) && !Array.isArray(e) && this._childParts.length > 0 && this.clearChildParts(), !(e instanceof l) && this._draft?.snapshot.keySet && (this._draft = void 0), t(e) ? this.updateComponent(e) : e instanceof f ? this.updateRepeat(e) : e instanceof l ? this.updateKey(e) : Array.isArray(e) ? this.updateArray(e) : this.updateNode(e);
432
+ } finally {
433
+ this._draft = void 0;
287
434
  }
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
435
  }
290
436
  _resolveGuard(e) {
291
437
  let t = Array.isArray(e.deps) ? [...e.deps] : [e.deps];
@@ -296,95 +442,119 @@ var m = class {
296
442
  for (let n = 0; n < e.length; n++) if (e[n] !== t[n]) return !1;
297
443
  return !0;
298
444
  }
299
- _hydrateValue(t) {
300
- if (e(t)) {
301
- this._adoptComponent(t);
445
+ _hydrateValue(e) {
446
+ if (t(e)) {
447
+ this._adoptComponent(e);
302
448
  return;
303
449
  }
304
- if (y(t)) {
305
- this._adoptTemplate(t);
450
+ if (v(e)) {
451
+ this._adoptTemplate(e);
306
452
  return;
307
453
  }
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)));
454
+ if (e instanceof f) {
455
+ this._adoptSequence(e.items.map((t, n) => e.templateFn(t, n)), e.items.map((t, n) => e.keyFn(t, n)));
310
456
  return;
311
457
  }
312
- if (Array.isArray(t)) {
313
- this._adoptSequence(t);
458
+ if (Array.isArray(e)) {
459
+ this._adoptSequence(e);
314
460
  return;
315
461
  }
316
- if (t instanceof c) {
317
- if (y(t.template)) {
318
- this._keySet = !0, this._key = t.value, this._adoptTemplate(t.template);
462
+ if (e instanceof l) {
463
+ if (v(e.template)) {
464
+ this._keySet = !0, this._key = e.value, this._adoptTemplate(e.template);
319
465
  return;
320
466
  }
321
- this.clear(), this._clearTemplateCache(), this.updateNode(t.template);
467
+ this.clear(), this._clearTemplateCache(), this.updateNode(e.template);
322
468
  return;
323
469
  }
324
- if (t instanceof o) {
325
- let e = this._resolveGuard(t);
326
- this._hydrateValue(e);
470
+ if (e instanceof s) {
471
+ let t = this._resolveGuard(e);
472
+ this._hydrateValue(t);
327
473
  return;
328
474
  }
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);
475
+ if (e instanceof o) {
476
+ let t = e.value;
477
+ if (v(t)) {
478
+ let e = R(t);
479
+ this._cacheMap.has(e) ? this.updateCache(t) : (this._adoptTemplate(t), this._cacheCurrent = e);
480
+ } else this._hydrateValue(t);
335
481
  return;
336
482
  }
337
- if (t === _ || t === "" || t == null || t === !1) {
483
+ if (e === _ || e === "" || e == null || e === !1) {
338
484
  this.clear();
339
485
  return;
340
486
  }
341
- if (!(t instanceof Node)) {
342
- if (C(t)) {
487
+ if (!(e instanceof Node)) {
488
+ if (x(e)) {
343
489
  if (this.startNode.nextSibling !== this.endNode) return;
344
- this.clear(), this._clearTemplateCache(), this.updateNode(t);
490
+ this.clear(), this._clearTemplateCache(), this.updateNode(e);
345
491
  return;
346
492
  }
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));
493
+ if (typeof e != "object") {
494
+ let t = String(e), n = this.startNode.nextSibling;
495
+ n && n.nodeType === Node.TEXT_NODE && n.nextSibling === this.endNode ? n.nodeValue !== t && (k && X(new J("text-content", ue(n), "text", "text"), A.get(this)?.boundary ?? "root", void 0, A.get(this), "text-reconcile"), n.nodeValue = t) : (this.clear(), this.startNode.parentNode.insertBefore(document.createTextNode(t), this.endNode));
350
496
  return;
351
497
  }
352
- this.clear(), this._clearTemplateCache(), this.updateNode(t);
498
+ this.clear(), this._clearTemplateCache(), this.updateNode(e);
353
499
  }
354
500
  }
355
501
  _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;
502
+ let t = e.parent || n.currentRenderingInstance;
503
+ if (this.componentInstance && (this.componentInstance.constructor !== e.clazz || this.componentInstance.__parent !== t) && this.disposeComponent(), !this.componentInstance) {
504
+ if (this.componentInstance = new e.clazz(), this.componentInstance.__parent = t, 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++}`), k) {
505
+ let t = this.componentInstance.hydrationMetadata, n = A.get(this);
506
+ A.set(this, {
507
+ ...n,
508
+ ...t,
509
+ component: t?.component ?? e.clazz.name ?? n?.component,
510
+ statePath: t?.statePath,
511
+ fields: t?.fields,
512
+ boundary: "component"
513
+ });
514
+ }
515
+ let n = !0;
516
+ this.renderListener = (t) => {
517
+ if (n) if (n = !1, v(t)) try {
518
+ this._adoptTemplate(t);
519
+ } catch (n) {
520
+ if (n instanceof J) k && X(n, "component", e.clazz.name || "anonymous", A.get(this)), this.clear(), this._clearTemplateCache(), this.updateComponentTemplate(t);
521
+ else throw n;
366
522
  }
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();
523
+ else t == null ? this.clear() : this.updateComponentTemplate(t);
524
+ else this.updateComponentTemplate(t);
525
+ }, 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;
526
+ try {
527
+ this.componentInstance._frameworkRendererAttached?.(), this.restoreComponentDraft(this.componentInstance), this.componentInstance.connectedCallback();
528
+ } catch (e) {
529
+ throw this.disposeComponent(), e;
530
+ }
370
531
  }
371
532
  let r = this.componentInstance;
372
- Object.assign(r, e.props), "props" in r && (r.props = e.props), r.children = e.children, r.requestUpdate();
533
+ this._draft?.snapshot.component?.instance === r && this.restoreComponentDraft(r), Object.assign(r, e.props), "props" in r && (r.props = e.props), r.children = e.children, r.requestUpdate();
373
534
  }
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]);
535
+ _adoptSequence(t, n) {
536
+ let r = le(this._nodesBetween());
537
+ if (r.length !== t.length) throw new J("sequence-length", "sequence", `${t.length} client items`, `${r.length} SSR items`);
538
+ this._childParts = [], this._partKeys = n ? [] : this._partKeys;
539
+ for (let i = 0; i < t.length; i++) {
540
+ let [a, o] = r[i], s = new e(a, o);
541
+ if (k) {
542
+ let e = A.get(this);
543
+ e && A.set(s, e);
544
+ }
545
+ s._beginHydration(), this._childParts.push(s), n && this._partKeys.push(n[i]), s.update(t[i]);
381
546
  }
382
547
  }
383
548
  _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;
549
+ let { fragment: t, parts: n } = K(e);
550
+ try {
551
+ let r = this._nodesBetween(), i = /* @__PURE__ */ new Map();
552
+ Q(t.childNodes, r, i, "root", k ? A.get(this) : void 0), $(n, i), j(n, k ? A.get(this) : void 0);
553
+ for (let t = 0; t < e.values.length; t++) n[t] && n[t].update(e.values[t]);
554
+ this._cachedTemplateIdentity = R(e), this._cachedParts = n;
555
+ } catch (e) {
556
+ throw I(n), e;
557
+ }
388
558
  }
389
559
  _nodesBetween() {
390
560
  let e = [], t = this.startNode.nextSibling;
@@ -392,20 +562,36 @@ var m = class {
392
562
  return e;
393
563
  }
394
564
  teardownComponent(e) {
395
- if (this.renderListener &&= (e.removeRenderListener(this.renderListener), null), e.disconnectedCallback(), typeof e.destroy == "function") try {
396
- e.destroy();
397
- } catch {}
565
+ this.renderListener &&= (e.removeRenderListener(this.renderListener), null);
566
+ try {
567
+ e.disconnectedCallback();
568
+ } catch (e) {
569
+ console.error("[cossack] Component disconnection failed:", e);
570
+ } finally {
571
+ try {
572
+ typeof e.destroy == "function" && e.destroy();
573
+ } catch {} finally {
574
+ e._frameworkRendererDetached?.();
575
+ }
576
+ }
398
577
  }
399
578
  disposeComponent() {
400
579
  this.componentInstance && (this.teardownComponent(this.componentInstance), this.componentInstance = null, this._clearTemplateCache());
401
580
  }
402
581
  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());
582
+ let t = e.parent || n.currentRenderingInstance;
583
+ if (this.componentInstance && this.componentInstance.__parent !== t && this.disposeComponent(), !this.componentInstance) {
584
+ this.componentInstance = new e.clazz(), this.componentInstance.__parent = t, 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) => {
585
+ this.updateComponentTemplate(e);
586
+ }, 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;
587
+ try {
588
+ this.componentInstance._frameworkRendererAttached?.(), this.restoreComponentDraft(this.componentInstance), this.componentInstance.connectedCallback();
589
+ } catch (e) {
590
+ throw this.disposeComponent(), e;
591
+ }
592
+ }
407
593
  let r = this.componentInstance;
408
- Object.assign(r, e.props), "props" in r && (r.props = e.props), r.children = e.children, r.requestUpdate();
594
+ this._draft?.snapshot.component?.instance === r && this.restoreComponentDraft(r), Object.assign(r, e.props), "props" in r && (r.props = e.props), r.children = e.children, r.requestUpdate();
409
595
  }
410
596
  updateRepeat(e) {
411
597
  let { items: t, keyFn: n, templateFn: r } = e, i = /* @__PURE__ */ new Map();
@@ -414,35 +600,57 @@ var m = class {
414
600
  n !== void 0 && i.set(n, e);
415
601
  });
416
602
  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;
603
+ try {
604
+ t.forEach((e, t) => {
605
+ let c = n(e, t);
606
+ o.push(c);
607
+ let l = i.get(c);
608
+ l ? (i.delete(c), l.startNode.previousSibling !== s && this.movePart(l, s)) : l = this.createChildPart(s), a.push(l);
609
+ let u = this._draft?.snapshot.keys.findIndex((e) => Object.is(e, c)) ?? -1;
610
+ this._draft && u !== -1 && l.restoreDraft(this._draft.snapshot.children[u], this._draft.context), l.update(r(e, t)), s = l.endNode;
611
+ }), i.forEach((e) => e.dispose()), this._childParts = a, this._partKeys = o;
612
+ } catch (e) {
613
+ throw I([.../* @__PURE__ */ new Set([...this._childParts, ...a])]), this._childParts = [], this._partKeys = [], e;
614
+ }
423
615
  }
424
616
  updateKey(e) {
617
+ this._draft && (!this._draft.snapshot.keySet || !Object.is(this._draft.snapshot.key, e.value)) && (this._draft = void 0);
425
618
  let t = !this._keySet || !Object.is(this._key, e.value);
426
619
  this._keySet = !0, this._key = e.value, t && (this.componentInstance && this.disposeComponent(), this._clearTemplateCache(), this.clear()), this.updateNode(e.template);
427
620
  }
621
+ prepareCacheDrafts() {
622
+ for (let [e, t] of this._cacheDrafts) (!t.context.active || !t.context.current()) && this._cacheDrafts.delete(e);
623
+ if (this._draft && this._draft.context.active && this._draft.context.current()) {
624
+ let { snapshot: e, context: t } = this._draft;
625
+ for (let [e, t] of this._cacheMap) e !== this._cachedTemplateIdentity && (I(t.parts), this._cacheMap.delete(e));
626
+ for (let [n, r] of e.cached) this._cacheDrafts.set(n, {
627
+ tree: r,
628
+ context: t
629
+ });
630
+ e.template && this._cacheDrafts.set(e.template.identity, {
631
+ tree: e.template,
632
+ context: t
633
+ });
634
+ }
635
+ }
428
636
  updateCache(e) {
429
- let t = L(e);
430
- if (this._cacheCurrent === t && this._cachedParts) {
431
- this._cachedParts.forEach((t, n) => {
637
+ let t = R(e), n = this._cacheDrafts.get(t);
638
+ if (this._cacheDrafts.delete(t), this._cacheCurrent === t && this._cachedParts) {
639
+ F(this._cachedParts, n, t), this._cachedParts.forEach((t, n) => {
432
640
  t.update(e.values[n]);
433
641
  });
434
642
  return;
435
643
  }
436
- (this._cacheCurrent || this._cachedParts) && this._stashCurrent();
437
- let n = this._cacheMap.get(t);
438
- if (n) {
644
+ if ((this._cacheCurrent || this._cachedParts) && (this._stashCurrent(), this._draft)) for (let [e, n] of this._cacheMap) e !== t && (I(n.parts), this._cacheMap.delete(e));
645
+ let r = this._cacheMap.get(t);
646
+ if (r) {
439
647
  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);
648
+ let i = this.startNode.parentNode;
649
+ if (!i) return;
650
+ for (let e of r.nodes) i.insertBefore(e, this.endNode);
651
+ this._cachedTemplateIdentity = t, this._cachedParts = r.parts, F(r.parts, n, t);
652
+ for (let t = 0; t < e.values.length; t++) r.parts[t] && r.parts[t].update(e.values[t]);
653
+ } else this.clear(), this.updateNode(e, n ?? null);
446
654
  this._cacheCurrent = t;
447
655
  }
448
656
  _stashCurrent() {
@@ -462,19 +670,19 @@ var m = class {
462
670
  updateArray(e) {
463
671
  this._partKeys = [];
464
672
  let t = Math.min(e.length, this._childParts.length);
465
- for (let n = 0; n < t; n++) this._childParts[n].update(e[n]);
673
+ 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
674
  if (e.length > this._childParts.length) {
467
675
  let n = this._childParts.length > 0 ? this._childParts[this._childParts.length - 1].endNode : this.startNode;
468
676
  for (let r = t; r < e.length; r++) {
469
677
  let t = this.createChildPart(n);
470
- this._childParts.push(t), t.update(e[r]), n = t.endNode;
678
+ 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
679
  }
472
680
  }
473
681
  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
682
  }
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);
683
+ createChildPart(t) {
684
+ let n = document.createComment("CRP-Item"), r = document.createComment("/CRP-Item"), i = t.nextSibling;
685
+ return t.parentNode.insertBefore(n, i), t.parentNode.insertBefore(r, i), new e(n, r);
478
686
  }
479
687
  movePart(e, t) {
480
688
  let n = t.parentNode, r = [], i = e.startNode;
@@ -487,40 +695,57 @@ var m = class {
487
695
  this._childParts.forEach((e) => e.dispose()), this._childParts = [], this._partKeys = [];
488
696
  }
489
697
  dispose() {
490
- if (this.clear(), this._clearTemplateCache(), this._cacheMap.size > 0) {
698
+ if (this._draft = void 0, this._componentDraft = void 0, this._cacheDrafts.clear(), this.clear(), this._clearTemplateCache(), this._cacheMap.size > 0) {
491
699
  for (let e of this._cacheMap.values()) for (let t of e.parts) t && typeof t.dispose == "function" && t.dispose();
492
700
  this._cacheMap.clear();
493
701
  }
494
702
  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
703
  }
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]);
704
+ updateNode(e, n) {
705
+ let r = n === void 0 ? this._draft?.snapshot.template ? {
706
+ tree: this._draft.snapshot.template,
707
+ context: this._draft.context
708
+ } : void 0 : n ?? void 0;
709
+ if (this.startNode.parentNode) if (v(e)) if (this._cachedTemplateIdentity === R(e) && this._cachedParts) F(this._cachedParts, r, R(e)), this._cachedParts.forEach((t, n) => {
710
+ t.update(e.values[n]);
499
711
  });
500
712
  else {
501
713
  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);
714
+ let t = document.createDocumentFragment();
715
+ r && N.set(t, r), q(e, t), this.startNode.parentNode.insertBefore(t, this.endNode);
716
+ let n = G.get(t.firstChild?.childNodes[0] || t);
505
717
  n && (this._cachedTemplateIdentity = n.identity, this._cachedParts = n.parts);
506
718
  }
507
- else if (C(t)) {
719
+ else if (x(e)) {
508
720
  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)) {
721
+ let t = this.startNode.parentNode, n = this.startNode.ownerDocument;
722
+ if (t instanceof Element && t.namespaceURI === "http://www.w3.org/2000/svg" && ![
723
+ "foreignObject",
724
+ "desc",
725
+ "title"
726
+ ].includes(t.localName)) {
727
+ let r = n.createElementNS(t.namespaceURI, t.localName);
728
+ r.innerHTML = e.value;
729
+ let i = n.createDocumentFragment();
730
+ for (; r.firstChild;) i.appendChild(r.firstChild);
731
+ t.insertBefore(i, this.endNode);
732
+ } else {
733
+ let r = n.createElement("template");
734
+ r.innerHTML = e.value, t.insertBefore(r.content, this.endNode);
735
+ }
736
+ } else if (t(e) || typeof e == "object" && e && !(e instanceof Node) && !Array.isArray(e)) {
512
737
  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();
738
+ let t = document.createDocumentFragment();
739
+ q(g`${e}`, t), this.startNode.parentNode.insertBefore(t, this.endNode);
740
+ } else if (e instanceof Node) this._clearTemplateCache(), this.clear(), this.startNode.parentNode.insertBefore(e, this.endNode);
741
+ else if (e === _ || e === "" || e == null || e === !1) this._clearTemplateCache(), this.clear();
517
742
  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);
743
+ let t = String(e), n = this.startNode.nextSibling;
744
+ if (n && n.nodeType === Node.TEXT_NODE && n.nextSibling === this.endNode) n.nodeValue !== t && (n.nodeValue = t);
520
745
  else {
521
746
  this._clearTemplateCache(), this.clear();
522
- let t = document.createTextNode(e);
523
- this.startNode.parentNode.insertBefore(t, this.endNode);
747
+ let e = document.createTextNode(t);
748
+ this.startNode.parentNode.insertBefore(e, this.endNode);
524
749
  }
525
750
  }
526
751
  }
@@ -535,8 +760,9 @@ var m = class {
535
760
  e.parentNode.removeChild(e), e = t;
536
761
  }
537
762
  }
538
- }, z = class {
763
+ }, B = class {
539
764
  element;
765
+ selectCommit = new H();
540
766
  previousValues = {};
541
767
  bindStates = /* @__PURE__ */ new Map();
542
768
  spreadClasses = [];
@@ -545,7 +771,7 @@ var m = class {
545
771
  }
546
772
  update(e) {
547
773
  if (e !== _ && (typeof e != "object" || !e)) return;
548
- let t = e === _ ? {} : e;
774
+ let t = e === _ ? {} : e, n = this.selectCommit.cancel();
549
775
  for (let e of Object.keys(this.previousValues)) if (!(e in t)) if (e.startsWith("@")) {
550
776
  let t = e.slice(1), n = `__crp_handler_${t}`, r = this.element[n];
551
777
  r && this.element.removeEventListener(t, r);
@@ -557,42 +783,45 @@ var m = class {
557
783
  for (let e of this.spreadClasses) this.element.classList.remove(e);
558
784
  this.spreadClasses = [];
559
785
  } 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;
786
+ for (let [e, r] of Object.entries(t)) if (e.startsWith("@")) {
787
+ let t = e.slice(1), n = `__crp_handler_${t}`, i = this.element[n];
788
+ if (i && this.element.removeEventListener(t, i), r === _ || typeof r != "function" && !(r instanceof d)) {
789
+ this.element[n] = void 0;
564
790
  continue;
565
791
  }
566
792
  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) => {
793
+ if (r instanceof d) {
794
+ let e = r.handler, t = this.element instanceof HTMLFormElement ? this.element : this.element.closest("form");
795
+ t instanceof HTMLFormElement && (r.novalidate ? t.setAttribute("novalidate", "") : t.removeAttribute("novalidate")), a = (t) => {
570
796
  t.preventDefault(), e.call(t.currentTarget, t);
571
797
  };
572
- } else a = n;
573
- this.element[r] = a, this.element.addEventListener(t, a);
798
+ } else a = r;
799
+ this.element[n] = a, this.element.addEventListener(t, a);
574
800
  } 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")) {
801
+ let t = e.slice(1), i = this.bindStates.get(e);
802
+ if (i && !(r instanceof a) && (this.element.removeEventListener(i.listener.__eventName, i.listener), this.bindStates.delete(e)), r === _) this.element[t] = void 0;
803
+ else if (r instanceof a && (t === "value" || t === "checked")) this.applySpreadBind(e, t, r);
804
+ else if (t === "value" && (this.element instanceof HTMLSelectElement || this.element instanceof HTMLTextAreaElement || this.element instanceof HTMLInputElement)) {
805
+ let t = r instanceof u, i = t ? r.value : r, o = i == null ? "" : String(i), s = this.previousValues[e], c = s instanceof u ? s.value : s, l = !(e in this.previousValues) || s instanceof a || s === _ || String(c ?? "") !== o;
806
+ (t ? this.element.value !== o : l) && (this.element.value = o), this.element instanceof HTMLSelectElement && (t || l || n) && this.selectCommit.schedule(this.element, o);
807
+ } else if (r instanceof u && (t === "value" || t === "checked")) {
579
808
  let e = this.element;
580
809
  if (t === "checked") {
581
- let t = !!n.value;
810
+ let t = !!r.value;
582
811
  e.checked !== t && (e.checked = t);
583
812
  } else {
584
- let t = n.value == null ? "" : String(n.value);
813
+ let t = r.value == null ? "" : String(r.value);
585
814
  e.value !== t && (e.value = t);
586
815
  }
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);
816
+ } else r instanceof a ? this.element[t] = p(r.component, r.fieldName) : r instanceof u ? this.element[t] = r.value : this.element[t] = r;
817
+ } else if (e.startsWith("?")) r !== _ && r ? this.element.setAttribute(e.slice(1), "") : this.element.removeAttribute(e.slice(1));
818
+ else if (r === _) e === "class" || e === "className" ? this.applySpreadClass("") : this.element.removeAttribute(e);
819
+ else if (r instanceof c) e === "class" || e === "className" ? this.applySpreadClass(r.value === void 0 ? "" : String(r.value)) : r.value === void 0 ? this.element.removeAttribute(e) : this.element.setAttribute(e, String(r.value));
820
+ else if (r == null) this.element.removeAttribute(e);
821
+ else if (typeof r == "boolean") {
822
+ let t = T(e, r);
594
823
  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));
824
+ } else (e === "class" || e === "className") && typeof r == "string" ? this.applySpreadClass(r) : this.element.setAttribute(e, String(r));
596
825
  this.previousValues = { ...t };
597
826
  }
598
827
  applySpreadClass(e) {
@@ -602,7 +831,7 @@ var m = class {
602
831
  this.spreadClasses = t;
603
832
  }
604
833
  applySpreadBind(e, t, n) {
605
- let r = this.element, i = n.component, a = f(i, n.fieldName), o = this.bindStates.get(e);
834
+ let r = this.element, i = n.component, a = p(i, n.fieldName), o = this.bindStates.get(e);
606
835
  if (t === "checked") {
607
836
  let e = !!a;
608
837
  (!o || o.lastKind !== "checked" || o.lastValue !== e) && (r.checked = e);
@@ -610,12 +839,12 @@ var m = class {
610
839
  let e = a == null ? "" : String(a);
611
840
  (!o || o.lastKind !== "value" || String(o.lastValue) !== e || this.element instanceof HTMLSelectElement && r.value !== e) && (r.value = e);
612
841
  }
613
- let s = H(this.element, t), c = !!o && (o.boundComponent !== i || o.boundField !== n.fieldName || o.listener.__eventName !== s);
842
+ let s = W(this.element, t), c = !!o && (o.boundComponent !== i || o.boundField !== n.fieldName || o.listener.__eventName !== s);
614
843
  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
844
  else {
616
845
  let r = (e) => {
617
846
  let r = e.currentTarget, a = t === "checked" ? !!r[t] : r[t];
618
- i && p(i, n.fieldName, a);
847
+ i && m(i, n.fieldName, a);
619
848
  };
620
849
  r.__eventName = s, this.element.addEventListener(s, r), o = {
621
850
  listener: r,
@@ -623,12 +852,13 @@ var m = class {
623
852
  lastValue: t === "checked" ? !!a : a == null ? "" : String(a),
624
853
  boundComponent: i,
625
854
  boundField: n.fieldName
626
- }, this.bindStates.set(e, o), t === "value" && V(this.element, () => this.bindStates.get(e) === o, () => f(o.boundComponent, o.boundField));
855
+ }, this.bindStates.set(e, o), t === "value" && U(this.element, () => this.bindStates.get(e) === o, () => p(o.boundComponent, o.boundField));
627
856
  }
628
857
  }
629
- }, B = class {
858
+ }, V = class {
630
859
  element;
631
860
  name;
861
+ selectCommit = new H();
632
862
  segments;
633
863
  isMulti;
634
864
  multiState;
@@ -655,6 +885,7 @@ var m = class {
655
885
  this.segments.push(n.substring(a)), this.isMulti = i.length > 1, this.multiState = r ?? null;
656
886
  }
657
887
  update(e) {
888
+ let t = this.selectCommit.cancel();
658
889
  if (e === _) {
659
890
  if (this.bindListener && this._detachBindListener(), this.isMulti && this.multiState) {
660
891
  this.multiState.currentValues[this.multiState.position] = _, this.element.removeAttribute(this.name);
@@ -667,10 +898,10 @@ var m = class {
667
898
  } else this.name.startsWith("?") && this.element.removeAttribute(this.name.slice(1)), this.element.removeAttribute(this.name);
668
899
  return;
669
900
  }
670
- let t = !1;
671
- e instanceof l && (t = !0, e = e.value);
672
901
  let n = !1;
673
- if (e instanceof s && (n = !0, e = e.value), e instanceof i) {
902
+ e instanceof u && (n = !0, e = e.value);
903
+ let r = !1;
904
+ if (e instanceof c && (r = !0, e = e.value), e instanceof a) {
674
905
  this.updateBind(e);
675
906
  return;
676
907
  }
@@ -699,7 +930,7 @@ var m = class {
699
930
  this.element[r] = e, this.element.addEventListener(t, e);
700
931
  }
701
932
  let i = e;
702
- if (i instanceof u) {
933
+ if (i instanceof d) {
703
934
  let e = i.handler, t = this.element instanceof HTMLFormElement ? this.element : this.element.closest("form");
704
935
  t instanceof HTMLFormElement && (i.novalidate ? t.setAttribute("novalidate", "") : t.removeAttribute("novalidate")), i = (t) => {
705
936
  t.preventDefault(), e.call(t.currentTarget, t);
@@ -707,33 +938,33 @@ var m = class {
707
938
  }
708
939
  this.element[n] = typeof i == "function" ? i : void 0, this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
709
940
  } 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");
941
+ let r = this.name.slice(1);
942
+ if ((r === "value" || r === "checked") && (this.element instanceof HTMLInputElement || this.element instanceof HTMLTextAreaElement || this.element instanceof HTMLSelectElement)) if (r === "checked") {
943
+ let t = !!e;
944
+ (n ? this.element.checked !== t : this.lastFormKind !== "checked" || this.lastFormValue !== t) && (this.element.checked = t, this.lastFormValue = t, this.lastFormKind = "checked");
714
945
  } 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");
946
+ let r = e == null ? "" : String(e), i = n ? this.element.value !== r : this.lastFormKind !== "value" || String(this.lastFormValue) !== r;
947
+ 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
948
  }
718
- else this.element[n] = e;
949
+ else this.element[r] = e;
719
950
  this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
720
951
  } else if (this.name.startsWith("?")) {
721
952
  let t = this.name.slice(1);
722
953
  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]);
954
+ } else if (r) e === void 0 ? this.element.removeAttribute(this.name) : this.element.setAttribute(this.name, this.segments[0] + String(e) + this.segments[1]);
724
955
  else if (e == null) this.element.removeAttribute(this.name);
725
956
  else if (typeof e == "boolean") {
726
- let t = D(this.name, e);
957
+ let t = T(this.name, e);
727
958
  t === null ? this.element.removeAttribute(this.name) : this.element.setAttribute(this.name, t);
728
959
  } else this.element.setAttribute(this.name, this.segments[0] + String(e) + this.segments[1]);
729
960
  }
730
961
  updateBind(e) {
731
962
  let t = this.name.startsWith(".") ? this.name.slice(1) : this.name;
732
963
  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();
964
+ this.element[t] = p(e.component, e.fieldName), this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name), this.bindListener && this._detachBindListener();
734
965
  return;
735
966
  }
736
- let n = this.element, r = e.component, i = f(r, e.fieldName);
967
+ let n = this.element, r = e.component, i = p(r, e.fieldName);
737
968
  if (t === "checked") {
738
969
  let e = !!i;
739
970
  (this.lastFormKind !== "checked" || this.lastFormValue !== e) && (n.checked = e, this.lastFormValue = e, this.lastFormKind = "checked");
@@ -741,26 +972,38 @@ var m = class {
741
972
  let e = i == null ? "" : String(i);
742
973
  (this.lastFormKind !== "value" || String(this.lastFormValue) !== e || this.element instanceof HTMLSelectElement && n.value !== e) && (n.value = e, this.lastFormValue = e, this.lastFormKind = "value");
743
974
  }
744
- let a = H(this.element, t);
975
+ let a = W(this.element, t);
745
976
  if (this.bindListener && (this.boundPropName !== t || this.boundComponent !== r || this.boundFieldName !== e.fieldName || this.bindListener.__eventName !== a) && this._detachBindListener(), !this.bindListener) {
746
977
  let n = (n) => {
747
978
  let i = n.currentTarget, a = t === "checked" ? !!i[t] : i[t];
748
- r && p(r, e.fieldName, a);
979
+ r && m(r, e.fieldName, a);
749
980
  };
750
981
  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
982
  let e = n;
752
- V(this.element, () => this.bindListener === e, () => f(this.boundComponent, this.boundFieldName));
983
+ U(this.element, () => this.bindListener === e, () => p(this.boundComponent, this.boundFieldName));
753
984
  }
754
985
  }
755
986
  this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
756
987
  }
757
- }, V = (e, t, n) => {
988
+ }, H = class {
989
+ pending = null;
990
+ cancel() {
991
+ let e = this.pending !== null;
992
+ return this.pending = null, e;
993
+ }
994
+ schedule(e, t) {
995
+ let n = {};
996
+ this.pending = n, queueMicrotask(() => {
997
+ this.pending === n && (this.pending = null, e.value !== t && (e.value = t));
998
+ });
999
+ }
1000
+ }, U = (e, t, n) => {
758
1001
  e instanceof HTMLSelectElement && queueMicrotask(() => {
759
1002
  if (!t()) return;
760
1003
  let r = n(), i = r == null ? "" : String(r);
761
1004
  e.value !== i && (e.value = i);
762
1005
  });
763
- }, H = (e, t) => {
1006
+ }, W = (e, t) => {
764
1007
  if (e instanceof HTMLSelectElement) return "change";
765
1008
  if (e instanceof HTMLTextAreaElement) return "input";
766
1009
  if (e instanceof HTMLInputElement) {
@@ -768,8 +1011,8 @@ var m = class {
768
1011
  return t === "checked" || n === "checkbox" || n === "radio" || n === "range" ? "change" : "input";
769
1012
  }
770
1013
  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_:-]+)=["']?$/;
1014
+ }, G = /* @__PURE__ */ new WeakMap(), K = (e) => {
1015
+ let t = [], n = D(e.strings, e.__cossackScope), r = "", i = !1, a = null, o = /(\.\.\.|[.@?]?[a-zA-Z0-9_:-]+)=["']?$/;
773
1016
  for (let e = 0; e < n.length - 1; e++) {
774
1017
  let t = n[e];
775
1018
  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);
@@ -800,7 +1043,7 @@ var m = class {
800
1043
  if (n) {
801
1044
  let r = parseInt(n[1]), i = document.createComment("/CRP");
802
1045
  e.parentNode.insertBefore(i, e.nextSibling);
803
- let a = new R(e, i);
1046
+ let a = new z(e, i);
804
1047
  t[r] = a;
805
1048
  }
806
1049
  } else if (e.nodeType === Node.ELEMENT_NODE) {
@@ -811,10 +1054,10 @@ var m = class {
811
1054
  if (r.length === 1) {
812
1055
  let i = parseInt(r[0][1]);
813
1056
  if (!t[i]) if (e.name === "...") {
814
- let e = new z(n);
1057
+ let e = new B(n);
815
1058
  t[i] = e, n.removeAttribute("...");
816
1059
  } else {
817
- let r = new B(n, e.name, e.value);
1060
+ let r = new V(n, e.name, e.value);
818
1061
  t[i] = r;
819
1062
  }
820
1063
  return;
@@ -824,7 +1067,7 @@ var m = class {
824
1067
  for (let r = 0; r < i.length; r++) {
825
1068
  let o = i[r];
826
1069
  if (!t[o]) {
827
- let i = new B(n, e.name, e.value, {
1070
+ let i = new V(n, e.name, e.value, {
828
1071
  currentValues: a,
829
1072
  position: r
830
1073
  });
@@ -837,31 +1080,70 @@ var m = class {
837
1080
  fragment: l,
838
1081
  parts: t
839
1082
  };
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) => {
1083
+ }, q = (e, t) => {
1084
+ let n = N.get(t);
1085
+ N.delete(t);
1086
+ let r = G.get(t), i = R(e);
1087
+ if (r && r.identity === i) {
1088
+ F(r.parts, n, i), r.parts.forEach((t, n) => {
844
1089
  t.update(e.values[n]);
845
1090
  });
846
1091
  return;
847
1092
  }
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
1093
+ if (r) {
1094
+ G.delete(t);
1095
+ for (let e of r.parts) e && typeof e.dispose == "function" && e.dispose();
1096
+ }
1097
+ let { fragment: a, parts: o } = K(e);
1098
+ F(o, n, i);
1099
+ let s = e.values;
1100
+ try {
1101
+ for (let e = 0; e < s.length; e++) o[e] && o[e].update(s[e]);
1102
+ if (typeof HTMLElement < "u" && t instanceof HTMLElement || typeof ShadowRoot < "u" && t instanceof ShadowRoot) t.innerHTML = "";
1103
+ else for (; t.firstChild;) t.removeChild(t.firstChild);
1104
+ t.appendChild(a), G.set(t, {
1105
+ identity: i,
1106
+ parts: o
1107
+ });
1108
+ } catch (e) {
1109
+ throw I(o), e;
1110
+ }
1111
+ }, J = class extends Error {
1112
+ category;
1113
+ path;
1114
+ expected;
1115
+ actual;
1116
+ constructor(e, t, n, r) {
1117
+ super(`${e} at ${t}: expected ${n}; found ${r}`), this.category = e, this.path = t, this.expected = n, this.actual = r;
1118
+ }
1119
+ }, Y = (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>", X = (e, t, n, r, i = "full-render-fallback") => {
1120
+ if (!k || r?.expectedClientOnly) return;
1121
+ n ??= r?.component;
1122
+ 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) ? {
1123
+ file: o,
1124
+ ...Number.isSafeInteger(r?.source?.line) && r.source.line > 0 ? { line: r.source.line } : {},
1125
+ ...Number.isSafeInteger(r?.source?.column) && r.source.column > 0 ? { column: r.source.column } : {}
1126
+ } : void 0, c = n?.replace(/[^a-zA-Z0-9_$]/g, "_").slice(0, 80);
1127
+ console.warn({
1128
+ event: "cossack.hydration.mismatch",
1129
+ boundary: t,
1130
+ ...c ? { component: c } : {},
1131
+ ...s ? { source: s } : {},
1132
+ ...a ? { statePath: a } : {},
1133
+ category: e.category,
1134
+ path: e.path,
1135
+ expected: e.expected,
1136
+ actual: e.actual,
1137
+ action: i
856
1138
  });
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) => {
1139
+ }, Z = (e) => {
858
1140
  if (e.nodeType === Node.TEXT_NODE) return (e.nodeValue || "").trim() === "";
859
1141
  if (e.nodeType === Node.COMMENT_NODE) {
860
1142
  let t = e.nodeValue || "";
861
1143
  return !/^CRP_\d+$/.test(t) && t !== "/CRP" && t !== "CSA-S" && t !== "CSA-E";
862
1144
  }
863
1145
  return !1;
864
- }, Y = (e) => {
1146
+ }, le = (e) => {
865
1147
  let t = [], n = [];
866
1148
  for (let r of e) {
867
1149
  if (r.nodeType !== Node.COMMENT_NODE) continue;
@@ -873,70 +1155,85 @@ var m = class {
873
1155
  }
874
1156
  }
875
1157
  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}]`);
1158
+ }, ue = (e) => {
1159
+ let t = [];
1160
+ for (let n = e; n?.parentNode && t.length < 12; n = n.parentNode) {
1161
+ let e = Array.prototype.indexOf.call(n.parentNode.childNodes, n);
1162
+ t.unshift(`${n.nodeType === Node.ELEMENT_NODE ? n.nodeName.toLowerCase() : "node"}[${e}]`);
1163
+ }
1164
+ return `root/${t.join("/")}`;
1165
+ }, Q = (e, t, n, r = "root", i) => {
1166
+ let a = 0;
1167
+ for (let o = 0; o < e.length; o++) {
1168
+ let s = e[o];
1169
+ if (Z(s)) continue;
1170
+ for (; a < t.length && Z(t[a]);) a++;
1171
+ let c = t[a], l = `${r}/${s.nodeType === Node.ELEMENT_NODE ? s.nodeName.toLowerCase() : "node"}[${o}]`;
1172
+ if (!c) throw new J("missing-node", l, Y(s), "<none>");
1173
+ if (s.nodeType === Node.ELEMENT_NODE) {
1174
+ if (c.nodeType !== Node.ELEMENT_NODE || s.nodeName !== c.nodeName) throw new J("element-tag", l, Y(s), Y(c));
1175
+ if (s.namespaceURI !== c.namespaceURI) throw new J("element-namespace", l, Y(s), Y(c));
1176
+ if (n.set(s, c), s.nodeName === "TEXTAREA") {
1177
+ a++;
1178
+ continue;
1179
+ }
1180
+ Q(s.childNodes, Array.from(c.childNodes), n, `${r}/${s.nodeName.toLowerCase()}[${o}]`, i);
887
1181
  } else {
888
- if (o.nodeType !== s.nodeType) throw new K(`type mismatch: blueprint ${q(o)} vs existing ${q(s)}`);
889
- n.set(o, s);
1182
+ if (s.nodeType !== c.nodeType) throw new J("node-type", l, Y(s), Y(c));
1183
+ s.nodeType === Node.TEXT_NODE && s.nodeValue !== c.nodeValue && (k && X(new J("text-content", l, "text", "text"), i?.boundary ?? "root", void 0, i, "text-reconcile"), c.nodeValue = s.nodeValue), n.set(s, c);
890
1184
  }
891
- if (i++, o.nodeType === Node.COMMENT_NODE && /^CRP_\d+$/.test(o.nodeValue || "")) {
1185
+ if (a++, s.nodeType === Node.COMMENT_NODE && /^CRP_\d+$/.test(s.nodeValue || "")) {
892
1186
  let e = 1;
893
- for (; i < t.length && e > 0;) {
894
- let n = t[i];
1187
+ for (; a < t.length && e > 0;) {
1188
+ let n = t[a];
895
1189
  if (n.nodeType === Node.COMMENT_NODE) {
896
1190
  if (/^CRP_\d+$/.test(n.nodeValue || "")) e++;
897
1191
  else if (n.nodeValue === "/CRP" && (e--, e === 0)) break;
898
1192
  }
899
- i++;
1193
+ a++;
900
1194
  }
901
1195
  }
902
1196
  }
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) => {
1197
+ for (; a < t.length && Z(t[a]);) a++;
1198
+ if (a < t.length) throw new J("unexpected-node", `${r}/node[${a}]`, "end of template", Y(t[a]));
1199
+ }, $ = (e, t) => {
906
1200
  for (let n of e) if (n) {
907
- if (n instanceof R) {
1201
+ if (n instanceof z) {
908
1202
  let e = t.get(n.startNode), r = t.get(n.endNode);
909
- if (!e || !r) throw new K("node part anchors not found");
1203
+ if (!e || !r) throw new J("missing-part-anchor", "part", "start and end anchors", "missing anchor");
910
1204
  n.startNode = e, n.endNode = r, n._beginHydration();
911
- } else if (n instanceof B || n instanceof z) {
1205
+ } else if (n instanceof V || n instanceof B) {
912
1206
  let e = t.get(n.element);
913
- if (!e) throw new K("attribute part element not found");
1207
+ if (!e) throw new J("missing-part-element", "part", "attribute element", "missing element");
914
1208
  n.element = e;
915
1209
  }
916
1210
  }
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]));
1211
+ }, de = (e, t, n) => {
1212
+ if (!t.hasChildNodes()) return q(e, t);
1213
+ let r = G.get(t), i = R(e);
1214
+ if (r && r.identity === i) {
1215
+ r.parts.forEach((t, n) => t.update(e.values[n]));
922
1216
  return;
923
1217
  }
924
- if (n) for (let e of n.parts) e && typeof e.dispose == "function" && e.dispose();
925
- let i;
1218
+ if (r) {
1219
+ G.delete(t);
1220
+ for (let e of r.parts) e && typeof e.dispose == "function" && e.dispose();
1221
+ }
1222
+ let a = [];
926
1223
  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
1224
+ let { fragment: r, parts: i } = K(e);
1225
+ a = i;
1226
+ let o = /* @__PURE__ */ new Map();
1227
+ Q(r.childNodes, Array.from(t.childNodes), o, "root", k ? n : void 0), $(a, o), j(a, n);
1228
+ for (let t = 0; t < e.values.length; t++) a[t] && a[t].update(e.values[t]);
1229
+ } catch (r) {
1230
+ if (I(a), r instanceof J) return k && X(r, "root", void 0, n), q(e, t);
1231
+ throw r;
1232
+ }
1233
+ G.set(t, {
1234
+ identity: i,
1235
+ parts: a
939
1236
  });
940
1237
  };
941
1238
  //#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 };
1239
+ export { h as TemplateResult, b as UnsafeHTMLResult, ce as captureRenderDrafts, te as component, S as escapeHtml, g as html, de as hydrate, v as isTemplateResult, x as isUnsafeHTML, _ as nothing, q as render, O as renderToString, ee as svg, ne as unsafeHTML };