@cossackframework/renderer 0.8.2 → 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,91 +14,130 @@ 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 = 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;
35
+ [y] = !0;
30
36
  constructor(e) {
31
37
  this.value = e;
32
38
  }
33
- }, x = (e) => new b(e), S = (e) => e instanceof b, C = (e) => String(e).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;"), w = (s, u = {}) => {
34
- if (s === _ || s === "" || s == null || s === !1) return "";
35
- if (Array.isArray(s)) return u.hydrate ? s.map((e) => `<!--CSA-S-->${w(e, u)}<!--CSA-E-->`).join("") : s.map((e) => w(e, u)).join("");
36
- if (y(s)) return N(s, u);
37
- if (s instanceof l) return w(s.value, u);
38
- if (s instanceof d) return u.hydrate ? s.items.map((e, t) => `<!--CSA-S-->${w(s.templateFn(e, t), u)}<!--CSA-E-->`).join("") : s.items.map((e, t) => w(s.templateFn(e, t), u)).join("");
39
- if (s instanceof c) return w(s.template, u);
40
- if (s instanceof o) return w(s.factory(), u);
41
- if (s instanceof a) return w(s.value, u);
42
- if (s instanceof i) return w(f(s.component, s.fieldName), u);
43
- if (e(s)) {
44
- let e = new s.clazz();
45
- 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());
46
- let i = e._finalizeRenderOutput(e.render()), a = "";
47
- return i && (a = y(i) ? N(i, u) : N(h`${i}`, u)), n(), a;
48
- }
49
- return S(s) ? s.value : typeof s == "function" || typeof s == "object" && s && "value" in s ? "" : C(s);
50
- }, T = /* @__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([
62
+ "allowfullscreen",
63
+ "async",
64
+ "autofocus",
65
+ "autoplay",
51
66
  "checked",
67
+ "controls",
68
+ "default",
69
+ "defer",
52
70
  "disabled",
53
- "readonly",
54
- "required",
71
+ "formnovalidate",
55
72
  "hidden",
56
- "selected",
73
+ "inert",
74
+ "ismap",
75
+ "itemscope",
76
+ "loop",
57
77
  "multiple",
58
- "autofocus",
59
- "open"
60
- ]), E = (e, t) => {
78
+ "muted",
79
+ "nomodule",
80
+ "novalidate",
81
+ "open",
82
+ "playsinline",
83
+ "readonly",
84
+ "required",
85
+ "reversed",
86
+ "selected"
87
+ ]), T = (e, t) => w.has(e.toLowerCase()) ? t ? "" : null : String(t), re = (e, t) => {
61
88
  if (!(e === _ || typeof e != "object" || !e)) for (let [n, r] of Object.entries(e)) {
62
89
  if (n.startsWith("@")) continue;
63
- let e = n, a = r;
90
+ let e = n, i = r;
64
91
  if (n.startsWith("?")) {
65
92
  r !== _ && r && (t.result += ` ${n.slice(1)}`);
66
93
  continue;
67
94
  }
68
95
  if (n.startsWith(".")) {
69
- if (e = n.slice(1), a instanceof i ? a = f(a.component, a.fieldName) : a instanceof l && (a = a.value), a === _) continue;
70
- if (T.has(e)) {
71
- 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}`);
72
103
  continue;
73
104
  }
74
105
  }
75
- if ((e === "class" || e === "className") && typeof a == "string" && a) {
76
- let e = D(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);
77
112
  if (e !== null) {
78
113
  t.result = e;
79
114
  continue;
80
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.");
81
117
  }
82
- a !== _ && (a instanceof s ? a.value !== void 0 && (t.result += ` ${e}="${C(String(a.value))}"`) : typeof a == "boolean" ? a && (t.result += ` ${e}`) : typeof a == "function" || a != null && (t.result += ` ${e}="${C(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);
121
+ n === "" ? t.result += ` ${e}` : n !== null && (t.result += ` ${e}="${n}"`);
122
+ } else typeof i == "function" || i != null && (t.result += ` ${e}="${S(i)}"`);
83
123
  }
84
- }, D = (e, t) => {
124
+ }, ie = (e, t) => {
85
125
  let n = e.lastIndexOf("<"), r = n >= 0 ? e.slice(n) : e, i = r.match(/(^|\s)class="([^"]*)"(\s|\/?>|$)/);
86
126
  if (!i) return null;
87
- 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());
88
128
  if (!c) return e;
89
- 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}`);
90
130
  return e.slice(0, n) + u;
91
- }, O = (e) => {
131
+ }, ae = (e) => {
92
132
  let t = [], n = !1, r = null, i = /(\.\.\.|[.@?]?[a-zA-Z0-9_:-]+)=["']?$/;
93
133
  for (let a = 0; a < e.length - 1; a++) {
94
134
  let o = e[a];
95
- for (let e = 0; e < o.length; e++) r ? o[e] === r && (r = null) : 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);
135
+ 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);
96
136
  let s = o.match(i);
97
137
  t[a] = !(n && (s || r));
98
138
  }
99
139
  return t;
100
- }, k = (e) => {
140
+ }, E = (e) => {
101
141
  let t = 0, n;
102
142
  do
103
143
  n = `\uE000cossack:${t++}:`;
@@ -107,9 +147,9 @@ var m = class {
107
147
  marker: (e) => `${n}${e}${r}`,
108
148
  pattern: RegExp(`${n}(\\d+)${r}`, "g")
109
149
  };
110
- }, A = (e, t) => {
150
+ }, D = (e, t) => {
111
151
  if (!t) return e;
112
- let n = k(e), r = "";
152
+ let n = E(e), r = "";
113
153
  for (let t = 0; t < e.length; t++) r += e[t], t < e.length - 1 && (r += n.marker(t));
114
154
  let i = "", a = !1, o = null, s = "", c = "";
115
155
  for (let e = 0; e < r.length;) {
@@ -138,8 +178,8 @@ var m = class {
138
178
  let l = [], u = 0, d;
139
179
  for (; d = n.pattern.exec(i);) l.push(i.slice(u, d.index)), u = d.index + d[0].length;
140
180
  return l.push(i.slice(u)), l;
141
- }, j = (e, t) => {
142
- let n = k(e), r = "";
181
+ }, oe = (e, t) => {
182
+ let n = E(e), r = "";
143
183
  for (let t = 0; t < e.length; t++) r += e[t], t < e.length - 1 && (r += n.marker(t));
144
184
  let i = /* @__PURE__ */ new Map(), a = /<(?![!/])(?:[^>"']|"[^"]*"|'[^']*')*>/g, o;
145
185
  for (; o = a.exec(r);) {
@@ -159,7 +199,7 @@ var m = class {
159
199
  }
160
200
  }
161
201
  return i;
162
- }, M = class {
202
+ }, se = class {
163
203
  resultObj;
164
204
  opts;
165
205
  result = "";
@@ -169,58 +209,178 @@ var m = class {
169
209
  strings;
170
210
  nothingAttributeGroups;
171
211
  constructor(e, t = {}) {
172
- this.resultObj = e, this.opts = t, this.strings = A(e.strings, e.__cossackScope), this.isNodePositions = O(this.strings), this.nothingAttributeGroups = j(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);
173
213
  }
174
214
  scan() {
175
- let { values: e } = this.resultObj, t = this.strings;
176
- for (; this.stringIdx < t.length;) {
177
- let n = t[this.stringIdx].substring(this.charIdxForNext || 0), r = n.match(/(\.\.\.|[.@?]?[a-zA-Z0-9_:-]+)=["']?$/);
178
- if (this.result += n, this.charIdxForNext = 0, this.stringIdx < t.length - 1) {
179
- let n = e[this.stringIdx], a = this.nothingAttributeGroups.get(this.stringIdx);
180
- if (a) {
181
- 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++;
182
222
  continue;
183
223
  }
184
- if (r) {
185
- let e = r[0], a = r[1];
186
- this.result = this.result.substring(0, this.result.length - e.length);
187
- let o = e.endsWith("\"") ? "\"" : e.endsWith("'") ? "'" : "", c = !0;
188
- if (a === "...") {
224
+ if (r && !this.isNodePositions[this.stringIdx]) {
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 === "...") {
229
+ this.result = this.result.trimEnd();
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") {
189
246
  this.result = this.result.trimEnd();
190
- let e = { result: this.result };
191
- E(n, e), this.result = e.result;
192
- } else if (a === "ref") this.result = this.result.trimEnd();
193
- else if (a.startsWith("@")) this.result = this.result.trimEnd();
194
- else if (a.startsWith("?")) n ? (this.result = this.result.trimEnd(), this.result += ` ${a.slice(1)}`) : this.result = this.result.trimEnd();
195
- else if (a.startsWith(".")) {
196
- let e, t = n instanceof i;
197
- e = t ? f(n.component, n.fieldName) : n;
198
- let r = a.slice(1);
199
- t && T.has(r) ? (this.result = this.result.trimEnd(), e && (this.result += ` ${r}`)) : e != null && e !== !1 ? (this.result = this.result.trimEnd(), this.result += ` ${r}="${w(e, this.opts)}"`) : this.result = this.result.trimEnd();
200
- } else if (n instanceof s) n.value === void 0 ? this.result = this.result.trimEnd() : (this.result = this.result.trimEnd(), this.result += ` ${a}="${C(String(n.value))}"`);
201
- else {
202
- this.result += e;
203
- let t = w(n, this.opts);
204
- o ? (this.result += t, c = !1) : this.result += `"${t}"`;
247
+ let t = T(i, e);
248
+ t === "" ? this.result += ` ${i}` : t !== null && (this.result += ` ${i}="${t}"`);
249
+ } else {
250
+ this.result += t;
251
+ let n = C(e, this.opts);
252
+ o ? (this.result += n, s = !1) : this.result += `"${n}"`;
205
253
  }
206
- if (o && c) {
207
- let e = t[this.stringIdx + 1];
254
+ if (o && s) {
255
+ let e = n[this.stringIdx + 1];
208
256
  e && e.startsWith(o) && (this.charIdxForNext = 1);
209
257
  }
210
- } else this.opts.hydrate && this.isNodePositions[this.stringIdx] ? (this.result += `<!--CRP_${this.stringIdx}-->`, this.result += w(n, this.opts), this.result += "<!--/CRP-->") : this.result += w(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);
211
259
  }
212
260
  this.stringIdx++;
213
261
  }
214
- return this.result;
262
+ return e(this.result);
215
263
  }
216
- }, N = (e, t = {}) => new M(e, t).scan(), P = /* @__PURE__ */ new WeakMap(), F = (e) => {
217
- let t = P.get(e.strings);
218
- t || (t = /* @__PURE__ */ new Map(), P.set(e.strings, t));
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);
326
+ }
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));
219
330
  let n = `${e._$litType$}:${e.__cossackScope ?? ""}`, r = t.get(n);
220
331
  return r || (r = {}, t.set(n, r)), r;
221
- }, I = class n {
332
+ }, z = class e {
222
333
  startNode;
223
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
+ }
224
384
  componentInstance = null;
225
385
  renderListener = null;
226
386
  _childParts = [];
@@ -241,25 +401,37 @@ var m = class {
241
401
  _beginHydration() {
242
402
  this._hydrating = !0;
243
403
  }
244
- update(t) {
245
- if (this._hydrating) {
246
- this._hydrating = !1, this._hydrateValue(t);
247
- return;
248
- }
249
- t instanceof o && (t = this._resolveGuard(t));
250
- let n = !1;
251
- if (t instanceof a) {
252
- let e = t.value;
253
- if (y(e)) {
254
- 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);
255
408
  return;
256
409
  }
257
- t = e, n = !0;
258
- } else if (this._cacheMap.size > 0 || this._cacheCurrent) {
259
- for (let e of this._cacheMap.values()) for (let t of e.parts) t && typeof t.dispose == "function" && t.dispose();
260
- 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;
261
434
  }
262
- 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);
263
435
  }
264
436
  _resolveGuard(e) {
265
437
  let t = Array.isArray(e.deps) ? [...e.deps] : [e.deps];
@@ -270,95 +442,119 @@ var m = class {
270
442
  for (let n = 0; n < e.length; n++) if (e[n] !== t[n]) return !1;
271
443
  return !0;
272
444
  }
273
- _hydrateValue(t) {
274
- if (e(t)) {
275
- this._adoptComponent(t);
445
+ _hydrateValue(e) {
446
+ if (t(e)) {
447
+ this._adoptComponent(e);
276
448
  return;
277
449
  }
278
- if (y(t)) {
279
- this._adoptTemplate(t);
450
+ if (v(e)) {
451
+ this._adoptTemplate(e);
280
452
  return;
281
453
  }
282
- if (t instanceof d) {
283
- 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)));
284
456
  return;
285
457
  }
286
- if (Array.isArray(t)) {
287
- this._adoptSequence(t);
458
+ if (Array.isArray(e)) {
459
+ this._adoptSequence(e);
288
460
  return;
289
461
  }
290
- if (t instanceof c) {
291
- if (y(t.template)) {
292
- 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);
293
465
  return;
294
466
  }
295
- this.clear(), this._clearTemplateCache(), this.updateNode(t.template);
467
+ this.clear(), this._clearTemplateCache(), this.updateNode(e.template);
296
468
  return;
297
469
  }
298
- if (t instanceof o) {
299
- let e = this._resolveGuard(t);
300
- this._hydrateValue(e);
470
+ if (e instanceof s) {
471
+ let t = this._resolveGuard(e);
472
+ this._hydrateValue(t);
301
473
  return;
302
474
  }
303
- if (t instanceof a) {
304
- let e = t.value;
305
- if (y(e)) {
306
- let t = F(e);
307
- this._cacheMap.has(t) ? this.updateCache(e) : (this._adoptTemplate(e), this._cacheCurrent = t);
308
- } 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);
309
481
  return;
310
482
  }
311
- if (t === _ || t === "" || t == null || t === !1) {
483
+ if (e === _ || e === "" || e == null || e === !1) {
312
484
  this.clear();
313
485
  return;
314
486
  }
315
- if (!(t instanceof Node)) {
316
- if (S(t)) {
487
+ if (!(e instanceof Node)) {
488
+ if (x(e)) {
317
489
  if (this.startNode.nextSibling !== this.endNode) return;
318
- this.clear(), this._clearTemplateCache(), this.updateNode(t);
490
+ this.clear(), this._clearTemplateCache(), this.updateNode(e);
319
491
  return;
320
492
  }
321
- if (typeof t != "object") {
322
- let e = String(t), n = this.startNode.nextSibling;
323
- 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));
324
496
  return;
325
497
  }
326
- this.clear(), this._clearTemplateCache(), this.updateNode(t);
498
+ this.clear(), this._clearTemplateCache(), this.updateNode(e);
327
499
  }
328
500
  }
329
501
  _adoptComponent(e) {
330
- let n = e.parent || t.currentRenderingInstance;
331
- if (this.componentInstance && (this.componentInstance.constructor !== e.clazz || this.componentInstance.__parent !== n) && this.disposeComponent(), !this.componentInstance) {
332
- 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++}`);
333
- let t = !0;
334
- this.renderListener = (e) => {
335
- if (t) if (t = !1, y(e)) try {
336
- this._adoptTemplate(e);
337
- } catch (t) {
338
- if (t instanceof W) this.clear(), this._clearTemplateCache(), this.updateNode(e);
339
- 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;
340
522
  }
341
- else e == null ? this.clear() : this.updateNode(e);
342
- else this.updateNode(e);
343
- }, 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
+ }
344
531
  }
345
532
  let r = this.componentInstance;
346
- 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();
347
534
  }
348
- _adoptSequence(e, t) {
349
- let r = q(this._nodesBetween());
350
- if (r.length !== e.length) throw new W(`sequence length mismatch: SSR has ${r.length} items, client has ${e.length}`);
351
- this._childParts = [], this._partKeys = t ? [] : this._partKeys;
352
- for (let i = 0; i < e.length; i++) {
353
- let [a, o] = r[i], s = new n(a, o);
354
- 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]);
355
546
  }
356
547
  }
357
548
  _adoptTemplate(e) {
358
- let { fragment: t, parts: n } = H(e), r = this._nodesBetween(), i = /* @__PURE__ */ new Map();
359
- J(t.childNodes, r, i), Y(n, i);
360
- for (let t = 0; t < e.values.length; t++) n[t] && n[t].update(e.values[t]);
361
- this._cachedTemplateIdentity = F(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
+ }
362
558
  }
363
559
  _nodesBetween() {
364
560
  let e = [], t = this.startNode.nextSibling;
@@ -366,20 +562,36 @@ var m = class {
366
562
  return e;
367
563
  }
368
564
  teardownComponent(e) {
369
- if (this.renderListener &&= (e.removeRenderListener(this.renderListener), null), e.disconnectedCallback(), typeof e.destroy == "function") try {
370
- e.destroy();
371
- } 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
+ }
372
577
  }
373
578
  disposeComponent() {
374
579
  this.componentInstance && (this.teardownComponent(this.componentInstance), this.componentInstance = null, this._clearTemplateCache());
375
580
  }
376
581
  updateComponent(e) {
377
- let n = e.parent || t.currentRenderingInstance;
378
- 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) => {
379
- this.updateNode(e);
380
- }, 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
+ }
381
593
  let r = this.componentInstance;
382
- 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();
383
595
  }
384
596
  updateRepeat(e) {
385
597
  let { items: t, keyFn: n, templateFn: r } = e, i = /* @__PURE__ */ new Map();
@@ -388,35 +600,57 @@ var m = class {
388
600
  n !== void 0 && i.set(n, e);
389
601
  });
390
602
  let a = [], o = [], s = this.startNode;
391
- t.forEach((e, t) => {
392
- let c = n(e, t);
393
- o.push(c);
394
- let l = i.get(c);
395
- 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;
396
- }), 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
+ }
397
615
  }
398
616
  updateKey(e) {
617
+ this._draft && (!this._draft.snapshot.keySet || !Object.is(this._draft.snapshot.key, e.value)) && (this._draft = void 0);
399
618
  let t = !this._keySet || !Object.is(this._key, e.value);
400
619
  this._keySet = !0, this._key = e.value, t && (this.componentInstance && this.disposeComponent(), this._clearTemplateCache(), this.clear()), this.updateNode(e.template);
401
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
+ }
402
636
  updateCache(e) {
403
- let t = F(e);
404
- if (this._cacheCurrent === t && this._cachedParts) {
405
- 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) => {
406
640
  t.update(e.values[n]);
407
641
  });
408
642
  return;
409
643
  }
410
- (this._cacheCurrent || this._cachedParts) && this._stashCurrent();
411
- let n = this._cacheMap.get(t);
412
- 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) {
413
647
  this.clear();
414
- let r = this.startNode.parentNode;
415
- if (!r) return;
416
- for (let e of n.nodes) r.insertBefore(e, this.endNode);
417
- this._cachedTemplateIdentity = t, this._cachedParts = n.parts;
418
- for (let t = 0; t < e.values.length; t++) n.parts[t] && n.parts[t].update(e.values[t]);
419
- } 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);
420
654
  this._cacheCurrent = t;
421
655
  }
422
656
  _stashCurrent() {
@@ -436,19 +670,19 @@ var m = class {
436
670
  updateArray(e) {
437
671
  this._partKeys = [];
438
672
  let t = Math.min(e.length, this._childParts.length);
439
- 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]);
440
674
  if (e.length > this._childParts.length) {
441
675
  let n = this._childParts.length > 0 ? this._childParts[this._childParts.length - 1].endNode : this.startNode;
442
676
  for (let r = t; r < e.length; r++) {
443
677
  let t = this.createChildPart(n);
444
- 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;
445
679
  }
446
680
  }
447
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();
448
682
  }
449
- createChildPart(e) {
450
- let t = document.createComment("CRP-Item"), r = document.createComment("/CRP-Item"), i = e.nextSibling;
451
- 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);
452
686
  }
453
687
  movePart(e, t) {
454
688
  let n = t.parentNode, r = [], i = e.startNode;
@@ -461,40 +695,57 @@ var m = class {
461
695
  this._childParts.forEach((e) => e.dispose()), this._childParts = [], this._partKeys = [];
462
696
  }
463
697
  dispose() {
464
- 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) {
465
699
  for (let e of this._cacheMap.values()) for (let t of e.parts) t && typeof t.dispose == "function" && t.dispose();
466
700
  this._cacheMap.clear();
467
701
  }
468
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);
469
703
  }
470
- updateNode(t) {
471
- if (this.startNode.parentNode) if (y(t)) if (this._cachedTemplateIdentity === F(t) && this._cachedParts) this._cachedParts.forEach((e, n) => {
472
- 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]);
473
711
  });
474
712
  else {
475
713
  this._clearTemplateCache(), this.clear();
476
- let e = document.createDocumentFragment();
477
- U(t, e), this.startNode.parentNode.insertBefore(e, this.endNode);
478
- let n = V.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);
479
717
  n && (this._cachedTemplateIdentity = n.identity, this._cachedParts = n.parts);
480
718
  }
481
- else if (S(t)) {
719
+ else if (x(e)) {
482
720
  this._clearTemplateCache(), this.clear();
483
- let e = document.createElement("template");
484
- e.innerHTML = t.value, this.startNode.parentNode.insertBefore(e.content, this.endNode);
485
- } 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)) {
486
737
  this._clearTemplateCache(), this.clear();
487
- let e = document.createDocumentFragment();
488
- U(h`${t}`, e), this.startNode.parentNode.insertBefore(e, this.endNode);
489
- } else if (t instanceof Node) this._clearTemplateCache(), this.clear(), this.startNode.parentNode.insertBefore(t, this.endNode);
490
- 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();
491
742
  else {
492
- let e = String(t), n = this.startNode.nextSibling;
493
- 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);
494
745
  else {
495
746
  this._clearTemplateCache(), this.clear();
496
- let t = document.createTextNode(e);
497
- this.startNode.parentNode.insertBefore(t, this.endNode);
747
+ let e = document.createTextNode(t);
748
+ this.startNode.parentNode.insertBefore(e, this.endNode);
498
749
  }
499
750
  }
500
751
  }
@@ -509,8 +760,9 @@ var m = class {
509
760
  e.parentNode.removeChild(e), e = t;
510
761
  }
511
762
  }
512
- }, L = class {
763
+ }, B = class {
513
764
  element;
765
+ selectCommit = new H();
514
766
  previousValues = {};
515
767
  bindStates = /* @__PURE__ */ new Map();
516
768
  spreadClasses = [];
@@ -519,7 +771,7 @@ var m = class {
519
771
  }
520
772
  update(e) {
521
773
  if (e !== _ && (typeof e != "object" || !e)) return;
522
- let t = e === _ ? {} : e;
774
+ let t = e === _ ? {} : e, n = this.selectCommit.cancel();
523
775
  for (let e of Object.keys(this.previousValues)) if (!(e in t)) if (e.startsWith("@")) {
524
776
  let t = e.slice(1), n = `__crp_handler_${t}`, r = this.element[n];
525
777
  r && this.element.removeEventListener(t, r);
@@ -531,35 +783,45 @@ var m = class {
531
783
  for (let e of this.spreadClasses) this.element.classList.remove(e);
532
784
  this.spreadClasses = [];
533
785
  } else this.element.removeAttribute(e);
534
- for (let [e, n] of Object.entries(t)) if (e.startsWith("@")) {
535
- let t = e.slice(1), r = `__crp_handler_${t}`, i = this.element[r];
536
- if (i && this.element.removeEventListener(t, i), n === _ || typeof n != "function" && !(n instanceof u)) {
537
- 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;
538
790
  continue;
539
791
  }
540
792
  let a;
541
- if (n instanceof u) {
542
- let e = n.handler, t = this.element instanceof HTMLFormElement ? this.element : this.element.closest("form");
543
- 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) => {
544
796
  t.preventDefault(), e.call(t.currentTarget, t);
545
797
  };
546
- } else a = n;
547
- this.element[r] = a, this.element.addEventListener(t, a);
798
+ } else a = r;
799
+ this.element[n] = a, this.element.addEventListener(t, a);
548
800
  } else if (e.startsWith(".")) {
549
- let t = e.slice(1), r = this.bindStates.get(e);
550
- if (r && !(n instanceof i) && (this.element.removeEventListener(r.listener.__eventName, r.listener), this.bindStates.delete(e)), n === _) this.element[t] = void 0;
551
- else if (n instanceof i && (t === "value" || t === "checked")) this.applySpreadBind(e, t, n);
552
- 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")) {
553
808
  let e = this.element;
554
809
  if (t === "checked") {
555
- let t = !!n.value;
810
+ let t = !!r.value;
556
811
  e.checked !== t && (e.checked = t);
557
812
  } else {
558
- let t = n.value == null ? "" : String(n.value);
813
+ let t = r.value == null ? "" : String(r.value);
559
814
  e.value !== t && (e.value = t);
560
815
  }
561
- } else n instanceof i ? this.element[t] = f(n.component, n.fieldName) : n instanceof l ? this.element[t] = n.value : this.element[t] = n;
562
- } else e.startsWith("?") ? n !== _ && n ? this.element.setAttribute(e.slice(1), "") : this.element.removeAttribute(e.slice(1)) : n === _ ? e === "class" || e === "className" ? this.applySpreadClass("") : this.element.removeAttribute(e) : n instanceof s ? n.value === void 0 ? this.element.removeAttribute(e) : this.element.setAttribute(e, String(n.value)) : typeof n == "boolean" ? n ? this.element.setAttribute(e, "") : this.element.removeAttribute(e) : (e === "class" || e === "className") && typeof n == "string" ? this.applySpreadClass(n) : n != null && this.element.setAttribute(e, String(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);
823
+ t === null ? this.element.removeAttribute(e) : this.element.setAttribute(e, t);
824
+ } else (e === "class" || e === "className") && typeof r == "string" ? this.applySpreadClass(r) : this.element.setAttribute(e, String(r));
563
825
  this.previousValues = { ...t };
564
826
  }
565
827
  applySpreadClass(e) {
@@ -569,7 +831,7 @@ var m = class {
569
831
  this.spreadClasses = t;
570
832
  }
571
833
  applySpreadBind(e, t, n) {
572
- 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);
573
835
  if (t === "checked") {
574
836
  let e = !!a;
575
837
  (!o || o.lastKind !== "checked" || o.lastValue !== e) && (r.checked = e);
@@ -577,12 +839,12 @@ var m = class {
577
839
  let e = a == null ? "" : String(a);
578
840
  (!o || o.lastKind !== "value" || String(o.lastValue) !== e || this.element instanceof HTMLSelectElement && r.value !== e) && (r.value = e);
579
841
  }
580
- let s = B(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);
581
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);
582
844
  else {
583
845
  let r = (e) => {
584
846
  let r = e.currentTarget, a = t === "checked" ? !!r[t] : r[t];
585
- i && p(i, n.fieldName, a);
847
+ i && m(i, n.fieldName, a);
586
848
  };
587
849
  r.__eventName = s, this.element.addEventListener(s, r), o = {
588
850
  listener: r,
@@ -590,12 +852,13 @@ var m = class {
590
852
  lastValue: t === "checked" ? !!a : a == null ? "" : String(a),
591
853
  boundComponent: i,
592
854
  boundField: n.fieldName
593
- }, this.bindStates.set(e, o), t === "value" && z(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));
594
856
  }
595
857
  }
596
- }, R = class {
858
+ }, V = class {
597
859
  element;
598
860
  name;
861
+ selectCommit = new H();
599
862
  segments;
600
863
  isMulti;
601
864
  multiState;
@@ -622,6 +885,7 @@ var m = class {
622
885
  this.segments.push(n.substring(a)), this.isMulti = i.length > 1, this.multiState = r ?? null;
623
886
  }
624
887
  update(e) {
888
+ let t = this.selectCommit.cancel();
625
889
  if (e === _) {
626
890
  if (this.bindListener && this._detachBindListener(), this.isMulti && this.multiState) {
627
891
  this.multiState.currentValues[this.multiState.position] = _, this.element.removeAttribute(this.name);
@@ -634,10 +898,10 @@ var m = class {
634
898
  } else this.name.startsWith("?") && this.element.removeAttribute(this.name.slice(1)), this.element.removeAttribute(this.name);
635
899
  return;
636
900
  }
637
- let t = !1;
638
- e instanceof l && (t = !0, e = e.value);
639
901
  let n = !1;
640
- 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) {
641
905
  this.updateBind(e);
642
906
  return;
643
907
  }
@@ -666,7 +930,7 @@ var m = class {
666
930
  this.element[r] = e, this.element.addEventListener(t, e);
667
931
  }
668
932
  let i = e;
669
- if (i instanceof u) {
933
+ if (i instanceof d) {
670
934
  let e = i.handler, t = this.element instanceof HTMLFormElement ? this.element : this.element.closest("form");
671
935
  t instanceof HTMLFormElement && (i.novalidate ? t.setAttribute("novalidate", "") : t.removeAttribute("novalidate")), i = (t) => {
672
936
  t.preventDefault(), e.call(t.currentTarget, t);
@@ -674,28 +938,33 @@ var m = class {
674
938
  }
675
939
  this.element[n] = typeof i == "function" ? i : void 0, this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
676
940
  } else if (this.name.startsWith(".")) {
677
- let n = this.name.slice(1);
678
- if ((n === "value" || n === "checked") && (this.element instanceof HTMLInputElement || this.element instanceof HTMLTextAreaElement || this.element instanceof HTMLSelectElement)) if (n === "checked") {
679
- let n = !!e;
680
- (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");
681
945
  } else {
682
- let n = String(e);
683
- (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);
684
948
  }
685
- else this.element[n] = e;
949
+ else this.element[r] = e;
686
950
  this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
687
951
  } else if (this.name.startsWith("?")) {
688
952
  let t = this.name.slice(1);
689
953
  e ? this.element.setAttribute(t, "") : this.element.removeAttribute(t), this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
690
- } else n ? e === void 0 ? this.element.removeAttribute(this.name) : this.element.setAttribute(this.name, this.segments[0] + String(e) + this.segments[1]) : typeof e == "boolean" ? e ? this.element.setAttribute(this.name, "") : 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]);
955
+ else if (e == null) this.element.removeAttribute(this.name);
956
+ else if (typeof e == "boolean") {
957
+ let t = T(this.name, e);
958
+ t === null ? this.element.removeAttribute(this.name) : this.element.setAttribute(this.name, t);
959
+ } else this.element.setAttribute(this.name, this.segments[0] + String(e) + this.segments[1]);
691
960
  }
692
961
  updateBind(e) {
693
962
  let t = this.name.startsWith(".") ? this.name.slice(1) : this.name;
694
963
  if (t !== "value" && t !== "checked") {
695
- 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();
696
965
  return;
697
966
  }
698
- 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);
699
968
  if (t === "checked") {
700
969
  let e = !!i;
701
970
  (this.lastFormKind !== "checked" || this.lastFormValue !== e) && (n.checked = e, this.lastFormValue = e, this.lastFormKind = "checked");
@@ -703,26 +972,38 @@ var m = class {
703
972
  let e = i == null ? "" : String(i);
704
973
  (this.lastFormKind !== "value" || String(this.lastFormValue) !== e || this.element instanceof HTMLSelectElement && n.value !== e) && (n.value = e, this.lastFormValue = e, this.lastFormKind = "value");
705
974
  }
706
- let a = B(this.element, t);
975
+ let a = W(this.element, t);
707
976
  if (this.bindListener && (this.boundPropName !== t || this.boundComponent !== r || this.boundFieldName !== e.fieldName || this.bindListener.__eventName !== a) && this._detachBindListener(), !this.bindListener) {
708
977
  let n = (n) => {
709
978
  let i = n.currentTarget, a = t === "checked" ? !!i[t] : i[t];
710
- r && p(r, e.fieldName, a);
979
+ r && m(r, e.fieldName, a);
711
980
  };
712
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") {
713
982
  let e = n;
714
- z(this.element, () => this.bindListener === e, () => f(this.boundComponent, this.boundFieldName));
983
+ U(this.element, () => this.bindListener === e, () => p(this.boundComponent, this.boundFieldName));
715
984
  }
716
985
  }
717
986
  this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
718
987
  }
719
- }, z = (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) => {
720
1001
  e instanceof HTMLSelectElement && queueMicrotask(() => {
721
1002
  if (!t()) return;
722
1003
  let r = n(), i = r == null ? "" : String(r);
723
1004
  e.value !== i && (e.value = i);
724
1005
  });
725
- }, B = (e, t) => {
1006
+ }, W = (e, t) => {
726
1007
  if (e instanceof HTMLSelectElement) return "change";
727
1008
  if (e instanceof HTMLTextAreaElement) return "input";
728
1009
  if (e instanceof HTMLInputElement) {
@@ -730,11 +1011,11 @@ var m = class {
730
1011
  return t === "checked" || n === "checkbox" || n === "radio" || n === "range" ? "change" : "input";
731
1012
  }
732
1013
  return "input";
733
- }, V = /* @__PURE__ */ new WeakMap(), H = (e) => {
734
- let t = [], n = A(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_:-]+)=["']?$/;
735
1016
  for (let e = 0; e < n.length - 1; e++) {
736
1017
  let t = n[e];
737
- for (let e = 0; e < t.length; e++) a ? t[e] === a && (a = null) : 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);
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);
738
1019
  r += t;
739
1020
  let s = t.match(o);
740
1021
  if (i && (s || a)) {
@@ -762,7 +1043,7 @@ var m = class {
762
1043
  if (n) {
763
1044
  let r = parseInt(n[1]), i = document.createComment("/CRP");
764
1045
  e.parentNode.insertBefore(i, e.nextSibling);
765
- let a = new I(e, i);
1046
+ let a = new z(e, i);
766
1047
  t[r] = a;
767
1048
  }
768
1049
  } else if (e.nodeType === Node.ELEMENT_NODE) {
@@ -773,10 +1054,10 @@ var m = class {
773
1054
  if (r.length === 1) {
774
1055
  let i = parseInt(r[0][1]);
775
1056
  if (!t[i]) if (e.name === "...") {
776
- let e = new L(n);
1057
+ let e = new B(n);
777
1058
  t[i] = e, n.removeAttribute("...");
778
1059
  } else {
779
- let r = new R(n, e.name, e.value);
1060
+ let r = new V(n, e.name, e.value);
780
1061
  t[i] = r;
781
1062
  }
782
1063
  return;
@@ -786,7 +1067,7 @@ var m = class {
786
1067
  for (let r = 0; r < i.length; r++) {
787
1068
  let o = i[r];
788
1069
  if (!t[o]) {
789
- let i = new R(n, e.name, e.value, {
1070
+ let i = new V(n, e.name, e.value, {
790
1071
  currentValues: a,
791
1072
  position: r
792
1073
  });
@@ -799,31 +1080,70 @@ var m = class {
799
1080
  fragment: l,
800
1081
  parts: t
801
1082
  };
802
- }, U = (e, t) => {
803
- let n = V.get(t), r = F(e);
804
- if (n && n.identity === r) {
805
- 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) => {
806
1089
  t.update(e.values[n]);
807
1090
  });
808
1091
  return;
809
1092
  }
810
- if (n) for (let e of n.parts) e && typeof e.dispose == "function" && e.dispose();
811
- let { fragment: i, parts: a } = H(e), o = e.values;
812
- for (let e = 0; e < o.length; e++) a[e] && a[e].update(o[e]);
813
- if (typeof HTMLElement < "u" && t instanceof HTMLElement || typeof ShadowRoot < "u" && t instanceof ShadowRoot) t.innerHTML = "";
814
- else for (; t.firstChild;) t.removeChild(t.firstChild);
815
- t.appendChild(i), V.set(t, {
816
- identity: r,
817
- 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
818
1138
  });
819
- }, W = class extends Error {}, G = (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>", K = (e) => {
1139
+ }, Z = (e) => {
820
1140
  if (e.nodeType === Node.TEXT_NODE) return (e.nodeValue || "").trim() === "";
821
1141
  if (e.nodeType === Node.COMMENT_NODE) {
822
1142
  let t = e.nodeValue || "";
823
1143
  return !/^CRP_\d+$/.test(t) && t !== "/CRP" && t !== "CSA-S" && t !== "CSA-E";
824
1144
  }
825
1145
  return !1;
826
- }, q = (e) => {
1146
+ }, le = (e) => {
827
1147
  let t = [], n = [];
828
1148
  for (let r of e) {
829
1149
  if (r.nodeType !== Node.COMMENT_NODE) continue;
@@ -835,70 +1155,85 @@ var m = class {
835
1155
  }
836
1156
  }
837
1157
  return t;
838
- }, J = (e, t, n) => {
839
- let r = 0;
840
- for (let i = 0; i < e.length; i++) {
841
- let a = e[i];
842
- if (K(a)) continue;
843
- for (; r < t.length && K(t[r]);) r++;
844
- let o = t[r];
845
- if (!o) throw new W(`existing shorter than blueprint (expected ${G(a)})`);
846
- if (a.nodeType === Node.ELEMENT_NODE) {
847
- if (o.nodeType !== Node.ELEMENT_NODE || a.nodeName !== o.nodeName || a.namespaceURI !== o.namespaceURI) throw new W(`tag mismatch: blueprint ${G(a)} vs existing ${G(o)}`);
848
- n.set(a, o), J(a.childNodes, Array.from(o.childNodes), n);
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);
849
1181
  } else {
850
- if (a.nodeType !== o.nodeType) throw new W(`type mismatch: blueprint ${G(a)} vs existing ${G(o)}`);
851
- n.set(a, o);
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);
852
1184
  }
853
- if (r++, a.nodeType === Node.COMMENT_NODE && /^CRP_\d+$/.test(a.nodeValue || "")) {
1185
+ if (a++, s.nodeType === Node.COMMENT_NODE && /^CRP_\d+$/.test(s.nodeValue || "")) {
854
1186
  let e = 1;
855
- for (; r < t.length && e > 0;) {
856
- let n = t[r];
1187
+ for (; a < t.length && e > 0;) {
1188
+ let n = t[a];
857
1189
  if (n.nodeType === Node.COMMENT_NODE) {
858
1190
  if (/^CRP_\d+$/.test(n.nodeValue || "")) e++;
859
1191
  else if (n.nodeValue === "/CRP" && (e--, e === 0)) break;
860
1192
  }
861
- r++;
1193
+ a++;
862
1194
  }
863
1195
  }
864
1196
  }
865
- for (; r < t.length && K(t[r]);) r++;
866
- if (r < t.length) throw new W(`existing longer than blueprint (unexpected ${G(t[r])})`);
867
- }, Y = (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) => {
868
1200
  for (let n of e) if (n) {
869
- if (n instanceof I) {
1201
+ if (n instanceof z) {
870
1202
  let e = t.get(n.startNode), r = t.get(n.endNode);
871
- if (!e || !r) throw new W("node part anchors not found");
1203
+ if (!e || !r) throw new J("missing-part-anchor", "part", "start and end anchors", "missing anchor");
872
1204
  n.startNode = e, n.endNode = r, n._beginHydration();
873
- } else if (n instanceof R || n instanceof L) {
1205
+ } else if (n instanceof V || n instanceof B) {
874
1206
  let e = t.get(n.element);
875
- if (!e) throw new W("attribute part element not found");
1207
+ if (!e) throw new J("missing-part-element", "part", "attribute element", "missing element");
876
1208
  n.element = e;
877
1209
  }
878
1210
  }
879
- }, X = (e, t) => {
880
- if (!t.hasChildNodes()) return U(e, t);
881
- let n = V.get(t), r = F(e);
882
- if (n && n.identity === r) {
883
- 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]));
884
1216
  return;
885
1217
  }
886
- if (n) for (let e of n.parts) e && typeof e.dispose == "function" && e.dispose();
887
- 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 = [];
888
1223
  try {
889
- let { fragment: n, parts: r } = H(e);
890
- i = r;
891
- let a = /* @__PURE__ */ new Map();
892
- J(n.childNodes, Array.from(t.childNodes), a), Y(i, a);
893
- for (let t = 0; t < e.values.length; t++) i[t] && i[t].update(e.values[t]);
894
- } catch (n) {
895
- if (n instanceof W) return console.warn("[cossack] hydration mismatch, falling back to render:", n.message), U(e, t);
896
- throw n;
897
- }
898
- V.set(t, {
899
- identity: r,
900
- 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
901
1236
  });
902
1237
  };
903
1238
  //#endregion
904
- export { m as TemplateResult, b as UnsafeHTMLResult, v as component, C as escapeHtml, h as html, X as hydrate, y as isTemplateResult, S as isUnsafeHTML, _ as nothing, U as render, N as renderToString, g as svg, x 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 };