@cossackframework/renderer 0.9.0 → 0.9.2

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,465 @@
1
+ // @ts-self-types="./diagnostics.d.ts"
2
+ import { parseTemplate as e, templateContextAt as t } from "./template-parse.js";
3
+ import { decodeEntities as n } from "./component-tags.js";
4
+ //#region src/diagnostics.ts
5
+ var r = /[\\/](?:@cossackframework[\\/]renderer|packages[\\/]renderer)[\\/]/, i = /<[A-Za-z/!]/, a = (e) => {
6
+ if (!e.includes("\\") && !e.includes("\r")) return {
7
+ toSource: (e) => e,
8
+ toCooked: (e) => e
9
+ };
10
+ let t = [];
11
+ for (let n = 0; n < e.length;) {
12
+ let r = e[n];
13
+ if (r === "\r") {
14
+ t.push(n), n += e[n + 1] === "\n" ? 2 : 1;
15
+ continue;
16
+ }
17
+ if (r !== "\\") {
18
+ t.push(n++);
19
+ continue;
20
+ }
21
+ let i = n, a = e[n + 1], o = 1;
22
+ if (a === "\r") n += e[n + 2] === "\n" ? 3 : 2, o = 0;
23
+ else if (a === "\n" || a === "\u2028" || a === "\u2029") n += 2, o = 0;
24
+ else if (a === "x") n += 4;
25
+ else if (a === "u" && e[n + 2] === "{") {
26
+ let t = e.indexOf("}", n);
27
+ o = parseInt(e.slice(n + 3, t), 16) > 65535 ? 2 : 1, n = t + 1;
28
+ } else a === "u" ? n += 6 : n += 2;
29
+ for (let e = 0; e < o; e++) t.push(i);
30
+ }
31
+ return t.push(e.length), {
32
+ toSource: (e) => t[Math.min(e, t.length - 1)],
33
+ toCooked: (e) => {
34
+ let n = 0;
35
+ for (; n + 1 < t.length && t[n + 1] <= e;) n++;
36
+ return n;
37
+ }
38
+ };
39
+ }, o = (e, t, n) => {
40
+ let r = t.getSymbolAtLocation(n);
41
+ return r && r.flags & e.SymbolFlags.Alias ? t.getAliasedSymbol(r) : r;
42
+ }, s = (e, t, n, i) => {
43
+ let a = e.isPropertyAccessExpression(t) ? t.name : e.isIdentifier(t) ? t : void 0;
44
+ if (!a) return;
45
+ if (!n) return {
46
+ name: a.text,
47
+ file: void 0
48
+ };
49
+ let s = o(e, n, a), c = s?.valueDeclaration ?? s?.declarations?.[0];
50
+ if (!s || !c) return;
51
+ let l = c.getSourceFile();
52
+ return (i.isRendererFile ?? ((e) => r.test(e)))(l.fileName) ? {
53
+ name: s.name,
54
+ file: l
55
+ } : void 0;
56
+ }, c = (e, t) => e.isNoSubstitutionTemplateLiteral(t) ? [t] : [t.head, ...t.templateSpans.map((e) => e.literal)], l = (e, t, n, r = {}) => {
57
+ let i = [], a = (o) => {
58
+ if (e.isTaggedTemplateExpression(o)) {
59
+ let a = s(e, o.tag, n, r);
60
+ a && (a.name === "html" || a.name === "svg") && i.push(u(e, t, o, a.name, a.file));
61
+ }
62
+ e.forEachChild(o, a);
63
+ };
64
+ return a(t), i;
65
+ }, u = (t, n, r, i, o) => {
66
+ let s = c(t, r.template), l = n.text, u = [], d = [], f = [];
67
+ for (let e of s) {
68
+ let r = e.getStart(n) + 1, i = e.getEnd() - (e.kind === t.SyntaxKind.TemplateHead || e.kind === t.SyntaxKind.TemplateMiddle ? 2 : 1);
69
+ u.push(r), d.push(i), f.push(a(l.slice(r, i)));
70
+ }
71
+ let p = s.map((e) => e.text);
72
+ return {
73
+ node: r,
74
+ kind: i,
75
+ strings: p,
76
+ expressions: t.isNoSubstitutionTemplateLiteral(r.template) ? [] : r.template.templateSpans.map((e) => e.expression),
77
+ declarationFile: o,
78
+ parsed: e(p, { svg: i === "svg" }),
79
+ position: (e) => u[e.string] + f[e.string].toSource(e.offset),
80
+ locationAt: (e) => {
81
+ let t = u.findIndex((t, n) => e >= t && e <= d[n]);
82
+ return t < 0 ? void 0 : {
83
+ string: t,
84
+ offset: f[t].toCooked(e - u[t])
85
+ };
86
+ }
87
+ };
88
+ }, d = [
89
+ "html",
90
+ "svg",
91
+ "global",
92
+ "svgGlobal",
93
+ "htmlElements",
94
+ "svgElements",
95
+ "omitted",
96
+ "ifDefined",
97
+ "live",
98
+ "bind",
99
+ "preventDefault",
100
+ "boolean",
101
+ "listener",
102
+ "ref",
103
+ "component"
104
+ ], f = /* @__PURE__ */ new WeakMap(), p = (e, t, n) => {
105
+ let r = f.get(t);
106
+ if (r || f.set(t, r = /* @__PURE__ */ new Map()), r.has(n)) return r.get(n);
107
+ let i = null, a = t.getSymbolAtLocation(n), o = a && t.getExportsOfModule(a).find((e) => e.name === "TemplateTypeMap");
108
+ if (o) {
109
+ let n = t.getDeclaredTypeOfSymbol(o.flags & e.SymbolFlags.Alias ? t.getAliasedSymbol(o) : o), r = {};
110
+ for (let e of d) {
111
+ let i = t.getPropertyOfType(n, e);
112
+ i && (r[e] = t.getTypeOfSymbol(i));
113
+ }
114
+ d.every((e) => r[e]) && (i = r);
115
+ }
116
+ return r.set(n, i), i;
117
+ }, m = (e, t, n, r = {}) => {
118
+ let i = r.types === !1 ? void 0 : t.getTypeChecker(), a = [], o = (e, t, r, i, o) => {
119
+ let { line: s, character: c } = n.getLineAndCharacterOfPosition(i);
120
+ a.push({
121
+ code: e,
122
+ severity: t,
123
+ message: r,
124
+ file: n.fileName,
125
+ start: i,
126
+ length: Math.max(o - i, 1),
127
+ line: s + 1,
128
+ column: c + 1
129
+ });
130
+ }, s = (e, t, r, i) => o(e, t, r, i.getStart(n), i.getEnd());
131
+ for (let t of l(e, n, i, r)) {
132
+ let n = (e) => [t.position(e.start), t.position(e.end)];
133
+ for (let e of t.parsed.problems) o(e.code, e.severity, e.message, ...n(e.range));
134
+ b(e, t, s);
135
+ let r = i && t.declarationFile ? p(e, i, t.declarationFile) : null;
136
+ i && r && new C(e, i, r, t, o, s).nodes(t.parsed.nodes);
137
+ }
138
+ return x(e, n, i, r, s), a.sort((e, t) => e.start - t.start || e.code.localeCompare(t.code));
139
+ }, h = (e, t) => {
140
+ for (; e.isParenthesizedExpression(t) || e.isAsExpression(t) || e.isSatisfiesExpression(t) || e.isNonNullExpression(t);) t = t.expression;
141
+ return t;
142
+ }, g = (e, t, n = []) => {
143
+ let r = h(e, t);
144
+ if (n.push(r), e.isConditionalExpression(r)) g(e, r.whenTrue, n), g(e, r.whenFalse, n);
145
+ else if (e.isBinaryExpression(r)) {
146
+ let t = r.operatorToken.kind;
147
+ t === e.SyntaxKind.AmpersandAmpersandToken ? g(e, r.right, n) : (t === e.SyntaxKind.BarBarToken || t === e.SyntaxKind.QuestionQuestionToken) && (g(e, r.left, n), g(e, r.right, n));
148
+ } else if (e.isArrayLiteralExpression(r)) for (let t of r.elements) g(e, e.isSpreadElement(t) ? t.expression : t, n);
149
+ else if (e.isCallExpression(r)) {
150
+ let t = h(e, r.expression), i = e.isPropertyAccessExpression(t) ? t.name.text : e.isIdentifier(t) ? t.text : "", a = i === "map" || i === "flatMap" ? e.isPropertyAccessExpression(t) ? [r.arguments[0]] : [r.arguments[1]] : i === "repeat" ? [r.arguments[r.arguments.length - 1]] : i === "when" ? [r.arguments[1], r.arguments[2]] : i === "guard" ? [r.arguments[1]] : i === "cache" ? [r.arguments[0]] : i === "key" ? [r.arguments[1]] : [];
151
+ for (let t of a) t && _(e, t, n);
152
+ }
153
+ return n;
154
+ }, _ = (e, t, n) => {
155
+ let r = h(e, t);
156
+ if (!e.isArrowFunction(r) && !e.isFunctionExpression(r)) {
157
+ g(e, r, n);
158
+ return;
159
+ }
160
+ if (!e.isBlock(r.body)) {
161
+ g(e, r.body, n);
162
+ return;
163
+ }
164
+ let i = (t) => {
165
+ e.isReturnStatement(t) && t.expression ? g(e, t.expression, n) : e.isFunctionLike(t) || e.forEachChild(t, i);
166
+ };
167
+ i(r.body);
168
+ }, v = (e, t, n) => {
169
+ for (let r of g(e, t)) {
170
+ if (!e.isNoSubstitutionTemplateLiteral(r) && !e.isTemplateExpression(r)) continue;
171
+ let t = e.isNoSubstitutionTemplateLiteral(r) ? r.text : [r.head.text, ...r.templateSpans.map((e) => e.literal.text)].join("");
172
+ i.test(t) && n("COSSACK_TEMPLATE_UNTAGGED_MARKUP", "error", "This template literal renders as escaped text, not markup. Tag it: html`...`.", r);
173
+ }
174
+ }, y = (e) => e.getText().replace(/\s+/g, ""), b = (e, t, n) => {
175
+ t.parsed.bindings.forEach((r, i) => {
176
+ let a = t.expressions[i];
177
+ if (a) {
178
+ if (r.kind === "node") v(e, a, n);
179
+ else if (r.kind === "tag" && r.closing) {
180
+ let e = t.expressions[r.component.binding];
181
+ e && y(e) !== y(a) && n("COSSACK_TEMPLATE_COMPONENT_CLOSE_MISMATCH", "error", `This closing tag binds ${a.getText()}, but its opening tag binds ${e.getText()}. Close a component tag with the same class.`, a);
182
+ }
183
+ }
184
+ });
185
+ }, x = (e, t, n, r, i) => {
186
+ let a = (c) => {
187
+ if (e.isCallExpression(c) && c.arguments.length > 0 && s(e, c.expression, n, r)?.name === "component") {
188
+ c.arguments[2] && v(e, c.arguments[2], i);
189
+ let a = h(e, c.arguments[0]), s = e.isExpressionWithTypeArguments(a) ? a.expression : a, l = n ? !!(o(e, n, s)?.flags ?? 0) && !!(o(e, n, s).flags & e.SymbolFlags.Class) : e.isIdentifier(s) && /^[A-Z]/.test(s.text);
190
+ if (r.hints !== !1 && l) {
191
+ let e = s.getText(t);
192
+ i("COSSACK_TEMPLATE_COMPONENT_CALL", "hint", `Write this as a component tag: <\${${e}} ...>...</\${${e}}>. Keep component() for classes chosen at runtime.`, c.expression);
193
+ }
194
+ }
195
+ e.forEachChild(c, a);
196
+ };
197
+ a(t);
198
+ }, S = /^\s*[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[-+]?\d+)?\s*$/i, C = class {
199
+ ts;
200
+ checker;
201
+ types;
202
+ template;
203
+ report;
204
+ reportNode;
205
+ constructor(e, t, n, r, i, a) {
206
+ this.ts = e, this.checker = t, this.types = n, this.template = r, this.report = i, this.reportNode = a;
207
+ }
208
+ nodes(e) {
209
+ for (let t of e) t.type === "element" ? this.element(t) : t.type === "component" && this.component(t), (t.type === "element" || t.type === "component") && this.nodes(t.children);
210
+ }
211
+ propertyType(e, t) {
212
+ let n = this.checker.getPropertyOfType(e, t);
213
+ if (n) return this.checker.getTypeOfSymbol(n);
214
+ for (let n of this.checker.getIndexInfosOfType(e)) if (this.checker.isTypeAssignableTo(this.checker.getStringLiteralType(t), n.keyType)) return n.type;
215
+ }
216
+ propertySymbol(e, t) {
217
+ return this.checker.getPropertyOfType(e, t);
218
+ }
219
+ parts(e) {
220
+ return e.isUnion() ? e.types : [e];
221
+ }
222
+ isAny(e) {
223
+ return (e.flags & this.ts.TypeFlags.Any) !== 0;
224
+ }
225
+ accepts(e, t) {
226
+ return this.checker.isTypeAssignableTo(t, e);
227
+ }
228
+ acceptsValue(e, t, n, r = !1) {
229
+ if (this.isAny(t)) return !0;
230
+ let i = r && this.accepts(e, this.checker.getStringType()), a = this.ts.TypeFlags.NumberLike | this.ts.TypeFlags.BigIntLike;
231
+ return this.parts(t).every((t) => this.accepts(e, t) || n.some((e) => this.accepts(e, t)) || i && (t.flags & a) !== 0);
232
+ }
233
+ show(e) {
234
+ return this.checker.typeToString(e, void 0, this.ts.TypeFormatFlags.NoTruncation);
235
+ }
236
+ showAccepted(e) {
237
+ let { omitted: t, ifDefined: n, preventDefault: r, live: i, bind: a } = this.types, o = this.parts(e).filter((e) => ![
238
+ t,
239
+ n,
240
+ r,
241
+ i,
242
+ a
243
+ ].some((t) => this.accepts(t, e)));
244
+ if (o.length === 0) return this.show(e);
245
+ let s = o.map((e) => this.show(e));
246
+ return s.includes("true") && s.includes("false") && (s.splice(s.indexOf("true"), 1), s.splice(s.indexOf("false"), 1, "boolean")), s.join(" | ");
247
+ }
248
+ wholeExpression(e) {
249
+ return e.bindings.length === 1 && e.segments.every((e) => e === "") ? this.template.expressions[e.bindings[0]] : void 0;
250
+ }
251
+ range(e, t = "name") {
252
+ let n = t === "name" ? e.nameRange : e.range;
253
+ return [this.template.position(n.start), this.template.position(n.end)];
254
+ }
255
+ element(e) {
256
+ if (e.namespace === "mathml") return;
257
+ let t = e.namespace === "svg", n = t ? this.types.svg : this.types.html, r = t ? this.svgTag(e.tag) : e.name, i = r !== void 0 && !!this.propertyType(n, r), a = i ? this.propertyType(n, r) : t ? this.types.svgGlobal : this.types.global, o = r === void 0 ? void 0 : this.propertyType(t ? this.types.svgElements : this.types.htmlElements, r);
258
+ for (let t of e.attributes) this.elementAttribute(e, t, a, i, o);
259
+ }
260
+ svgTag(e) {
261
+ if (this.checker.getPropertyOfType(this.types.svg, e)) return e;
262
+ let t = e.toLowerCase();
263
+ return this.checker.getPropertiesOfType(this.types.svg).find((e) => e.name.toLowerCase() === t)?.name;
264
+ }
265
+ elementAttribute(e, t, n, r, i) {
266
+ let { name: a } = t;
267
+ if (a === "...") return;
268
+ let o = this.wholeExpression(t), s = a[0];
269
+ if ((s === "." || s === "?" || s === "@") && !o) return;
270
+ let c = `<${e.tag}>`, l = e.namespace === "html", u = (e) => r ? this.report("COSSACK_TEMPLATE_UNKNOWN_ATTRIBUTE", "error", `${c} has no attribute "${e}".`, ...this.range(t)) : this.report("COSSACK_TEMPLATE_UNKNOWN_ELEMENT_ATTRIBUTE", "warning", `${c} is not a known element, so only global attributes are known. "${e}" is not one.`, ...this.range(t));
271
+ if (a === "ref") {
272
+ o ? this.value(o, this.types.ref, [], "COSSACK_TEMPLATE_REF_BINDING", "ref takes a ref object or a callback") : this.report("COSSACK_TEMPLATE_REF_BINDING", "error", "ref takes a ref object or a callback: ref=${ref}.", ...this.range(t, "all"));
273
+ return;
274
+ }
275
+ if (s === ".") {
276
+ let e = a.slice(1), s = this.propertyType(n, a);
277
+ if (s) {
278
+ this.value(o, s, [this.types.omitted], "COSSACK_TEMPLATE_PROPERTY_TYPE", `${c} property ${e}`);
279
+ return;
280
+ }
281
+ if (!i) return;
282
+ let l = this.propertySymbol(i, e);
283
+ if (!l) {
284
+ this.report("COSSACK_TEMPLATE_UNKNOWN_PROPERTY", r ? "error" : "warning", `${c} has no property "${e}".`, ...this.range(t));
285
+ return;
286
+ }
287
+ let u = l.valueDeclaration ?? l.declarations?.[0];
288
+ if (u && (this.ts.getCombinedModifierFlags(u) & this.ts.ModifierFlags.Readonly) !== 0) {
289
+ this.report("COSSACK_TEMPLATE_PROPERTY_READONLY", "error", `${c} property ${e} is read-only.`, ...this.range(t));
290
+ return;
291
+ }
292
+ let d = e === "value" || e === "checked" ? [
293
+ this.types.omitted,
294
+ this.types.live,
295
+ this.types.bind
296
+ ] : [this.types.omitted, this.types.live];
297
+ this.value(o, this.checker.getTypeOfSymbol(l), d, "COSSACK_TEMPLATE_PROPERTY_TYPE", `${c} property ${e}`);
298
+ return;
299
+ }
300
+ if (s === "@") {
301
+ let e = this.propertyType(n, a);
302
+ this.value(o, e ?? this.types.listener, [this.types.omitted, this.types.preventDefault], "COSSACK_TEMPLATE_EVENT_HANDLER", e ? `The ${a} handler of ${c}` : `The ${a} handler`);
303
+ return;
304
+ }
305
+ if (s === "?") {
306
+ let e = l ? a.slice(1).toLowerCase() : a.slice(1);
307
+ if (!this.propertyType(n, e) && !this.propertyType(n, `?${e}`)) {
308
+ u(e);
309
+ return;
310
+ }
311
+ this.value(o, this.types.boolean, [], "COSSACK_TEMPLATE_BOOLEAN_BINDING", `${a} on ${c}`, " Use a boolean, such as !!value.");
312
+ return;
313
+ }
314
+ if (l && /^on[a-z]/i.test(a)) {
315
+ this.report("COSSACK_TEMPLATE_EVENT_ATTRIBUTE", "error", `"${a}" is an inline event attribute. Bind a listener instead: @${a.slice(2).toLowerCase()}=\${handler}.`, ...this.range(t));
316
+ return;
317
+ }
318
+ if (!/^[A-Za-z_]/.test(a)) return;
319
+ let d = l ? a.toLowerCase() : a, f = this.propertyType(n, d);
320
+ if (!f) {
321
+ u(d);
322
+ return;
323
+ }
324
+ t.bindings.length === 0 ? this.staticValue(t, f, `${a} on ${c}`) : o && this.value(o, f, [
325
+ this.types.omitted,
326
+ this.types.ifDefined,
327
+ this.types.live
328
+ ], "COSSACK_TEMPLATE_ATTRIBUTE_TYPE", `${a} on ${c}`, "", !0);
329
+ }
330
+ value(e, t, n, r, i, a = "", o = !1) {
331
+ let s = this.checker.getTypeAtLocation(e);
332
+ this.acceptsValue(t, s, n, o) || this.reportNode(r, "error", `${i} takes ${this.showAccepted(t)}, not ${this.show(s)}.${a}`, e);
333
+ }
334
+ staticValue(e, t, r) {
335
+ let { checker: i } = this;
336
+ if (!e.hasValue) {
337
+ if (this.accepts(t, i.getTrueType()) || this.accepts(t, i.getStringLiteralType(""))) return;
338
+ this.report("COSSACK_TEMPLATE_ATTRIBUTE_TYPE", "error", `${r} needs a value: it takes ${this.showAccepted(t)}.`, ...this.range(e));
339
+ return;
340
+ }
341
+ let a = n(e.segments[0]);
342
+ if (this.accepts(t, i.getStringLiteralType(a)) || this.accepts(t, i.getStringLiteralType(a.toLowerCase())) || S.test(a) && this.accepts(t, i.getNumberType())) return;
343
+ let o = this.accepts(t, i.getTrueType());
344
+ if (o && (a === "" || a.toLowerCase() === e.name.toLowerCase())) return;
345
+ let s = o && a.toLowerCase() === "false" ? " A boolean attribute is on whenever it is present; leave it out, or bind ?name=${false}." : "";
346
+ this.report("COSSACK_TEMPLATE_ATTRIBUTE_TYPE", "error", `${r} takes ${this.showAccepted(t)}, not "${a}".${s}`, ...this.range(e, "all"));
347
+ }
348
+ component(e) {
349
+ let t = this.template.expressions[e.binding];
350
+ if (!t) return;
351
+ let n = this.checker.getTypeAtLocation(t);
352
+ if (this.isAny(n)) return;
353
+ let r = this.checker.getSignaturesOfType(n, this.ts.SignatureKind.Construct), i = r[0] && this.checker.getReturnTypeOfSignature(r[0]);
354
+ if (!(i && (this.accepts(this.types.component, i) || this.checker.getPropertyOfType(i, "render") && this.checker.getPropertyOfType(i, "props")))) {
355
+ this.reportNode("COSSACK_TEMPLATE_NOT_A_COMPONENT", "error", `A component tag binds a component class; ${t.getText()} is ${this.show(n)}.`, t);
356
+ return;
357
+ }
358
+ let a = this.checker.getPropertyOfType(i, "props");
359
+ if (!a) return;
360
+ let o = this.checker.getNonNullableType(this.checker.getTypeOfSymbol(a));
361
+ if (this.isAny(o)) return;
362
+ let s = t.getText(), c = /* @__PURE__ */ new Set(), l = !1;
363
+ for (let t of e.attributes) {
364
+ if (t.name === "...") {
365
+ l = !0;
366
+ continue;
367
+ }
368
+ c.add(t.name), this.prop(s, o, t);
369
+ }
370
+ if (l) return;
371
+ let u = this.checker.getPropertiesOfType(o).filter((e) => !(e.flags & this.ts.SymbolFlags.Optional) && !c.has(e.name)).filter((e) => !this.accepts(this.checker.getTypeOfSymbol(e), this.checker.getUndefinedType())).map((e) => e.name);
372
+ u.length > 0 && this.report("COSSACK_TEMPLATE_MISSING_PROP", "error", `<\${${s}}> needs ${u.map((e) => `"${e}"`).join(", ")}.`, this.template.position(e.startTag.start), this.template.position(e.startTag.end));
373
+ }
374
+ prop(e, t, r) {
375
+ let i = this.propertyType(t, r.name);
376
+ if (!i) {
377
+ this.report("COSSACK_TEMPLATE_UNKNOWN_PROP", "error", `"${r.name}" is not a prop of ${e}.`, ...this.range(r));
378
+ return;
379
+ }
380
+ let a = this.wholeExpression(r);
381
+ if (a) {
382
+ this.value(a, i, [], "COSSACK_TEMPLATE_PROP_TYPE", `The ${r.name} prop of ${e}`);
383
+ return;
384
+ }
385
+ let { checker: o } = this, s = r.hasValue ? r.bindings.length === 0 ? o.getStringLiteralType(n(r.segments[0])) : o.getStringType() : o.getTrueType();
386
+ if (this.accepts(i, s)) return;
387
+ let c = r.hasValue ? r.bindings.length === 0 ? `"${n(r.segments[0])}"` : "text" : "true (a bare attribute)", l = r.hasValue && !this.accepts(i, o.getStringType()) && !this.parts(i).some((e) => e.isStringLiteral()) ? ` Text attributes pass strings; bind other values: ${r.name}=\${value}.` : "";
388
+ this.report("COSSACK_TEMPLATE_PROP_TYPE", "error", `The ${r.name} prop of ${e} takes ${this.showAccepted(i)}, not ${c}.${l}`, ...this.range(r, "all"));
389
+ }
390
+ }, w = (e, n, r, i, a = {}) => {
391
+ let o = n.getTypeChecker(), s = l(e, r, o, a).filter((e) => e.locationAt(i) !== void 0).sort((e, t) => e.node.getEnd() - e.node.getStart() - (t.node.getEnd() - t.node.getStart()))[0];
392
+ if (!s?.declarationFile) return;
393
+ let c = p(e, o, s.declarationFile);
394
+ if (!c) return;
395
+ let u = t(s.strings, s.locationAt(i), { svg: s.kind === "svg" }), d = (e) => ({
396
+ start: i - e.length,
397
+ length: e.length
398
+ }), f = (t) => o.typeToString(t, void 0, e.TypeFormatFlags.NoTruncation), m = (e) => o.getPropertiesOfType(e).map((e) => e.name), h = (e, t) => {
399
+ let n = o.getPropertyOfType(e, t);
400
+ return n && o.getTypeOfSymbol(n);
401
+ }, g = (e) => e.sort((e, t) => e.name.localeCompare(t.name));
402
+ if (u.kind === "tag-name") return {
403
+ entries: g(m(u.namespace === "svg" ? c.svg : c.html).map((e) => ({
404
+ name: e,
405
+ kind: "element"
406
+ }))),
407
+ replace: d(u.prefix)
408
+ };
409
+ if (u.kind === "end-tag") return u.open ? {
410
+ entries: [{
411
+ name: u.open.tag,
412
+ kind: "end-tag"
413
+ }],
414
+ replace: d(u.prefix)
415
+ } : void 0;
416
+ if (u.kind !== "attribute-name" && u.kind !== "attribute-value") return;
417
+ let _, v, y = !1, b = u.tag;
418
+ if (b.component) {
419
+ let t = s.expressions[b.binding], n = t && o.getSignaturesOfType(o.getTypeAtLocation(t), e.SignatureKind.Construct)[0], r = n && h(o.getReturnTypeOfSignature(n), "props");
420
+ _ = r && o.getNonNullableType(r), y = !0;
421
+ } else if (b.namespace !== "mathml") {
422
+ let e = b.namespace === "svg", t = e ? m(c.svg).find((e) => e.toLowerCase() === b.tag.toLowerCase()) : b.name;
423
+ _ = t && h(e ? c.svg : c.html, t) || (e ? c.svgGlobal : c.global), v = t ? h(e ? c.svgElements : c.htmlElements, t) : void 0;
424
+ }
425
+ if (!_) return;
426
+ if (u.kind === "attribute-value") {
427
+ let e = !b.component && b.namespace === "html", t = h(_, e ? u.name.toLowerCase() : u.name);
428
+ if (!t) return;
429
+ let n = (t.isUnion() ? t.types : [t]).filter((e) => e.isStringLiteral()).map((e) => e.value);
430
+ return n.length ? {
431
+ entries: g(n.map((e) => ({
432
+ name: e,
433
+ kind: "value"
434
+ }))),
435
+ replace: d(u.prefix)
436
+ } : void 0;
437
+ }
438
+ let x = new Set(u.attributes.map((e) => y ? e : e.toLowerCase())), S = u.prefix[0], C = [], w = (e, t, n) => {
439
+ x.has(y ? e : e.toLowerCase()) || C.push({
440
+ name: e,
441
+ kind: t,
442
+ ...n ? { type: f(n) } : {}
443
+ });
444
+ };
445
+ if (y) for (let e of o.getPropertiesOfType(_)) w(e.name, "prop", o.getTypeOfSymbol(e));
446
+ else if (S === "@") for (let e of m(_)) e.startsWith("@") && w(e, "event");
447
+ else if (S === ".") for (let t of v ? o.getPropertiesOfType(v) : []) {
448
+ let n = t.valueDeclaration ?? t.declarations?.[0];
449
+ !(n && (e.getCombinedModifierFlags(n) & e.ModifierFlags.Readonly) !== 0) && t.flags & e.SymbolFlags.Property && w(`.${t.name}`, "property", o.getTypeOfSymbol(t));
450
+ }
451
+ else if (S === "?") for (let e of m(_)) {
452
+ let t = h(_, e);
453
+ !/^[.?@]/.test(e) && t && o.isTypeAssignableTo(o.getTrueType(), t) && w(`?${e}`, "boolean");
454
+ }
455
+ else {
456
+ for (let e of m(_)) /^[.?@]/.test(e) || w(e, "attribute", h(_, e));
457
+ w("ref", "attribute"), w("...", "attribute");
458
+ }
459
+ return {
460
+ entries: g(C),
461
+ replace: d(u.prefix)
462
+ };
463
+ };
464
+ //#endregion
465
+ export { m as checkTemplates, w as completeTemplate, l as findTemplates, e as parseTemplate, t as templateContextAt };
@@ -57,8 +57,9 @@ export declare function repeat<T>(items: readonly T[], keyFn: (item: T, index: n
57
57
  */
58
58
  export declare const classMap: (classInfo: Record<string, string | boolean | number>) => string;
59
59
  /**
60
- * Generates a style string from an object.
61
- * Usage: style="${styleMap({ color: 'red' })}"
60
+ * Generates a style string from an object. Keys may be CSS names
61
+ * (`'font-size'`, `'--gap'`) or camelCase (`fontSize`).
62
+ * Usage: style="${styleMap({ color: 'red', backgroundColor: 'white' })}"
62
63
  */
63
64
  export declare const styleMap: (styleInfo: Record<string, string | undefined | null>) => string;
64
65
  /**
@@ -129,12 +130,13 @@ export declare const preventDefault: (handler: EventListener, options?: {
129
130
  * the Lit-faithful `ifDefined` directive.
130
131
  *
131
132
  * By default a plain Cossack attribute binding `href="${url}"` already omits the
132
- * attribute for `null`/`undefined`/`false`, and renders everything else. The
133
- * difference `ifDefined` makes is two-fold:
133
+ * attribute for `null`/`undefined` and renders everything else; `false` renders
134
+ * as `"false"` (only boolean attributes such as `disabled` are omitted for
135
+ * `false`). The difference `ifDefined` makes is two-fold:
134
136
  *
135
- * 1. Only `undefined` drops the attribute — `null` is rendered as `"null"`,
136
- * `false` as `"false"` (useful for data attributes where you want the
137
- * literal string rather than an omission).
137
+ * 1. Only `undefined` drops the attribute — `null` is rendered as `"null"`
138
+ * (useful for data attributes where you want the literal string rather
139
+ * than an omission).
138
140
  * 2. When the value transitions from defined back to `undefined` on a
139
141
  * re-render, the attribute is explicitly removed.
140
142
  *
@@ -50,8 +50,8 @@ var e = class {
50
50
  function l(e, n, r) {
51
51
  return r === void 0 ? new t(e, (e, t) => t, n) : new t(e, n, r);
52
52
  }
53
- var 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 n(e, t), p = (e, t) => new r(e, t);
54
- function m(e, t) {
53
+ var u = (e) => Object.keys(e).filter((t) => e[t]).join(" "), d = (e) => e.includes("-") ? e : e.replace(/(?:^(webkit|moz|ms|o)|)(?=[A-Z])/g, "-$&").toLowerCase(), f = (e) => Object.keys(e).filter((t) => e[t] != null).map((t) => `${d(t)}:${e[t]}`).join(";"), p = (e, t) => new n(e, t), m = (e, t) => new r(e, t);
54
+ function h(e, t) {
55
55
  let n = e;
56
56
  if (!t.includes(".")) return n?.[t];
57
57
  let r = t.split("."), i = n?.[r[0]];
@@ -61,7 +61,7 @@ function m(e, t) {
61
61
  }
62
62
  return i;
63
63
  }
64
- function h(e, t, n) {
64
+ function g(e, t, n) {
65
65
  let r = e;
66
66
  if (!r) return;
67
67
  if (!t.includes(".")) {
@@ -77,17 +77,17 @@ function h(e, t, n) {
77
77
  }
78
78
  typeof a == "object" && a && (a[i[i.length - 1]] = n);
79
79
  }
80
- var g = (e, t) => new i(e, t?.novalidate ?? !0), _ = (e) => new a(e), v = (e, t) => new o(e, t), y = (e) => new s(e), b = (e, t, n) => e ? t(e) : n?.(e), x = (e, t, n) => {
80
+ var _ = (e, t) => new i(e, t?.novalidate ?? !0), v = (e) => new a(e), y = (e, t) => new o(e, t), b = (e) => new s(e), x = (e, t, n) => e ? t(e) : n?.(e), S = (e, t, n) => {
81
81
  for (let n = 0; n < t.length; n++) {
82
82
  let [r, i] = t[n];
83
83
  if (r === e) return i(e, n);
84
84
  }
85
85
  return n?.();
86
- }, S = (e, t) => Array.from(e, t), C = (e, t, n) => {
86
+ }, C = (e, t) => Array.from(e, t), w = (e, t, n) => {
87
87
  let r = Array.from(e), i = [], a = typeof n == "function" ? n : () => n;
88
88
  for (let e = 0; e < r.length; e++) i.push(t(r[e], e)), e < r.length - 1 && i.push(a(e));
89
89
  return i;
90
- }, w = (e, t, n = 1) => {
90
+ }, T = (e, t, n = 1) => {
91
91
  let r = t === void 0 ? 0 : e, i = t === void 0 ? e : t, a = [];
92
92
  if (n === 0) return a;
93
93
  if (n > 0) for (let e = r; e < i; e += n) a.push(e);
@@ -95,4 +95,4 @@ var g = (e, t) => new i(e, t?.novalidate ?? !0), _ = (e) => new a(e), v = (e, t)
95
95
  return a;
96
96
  };
97
97
  //#endregion
98
- export { r as BindResult, s as CacheResult, o as GuardResult, a as IfDefinedResult, n as KeyResult, e as LiveResult, i as PreventDefaultResult, t as RepeatResult, p as bind, y as cache, x as choose, u as classMap, v as guard, _ as ifDefined, C as join, f as key, c as live, S as map, g as preventDefault, w as range, l as repeat, m as resolveField, h as setField, d as styleMap, b as when };
98
+ export { r as BindResult, s as CacheResult, o as GuardResult, a as IfDefinedResult, n as KeyResult, e as LiveResult, i as PreventDefaultResult, t as RepeatResult, m as bind, b as cache, S as choose, u as classMap, y as guard, v as ifDefined, w as join, p as key, c as live, C as map, _ as preventDefault, T as range, l as repeat, h as resolveField, g as setField, f as styleMap, x as when };
package/dist/index.d.ts CHANGED
@@ -5,3 +5,5 @@ export * from './context.js';
5
5
  export * from './directives.js';
6
6
  export { css, unsafeCSS, CSSResult } from './css.js';
7
7
  export type { CSSResultGroup } from './css.js';
8
+ export { collectComponentStyles, renderComponentStyles } from './style-runtime.js';
9
+ export type { ComponentStyle } from './style-runtime.js';
package/dist/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  // @ts-self-types="./index.d.ts"
2
- import { isComponentResult as e } from "./component.js";
3
- import { CSSResult as t, css as n, unsafeCSS as r } from "./css.js";
4
- import { CossackElement as i, instanceStack as a, popCurrentInstance as o, pushCurrentInstance as s } from "./cossack-element.js";
5
- import { BindResult as c, CacheResult as l, GuardResult as u, IfDefinedResult as d, KeyResult as f, LiveResult as p, PreventDefaultResult as m, RepeatResult as h, bind as g, cache as _, choose as v, classMap as y, guard as b, ifDefined as x, join as S, key as C, live as w, map as T, preventDefault as E, range as D, repeat as O, resolveField as k, setField as A, styleMap as j, when as M } from "./directives.js";
6
- import { TemplateResult as N, UnsafeHTMLResult as P, captureRenderDrafts as F, component as I, escapeHtml as L, html as R, hydrate as z, isTemplateResult as B, isUnsafeHTML as V, nothing as H, render as U, renderToString as W, svg as G, unsafeHTML as K } from "./cossack-html.js";
7
- import { createContext as q } from "./context.js";
8
- export { c as BindResult, t as CSSResult, l as CacheResult, i as CossackElement, u as GuardResult, d as IfDefinedResult, f as KeyResult, p as LiveResult, m as PreventDefaultResult, h as RepeatResult, N as TemplateResult, P as UnsafeHTMLResult, g as bind, _ as cache, F as captureRenderDrafts, v as choose, y as classMap, I as component, q as createContext, n as css, L as escapeHtml, b as guard, R as html, z as hydrate, x as ifDefined, a as instanceStack, e as isComponentResult, B as isTemplateResult, V as isUnsafeHTML, S as join, C as key, w as live, T as map, H as nothing, o as popCurrentInstance, E as preventDefault, s as pushCurrentInstance, D as range, U as render, W as renderToString, O as repeat, k as resolveField, A as setField, j as styleMap, G as svg, r as unsafeCSS, K as unsafeHTML, M as when };
2
+ import { collectComponentStyles as e, renderComponentStyles as t } from "./style-runtime.js";
3
+ import { isComponentResult as n } from "./component.js";
4
+ import { COMPONENT_RANGE as r, CossackElement as i, getComponentRange as a, instanceStack as o, popCurrentInstance as s, pushCurrentInstance as c, setComponentRange as l } from "./cossack-element.js";
5
+ import { BindResult as u, CacheResult as d, GuardResult as f, IfDefinedResult as p, KeyResult as m, LiveResult as h, PreventDefaultResult as g, RepeatResult as _, bind as v, cache as y, choose as b, classMap as x, guard as S, ifDefined as C, join as w, key as T, live as E, map as D, preventDefault as O, range as k, repeat as A, resolveField as j, setField as M, styleMap as N, when as P } from "./directives.js";
6
+ import { TemplateResult as F, UnsafeHTMLResult as I, captureRenderDrafts as L, component as R, escapeHtml as z, html as B, hydrate as V, isTemplateResult as H, isUnsafeHTML as U, nothing as W, render as G, renderToString as K, svg as q, unsafeHTML as J } from "./cossack-html.js";
7
+ import { createContext as Y } from "./context.js";
8
+ import { CSSResult as X } from "./css-result.js";
9
+ import { css as Z, unsafeCSS as Q } from "./css.js";
10
+ export { u as BindResult, r as COMPONENT_RANGE, X as CSSResult, d as CacheResult, i as CossackElement, f as GuardResult, p as IfDefinedResult, m as KeyResult, h as LiveResult, g as PreventDefaultResult, _ as RepeatResult, F as TemplateResult, I as UnsafeHTMLResult, v as bind, y as cache, L as captureRenderDrafts, b as choose, x as classMap, e as collectComponentStyles, R as component, Y as createContext, Z as css, z as escapeHtml, a as getComponentRange, S as guard, B as html, V as hydrate, C as ifDefined, o as instanceStack, n as isComponentResult, H as isTemplateResult, U as isUnsafeHTML, w as join, T as key, E as live, D as map, W as nothing, s as popCurrentInstance, O as preventDefault, c as pushCurrentInstance, k as range, G as render, t as renderComponentStyles, K as renderToString, A as repeat, j as resolveField, l as setComponentRange, M as setField, N as styleMap, q as svg, Q as unsafeCSS, J as unsafeHTML, P as when };
@@ -1 +1,23 @@
1
+ /**
2
+ * Minify a rendered HTML document for production responses without changing
3
+ * how it renders.
4
+ *
5
+ * The markup is read with the renderer's shared tokenizer, so text, attribute
6
+ * values, comments and raw-text elements are told apart exactly as the
7
+ * browser parses them:
8
+ * - text: each run of whitespace collapses to one space, or to one line
9
+ * break when it holds one. Whitespace between tags is collapsed, not
10
+ * removed, so inline elements keep their spacing. It is removed only
11
+ * before <html>, between <html>, <head> and <body>, and inside <head>.
12
+ * - <script>, <style>, <textarea> and <title> text is kept as is, and so is
13
+ * everything inside <pre>, <listing>, <xmp>, <plaintext> and elements
14
+ * whose `style` or Tailwind class preserves whitespace. Whitespace kept by
15
+ * a stylesheet cannot be seen here; mark such elements with one of those.
16
+ * - comments are removed, except conditional comments and hydration markers.
17
+ * - tags: simple attribute values lose their quotes, `class` whitespace
18
+ * collapses, default `type` attributes and void-element slashes go away.
19
+ * Other attribute values are kept byte for byte. Start tags of elements
20
+ * whose content is kept as written are kept as written too, apart from a
21
+ * default `type`.
22
+ */
1
23
  export declare function minifyHtml(html: string): string;