@cossackframework/renderer 0.5.2 → 0.6.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.
@@ -0,0 +1 @@
1
+ export {};
@@ -90,7 +90,7 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
90
90
  }
91
91
  mount(e) {
92
92
  this.addRenderListener((t) => {
93
- t && (h(t) ? O(t, e) : O(f`${t}`, e));
93
+ t && (g(t) ? k(t, e) : k(m`${t}`, e));
94
94
  }), this.requestUpdate();
95
95
  }
96
96
  addEventListener(e, t, n) {
@@ -133,37 +133,44 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
133
133
  constructor(e, t, n) {
134
134
  this.items = e, this.keyFn = t, this.templateFn = n;
135
135
  }
136
- }, s = (e) => new a(e), c = (e, t, n) => n === void 0 ? new o(e, (e, t) => t, t) : new o(e, t, n), l = (e) => Object.keys(e).filter((t) => e[t]).join(" "), u = (e) => Object.keys(e).filter((t) => e[t] != null).map((t) => `${t}:${e[t]}`).join(";"), d = class {
136
+ }, s = class {
137
+ value;
138
+ template;
139
+ constructor(e, t) {
140
+ this.value = e, this.template = t;
141
+ }
142
+ }, c = (e) => new a(e), l = (e, t, n) => n === void 0 ? new o(e, (e, t) => t, t) : new o(e, t, n), u = (e) => Object.keys(e).filter((t) => e[t]).join(" "), d = (e) => Object.keys(e).filter((t) => e[t] != null).map((t) => `${t}:${e[t]}`).join(";"), f = (e, t) => new s(e, t), p = class {
137
143
  strings;
138
144
  values;
139
145
  _cossack_template_result = !0;
140
146
  constructor(e, t) {
141
147
  this.strings = e, this.values = t;
142
148
  }
143
- }, f = (e, ...t) => new d(e, t), p = f, m = (e, t = {}, n) => f`${{
149
+ }, m = (e, ...t) => new p(e, t), h = (e, t = {}, n) => m`${{
144
150
  _type: "COMPONENT",
145
151
  clazz: e,
146
152
  props: t,
147
153
  children: n
148
- }}`, h = (e) => typeof e == "object" && !!e && e._cossack_template_result === !0, g = class {
154
+ }}`, g = (e) => typeof e == "object" && !!e && e._cossack_template_result === !0, _ = class {
149
155
  value;
150
156
  constructor(e) {
151
157
  this.value = e;
152
158
  }
153
- }, _ = (e) => new g(e), v = (e) => e instanceof g, y = (e) => String(e).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;"), b = (t) => {
159
+ }, v = (e) => new _(e), y = (e) => e instanceof _, b = (e) => String(e).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;"), x = (t) => {
154
160
  if (t == null || t === !1) return "";
155
- if (Array.isArray(t)) return t.map(b).join("");
156
- if (h(t)) return C(t);
157
- if (t instanceof a) return b(t.value);
158
- if (t instanceof o) return t.items.map((e, n) => b(t.templateFn(e, n))).join("");
161
+ if (Array.isArray(t)) return t.map(x).join("");
162
+ if (g(t)) return w(t);
163
+ if (t instanceof a) return x(t.value);
164
+ if (t instanceof o) return t.items.map((e, n) => x(t.templateFn(e, n))).join("");
165
+ if (t instanceof s) return x(t.template);
159
166
  if (e(t)) {
160
167
  let e = new t.clazz();
161
168
  Object.assign(e, t.props), "props" in e && (e.props = t.props), e.children = t.children, e.__parent = i.currentRenderingInstance, e.__parent && (e._id = `${e.__parent._id}:${e.__parent._childCounter++}`), e.connectedCallback(), n(e), e.willUpdate(/* @__PURE__ */ new Map());
162
169
  let a = e.render(), o = "";
163
- return a && (o = h(a) ? C(a) : C(f`${a}`)), r(), o;
170
+ return a && (o = g(a) ? w(a) : w(m`${a}`)), r(), o;
164
171
  }
