@cossackframework/renderer 0.8.2 → 0.8.3
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.
- package/README.md +5 -4
- package/dist/cossack-html.d.ts +3 -0
- package/dist/cossack-html.js +150 -112
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
# Cossack Renderer Package
|
|
2
2
|
|
|
3
|
-
Cossack Renderer is a Lit-compatible rendering engine
|
|
3
|
+
Cossack Renderer is a Lit-compatible rendering engine for **Light DOM** and
|
|
4
|
+
**SSR**. It returns HTML strings for SSR and updates the DOM without Shadow DOM
|
|
5
|
+
isolation.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
solution.
|
|
9
|
+
Projects created with `cossack create` include this package. You can also use
|
|
10
|
+
it on its own in another project.
|
|
10
11
|
|
|
11
12
|
```bash
|
|
12
13
|
pnpm add @cossackframework/renderer
|
package/dist/cossack-html.d.ts
CHANGED
|
@@ -20,8 +20,10 @@ export type ValueSanitizer = (value: unknown) => unknown;
|
|
|
20
20
|
export type SanitizerFactory = (node: Node, name: string, type: 'property' | 'attribute') => ValueSanitizer;
|
|
21
21
|
export declare const component: <T extends CossackElement>(clazz: new () => T, props?: T['props'] & Record<string, unknown>, children?: unknown) => TemplateResult<1>;
|
|
22
22
|
export declare const isTemplateResult: (value: unknown) => value is TemplateResult;
|
|
23
|
+
declare const UNSAFE_HTML_BRAND: unique symbol;
|
|
23
24
|
export declare class UnsafeHTMLResult {
|
|
24
25
|
readonly value: string;
|
|
26
|
+
readonly [UNSAFE_HTML_BRAND] = true;
|
|
25
27
|
constructor(value: string);
|
|
26
28
|
}
|
|
27
29
|
export declare const unsafeHTML: (value: string) => UnsafeHTMLResult;
|
|
@@ -42,3 +44,4 @@ export declare const render: (result: TemplateResult, container: Node) => void;
|
|
|
42
44
|
* back to a full `render()` so behaviour is always correct.
|
|
43
45
|
*/
|
|
44
46
|
export declare const hydrate: (result: TemplateResult, container: Node) => void;
|
|
47
|
+
export {};
|
package/dist/cossack-html.js
CHANGED
|
@@ -25,39 +25,56 @@ var m = class {
|
|
|
25
25
|
props: n ?? {},
|
|
26
26
|
children: r,
|
|
27
27
|
parent: t.currentRenderingInstance
|
|
28
|
-
}}`, y = (e) => typeof e == "object" && !!e && e._cossack_template_result === !0, b = class {
|
|
28
|
+
}}`, y = (e) => typeof e == "object" && !!e && e._cossack_template_result === !0, b = Symbol.for("@cossackframework/renderer/unsafe-html"), x = class {
|
|
29
29
|
value;
|
|
30
|
+
[b] = !0;
|
|
30
31
|
constructor(e) {
|
|
31
32
|
this.value = e;
|
|
32
33
|
}
|
|
33
|
-
},
|
|
34
|
+
}, S = (e) => new x(e), C = (e) => typeof e == "object" && !!e && e[b] === !0, w = (e) => String(e).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'"), T = (s, u = {}) => {
|
|
34
35
|
if (s === _ || s === "" || s == null || s === !1) return "";
|
|
35
|
-
if (Array.isArray(s)) return u.hydrate ? s.map((e) => `<!--CSA-S-->${
|
|
36
|
-
if (y(s)) return
|
|
37
|
-
if (s instanceof l) return
|
|
38
|
-
if (s instanceof d) return u.hydrate ? s.items.map((e, t) => `<!--CSA-S-->${
|
|
39
|
-
if (s instanceof c) return
|
|
40
|
-
if (s instanceof o) return
|
|
41
|
-
if (s instanceof a) return
|
|
42
|
-
if (s instanceof i) return
|
|
36
|
+
if (Array.isArray(s)) return u.hydrate ? s.map((e) => `<!--CSA-S-->${T(e, u)}<!--CSA-E-->`).join("") : s.map((e) => T(e, u)).join("");
|
|
37
|
+
if (y(s)) return F(s, u);
|
|
38
|
+
if (s instanceof l) return T(s.value, u);
|
|
39
|
+
if (s instanceof d) return u.hydrate ? s.items.map((e, t) => `<!--CSA-S-->${T(s.templateFn(e, t), u)}<!--CSA-E-->`).join("") : s.items.map((e, t) => T(s.templateFn(e, t), u)).join("");
|
|
40
|
+
if (s instanceof c) return T(s.template, u);
|
|
41
|
+
if (s instanceof o) return T(s.factory(), u);
|
|
42
|
+
if (s instanceof a) return T(s.value, u);
|
|
43
|
+
if (s instanceof i) return T(f(s.component, s.fieldName), u);
|
|
43
44
|
if (e(s)) {
|
|
44
45
|
let e = new s.clazz();
|
|
45
46
|
Object.assign(e, s.props), "props" in e && (e.props = s.props), e.children = s.children, e.__parent = s.parent || t.currentRenderingInstance, s.serviceScope && typeof e._setServiceScope == "function" && e._setServiceScope(s.serviceScope), e.__parent && (e._id = `${e.__parent._id}:${e.__parent._childCounter++}`), e.connectedCallback(), r(e), e.willUpdate(/* @__PURE__ */ new Map());
|
|
46
47
|
let i = e._finalizeRenderOutput(e.render()), a = "";
|
|
47
|
-
return i && (a = y(i) ?
|
|
48
|
+
return i && (a = y(i) ? F(i, u) : F(h`${i}`, u)), n(), a;
|
|
48
49
|
}
|
|
49
|
-
return
|
|
50
|
-
},
|
|
50
|
+
return C(s) ? s.value : typeof s == "function" || typeof s == "object" && s && "value" in s ? "" : w(s);
|
|
51
|
+
}, E = /* @__PURE__ */ new Set([
|
|
52
|
+
"allowfullscreen",
|
|
53
|
+
"async",
|
|
54
|
+
"autofocus",
|
|
55
|
+
"autoplay",
|
|
51
56
|
"checked",
|
|
57
|
+
"controls",
|
|
58
|
+
"default",
|
|
59
|
+
"defer",
|
|
52
60
|
"disabled",
|
|
53
|
-
"
|
|
54
|
-
"required",
|
|
61
|
+
"formnovalidate",
|
|
55
62
|
"hidden",
|
|
56
|
-
"
|
|
63
|
+
"inert",
|
|
64
|
+
"ismap",
|
|
65
|
+
"itemscope",
|
|
66
|
+
"loop",
|
|
57
67
|
"multiple",
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
|
|
68
|
+
"muted",
|
|
69
|
+
"nomodule",
|
|
70
|
+
"novalidate",
|
|
71
|
+
"open",
|
|
72
|
+
"playsinline",
|
|
73
|
+
"readonly",
|
|
74
|
+
"required",
|
|
75
|
+
"reversed",
|
|
76
|
+
"selected"
|
|
77
|
+
]), D = (e, t) => E.has(e.toLowerCase()) ? t ? "" : null : String(t), O = (e, t) => {
|
|
61
78
|
if (!(e === _ || typeof e != "object" || !e)) for (let [n, r] of Object.entries(e)) {
|
|
62
79
|
if (n.startsWith("@")) continue;
|
|
63
80
|
let e = n, a = r;
|
|
@@ -67,37 +84,41 @@ var m = class {
|
|
|
67
84
|
}
|
|
68
85
|
if (n.startsWith(".")) {
|
|
69
86
|
if (e = n.slice(1), a instanceof i ? a = f(a.component, a.fieldName) : a instanceof l && (a = a.value), a === _) continue;
|
|
70
|
-
if (
|
|
87
|
+
if (E.has(e)) {
|
|
71
88
|
a && (t.result += ` ${e}`);
|
|
72
89
|
continue;
|
|
73
90
|
}
|
|
74
91
|
}
|
|
75
92
|
if ((e === "class" || e === "className") && typeof a == "string" && a) {
|
|
76
|
-
let e =
|
|
93
|
+
let e = k(t.result, a);
|
|
77
94
|
if (e !== null) {
|
|
78
95
|
t.result = e;
|
|
79
96
|
continue;
|
|
80
97
|
}
|
|
81
98
|
}
|
|
82
|
-
a !== _
|
|
99
|
+
if (a !== _) if (a instanceof s) a.value !== void 0 && (t.result += ` ${e}="${w(String(a.value))}"`);
|
|
100
|
+
else if (typeof a == "boolean") {
|
|
101
|
+
let n = D(e, a);
|
|
102
|
+
n === "" ? t.result += ` ${e}` : n !== null && (t.result += ` ${e}="${n}"`);
|
|
103
|
+
} else typeof a == "function" || a != null && (t.result += ` ${e}="${w(a)}"`);
|
|
83
104
|
}
|
|
84
|
-
},
|
|
105
|
+
}, k = (e, t) => {
|
|
85
106
|
let n = e.lastIndexOf("<"), r = n >= 0 ? e.slice(n) : e, i = r.match(/(^|\s)class="([^"]*)"(\s|\/?>|$)/);
|
|
86
107
|
if (!i) return null;
|
|
87
108
|
let a = i[1], o = i[2], s = i[3], c = t.trim();
|
|
88
109
|
if (!c) return e;
|
|
89
110
|
let l = o ? `${o} ${c}` : c, u = r.replace(i[0], `${a}class="${l}"${s}`);
|
|
90
111
|
return e.slice(0, n) + u;
|
|
91
|
-
},
|
|
112
|
+
}, A = (e) => {
|
|
92
113
|
let t = [], n = !1, r = null, i = /(\.\.\.|[.@?]?[a-zA-Z0-9_:-]+)=["']?$/;
|
|
93
114
|
for (let a = 0; a < e.length - 1; a++) {
|
|
94
115
|
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);
|
|
116
|
+
for (let e = 0; e < o.length; e++) r ? o[e] === r && (r = null) : n && (o[e] === "\"" || o[e] === "'") ? e > 0 && o[e - 1] === "=" && (r = o[e]) : o[e] === "<" && o[e + 1] !== "!" && o[e + 1] !== "/" ? n = !0 : o[e] === ">" && (n = !1);
|
|
96
117
|
let s = o.match(i);
|
|
97
118
|
t[a] = !(n && (s || r));
|
|
98
119
|
}
|
|
99
120
|
return t;
|
|
100
|
-
},
|
|
121
|
+
}, j = (e) => {
|
|
101
122
|
let t = 0, n;
|
|
102
123
|
do
|
|
103
124
|
n = `\uE000cossack:${t++}:`;
|
|
@@ -107,9 +128,9 @@ var m = class {
|
|
|
107
128
|
marker: (e) => `${n}${e}${r}`,
|
|
108
129
|
pattern: RegExp(`${n}(\\d+)${r}`, "g")
|
|
109
130
|
};
|
|
110
|
-
},
|
|
131
|
+
}, M = (e, t) => {
|
|
111
132
|
if (!t) return e;
|
|
112
|
-
let n =
|
|
133
|
+
let n = j(e), r = "";
|
|
113
134
|
for (let t = 0; t < e.length; t++) r += e[t], t < e.length - 1 && (r += n.marker(t));
|
|
114
135
|
let i = "", a = !1, o = null, s = "", c = "";
|
|
115
136
|
for (let e = 0; e < r.length;) {
|
|
@@ -138,8 +159,8 @@ var m = class {
|
|
|
138
159
|
let l = [], u = 0, d;
|
|
139
160
|
for (; d = n.pattern.exec(i);) l.push(i.slice(u, d.index)), u = d.index + d[0].length;
|
|
140
161
|
return l.push(i.slice(u)), l;
|
|
141
|
-
},
|
|
142
|
-
let n =
|
|
162
|
+
}, N = (e, t) => {
|
|
163
|
+
let n = j(e), r = "";
|
|
143
164
|
for (let t = 0; t < e.length; t++) r += e[t], t < e.length - 1 && (r += n.marker(t));
|
|
144
165
|
let i = /* @__PURE__ */ new Map(), a = /<(?![!/])(?:[^>"']|"[^"]*"|'[^']*')*>/g, o;
|
|
145
166
|
for (; o = a.exec(r);) {
|
|
@@ -159,7 +180,7 @@ var m = class {
|
|
|
159
180
|
}
|
|
160
181
|
}
|
|
161
182
|
return i;
|
|
162
|
-
},
|
|
183
|
+
}, P = class {
|
|
163
184
|
resultObj;
|
|
164
185
|
opts;
|
|
165
186
|
result = "";
|
|
@@ -169,7 +190,7 @@ var m = class {
|
|
|
169
190
|
strings;
|
|
170
191
|
nothingAttributeGroups;
|
|
171
192
|
constructor(e, t = {}) {
|
|
172
|
-
this.resultObj = e, this.opts = t, this.strings =
|
|
193
|
+
this.resultObj = e, this.opts = t, this.strings = M(e.strings, e.__cossackScope), this.isNodePositions = A(this.strings), this.nothingAttributeGroups = N(this.strings, e.values);
|
|
173
194
|
}
|
|
174
195
|
scan() {
|
|
175
196
|
let { values: e } = this.resultObj, t = this.strings;
|
|
@@ -181,14 +202,14 @@ var m = class {
|
|
|
181
202
|
this.result.endsWith(a.prefix) && (this.result = this.result.slice(0, -a.prefix.length)), this.stringIdx = a.last, this.charIdxForNext = a.suffix.length, this.stringIdx++;
|
|
182
203
|
continue;
|
|
183
204
|
}
|
|
184
|
-
if (r) {
|
|
205
|
+
if (r && !this.isNodePositions[this.stringIdx]) {
|
|
185
206
|
let e = r[0], a = r[1];
|
|
186
207
|
this.result = this.result.substring(0, this.result.length - e.length);
|
|
187
208
|
let o = e.endsWith("\"") ? "\"" : e.endsWith("'") ? "'" : "", c = !0;
|
|
188
209
|
if (a === "...") {
|
|
189
210
|
this.result = this.result.trimEnd();
|
|
190
211
|
let e = { result: this.result };
|
|
191
|
-
|
|
212
|
+
O(n, e), this.result = e.result;
|
|
192
213
|
} else if (a === "ref") this.result = this.result.trimEnd();
|
|
193
214
|
else if (a.startsWith("@")) this.result = this.result.trimEnd();
|
|
194
215
|
else if (a.startsWith("?")) n ? (this.result = this.result.trimEnd(), this.result += ` ${a.slice(1)}`) : this.result = this.result.trimEnd();
|
|
@@ -196,29 +217,34 @@ var m = class {
|
|
|
196
217
|
let e, t = n instanceof i;
|
|
197
218
|
e = t ? f(n.component, n.fieldName) : n;
|
|
198
219
|
let r = a.slice(1);
|
|
199
|
-
t &&
|
|
200
|
-
} else if (n instanceof s) n.value === void 0 ? this.result = this.result.trimEnd() : (this.result = this.result.trimEnd(), this.result += ` ${a}="${
|
|
201
|
-
else
|
|
220
|
+
t && E.has(r) ? (this.result = this.result.trimEnd(), e && (this.result += ` ${r}`)) : e != null && e !== !1 ? (this.result = this.result.trimEnd(), this.result += ` ${r}="${T(e, this.opts)}"`) : this.result = this.result.trimEnd();
|
|
221
|
+
} else if (n instanceof s) n.value === void 0 ? this.result = this.result.trimEnd() : (this.result = this.result.trimEnd(), this.result += ` ${a}="${w(String(n.value))}"`);
|
|
222
|
+
else if (n == null || n === _) this.result = this.result.trimEnd();
|
|
223
|
+
else if (typeof n == "boolean") {
|
|
224
|
+
this.result = this.result.trimEnd();
|
|
225
|
+
let e = D(a, n);
|
|
226
|
+
e === "" ? this.result += ` ${a}` : e !== null && (this.result += ` ${a}="${e}"`);
|
|
227
|
+
} else {
|
|
202
228
|
this.result += e;
|
|
203
|
-
let t =
|
|
229
|
+
let t = T(n, this.opts);
|
|
204
230
|
o ? (this.result += t, c = !1) : this.result += `"${t}"`;
|
|
205
231
|
}
|
|
206
232
|
if (o && c) {
|
|
207
233
|
let e = t[this.stringIdx + 1];
|
|
208
234
|
e && e.startsWith(o) && (this.charIdxForNext = 1);
|
|
209
235
|
}
|
|
210
|
-
} else this.opts.hydrate && this.isNodePositions[this.stringIdx] ? (this.result += `<!--CRP_${this.stringIdx}-->`, this.result +=
|
|
236
|
+
} else this.opts.hydrate && this.isNodePositions[this.stringIdx] ? (this.result += `<!--CRP_${this.stringIdx}-->`, this.result += T(n, this.opts), this.result += "<!--/CRP-->") : this.result += T(n, this.opts);
|
|
211
237
|
}
|
|
212
238
|
this.stringIdx++;
|
|
213
239
|
}
|
|
214
240
|
return this.result;
|
|
215
241
|
}
|
|
216
|
-
},
|
|
217
|
-
let t =
|
|
218
|
-
t || (t = /* @__PURE__ */ new Map(),
|
|
242
|
+
}, F = (e, t = {}) => new P(e, t).scan(), I = /* @__PURE__ */ new WeakMap(), L = (e) => {
|
|
243
|
+
let t = I.get(e.strings);
|
|
244
|
+
t || (t = /* @__PURE__ */ new Map(), I.set(e.strings, t));
|
|
219
245
|
let n = `${e._$litType$}:${e.__cossackScope ?? ""}`, r = t.get(n);
|
|
220
246
|
return r || (r = {}, t.set(n, r)), r;
|
|
221
|
-
},
|
|
247
|
+
}, R = class n {
|
|
222
248
|
startNode;
|
|
223
249
|
endNode;
|
|
224
250
|
componentInstance = null;
|
|
@@ -303,7 +329,7 @@ var m = class {
|
|
|
303
329
|
if (t instanceof a) {
|
|
304
330
|
let e = t.value;
|
|
305
331
|
if (y(e)) {
|
|
306
|
-
let t =
|
|
332
|
+
let t = L(e);
|
|
307
333
|
this._cacheMap.has(t) ? this.updateCache(e) : (this._adoptTemplate(e), this._cacheCurrent = t);
|
|
308
334
|
} else this._hydrateValue(e);
|
|
309
335
|
return;
|
|
@@ -313,7 +339,7 @@ var m = class {
|
|
|
313
339
|
return;
|
|
314
340
|
}
|
|
315
341
|
if (!(t instanceof Node)) {
|
|
316
|
-
if (
|
|
342
|
+
if (C(t)) {
|
|
317
343
|
if (this.startNode.nextSibling !== this.endNode) return;
|
|
318
344
|
this.clear(), this._clearTemplateCache(), this.updateNode(t);
|
|
319
345
|
return;
|
|
@@ -335,7 +361,7 @@ var m = class {
|
|
|
335
361
|
if (t) if (t = !1, y(e)) try {
|
|
336
362
|
this._adoptTemplate(e);
|
|
337
363
|
} catch (t) {
|
|
338
|
-
if (t instanceof
|
|
364
|
+
if (t instanceof K) this.clear(), this._clearTemplateCache(), this.updateNode(e);
|
|
339
365
|
else throw t;
|
|
340
366
|
}
|
|
341
367
|
else e == null ? this.clear() : this.updateNode(e);
|
|
@@ -346,8 +372,8 @@ var m = class {
|
|
|
346
372
|
Object.assign(r, e.props), "props" in r && (r.props = e.props), r.children = e.children, r.requestUpdate();
|
|
347
373
|
}
|
|
348
374
|
_adoptSequence(e, t) {
|
|
349
|
-
let r =
|
|
350
|
-
if (r.length !== e.length) throw new
|
|
375
|
+
let r = Y(this._nodesBetween());
|
|
376
|
+
if (r.length !== e.length) throw new K(`sequence length mismatch: SSR has ${r.length} items, client has ${e.length}`);
|
|
351
377
|
this._childParts = [], this._partKeys = t ? [] : this._partKeys;
|
|
352
378
|
for (let i = 0; i < e.length; i++) {
|
|
353
379
|
let [a, o] = r[i], s = new n(a, o);
|
|
@@ -355,10 +381,10 @@ var m = class {
|
|
|
355
381
|
}
|
|
356
382
|
}
|
|
357
383
|
_adoptTemplate(e) {
|
|
358
|
-
let { fragment: t, parts: n } =
|
|
359
|
-
|
|
384
|
+
let { fragment: t, parts: n } = W(e), r = this._nodesBetween(), i = /* @__PURE__ */ new Map();
|
|
385
|
+
X(t.childNodes, r, i), Z(n, i);
|
|
360
386
|
for (let t = 0; t < e.values.length; t++) n[t] && n[t].update(e.values[t]);
|
|
361
|
-
this._cachedTemplateIdentity =
|
|
387
|
+
this._cachedTemplateIdentity = L(e), this._cachedParts = n;
|
|
362
388
|
}
|
|
363
389
|
_nodesBetween() {
|
|
364
390
|
let e = [], t = this.startNode.nextSibling;
|
|
@@ -400,7 +426,7 @@ var m = class {
|
|
|
400
426
|
this._keySet = !0, this._key = e.value, t && (this.componentInstance && this.disposeComponent(), this._clearTemplateCache(), this.clear()), this.updateNode(e.template);
|
|
401
427
|
}
|
|
402
428
|
updateCache(e) {
|
|
403
|
-
let t =
|
|
429
|
+
let t = L(e);
|
|
404
430
|
if (this._cacheCurrent === t && this._cachedParts) {
|
|
405
431
|
this._cachedParts.forEach((t, n) => {
|
|
406
432
|
t.update(e.values[n]);
|
|
@@ -468,24 +494,24 @@ var m = class {
|
|
|
468
494
|
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
495
|
}
|
|
470
496
|
updateNode(t) {
|
|
471
|
-
if (this.startNode.parentNode) if (y(t)) if (this._cachedTemplateIdentity ===
|
|
497
|
+
if (this.startNode.parentNode) if (y(t)) if (this._cachedTemplateIdentity === L(t) && this._cachedParts) this._cachedParts.forEach((e, n) => {
|
|
472
498
|
e.update(t.values[n]);
|
|
473
499
|
});
|
|
474
500
|
else {
|
|
475
501
|
this._clearTemplateCache(), this.clear();
|
|
476
502
|
let e = document.createDocumentFragment();
|
|
477
|
-
|
|
478
|
-
let n =
|
|
503
|
+
G(t, e), this.startNode.parentNode.insertBefore(e, this.endNode);
|
|
504
|
+
let n = U.get(e.firstChild?.childNodes[0] || e);
|
|
479
505
|
n && (this._cachedTemplateIdentity = n.identity, this._cachedParts = n.parts);
|
|
480
506
|
}
|
|
481
|
-
else if (
|
|
507
|
+
else if (C(t)) {
|
|
482
508
|
this._clearTemplateCache(), this.clear();
|
|
483
509
|
let e = document.createElement("template");
|
|
484
510
|
e.innerHTML = t.value, this.startNode.parentNode.insertBefore(e.content, this.endNode);
|
|
485
511
|
} else if (e(t) || typeof t == "object" && t && !(t instanceof Node) && !Array.isArray(t)) {
|
|
486
512
|
this._clearTemplateCache(), this.clear();
|
|
487
513
|
let e = document.createDocumentFragment();
|
|
488
|
-
|
|
514
|
+
G(h`${t}`, e), this.startNode.parentNode.insertBefore(e, this.endNode);
|
|
489
515
|
} else if (t instanceof Node) this._clearTemplateCache(), this.clear(), this.startNode.parentNode.insertBefore(t, this.endNode);
|
|
490
516
|
else if (t === _ || t === "" || t == null || t === !1) this._clearTemplateCache(), this.clear();
|
|
491
517
|
else {
|
|
@@ -509,7 +535,7 @@ var m = class {
|
|
|
509
535
|
e.parentNode.removeChild(e), e = t;
|
|
510
536
|
}
|
|
511
537
|
}
|
|
512
|
-
},
|
|
538
|
+
}, z = class {
|
|
513
539
|
element;
|
|
514
540
|
previousValues = {};
|
|
515
541
|
bindStates = /* @__PURE__ */ new Map();
|
|
@@ -559,7 +585,14 @@ var m = class {
|
|
|
559
585
|
e.value !== t && (e.value = t);
|
|
560
586
|
}
|
|
561
587
|
} else n instanceof i ? this.element[t] = f(n.component, n.fieldName) : n instanceof l ? this.element[t] = n.value : this.element[t] = n;
|
|
562
|
-
} else e.startsWith("?")
|
|
588
|
+
} else if (e.startsWith("?")) n !== _ && n ? this.element.setAttribute(e.slice(1), "") : this.element.removeAttribute(e.slice(1));
|
|
589
|
+
else if (n === _) e === "class" || e === "className" ? this.applySpreadClass("") : this.element.removeAttribute(e);
|
|
590
|
+
else if (n instanceof s) n.value === void 0 ? this.element.removeAttribute(e) : this.element.setAttribute(e, String(n.value));
|
|
591
|
+
else if (n == null) this.element.removeAttribute(e);
|
|
592
|
+
else if (typeof n == "boolean") {
|
|
593
|
+
let t = D(e, n);
|
|
594
|
+
t === null ? this.element.removeAttribute(e) : this.element.setAttribute(e, t);
|
|
595
|
+
} else (e === "class" || e === "className") && typeof n == "string" ? this.applySpreadClass(n) : this.element.setAttribute(e, String(n));
|
|
563
596
|
this.previousValues = { ...t };
|
|
564
597
|
}
|
|
565
598
|
applySpreadClass(e) {
|
|
@@ -577,7 +610,7 @@ var m = class {
|
|
|
577
610
|
let e = a == null ? "" : String(a);
|
|
578
611
|
(!o || o.lastKind !== "value" || String(o.lastValue) !== e || this.element instanceof HTMLSelectElement && r.value !== e) && (r.value = e);
|
|
579
612
|
}
|
|
580
|
-
let s =
|
|
613
|
+
let s = H(this.element, t), c = !!o && (o.boundComponent !== i || o.boundField !== n.fieldName || o.listener.__eventName !== s);
|
|
581
614
|
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
615
|
else {
|
|
583
616
|
let r = (e) => {
|
|
@@ -590,10 +623,10 @@ var m = class {
|
|
|
590
623
|
lastValue: t === "checked" ? !!a : a == null ? "" : String(a),
|
|
591
624
|
boundComponent: i,
|
|
592
625
|
boundField: n.fieldName
|
|
593
|
-
}, this.bindStates.set(e, o), t === "value" &&
|
|
626
|
+
}, this.bindStates.set(e, o), t === "value" && V(this.element, () => this.bindStates.get(e) === o, () => f(o.boundComponent, o.boundField));
|
|
594
627
|
}
|
|
595
628
|
}
|
|
596
|
-
},
|
|
629
|
+
}, B = class {
|
|
597
630
|
element;
|
|
598
631
|
name;
|
|
599
632
|
segments;
|
|
@@ -687,7 +720,12 @@ var m = class {
|
|
|
687
720
|
} else if (this.name.startsWith("?")) {
|
|
688
721
|
let t = this.name.slice(1);
|
|
689
722
|
e ? this.element.setAttribute(t, "") : this.element.removeAttribute(t), this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
|
|
690
|
-
} else n
|
|
723
|
+
} else if (n) e === void 0 ? this.element.removeAttribute(this.name) : this.element.setAttribute(this.name, this.segments[0] + String(e) + this.segments[1]);
|
|
724
|
+
else if (e == null) this.element.removeAttribute(this.name);
|
|
725
|
+
else if (typeof e == "boolean") {
|
|
726
|
+
let t = D(this.name, e);
|
|
727
|
+
t === null ? this.element.removeAttribute(this.name) : this.element.setAttribute(this.name, t);
|
|
728
|
+
} else this.element.setAttribute(this.name, this.segments[0] + String(e) + this.segments[1]);
|
|
691
729
|
}
|
|
692
730
|
updateBind(e) {
|
|
693
731
|
let t = this.name.startsWith(".") ? this.name.slice(1) : this.name;
|
|
@@ -703,7 +741,7 @@ var m = class {
|
|
|
703
741
|
let e = i == null ? "" : String(i);
|
|
704
742
|
(this.lastFormKind !== "value" || String(this.lastFormValue) !== e || this.element instanceof HTMLSelectElement && n.value !== e) && (n.value = e, this.lastFormValue = e, this.lastFormKind = "value");
|
|
705
743
|
}
|
|
706
|
-
let a =
|
|
744
|
+
let a = H(this.element, t);
|
|
707
745
|
if (this.bindListener && (this.boundPropName !== t || this.boundComponent !== r || this.boundFieldName !== e.fieldName || this.bindListener.__eventName !== a) && this._detachBindListener(), !this.bindListener) {
|
|
708
746
|
let n = (n) => {
|
|
709
747
|
let i = n.currentTarget, a = t === "checked" ? !!i[t] : i[t];
|
|
@@ -711,18 +749,18 @@ var m = class {
|
|
|
711
749
|
};
|
|
712
750
|
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
751
|
let e = n;
|
|
714
|
-
|
|
752
|
+
V(this.element, () => this.bindListener === e, () => f(this.boundComponent, this.boundFieldName));
|
|
715
753
|
}
|
|
716
754
|
}
|
|
717
755
|
this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
|
|
718
756
|
}
|
|
719
|
-
},
|
|
757
|
+
}, V = (e, t, n) => {
|
|
720
758
|
e instanceof HTMLSelectElement && queueMicrotask(() => {
|
|
721
759
|
if (!t()) return;
|
|
722
760
|
let r = n(), i = r == null ? "" : String(r);
|
|
723
761
|
e.value !== i && (e.value = i);
|
|
724
762
|
});
|
|
725
|
-
},
|
|
763
|
+
}, H = (e, t) => {
|
|
726
764
|
if (e instanceof HTMLSelectElement) return "change";
|
|
727
765
|
if (e instanceof HTMLTextAreaElement) return "input";
|
|
728
766
|
if (e instanceof HTMLInputElement) {
|
|
@@ -730,11 +768,11 @@ var m = class {
|
|
|
730
768
|
return t === "checked" || n === "checkbox" || n === "radio" || n === "range" ? "change" : "input";
|
|
731
769
|
}
|
|
732
770
|
return "input";
|
|
733
|
-
},
|
|
734
|
-
let t = [], n =
|
|
771
|
+
}, U = /* @__PURE__ */ new WeakMap(), W = (e) => {
|
|
772
|
+
let t = [], n = M(e.strings, e.__cossackScope), r = "", i = !1, a = null, o = /(\.\.\.|[.@?]?[a-zA-Z0-9_:-]+)=["']?$/;
|
|
735
773
|
for (let e = 0; e < n.length - 1; e++) {
|
|
736
774
|
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);
|
|
775
|
+
for (let e = 0; e < t.length; e++) a ? t[e] === a && (a = null) : i && (t[e] === "\"" || t[e] === "'") ? e > 0 && t[e - 1] === "=" && (a = t[e]) : t[e] === "<" && t[e + 1] !== "!" && t[e + 1] !== "/" ? i = !0 : t[e] === ">" && (i = !1);
|
|
738
776
|
r += t;
|
|
739
777
|
let s = t.match(o);
|
|
740
778
|
if (i && (s || a)) {
|
|
@@ -762,7 +800,7 @@ var m = class {
|
|
|
762
800
|
if (n) {
|
|
763
801
|
let r = parseInt(n[1]), i = document.createComment("/CRP");
|
|
764
802
|
e.parentNode.insertBefore(i, e.nextSibling);
|
|
765
|
-
let a = new
|
|
803
|
+
let a = new R(e, i);
|
|
766
804
|
t[r] = a;
|
|
767
805
|
}
|
|
768
806
|
} else if (e.nodeType === Node.ELEMENT_NODE) {
|
|
@@ -773,10 +811,10 @@ var m = class {
|
|
|
773
811
|
if (r.length === 1) {
|
|
774
812
|
let i = parseInt(r[0][1]);
|
|
775
813
|
if (!t[i]) if (e.name === "...") {
|
|
776
|
-
let e = new
|
|
814
|
+
let e = new z(n);
|
|
777
815
|
t[i] = e, n.removeAttribute("...");
|
|
778
816
|
} else {
|
|
779
|
-
let r = new
|
|
817
|
+
let r = new B(n, e.name, e.value);
|
|
780
818
|
t[i] = r;
|
|
781
819
|
}
|
|
782
820
|
return;
|
|
@@ -786,7 +824,7 @@ var m = class {
|
|
|
786
824
|
for (let r = 0; r < i.length; r++) {
|
|
787
825
|
let o = i[r];
|
|
788
826
|
if (!t[o]) {
|
|
789
|
-
let i = new
|
|
827
|
+
let i = new B(n, e.name, e.value, {
|
|
790
828
|
currentValues: a,
|
|
791
829
|
position: r
|
|
792
830
|
});
|
|
@@ -799,8 +837,8 @@ var m = class {
|
|
|
799
837
|
fragment: l,
|
|
800
838
|
parts: t
|
|
801
839
|
};
|
|
802
|
-
},
|
|
803
|
-
let n =
|
|
840
|
+
}, G = (e, t) => {
|
|
841
|
+
let n = U.get(t), r = L(e);
|
|
804
842
|
if (n && n.identity === r) {
|
|
805
843
|
n.parts.forEach((t, n) => {
|
|
806
844
|
t.update(e.values[n]);
|
|
@@ -808,22 +846,22 @@ var m = class {
|
|
|
808
846
|
return;
|
|
809
847
|
}
|
|
810
848
|
if (n) for (let e of n.parts) e && typeof e.dispose == "function" && e.dispose();
|
|
811
|
-
let { fragment: i, parts: a } =
|
|
849
|
+
let { fragment: i, parts: a } = W(e), o = e.values;
|
|
812
850
|
for (let e = 0; e < o.length; e++) a[e] && a[e].update(o[e]);
|
|
813
851
|
if (typeof HTMLElement < "u" && t instanceof HTMLElement || typeof ShadowRoot < "u" && t instanceof ShadowRoot) t.innerHTML = "";
|
|
814
852
|
else for (; t.firstChild;) t.removeChild(t.firstChild);
|
|
815
|
-
t.appendChild(i),
|
|
853
|
+
t.appendChild(i), U.set(t, {
|
|
816
854
|
identity: r,
|
|
817
855
|
parts: a
|
|
818
856
|
});
|
|
819
|
-
},
|
|
857
|
+
}, K = class extends Error {}, q = (e) => e ? e.nodeType === Node.ELEMENT_NODE ? `<${e.nodeName.toLowerCase()}>` : e.nodeType === Node.COMMENT_NODE ? `<!--${e.nodeValue}-->` : e.nodeType === Node.TEXT_NODE ? `text"${(e.nodeValue || "").slice(0, 30)}"` : `nodeType:${e.nodeType}` : "<none>", J = (e) => {
|
|
820
858
|
if (e.nodeType === Node.TEXT_NODE) return (e.nodeValue || "").trim() === "";
|
|
821
859
|
if (e.nodeType === Node.COMMENT_NODE) {
|
|
822
860
|
let t = e.nodeValue || "";
|
|
823
861
|
return !/^CRP_\d+$/.test(t) && t !== "/CRP" && t !== "CSA-S" && t !== "CSA-E";
|
|
824
862
|
}
|
|
825
863
|
return !1;
|
|
826
|
-
},
|
|
864
|
+
}, Y = (e) => {
|
|
827
865
|
let t = [], n = [];
|
|
828
866
|
for (let r of e) {
|
|
829
867
|
if (r.nodeType !== Node.COMMENT_NODE) continue;
|
|
@@ -835,50 +873,50 @@ var m = class {
|
|
|
835
873
|
}
|
|
836
874
|
}
|
|
837
875
|
return t;
|
|
838
|
-
},
|
|
839
|
-
let
|
|
840
|
-
for (let
|
|
841
|
-
let
|
|
842
|
-
if (
|
|
843
|
-
for (;
|
|
844
|
-
let
|
|
845
|
-
if (!
|
|
846
|
-
if (
|
|
847
|
-
if (
|
|
848
|
-
n.set(
|
|
876
|
+
}, X = (e, t, n, r = "root") => {
|
|
877
|
+
let i = 0;
|
|
878
|
+
for (let a = 0; a < e.length; a++) {
|
|
879
|
+
let o = e[a];
|
|
880
|
+
if (J(o)) continue;
|
|
881
|
+
for (; i < t.length && J(t[i]);) i++;
|
|
882
|
+
let s = t[i];
|
|
883
|
+
if (!s) throw new K(`existing shorter than blueprint at ${r} (expected ${q(o)})`);
|
|
884
|
+
if (o.nodeType === Node.ELEMENT_NODE) {
|
|
885
|
+
if (s.nodeType !== Node.ELEMENT_NODE || o.nodeName !== s.nodeName || o.namespaceURI !== s.namespaceURI) throw new K(`tag mismatch: blueprint ${q(o)} vs existing ${q(s)}`);
|
|
886
|
+
n.set(o, s), X(o.childNodes, Array.from(s.childNodes), n, `${r}/${o.nodeName.toLowerCase()}[${a}]`);
|
|
849
887
|
} else {
|
|
850
|
-
if (
|
|
851
|
-
n.set(
|
|
888
|
+
if (o.nodeType !== s.nodeType) throw new K(`type mismatch: blueprint ${q(o)} vs existing ${q(s)}`);
|
|
889
|
+
n.set(o, s);
|
|
852
890
|
}
|
|
853
|
-
if (
|
|
891
|
+
if (i++, o.nodeType === Node.COMMENT_NODE && /^CRP_\d+$/.test(o.nodeValue || "")) {
|
|
854
892
|
let e = 1;
|
|
855
|
-
for (;
|
|
856
|
-
let n = t[
|
|
893
|
+
for (; i < t.length && e > 0;) {
|
|
894
|
+
let n = t[i];
|
|
857
895
|
if (n.nodeType === Node.COMMENT_NODE) {
|
|
858
896
|
if (/^CRP_\d+$/.test(n.nodeValue || "")) e++;
|
|
859
897
|
else if (n.nodeValue === "/CRP" && (e--, e === 0)) break;
|
|
860
898
|
}
|
|
861
|
-
|
|
899
|
+
i++;
|
|
862
900
|
}
|
|
863
901
|
}
|
|
864
902
|
}
|
|
865
|
-
for (;
|
|
866
|
-
if (
|
|
867
|
-
},
|
|
903
|
+
for (; i < t.length && J(t[i]);) i++;
|
|
904
|
+
if (i < t.length) throw new K(`existing longer than blueprint (unexpected ${q(t[i])})`);
|
|
905
|
+
}, Z = (e, t) => {
|
|
868
906
|
for (let n of e) if (n) {
|
|
869
|
-
if (n instanceof
|
|
907
|
+
if (n instanceof R) {
|
|
870
908
|
let e = t.get(n.startNode), r = t.get(n.endNode);
|
|
871
|
-
if (!e || !r) throw new
|
|
909
|
+
if (!e || !r) throw new K("node part anchors not found");
|
|
872
910
|
n.startNode = e, n.endNode = r, n._beginHydration();
|
|
873
|
-
} else if (n instanceof
|
|
911
|
+
} else if (n instanceof B || n instanceof z) {
|
|
874
912
|
let e = t.get(n.element);
|
|
875
|
-
if (!e) throw new
|
|
913
|
+
if (!e) throw new K("attribute part element not found");
|
|
876
914
|
n.element = e;
|
|
877
915
|
}
|
|
878
916
|
}
|
|
879
|
-
},
|
|
880
|
-
if (!t.hasChildNodes()) return
|
|
881
|
-
let n =
|
|
917
|
+
}, Q = (e, t) => {
|
|
918
|
+
if (!t.hasChildNodes()) return G(e, t);
|
|
919
|
+
let n = U.get(t), r = L(e);
|
|
882
920
|
if (n && n.identity === r) {
|
|
883
921
|
n.parts.forEach((t, n) => t.update(e.values[n]));
|
|
884
922
|
return;
|
|
@@ -886,19 +924,19 @@ var m = class {
|
|
|
886
924
|
if (n) for (let e of n.parts) e && typeof e.dispose == "function" && e.dispose();
|
|
887
925
|
let i;
|
|
888
926
|
try {
|
|
889
|
-
let { fragment: n, parts: r } =
|
|
927
|
+
let { fragment: n, parts: r } = W(e);
|
|
890
928
|
i = r;
|
|
891
929
|
let a = /* @__PURE__ */ new Map();
|
|
892
|
-
|
|
930
|
+
X(n.childNodes, Array.from(t.childNodes), a), Z(i, a);
|
|
893
931
|
for (let t = 0; t < e.values.length; t++) i[t] && i[t].update(e.values[t]);
|
|
894
932
|
} catch (n) {
|
|
895
|
-
if (n instanceof
|
|
933
|
+
if (n instanceof K) return console.warn("[cossack] hydration mismatch, falling back to render:", n.message), G(e, t);
|
|
896
934
|
throw n;
|
|
897
935
|
}
|
|
898
|
-
|
|
936
|
+
U.set(t, {
|
|
899
937
|
identity: r,
|
|
900
938
|
parts: i
|
|
901
939
|
});
|
|
902
940
|
};
|
|
903
941
|
//#endregion
|
|
904
|
-
export { m as TemplateResult,
|
|
942
|
+
export { m as TemplateResult, x as UnsafeHTMLResult, v as component, w as escapeHtml, h as html, Q as hydrate, y as isTemplateResult, C as isUnsafeHTML, _ as nothing, G as render, F as renderToString, g as svg, S as unsafeHTML };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cossackframework/renderer",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "The rendering engine for Cossack Framework. Lit inspired, light dom, ssr",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"oxlint": "^1.41.0",
|
|
38
38
|
"typescript": "^7.0.2",
|
|
39
39
|
"vite": "^8.1.4",
|
|
40
|
-
"vitest": "^4.1.
|
|
40
|
+
"vitest": "^4.1.11"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"build": "vite build && tsc -p tsconfig.declarations.json && node ../../scripts/rewrite-declaration-imports.mjs dist",
|