@fluixi/template-parser 1.0.0-alpha.108 → 1.0.0-alpha.109

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,415 @@
1
+ import {
2
+ checkAccessibility
3
+ } from "./chunk-DK3MEEE6.mjs";
4
+ import {
5
+ setParents
6
+ } from "./chunk-R4NGOY2K.mjs";
7
+ import {
8
+ CORE_DIRECTIVES,
9
+ resolveAttribute
10
+ } from "./chunk-IP5F3OXX.mjs";
11
+ import {
12
+ Reader
13
+ } from "./chunk-UGZDQT22.mjs";
14
+ import {
15
+ RAW_TEXT_ELEMENTS,
16
+ SVG_TAGS,
17
+ VOID_ELEMENTS,
18
+ isComponentTag
19
+ } from "./chunk-6YLLM7OB.mjs";
20
+
21
+ // src/parser.ts
22
+ var NAME_CHAR = /[A-Za-z0-9\-_:@.?]/;
23
+ var Parser = class {
24
+ constructor(pieces, opts) {
25
+ this.diagnostics = [];
26
+ this.r = new Reader(pieces);
27
+ this.svg = opts.svg ?? false;
28
+ this.directives = opts.directives ?? CORE_DIRECTIVES;
29
+ this.accessibility = opts.accessibility ?? true;
30
+ }
31
+ parse() {
32
+ const start = this.r.pos();
33
+ const children = [];
34
+ while (!this.r.eof()) {
35
+ children.push(...this.parseChildren());
36
+ if (this.r.eof()) break;
37
+ const s = this.r.pos();
38
+ this.consumeCloseTag();
39
+ this.report("stray-close-tag", "error", "Close tag without a matching open tag", this.r.span(s));
40
+ }
41
+ const root = { kind: "Root", children, loc: this.r.span(start) };
42
+ setParents(root);
43
+ this.validateTree(root);
44
+ if (this.accessibility) {
45
+ this.diagnostics.push(...checkAccessibility(root));
46
+ }
47
+ return { root, diagnostics: this.diagnostics };
48
+ }
49
+ /** Cross-node validation that needs the built tree (sibling relationships). */
50
+ validateTree(root) {
51
+ const visit = (nodes) => {
52
+ let prevBranch = null;
53
+ for (const node of nodes) {
54
+ if (node.kind === "Text" && /^\s*$/.test(node.value)) continue;
55
+ if (node.kind === "Element" || node.kind === "Component") {
56
+ const hasElse = node.attributes.some((a) => a.kind === "ElseDirective");
57
+ if (hasElse) {
58
+ const prevHasIf = prevBranch != null && (prevBranch.kind === "Element" || prevBranch.kind === "Component") && prevBranch.attributes.some((a) => a.kind === "IfDirective");
59
+ if (!prevHasIf) {
60
+ this.report("else-without-if", "error", "`else` must immediately follow an element with `if`", node.loc);
61
+ }
62
+ }
63
+ prevBranch = node;
64
+ } else {
65
+ prevBranch = null;
66
+ }
67
+ if ("children" in node && node.children) visit(node.children);
68
+ }
69
+ };
70
+ visit(root.children);
71
+ }
72
+ // content
73
+ /** Parse sibling nodes until EOF or a `</` close the caller must consume. */
74
+ parseChildren() {
75
+ const out = [];
76
+ while (!this.r.eof()) {
77
+ if (this.r.startsWith("</")) break;
78
+ if (this.r.startsWith("<!--")) {
79
+ out.push(this.parseComment());
80
+ continue;
81
+ }
82
+ if (this.isFragmentStart()) {
83
+ out.push(this.parseFragment());
84
+ continue;
85
+ }
86
+ if (this.isDynamicTagStart()) {
87
+ out.push(this.parseDynamicComponent());
88
+ continue;
89
+ }
90
+ if (this.isTagStart()) {
91
+ out.push(this.parseElement());
92
+ continue;
93
+ }
94
+ if (this.r.atHole()) {
95
+ const h = this.r.takeHole();
96
+ const node = { kind: "Expression", hole: h.index, loc: h.loc };
97
+ out.push(node);
98
+ continue;
99
+ }
100
+ const text = this.readText();
101
+ if (text) out.push(text);
102
+ }
103
+ return out;
104
+ }
105
+ isTagStart() {
106
+ return this.r.peek() === "<" && /[A-Za-z]/.test(this.r.peek(1));
107
+ }
108
+ isFragmentStart() {
109
+ return this.r.peek() === "<" && this.r.peek(1) === ">";
110
+ }
111
+ /** `<${Comp} ...>`: a component whose tag is an interpolated expression. */
112
+ isDynamicTagStart() {
113
+ return this.r.peek() === "<" && this.r.holeFollows();
114
+ }
115
+ /** Parse `<${Comp} .../>` or `<${Comp} ...>...</${Comp}>` into a dynamic ComponentNode. */
116
+ parseDynamicComponent() {
117
+ const start = this.r.pos();
118
+ this.r.advance();
119
+ const tagHole = this.r.takeHole().index;
120
+ const attributes = this.parseAttributes(true);
121
+ this.r.skipWhitespace();
122
+ let selfClosing = false;
123
+ let children = [];
124
+ if (this.r.startsWith("/>")) {
125
+ this.r.advance();
126
+ this.r.advance();
127
+ selfClosing = true;
128
+ } else {
129
+ if (this.r.peek() === ">") this.r.advance();
130
+ children = this.parseChildren();
131
+ if (this.r.startsWith("</")) {
132
+ this.r.advance();
133
+ this.r.advance();
134
+ if (this.r.atHole()) this.r.takeHole();
135
+ else this.readName();
136
+ this.r.skipWhitespace();
137
+ if (this.r.peek() === ">") this.r.advance();
138
+ } else {
139
+ this.report("unclosed-tag", "error", "Unclosed dynamic component", this.r.span(start));
140
+ }
141
+ }
142
+ return { kind: "Component", tag: "", tagHole, attributes, children, selfClosing, loc: this.r.span(start) };
143
+ }
144
+ /** A raw text run (preserved verbatim; whitespace is normalized at lowering). */
145
+ readText() {
146
+ const start = this.r.pos();
147
+ let value = "";
148
+ while (!this.r.eof() && !this.r.atHole()) {
149
+ if (this.r.startsWith("</") || this.r.startsWith("<!--") || this.isTagStart() || this.isFragmentStart() || this.isDynamicTagStart()) break;
150
+ value += this.r.advance();
151
+ }
152
+ if (!value) return null;
153
+ return { kind: "Text", value, raw: false, loc: this.r.span(start) };
154
+ }
155
+ parseComment() {
156
+ const start = this.r.pos();
157
+ this.r.advance();
158
+ this.r.advance();
159
+ this.r.advance();
160
+ this.r.advance();
161
+ let value = "";
162
+ while (!this.r.eof() && !this.r.startsWith("-->")) {
163
+ if (this.r.atHole()) {
164
+ this.r.takeHole();
165
+ value += "\0";
166
+ continue;
167
+ }
168
+ value += this.r.advance();
169
+ }
170
+ if (this.r.startsWith("-->")) {
171
+ this.r.advance();
172
+ this.r.advance();
173
+ this.r.advance();
174
+ } else {
175
+ this.report("unclosed-comment", "warning", "Unterminated comment", this.r.span(start));
176
+ }
177
+ return { kind: "Comment", value, loc: this.r.span(start) };
178
+ }
179
+ parseFragment() {
180
+ const start = this.r.pos();
181
+ this.r.advance();
182
+ this.r.advance();
183
+ const children = this.parseChildren();
184
+ if (this.r.startsWith("</")) {
185
+ const name = this.consumeCloseTag();
186
+ if (name) this.report("mismatched-close-tag", "error", `Expected </> to close fragment, got </${name}>`, this.r.span(start));
187
+ } else {
188
+ this.report("unclosed-tag", "error", "Unterminated fragment", this.r.span(start));
189
+ }
190
+ return { kind: "Fragment", children, loc: this.r.span(start) };
191
+ }
192
+ parseElement() {
193
+ const start = this.r.pos();
194
+ this.r.advance();
195
+ const tag = this.readName();
196
+ const component = isComponentTag(tag);
197
+ const attributes = this.parseAttributes(component);
198
+ this.foldEachKey(attributes, start);
199
+ this.r.skipWhitespace();
200
+ const lower = tag.toLowerCase();
201
+ const rawText = !component && RAW_TEXT_ELEMENTS.has(lower);
202
+ const isVoid = !component && VOID_ELEMENTS.has(lower);
203
+ let selfClosing = false;
204
+ let children = [];
205
+ if (this.r.startsWith("/>")) {
206
+ this.r.advance();
207
+ this.r.advance();
208
+ selfClosing = true;
209
+ } else {
210
+ if (this.r.peek() === ">") this.r.advance();
211
+ if (!isVoid) {
212
+ children = rawText ? this.parseRawText(tag) : this.parseChildren();
213
+ if (this.r.startsWith("</")) {
214
+ const name = this.consumeCloseTag();
215
+ if (name && name !== tag) {
216
+ this.report("mismatched-close-tag", "error", `Expected </${tag}>, got </${name}>`, this.r.span(start));
217
+ }
218
+ } else {
219
+ this.report("unclosed-tag", "error", `Unclosed <${tag}>`, this.r.span(start));
220
+ }
221
+ }
222
+ }
223
+ const loc = this.r.span(start);
224
+ if (component) {
225
+ return { kind: "Component", tag, tagHole: null, attributes, children, selfClosing, loc };
226
+ }
227
+ const namespace = this.svg || SVG_TAGS.has(tag) ? "svg" : "html";
228
+ return { kind: "Element", tag, namespace, attributes, children, selfClosing, rawText, loc };
229
+ }
230
+ /** Read a raw-text element body: only holes break the text run. */
231
+ parseRawText(tag) {
232
+ const out = [];
233
+ const close = "</" + tag.toLowerCase();
234
+ let text = "";
235
+ let start = this.r.pos();
236
+ const flush = () => {
237
+ if (text) out.push({ kind: "Text", value: text, raw: true, loc: this.r.span(start) });
238
+ text = "";
239
+ };
240
+ while (!this.r.eof()) {
241
+ if (this.startsWithCloseFor(close)) break;
242
+ if (this.r.atHole()) {
243
+ flush();
244
+ const h = this.r.takeHole();
245
+ out.push({ kind: "Expression", hole: h.index, loc: h.loc });
246
+ start = this.r.pos();
247
+ continue;
248
+ }
249
+ if (!text) start = this.r.pos();
250
+ text += this.r.advance();
251
+ }
252
+ flush();
253
+ return out;
254
+ }
255
+ startsWithCloseFor(close) {
256
+ let i = 0;
257
+ while (i < close.length) {
258
+ const ch = this.r.peek(i);
259
+ if (ch === "" || ch.toLowerCase() !== close[i]) return false;
260
+ i++;
261
+ }
262
+ return true;
263
+ }
264
+ // attributes
265
+ parseAttributes(component) {
266
+ const props = [];
267
+ while (!this.r.eof()) {
268
+ this.r.skipWhitespace();
269
+ if (this.r.peek() === ">" || this.r.startsWith("/>")) break;
270
+ if (this.r.atHole()) {
271
+ const h = this.r.takeHole();
272
+ props.push({ kind: "Spread", hole: h.index, loc: h.loc });
273
+ continue;
274
+ }
275
+ if (this.r.startsWith("...")) {
276
+ const s = this.r.pos();
277
+ this.r.advance();
278
+ this.r.advance();
279
+ this.r.advance();
280
+ if (this.r.atHole()) {
281
+ const h = this.r.takeHole();
282
+ props.push({ kind: "Spread", hole: h.index, loc: this.r.span(s) });
283
+ } else {
284
+ this.report("invalid-binding", "error", "Spread `...` must be followed by ${…}", this.r.span(s));
285
+ }
286
+ continue;
287
+ }
288
+ const nameStart = this.r.pos();
289
+ const name = this.readName();
290
+ if (!name) {
291
+ this.r.advance();
292
+ continue;
293
+ }
294
+ const nameLoc = this.r.span(nameStart);
295
+ this.r.skipWhitespace();
296
+ let value = null;
297
+ if (this.r.peek() === "=") {
298
+ this.r.advance();
299
+ this.r.skipWhitespace();
300
+ value = this.parseAttrValue();
301
+ }
302
+ const raw = {
303
+ name,
304
+ nameLoc,
305
+ value,
306
+ loc: this.r.span(nameStart),
307
+ report: (code, message) => this.report(code, "error", message, this.r.span(nameStart))
308
+ };
309
+ props.push(resolveAttribute(raw, this.directives, { component }));
310
+ }
311
+ return props;
312
+ }
313
+ parseAttrValue() {
314
+ if (this.r.atHole()) {
315
+ const h = this.r.takeHole();
316
+ return { kind: "hole", hole: h.index };
317
+ }
318
+ const quote = this.r.peek();
319
+ if (quote === '"' || quote === "'") {
320
+ return this.classifyParts(this.readQuotedParts(quote));
321
+ }
322
+ const value = this.r.readWhile((ch) => !/[\s>]/.test(ch));
323
+ return { kind: "static", value };
324
+ }
325
+ readQuotedParts(quote) {
326
+ this.r.advance();
327
+ const parts = [];
328
+ let text = "";
329
+ while (!this.r.eof() && this.r.peek() !== quote) {
330
+ if (this.r.atHole()) {
331
+ if (text) {
332
+ parts.push({ text });
333
+ text = "";
334
+ }
335
+ parts.push({ hole: this.r.takeHole().index });
336
+ continue;
337
+ }
338
+ const ch = this.r.advance();
339
+ if (ch === "") break;
340
+ text += ch;
341
+ }
342
+ if (text) parts.push({ text });
343
+ if (this.r.peek() === quote) this.r.advance();
344
+ return parts;
345
+ }
346
+ classifyParts(parts) {
347
+ const holes = parts.filter((p) => "hole" in p);
348
+ if (holes.length === 0) {
349
+ return { kind: "static", value: parts.map((p) => "text" in p ? p.text : "").join("") };
350
+ }
351
+ if (parts.length === 1) return { kind: "hole", hole: holes[0].hole };
352
+ return { kind: "mixed", parts };
353
+ }
354
+ /** Fold `key` into `each`, and validate an element's directive combination. */
355
+ foldEachKey(attrs, elLoc) {
356
+ let each = null;
357
+ let ifCount = 0;
358
+ let eachCount = 0;
359
+ let elseCount = 0;
360
+ let refCount = 0;
361
+ for (const a of attrs) {
362
+ if (a.kind === "EachDirective") {
363
+ each = a;
364
+ eachCount++;
365
+ } else if (a.kind === "IfDirective") ifCount++;
366
+ else if (a.kind === "ElseDirective") elseCount++;
367
+ else if (a.kind === "RefBinding") refCount++;
368
+ }
369
+ const span = this.r.span(elLoc);
370
+ if (eachCount > 1) this.report("duplicate-directive", "error", "Multiple `each` directives on one element", span);
371
+ if (ifCount > 1) this.report("duplicate-directive", "error", "Multiple `if` directives on one element", span);
372
+ if (refCount > 1) this.report("duplicate-directive", "error", "Multiple `ref` bindings on one element", span);
373
+ if (ifCount > 0 && eachCount > 0) {
374
+ this.report("unsupported-combination", "error", "`if` and `each` cannot both apply to one element — wrap one in another element", span);
375
+ }
376
+ if (ifCount > 0 && elseCount > 0) {
377
+ this.report("invalid-directive", "error", "`if` and `else` cannot be on the same element", span);
378
+ }
379
+ const keyIdx = attrs.findIndex((a) => a.kind === "Attribute" && a.name === "key");
380
+ if (!each) {
381
+ if (keyIdx !== -1) this.report("invalid-directive", "error", "`key` requires an `each` directive on the same element", span);
382
+ return;
383
+ }
384
+ if (keyIdx === -1) return;
385
+ const key = attrs[keyIdx];
386
+ if (key.kind === "Attribute" && key.value) {
387
+ if (key.value.kind === "static") each.key = { static: key.value.value };
388
+ else if (key.value.kind === "hole") each.key = { hole: key.value.hole };
389
+ }
390
+ attrs.splice(keyIdx, 1);
391
+ }
392
+ // low level
393
+ readName() {
394
+ return this.r.readWhile((ch) => NAME_CHAR.test(ch));
395
+ }
396
+ /** Consume `</name>` (or `</>`), returning the close-tag name (may be ''). */
397
+ consumeCloseTag() {
398
+ this.r.advance();
399
+ this.r.advance();
400
+ const name = this.readName();
401
+ this.r.skipWhitespace();
402
+ if (this.r.peek() === ">") this.r.advance();
403
+ return name;
404
+ }
405
+ report(code, severity, message, loc) {
406
+ this.diagnostics.push({ code, severity, message, loc });
407
+ }
408
+ };
409
+ function parseTemplate(pieces, opts = {}) {
410
+ return new Parser(pieces, opts).parse();
411
+ }
412
+
413
+ export {
414
+ parseTemplate
415
+ };
@@ -115,12 +115,18 @@ var CORE_DIRECTIVES = [
115
115
  {
116
116
  id: "load",
117
117
  match: (n) => n.startsWith("load:"),
118
- build: (raw) => ({
119
- kind: "LoadDirective",
120
- strategy: raw.name.slice("load:".length),
121
- modifier: raw.value && raw.value.kind === "static" ? raw.value.value : null,
122
- loc: raw.loc
123
- })
118
+ build: (raw) => {
119
+ if (raw.value && raw.value.kind !== "static") {
120
+ raw.report("invalid-binding", `'${raw.name}' needs a literal value, not an expression: the strategy is compile-time.`);
121
+ }
122
+ return {
123
+ kind: "LoadDirective",
124
+ strategy: raw.name.slice("load:".length),
125
+ modifier: raw.value && raw.value.kind === "static" ? raw.value.value : null,
126
+ ...raw.value && raw.value.kind !== "static" ? { dynamic: true } : {},
127
+ loc: raw.loc
128
+ };
129
+ }
124
130
  },
125
131
  // Custom directive: `use=${d}` or `use:tooltip[=${opts}]`.
126
132
  {
@@ -1 +1 @@
1
- {"version":3,"file":"directives.d.ts","sourceRoot":"","sources":["../src/directives.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,EACV,iBAAiB,EAEjB,SAAS,EAGT,UAAU,EACV,cAAc,EAOd,UAAU,EAIX,MAAM,UAAU,CAAC;AAElB,mFAAmF;AACnF,eAAO,MAAM,eAAe,aAAqE,CAAC;AAElG,kEAAkE;AAClE,MAAM,WAAW,YAAY;IAC3B,8EAA8E;IAC9E,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,UAAU,CAAC;IACpB,kCAAkC;IAClC,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;IACxB,GAAG,EAAE,UAAU,CAAC;IAChB,0DAA0D;IAC1D,MAAM,CAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACrD;AAED,MAAM,WAAW,mBAAmB;IAClC,sCAAsC;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,6EAA6E;IAC7E,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAC7B,KAAK,CAAC,GAAG,EAAE,YAAY,GAAG,iBAAiB,CAAC;CAC7C;AAED,oEAAoE;AACpE,MAAM,WAAW,WAAW;IAC1B,SAAS,EAAE,OAAO,CAAC;CACpB;AAkBD;;;GAGG;AACH,eAAO,MAAM,eAAe,EAAE,mBAAmB,EA4JhD,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,YAAY,EACjB,UAAU,GAAE,mBAAmB,EAAoB,EACnD,GAAG,GAAE,WAAkC,GACtC,iBAAiB,CAMnB;AAED,YAAY,EAAE,UAAU,EAAE,CAAC"}
1
+ {"version":3,"file":"directives.d.ts","sourceRoot":"","sources":["../src/directives.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,EACV,iBAAiB,EAEjB,SAAS,EAGT,UAAU,EACV,cAAc,EAOd,UAAU,EAIX,MAAM,UAAU,CAAC;AAElB,mFAAmF;AACnF,eAAO,MAAM,eAAe,aAAqE,CAAC;AAElG,kEAAkE;AAClE,MAAM,WAAW,YAAY;IAC3B,8EAA8E;IAC9E,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,UAAU,CAAC;IACpB,kCAAkC;IAClC,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;IACxB,GAAG,EAAE,UAAU,CAAC;IAChB,0DAA0D;IAC1D,MAAM,CAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACrD;AAED,MAAM,WAAW,mBAAmB;IAClC,sCAAsC;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,6EAA6E;IAC7E,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAC7B,KAAK,CAAC,GAAG,EAAE,YAAY,GAAG,iBAAiB,CAAC;CAC7C;AAED,oEAAoE;AACpE,MAAM,WAAW,WAAW;IAC1B,SAAS,EAAE,OAAO,CAAC;CACpB;AAkBD;;;GAGG;AACH,eAAO,MAAM,eAAe,EAAE,mBAAmB,EAoKhD,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,YAAY,EACjB,UAAU,GAAE,mBAAmB,EAAoB,EACnD,GAAG,GAAE,WAAkC,GACtC,iBAAiB,CAMnB;AAED,YAAY,EAAE,UAAU,EAAE,CAAC"}
@@ -103,12 +103,20 @@ export const CORE_DIRECTIVES = [
103
103
  {
104
104
  id: 'load',
105
105
  match: (n) => n.startsWith('load:'),
106
- build: (raw) => ({
107
- kind: 'LoadDirective',
108
- strategy: raw.name.slice('load:'.length),
109
- modifier: raw.value && raw.value.kind === 'static' ? raw.value.value : null,
110
- loc: raw.loc,
111
- }),
106
+ build: (raw) => {
107
+ // The strategy is fixed at compile time: a hole would be dropped without a word, the
108
+ // way `load:visible=\${margin}` once lost its margin. Same rule, same words as in JSX.
109
+ if (raw.value && raw.value.kind !== 'static') {
110
+ raw.report('invalid-binding', `'${raw.name}' needs a literal value, not an expression: the strategy is compile-time.`);
111
+ }
112
+ return {
113
+ kind: 'LoadDirective',
114
+ strategy: raw.name.slice('load:'.length),
115
+ modifier: raw.value && raw.value.kind === 'static' ? raw.value.value : null,
116
+ ...(raw.value && raw.value.kind !== 'static' ? { dynamic: true } : {}),
117
+ loc: raw.loc,
118
+ };
119
+ },
112
120
  },
113
121
  // Custom directive: `use=${d}` or `use:tooltip[=${opts}]`.
114
122
  {
@@ -1,183 +1,8 @@
1
- // src/directives.ts
2
- var EVENT_MODIFIERS = /* @__PURE__ */ new Set(["capture", "once", "passive", "prevent", "stop", "self"]);
3
- function requireHole(raw, what) {
4
- if (raw.value && raw.value.kind === "hole") return raw.value.hole;
5
- raw.report("invalid-binding", `${what} requires an expression value: ${raw.name}=\${…}`);
6
- return -1;
7
- }
8
- var attribute = (raw, boolean) => ({
9
- kind: "Attribute",
10
- name: boolean ? raw.name.slice(1) : raw.name,
11
- value: raw.value,
12
- boolean,
13
- loc: raw.loc
14
- });
15
- var CORE_DIRECTIVES = [
16
- // Events: `@click=${h}`, `on:click=${h}` (native), `onClick=${h}`, each with
17
- // optional dotted modifiers (`@click.capture.once`, `on:input.passive`).
18
- {
19
- id: "event",
20
- match: (n) => n[0] === "@" || n.startsWith("on:") || /^on[A-Z]/.test(n),
21
- build: (raw) => {
22
- let syntax;
23
- let rest;
24
- if (raw.name[0] === "@") {
25
- syntax = "at";
26
- rest = raw.name.slice(1);
27
- } else if (raw.name.startsWith("on:")) {
28
- syntax = "colon";
29
- rest = raw.name.slice(3);
30
- } else {
31
- syntax = "on";
32
- rest = raw.name.slice(2);
33
- }
34
- const [raw_name, ...modifiers] = rest.split(".");
35
- const name = raw_name.toLowerCase();
36
- for (const m of modifiers) {
37
- if (!EVENT_MODIFIERS.has(m)) {
38
- raw.report("invalid-directive", `Unknown event modifier ".${m}" — expected one of ${[...EVENT_MODIFIERS].join(", ")}`);
39
- }
40
- }
41
- return {
42
- kind: "EventBinding",
43
- name,
44
- syntax,
45
- modifiers,
46
- hole: requireHole(raw, "event binding"),
47
- loc: raw.loc
48
- };
49
- }
50
- },
51
- // Two-way binding: `bind:value=${signal}`.
52
- {
53
- id: "bind",
54
- elementOnly: true,
55
- match: (n) => n.startsWith("bind:"),
56
- build: (raw) => ({
57
- kind: "BindDirective",
58
- name: raw.name.slice("bind:".length),
59
- hole: requireHole(raw, "two-way binding"),
60
- loc: raw.loc
61
- })
62
- },
63
- // Dynamic class toggle: `class:active=${flag}`.
64
- {
65
- id: "class-directive",
66
- elementOnly: true,
67
- match: (n) => n.startsWith("class:"),
68
- build: (raw) => ({
69
- kind: "ClassDirective",
70
- name: raw.name.slice("class:".length),
71
- hole: requireHole(raw, "class directive"),
72
- loc: raw.loc
73
- })
74
- },
75
- // Dynamic style: `style:color=${c}`.
76
- {
77
- id: "style-directive",
78
- elementOnly: true,
79
- match: (n) => n.startsWith("style:"),
80
- build: (raw) => ({
81
- kind: "StyleDirective",
82
- name: raw.name.slice("style:".length),
83
- hole: requireHole(raw, "style directive"),
84
- loc: raw.loc
85
- })
86
- },
87
- // Load strategy: `load:visible[="200px"]`. Components only, an intrinsic element
88
- // has no module to defer.
89
- {
90
- id: "load",
91
- match: (n) => n.startsWith("load:"),
92
- build: (raw) => ({
93
- kind: "LoadDirective",
94
- strategy: raw.name.slice("load:".length),
95
- modifier: raw.value && raw.value.kind === "static" ? raw.value.value : null,
96
- loc: raw.loc
97
- })
98
- },
99
- // Custom directive: `use=${d}` or `use:tooltip[=${opts}]`.
100
- {
101
- id: "use",
102
- match: (n) => n === "use" || n.startsWith("use:"),
103
- build: (raw) => {
104
- const named = raw.name.startsWith("use:");
105
- return {
106
- kind: "UseDirective",
107
- name: named ? raw.name.slice("use:".length) : null,
108
- hole: raw.value && raw.value.kind === "hole" ? raw.value.hole : null,
109
- loc: raw.loc
110
- };
111
- }
112
- },
113
- // Conditional rendering: `if=${cond}`.
114
- {
115
- id: "if",
116
- elementOnly: true,
117
- match: (n) => n === "if",
118
- build: (raw) => ({
119
- kind: "IfDirective",
120
- hole: requireHole(raw, "if directive"),
121
- loc: raw.loc
122
- })
123
- },
124
- // Loop rendering: `each=${items}` (a sibling `key` folds in at parse time).
125
- {
126
- id: "each",
127
- elementOnly: true,
128
- match: (n) => n === "each",
129
- build: (raw) => ({
130
- kind: "EachDirective",
131
- hole: requireHole(raw, "each directive"),
132
- key: null,
133
- loc: raw.loc
134
- })
135
- },
136
- // `else` (valueless).
137
- {
138
- id: "else",
139
- elementOnly: true,
140
- match: (n) => n === "else",
141
- build: (raw) => {
142
- if (raw.value !== null) raw.report("invalid-directive", "`else` takes no value");
143
- return { kind: "ElseDirective", loc: raw.loc };
144
- }
145
- },
146
- // Ref binding: `ref=${el}`.
147
- {
148
- id: "ref",
149
- match: (n) => n === "ref",
150
- build: (raw) => ({
151
- kind: "RefBinding",
152
- hole: requireHole(raw, "ref binding"),
153
- loc: raw.loc
154
- })
155
- },
156
- // Property binding: `.currentTime=${t}`.
157
- {
158
- id: "property",
159
- match: (n) => n[0] === ".",
160
- build: (raw) => ({
161
- kind: "PropertyBinding",
162
- name: raw.name.slice(1),
163
- hole: requireHole(raw, "property binding"),
164
- loc: raw.loc
165
- })
166
- },
167
- // Boolean attribute (lit `?disabled`): a plain attribute flagged boolean.
168
- {
169
- id: "boolean",
170
- match: (n) => n[0] === "?",
171
- build: (raw) => attribute(raw, true)
172
- }
173
- ];
174
- function resolveAttribute(raw, directives = CORE_DIRECTIVES, ctx = { component: false }) {
175
- for (const d of directives) {
176
- if (d.elementOnly && ctx.component) continue;
177
- if (d.match(raw.name)) return d.build(raw);
178
- }
179
- return attribute(raw, false);
180
- }
1
+ import {
2
+ CORE_DIRECTIVES,
3
+ EVENT_MODIFIERS,
4
+ resolveAttribute
5
+ } from "./chunk-IP5F3OXX.mjs";
181
6
  export {
182
7
  CORE_DIRECTIVES,
183
8
  EVENT_MODIFIERS,
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! @fluixi/template-parser v1.0.0-alpha.108 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
1
+ /*! @fluixi/template-parser v1.0.0-alpha.109 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
2
  "use strict";
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -245,12 +245,18 @@ var CORE_DIRECTIVES = [
245
245
  {
246
246
  id: "load",
247
247
  match: (n) => n.startsWith("load:"),
248
- build: (raw) => ({
249
- kind: "LoadDirective",
250
- strategy: raw.name.slice("load:".length),
251
- modifier: raw.value && raw.value.kind === "static" ? raw.value.value : null,
252
- loc: raw.loc
253
- })
248
+ build: (raw) => {
249
+ if (raw.value && raw.value.kind !== "static") {
250
+ raw.report("invalid-binding", `'${raw.name}' needs a literal value, not an expression: the strategy is compile-time.`);
251
+ }
252
+ return {
253
+ kind: "LoadDirective",
254
+ strategy: raw.name.slice("load:".length),
255
+ modifier: raw.value && raw.value.kind === "static" ? raw.value.value : null,
256
+ ...raw.value && raw.value.kind !== "static" ? { dynamic: true } : {},
257
+ loc: raw.loc
258
+ };
259
+ }
254
260
  },
255
261
  // Custom directive: `use=${d}` or `use:tooltip[=${opts}]`.
256
262
  {