165
- return v(t) ? t.value : typeof t == "function" || typeof t == "object" && t && "value" in t ? "" : y(t);
166
- }, x = (e) => {
172
+ return y(t) ? t.value : typeof t == "function" || typeof t == "object" && t && "value" in t ? "" : b(t);
173
+ }, S = (e) => {
167
174
  let t = "";
168
175
  if (typeof e == "object" && e) for (let [n, r] of Object.entries(e)) {
169
176
  if (n.startsWith("@")) continue;
@@ -172,10 +179,10 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
172
179
  r && (t += ` ${n.slice(1)}`);
173
180
  continue;
174
181
  }
175
- n.startsWith(".") && (e = n.slice(1)), typeof i == "boolean" ? i && (t += ` ${e}`) : typeof i == "function" || i != null && (t += ` ${e}="${y(i)}"`);
182
+ n.startsWith(".") && (e = n.slice(1)), typeof i == "boolean" ? i && (t += ` ${e}`) : typeof i == "function" || i != null && (t += ` ${e}="${b(i)}"`);
176
183
  }
177
184
  return t;
178
- }, S = class {
185
+ }, C = class {
179
186
  resultObj;
180
187
  result = "";
181
188
  stringIdx = 0;
@@ -193,17 +200,17 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
193
200
  let t = r[0], i = r[1];
194
201
  this.result = this.result.substring(0, this.result.length - t.length);
195
202
  let a = t.endsWith("\"") ? "\"" : t.endsWith("'") ? "'" : "", o = !0;
196
- if (i === "..." ? (this.result = this.result.trimEnd(), this.result += x(n)) : i.startsWith("@") ? this.result = this.result.trimEnd() : i.startsWith("?") ? n ? (this.result = this.result.trimEnd(), this.result += ` ${i.slice(1)}`) : this.result = this.result.trimEnd() : i.startsWith(".") ? n != null && n !== !1 ? (this.result = this.result.trimEnd(), this.result += ` ${i.slice(1)}="${b(n)}"`) : this.result = this.result.trimEnd() : (this.result += t, this.result += b(n), o = !1), a && o) {
203
+ if (i === "..." ? (this.result = this.result.trimEnd(), this.result += S(n)) : i.startsWith("@") ? this.result = this.result.trimEnd() : i.startsWith("?") ? n ? (this.result = this.result.trimEnd(), this.result += ` ${i.slice(1)}`) : this.result = this.result.trimEnd() : i.startsWith(".") ? n != null && n !== !1 ? (this.result = this.result.trimEnd(), this.result += ` ${i.slice(1)}="${x(n)}"`) : this.result = this.result.trimEnd() : (this.result += t, this.result += x(n), o = !1), a && o) {
197
204
  let t = e[this.stringIdx + 1];
198
205
  t && t.startsWith(a) && (this.charIdxForNext = 1);
199
206
  }
200
- } else this.result += b(n);
207
+ } else this.result += x(n);
201
208
  }
202
209
  this.stringIdx++;
203
210
  }
204
211
  return this.result;
205
212
  }
206
- }, C = (e) => new S(e).scan(), w = class t {
213
+ }, w = (e) => new C(e).scan(), T = class t {
207
214
  startNode;
208
215
  endNode;
209
216
  componentInstance = null;
@@ -212,14 +219,21 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
212
219
  _partKeys = [];
213
220
  _cachedTemplateStrings = null;
214
221
  _cachedParts = null;
222
+ _key = void 0;
223
+ _keySet = !1;
215
224
  constructor(e, t) {
216
225
  this.startNode = e, this.endNode = t;
217
226
  }
218
227
  update(t) {
219
- this.componentInstance && (!e(t) || t.clazz !== this.componentInstance.constructor) && this.disposeComponent(), !(t instanceof o) && !Array.isArray(t) && this._childParts.length > 0 && this.clearChildParts(), e(t) ? this.updateComponent(t) : t instanceof o ? this.updateRepeat(t) : Array.isArray(t) ? this.updateArray(t) : this.updateNode(t);
228
+ this.componentInstance && (!e(t) || t.clazz !== this.componentInstance.constructor) && this.disposeComponent(), !(t instanceof o) && !Array.isArray(t) && this._childParts.length > 0 && this.clearChildParts(), e(t) ? this.updateComponent(t) : t instanceof o ? this.updateRepeat(t) : t instanceof s ? this.updateKey(t) : Array.isArray(t) ? this.updateArray(t) : this.updateNode(t);
229
+ }
230
+ teardownComponent(e) {
231
+ if (this.renderListener &&= (e.removeRenderListener(this.renderListener), null), e.disconnectedCallback(), typeof e.destroy == "function") try {
232
+ e.destroy();
233
+ } catch {}
220
234
  }
221
235
  disposeComponent() {
222
- this.componentInstance && (this.renderListener && this.componentInstance.removeRenderListener(this.renderListener), this.componentInstance.disconnectedCallback(), this.componentInstance = null, this.renderListener = null, this._clearTemplateCache());
236
+ this.componentInstance && (this.teardownComponent(this.componentInstance), this.componentInstance = null, this._clearTemplateCache());
223
237
  }
224
238
  updateComponent(e) {
225
239
  this.componentInstance || (this.componentInstance = new e.clazz(), this.componentInstance.__parent = i.currentRenderingInstance, this.componentInstance.__parent && (this.componentInstance._id = `${this.componentInstance.__parent._id}:${this.componentInstance.__parent._childCounter++}`), this.renderListener = (e) => {
@@ -242,6 +256,10 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
242
256
  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;
243
257
  }), i.forEach((e) => e.dispose()), this._childParts = a, this._partKeys = o;
244
258
  }
259
+ updateKey(e) {
260
+ let t = !this._keySet || !Object.is(this._key, e.value);
261
+ this._keySet = !0, this._key = e.value, t && (this.componentInstance && this.disposeComponent(), this._clearTemplateCache(), this.clear()), this.updateNode(e.template);
262
+ }
245
263
  updateArray(e) {
246
264
  this._partKeys = [];
247
265
  let t = Math.min(e.length, this._childParts.length);
@@ -270,27 +288,27 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
270
288
  this._childParts.forEach((e) => e.dispose()), this._childParts = [], this._partKeys = [];
271
289
  }
272
290
  dispose() {
273
- this.clear(), this._clearTemplateCache(), this.clearChildParts(), this.startNode.parentNode && this.startNode.parentNode.removeChild(this.startNode), this.endNode.parentNode && this.endNode.parentNode.removeChild(this.endNode), this.componentInstance && (this.renderListener && this.componentInstance.removeRenderListener(this.renderListener), this.componentInstance.disconnectedCallback());
291
+ this.clear(), this._clearTemplateCache(), 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);
274
292
  }
275
293
  updateNode(t) {
276
- if (this.startNode.parentNode) if (h(t)) if (this._cachedTemplateStrings === t.strings && this._cachedParts) this._cachedParts.forEach((e, n) => {
294
+ if (this.startNode.parentNode) if (g(t)) if (this._cachedTemplateStrings === t.strings && this._cachedParts) this._cachedParts.forEach((e, n) => {
277
295
  e.update(t.values[n]);
278
296
  });
279
297
  else {
280
298
  this._clearTemplateCache(), this.clear();
281
299
  let e = document.createDocumentFragment();
282
- O(t, e), this.startNode.parentNode.insertBefore(e, this.endNode);
283
- let n = D.get(e.firstChild?.childNodes[0] || e);
300
+ k(t, e), this.startNode.parentNode.insertBefore(e, this.endNode);
301
+ let n = O.get(e.firstChild?.childNodes[0] || e);
284
302
  n && (this._cachedTemplateStrings = n.strings, this._cachedParts = n.parts);
285
303
  }
286
- else if (v(t)) {
304
+ else if (y(t)) {
287
305
  this._clearTemplateCache(), this.clear();
288
306
  let e = document.createElement("template");
289
307
  e.innerHTML = t.value, this.startNode.parentNode.insertBefore(e.content, this.endNode);
290
308
  } else if (e(t) || typeof t == "object" && t && !(t instanceof Node) && !Array.isArray(t)) {
291
309
  this._clearTemplateCache(), this.clear();
292
310
  let e = document.createDocumentFragment();
293
- O(f`${t}`, e), this.startNode.parentNode.insertBefore(e, this.endNode);
311
+ k(m`${t}`, e), this.startNode.parentNode.insertBefore(e, this.endNode);
294
312
  } else if (t instanceof Node) this._clearTemplateCache(), this.clear(), this.startNode.parentNode.insertBefore(t, this.endNode);
295
313
  else if (t == null || t === !1) this._clearTemplateCache(), this.clear();
296
314
  else {
@@ -304,6 +322,7 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
304
322
  }
305
323
  }
306
324
  _clearTemplateCache() {
325
+ if (this._cachedParts) for (let e of this._cachedParts) e && typeof e.dispose == "function" && e.dispose();
307
326
  this._cachedTemplateStrings = null, this._cachedParts = null;
308
327
  }
309
328
  clear() {
@@ -313,7 +332,7 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
313
332
  e.parentNode.removeChild(e), e = t;
314
333
  }
315
334
  }
316
- }, T = class {
335
+ }, E = class {
317
336
  element;
318
337
  previousValues = {};
319
338
  constructor(e) {
@@ -332,7 +351,7 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
332
351
  } else e.startsWith(".") ? this.element[e.slice(1)] = n : e.startsWith("?") ? n ? this.element.setAttribute(e.slice(1), "") : this.element.removeAttribute(e.slice(1)) : typeof n == "boolean" ? n ? this.element.setAttribute(e, "") : this.element.removeAttribute(e) : n != null && this.element.setAttribute(e, String(n));
333
352
  this.previousValues = { ...t };
334
353
  }
335
- }, E = class {
354
+ }, D = class {
336
355
  element;
337
356
  name;
338
357
  segments;
@@ -377,14 +396,15 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
377
396
  this.element.checked !== t && (this.element.checked = t), t ? this.element.setAttribute(this.name, "") : this.element.removeAttribute(this.name);
378
397
  } else 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]);
379
398
  }
380
- }, D = /* @__PURE__ */ new WeakMap(), O = (e, t) => {
381
- let n = D.get(t);
399
+ }, O = /* @__PURE__ */ new WeakMap(), k = (e, t) => {
400
+ let n = O.get(t);
382
401
  if (n && n.strings === e.strings) {
383
402
  n.parts.forEach((t, n) => {
384
403
  t.update(e.values[n]);
385
404
  });
386
405
  return;
387
406
  }
407
+ if (n) for (let e of n.parts) e && typeof e.dispose == "function" && e.dispose();
388
408
  let r = [], i = e.values, a = "", o = !1, s = null, c = /(\.\.\.|[.@?]?[a-zA-Z0-9_-]+)=["']?$/;
389
409
  for (let t = 0; t < e.strings.length - 1; t++) {
390
410
  let n = e.strings[t];
@@ -410,7 +430,7 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
410
430
  let t = e.nodeValue?.match(/CRP_(\d+)/);
411
431
  if (t) {
412
432
  let n = parseInt(t[1]), i = document.createComment("/CRP");
413
- e.parentNode.insertBefore(i, e.nextSibling), r[n] = new w(e, i);
433
+ e.parentNode.insertBefore(i, e.nextSibling), r[n] = new T(e, i);
414
434
  }
415
435
  } else if (e.nodeType === Node.ELEMENT_NODE) {
416
436
  let t = e;
@@ -419,14 +439,14 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
419
439
  if (n.length === 0) return;
420
440
  if (n.length === 1) {
421
441
  let i = parseInt(n[0][1]);
422
- r[i] || (e.name === "..." ? (r[i] = new T(t), t.removeAttribute("...")) : r[i] = new E(t, e.name, e.value));
442
+ r[i] || (e.name === "..." ? (r[i] = new E(t), t.removeAttribute("...")) : r[i] = new D(t, e.name, e.value));
423
443
  return;
424
444
  }
425
445
  if (e.name === "...") return;
426
446
  let i = n.map((e) => parseInt(e[1])), a = Array(n.length).fill(void 0);
427
447
  for (let n = 0; n < i.length; n++) {
428
448
  let o = i[n];
429
- r[o] || (r[o] = new E(t, e.name, e.value, {
449
+ r[o] || (r[o] = new D(t, e.name, e.value, {
430
450
  currentValues: a,
431
451
  position: n
432
452
  }));
@@ -440,10 +460,10 @@ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n
440
460
  for (; t.firstChild;) t.removeChild(t.firstChild);
441
461
  t.appendChild(d);
442
462
  }
443
- D.set(t, {
463
+ O.set(t, {
444
464
  strings: e.strings,
445
465
  parts: r
446
466
  });
447
467
  };
448
468
  //#endregion
449
- export { e as S, u as _, f as a, r as b, O as c, _ as d, a as f, c as g, s as h, y as i, C as l, l as m, g as n, h as o, o as p, m as r, v as s, d as t, p as u, i as v, n as x, t as y };
469
+ export { e as C, n as S, l as _, m as a, t as b, k as c, s as d, a as f, c as g, f as h, b as i, w as l, u as m, _ as n, g as o, o as p, h as r, y as s, p as t, v as u, d as v, r as x, i as y };
@@ -7,7 +7,6 @@ export declare class TemplateResult {
7
7
  constructor(strings: TemplateStringsArray, values: unknown[]);
8
8
  }
9
9
  export declare const html: (strings: TemplateStringsArray, ...values: unknown[]) => TemplateResult;
10
- export declare const svg: (strings: TemplateStringsArray, ...values: unknown[]) => TemplateResult;
11
10
  export declare const component: <T extends CossackElement>(clazz: new () => T, props?: Record<string, unknown>, children?: unknown) => TemplateResult;
12
11
  export declare const isTemplateResult: (value: unknown) => value is TemplateResult;
13
12
  export declare class UnsafeHTMLResult {
@@ -8,6 +8,11 @@ export declare class RepeatResult {
8
8
  readonly templateFn: (item: any, index: number) => unknown;
9
9
  constructor(items: unknown[], keyFn: (item: any, index: number) => unknown, templateFn: (item: any, index: number) => unknown);
10
10
  }
11
+ export declare class KeyResult {
12
+ readonly value: unknown;
13
+ readonly template: unknown;
14
+ constructor(value: unknown, template: unknown);
15
+ }
11
16
  /**
12
17
  * Checks against the DOM value instead of the previous rendered value.
13
18
  * Usage: <input .value=${live(x)}>
@@ -28,4 +33,10 @@ export declare const classMap: (classInfo: Record<string, string | boolean | num
28
33
  * Usage: style="${styleMap({ color: 'red' })}"
29
34
  */
30
35
  export declare const styleMap: (styleInfo: Record<string, string | undefined | null>) => string;
36
+ /**
37
+ * Forces its subtree to be recreated when `value` changes.
38
+ * Useful for re-triggering CSS animations and forcing remounts.
39
+ * Usage: html`${key(id, html`<child></child>`)}`
40
+ */
41
+ export declare const key: (value: unknown, template: unknown) => KeyResult;
31
42
  export type RefCallback = (el: Element | undefined) => void;
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import { S as e, _ as t, a as n, b as r, c as i, d as a, f as o, g as s, h as c, i as l, l as u, m as d, n as f, o as p, p as m, r as h, s as g, t as _, u as v, v as y, x as b, y as x } from "./cossack-html-BUi5cuB1.js";
1
+ import { C as e, S as t, _ as n, a as r, b as i, c as a, d as o, f as s, g as c, h as l, i as u, l as d, m as f, n as p, o as m, p as h, r as g, s as _, t as v, u as y, v as b, x, y as S } from "./cossack-html-QsGiJk7a.js";
2
2
  //#region src/context.ts
3
- function S(e) {
3
+ function C(e) {
4
4
  return {
5
5
  id: Math.random().toString(36).slice(2),
6
6
  defaultValue: e
7
7
  };
8
8
  }
9
9
  //#endregion
10
- export { y as CossackElement, o as LiveResult, m as RepeatResult, _ as TemplateResult, f as UnsafeHTMLResult, d as classMap, h as component, S as createContext, l as escapeHtml, n as html, x as instanceStack, e as isComponentResult, p as isTemplateResult, g as isUnsafeHTML, c as live, r as popCurrentInstance, b as pushCurrentInstance, i as render, u as renderToString, s as repeat, t as styleMap, v as svg, a as unsafeHTML };
10
+ export { S as CossackElement, o as KeyResult, s as LiveResult, h as RepeatResult, v as TemplateResult, p as UnsafeHTMLResult, f as classMap, g as component, C as createContext, u as escapeHtml, r as html, i as instanceStack, e as isComponentResult, m as isTemplateResult, _ as isUnsafeHTML, l as key, c as live, x as popCurrentInstance, t as pushCurrentInstance, a as render, d as renderToString, n as repeat, b as styleMap, y as unsafeHTML };
package/dist/server.js CHANGED
@@ -1,4 +1,4 @@
1
- import { i as e, l as t } from "./cossack-html-BUi5cuB1.js";
1
+ import { i as e, l as t } from "./cossack-html-QsGiJk7a.js";
2
2
  //#region src/minify-html.ts
3
3
  var n = /<(script|style|pre|textarea)\b[^>]*>[\s\S]*?<\/\1>/gi, r = RegExp(`<(${[.../* @__PURE__ */ new Set([
4
4
  "area",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cossackframework/renderer",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "description": "Light DOM rendering engine for the Cossack Framework",
6
6
  "license": "MIT",
@@ -28,8 +28,6 @@
28
28
  "access": "public"
29
29
  },
30
30
  "devDependencies": {
31
- "htmlparser2": "^10.0.0",
32
- "magic-string": "^0.30.21",
33
31
  "oxlint": "^1.41.0",
34
32
  "vite": "^8.1.0",
35
33
  "vite-plugin-dts": "^3.9.1",