@ng-desktop/builders 0.0.0-stage → 0.3.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.
- package/LICENSE +21 -0
- package/README.md +13 -2
- package/bin/ng-desktop.mjs +191 -0
- package/builders.json +10 -0
- package/fesm2022/ast-metadata-extractor.d.mts +40 -0
- package/fesm2022/ast-metadata-extractor.mjs +420 -0
- package/fesm2022/build-bundle.d.mts +36 -0
- package/fesm2022/build-bundle.mjs +315 -0
- package/fesm2022/builder.d.ts +67 -0
- package/fesm2022/builder.d.ts.map +1 -0
- package/fesm2022/builder.js +124 -0
- package/fesm2022/builder.js.map +1 -0
- package/fesm2022/dev-server.mjs +47 -0
- package/fesm2022/hmr-client.d.mts +19 -0
- package/fesm2022/hmr-client.mjs +77 -0
- package/fesm2022/icon-compiler.d.mts +6 -0
- package/fesm2022/icon-compiler.mjs +148 -0
- package/fesm2022/index.d.ts +11 -0
- package/fesm2022/index.d.ts.map +1 -0
- package/fesm2022/index.js +11 -0
- package/fesm2022/index.js.map +1 -0
- package/fesm2022/permission-analyzer.d.mts +45 -0
- package/fesm2022/permission-analyzer.mjs +313 -0
- package/fesm2022/resolve-engine-binary.d.mts +35 -0
- package/fesm2022/resolve-engine-binary.mjs +130 -0
- package/fesm2022/style-compiler.d.mts +14 -0
- package/fesm2022/style-compiler.mjs +97 -0
- package/fesm2022/template-compiler.d.mts +53 -0
- package/fesm2022/template-compiler.mjs +810 -0
- package/fesm2022/workspace-resolver.d.mts +24 -0
- package/fesm2022/workspace-resolver.mjs +199 -0
- package/package.json +78 -4
- package/schema/angular-native-schema.json +52 -0
- package/schema/native-builder-schema.json +79 -0
- package/schematics/collection.json +10 -0
- package/schematics/ng-add/index.d.mts +20 -0
- package/schematics/ng-add/index.mjs +56 -0
- package/schematics/ng-add/schema.json +24 -0
|
@@ -0,0 +1,810 @@
|
|
|
1
|
+
import { ParenthesizedExpression, parseTemplate, PrefixNot } from '@angular/compiler';
|
|
2
|
+
|
|
3
|
+
const CURRENCY_SYMBOLS = {
|
|
4
|
+
USD: '$',
|
|
5
|
+
EUR: '€',
|
|
6
|
+
GBP: '£',
|
|
7
|
+
JPY: '¥',
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
function parseDecimalDigitsInfo(digitsInfo = '1.0-3') {
|
|
11
|
+
const match = String(digitsInfo).match(/^(\d+)\.(\d+)-(\d+)$/);
|
|
12
|
+
if (!match) {
|
|
13
|
+
return { minInt: 1, minFrac: 0, maxFrac: 3 };
|
|
14
|
+
}
|
|
15
|
+
const minInt = Number.parseInt(match[1], 10);
|
|
16
|
+
const minFrac = Number.parseInt(match[2], 10);
|
|
17
|
+
const maxFrac = Math.max(minFrac, Number.parseInt(match[3], 10));
|
|
18
|
+
return { minInt, minFrac, maxFrac };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function formatNumberDigits(rawValue, digitsInfo = '1.0-3') {
|
|
22
|
+
const num = Number(rawValue);
|
|
23
|
+
if (!Number.isFinite(num)) return String(rawValue ?? '');
|
|
24
|
+
const { minInt, minFrac, maxFrac } = parseDecimalDigitsInfo(digitsInfo);
|
|
25
|
+
return new Intl.NumberFormat('en-US', {
|
|
26
|
+
minimumIntegerDigits: Math.max(1, minInt),
|
|
27
|
+
minimumFractionDigits: minFrac,
|
|
28
|
+
maximumFractionDigits: maxFrac,
|
|
29
|
+
}).format(num);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Applies built-in Angular pipe transformations (`uppercase`, `lowercase`, `titlecase`,
|
|
34
|
+
* `number`, `currency`, `percent`, `slice`, `json`, `default`) at template compile time.
|
|
35
|
+
*/
|
|
36
|
+
export function applyPipeTransform(value, pipeName, args = []) {
|
|
37
|
+
const name = String(pipeName).trim();
|
|
38
|
+
switch (name) {
|
|
39
|
+
case 'uppercase':
|
|
40
|
+
return String(value ?? '').toUpperCase();
|
|
41
|
+
case 'lowercase':
|
|
42
|
+
return String(value ?? '').toLowerCase();
|
|
43
|
+
case 'titlecase':
|
|
44
|
+
return String(value ?? '').replace(/\w\S*/g, (word) => {
|
|
45
|
+
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
|
46
|
+
});
|
|
47
|
+
case 'number':
|
|
48
|
+
case 'decimal':
|
|
49
|
+
return formatNumberDigits(value, args[0] ?? '1.0-3');
|
|
50
|
+
case 'currency': {
|
|
51
|
+
const code = String(args[0] ?? 'USD').toUpperCase();
|
|
52
|
+
const display = String(args[1] ?? 'symbol');
|
|
53
|
+
const symbol = display === 'code' ? `${code} ` : (CURRENCY_SYMBOLS[code] ?? `${code} `);
|
|
54
|
+
const formatted = formatNumberDigits(value, args[2] ?? '1.2-2');
|
|
55
|
+
return `${symbol}${formatted}`;
|
|
56
|
+
}
|
|
57
|
+
case 'percent': {
|
|
58
|
+
const num = Number(value);
|
|
59
|
+
if (!Number.isFinite(num)) return String(value ?? '');
|
|
60
|
+
return `${formatNumberDigits(num * 100, args[0] ?? '1.0-2')}%`;
|
|
61
|
+
}
|
|
62
|
+
case 'slice': {
|
|
63
|
+
const start = Number(args[0] ?? 0);
|
|
64
|
+
const end = args[1] !== undefined ? Number(args[1]) : undefined;
|
|
65
|
+
if (Array.isArray(value) || typeof value === 'string') {
|
|
66
|
+
return value.slice(start, end);
|
|
67
|
+
}
|
|
68
|
+
return String(value ?? '').slice(start, end);
|
|
69
|
+
}
|
|
70
|
+
case 'json':
|
|
71
|
+
return typeof value === 'string' ? value : JSON.stringify(value);
|
|
72
|
+
case 'default':
|
|
73
|
+
case 'coalesce':
|
|
74
|
+
return value === null || value === undefined || value === '' ? (args[0] ?? '') : value;
|
|
75
|
+
default:
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function readStatePath(pathParts, state) {
|
|
81
|
+
if (pathParts.length === 0) return { found: false, value: undefined };
|
|
82
|
+
|
|
83
|
+
const joined = pathParts.join('.');
|
|
84
|
+
if (joined in state) {
|
|
85
|
+
return { found: true, value: state[joined] };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let current = state[pathParts[0]];
|
|
89
|
+
if (!(pathParts[0] in state)) {
|
|
90
|
+
return { found: false, value: undefined };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
for (let i = 1; i < pathParts.length; i++) {
|
|
94
|
+
if (typeof current === 'string' && current.trim().startsWith('{')) {
|
|
95
|
+
try {
|
|
96
|
+
current = JSON.parse(current);
|
|
97
|
+
} catch {
|
|
98
|
+
return { found: false, value: undefined };
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (current && typeof current === 'object' && pathParts[i] in current) {
|
|
102
|
+
current = current[pathParts[i]];
|
|
103
|
+
} else {
|
|
104
|
+
return { found: false, value: undefined };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return { found: true, value: current };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function collectReceiverChain(exprAst) {
|
|
111
|
+
if (!exprAst) return null;
|
|
112
|
+
if (exprAst.exp && typeof exprAst.name === 'string') {
|
|
113
|
+
return collectReceiverChain(exprAst.exp);
|
|
114
|
+
}
|
|
115
|
+
if (exprAst.receiver && Array.isArray(exprAst.args)) {
|
|
116
|
+
return collectReceiverChain(exprAst.receiver);
|
|
117
|
+
}
|
|
118
|
+
if (typeof exprAst.name === 'string') {
|
|
119
|
+
if (!exprAst.receiver || exprAst.receiver.name === undefined) {
|
|
120
|
+
return [exprAst.name];
|
|
121
|
+
}
|
|
122
|
+
const parent = collectReceiverChain(exprAst.receiver);
|
|
123
|
+
return parent ? [...parent, exprAst.name] : [exprAst.name];
|
|
124
|
+
}
|
|
125
|
+
if (exprAst.receiver && typeof exprAst.receiver.name === 'string') {
|
|
126
|
+
return [exprAst.receiver.name];
|
|
127
|
+
}
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function extractExpressionIdentifier(exprAst) {
|
|
132
|
+
const chain = collectReceiverChain(exprAst);
|
|
133
|
+
return chain && chain.length > 0 ? chain.join('.') : null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function evaluateScalarAst(ast, state) {
|
|
137
|
+
if (!ast) {
|
|
138
|
+
return { value: undefined, signalName: null, pipes: [] };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (ast instanceof ParenthesizedExpression) {
|
|
142
|
+
return evaluateScalarAst(ast.expression, state);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (ast instanceof PrefixNot) {
|
|
146
|
+
const inner = evaluateScalarAst(ast.expression, state);
|
|
147
|
+
return { value: !isTruthyCondition(inner.value), signalName: null, pipes: [] };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (typeof ast.name === 'string' && 'exp' in ast && Array.isArray(ast.args)) {
|
|
151
|
+
const inner = evaluateScalarAst(ast.exp, state);
|
|
152
|
+
const evaluatedArgs = ast.args.map((argAst) => evaluateScalarAst(argAst, state).value);
|
|
153
|
+
const transformed = applyPipeTransform(inner.value, ast.name, evaluatedArgs);
|
|
154
|
+
return {
|
|
155
|
+
value: transformed,
|
|
156
|
+
signalName: inner.signalName,
|
|
157
|
+
pipes: [...inner.pipes, ast.name],
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if ('value' in ast && typeof ast.value !== 'object') {
|
|
162
|
+
return { value: ast.value, signalName: null, pipes: [] };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (Array.isArray(ast.elements)) {
|
|
166
|
+
return {
|
|
167
|
+
value: ast.elements.map((el) => evaluateScalarAst(el, state).value),
|
|
168
|
+
signalName: null,
|
|
169
|
+
pipes: [],
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (typeof ast.operation === 'string' && ast.left !== undefined && ast.right !== undefined) {
|
|
174
|
+
const l = evaluateScalarAst(ast.left, state).value;
|
|
175
|
+
const r = evaluateScalarAst(ast.right, state).value;
|
|
176
|
+
switch (ast.operation) {
|
|
177
|
+
case '+':
|
|
178
|
+
return {
|
|
179
|
+
value:
|
|
180
|
+
typeof l === 'string' || typeof r === 'string' ? `${l}${r}` : Number(l) + Number(r),
|
|
181
|
+
signalName: null,
|
|
182
|
+
pipes: [],
|
|
183
|
+
};
|
|
184
|
+
case '-':
|
|
185
|
+
return { value: Number(l) - Number(r), signalName: null, pipes: [] };
|
|
186
|
+
case '*':
|
|
187
|
+
return { value: Number(l) * Number(r), signalName: null, pipes: [] };
|
|
188
|
+
case '/':
|
|
189
|
+
return { value: Number(r) === 0 ? 0 : Number(l) / Number(r), signalName: null, pipes: [] };
|
|
190
|
+
case '==':
|
|
191
|
+
case '===':
|
|
192
|
+
return { value: String(l) === String(r), signalName: null, pipes: [] };
|
|
193
|
+
case '!=':
|
|
194
|
+
case '!==':
|
|
195
|
+
return { value: String(l) !== String(r), signalName: null, pipes: [] };
|
|
196
|
+
case '>':
|
|
197
|
+
return { value: Number(l) > Number(r), signalName: null, pipes: [] };
|
|
198
|
+
case '<':
|
|
199
|
+
return { value: Number(l) < Number(r), signalName: null, pipes: [] };
|
|
200
|
+
case '>=':
|
|
201
|
+
return { value: Number(l) >= Number(r), signalName: null, pipes: [] };
|
|
202
|
+
case '<=':
|
|
203
|
+
return { value: Number(l) <= Number(r), signalName: null, pipes: [] };
|
|
204
|
+
case '&&':
|
|
205
|
+
return { value: Boolean(l) && Boolean(r), signalName: null, pipes: [] };
|
|
206
|
+
case '||':
|
|
207
|
+
return { value: Boolean(l) || Boolean(r), signalName: null, pipes: [] };
|
|
208
|
+
case '??':
|
|
209
|
+
return { value: l ?? r, signalName: null, pipes: [] };
|
|
210
|
+
default:
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (ast.condition !== undefined && ast.trueExp !== undefined && ast.falseExp !== undefined) {
|
|
216
|
+
const cond = evaluateScalarAst(ast.condition, state).value;
|
|
217
|
+
const isTruthy = cond !== 'false' && cond !== '0' && cond !== '' && Boolean(cond);
|
|
218
|
+
return isTruthy
|
|
219
|
+
? evaluateScalarAst(ast.trueExp, state)
|
|
220
|
+
: evaluateScalarAst(ast.falseExp, state);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const chain = collectReceiverChain(ast);
|
|
224
|
+
if (chain) {
|
|
225
|
+
const lookedUp = readStatePath(chain, state);
|
|
226
|
+
return {
|
|
227
|
+
value: lookedUp.found ? lookedUp.value : undefined,
|
|
228
|
+
signalName: chain.length === 1 ? chain[0] : null,
|
|
229
|
+
pipes: [],
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return { value: undefined, signalName: null, pipes: [] };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function buildPipePlaceholder(pipes) {
|
|
237
|
+
if (!pipes || pipes.length === 0) return '{}';
|
|
238
|
+
const last = pipes[pipes.length - 1];
|
|
239
|
+
if (last === 'uppercase' || last === 'lowercase' || last === 'titlecase' || last === 'percent') {
|
|
240
|
+
return `{|${last}}`;
|
|
241
|
+
}
|
|
242
|
+
return '{}';
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function evaluateBoundValue(boundValue, state) {
|
|
246
|
+
const ast = boundValue?.ast;
|
|
247
|
+
if (!ast) {
|
|
248
|
+
return { text: '', rawValue: undefined, signalName: null, templatePattern: '{}' };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (Array.isArray(ast.strings) && Array.isArray(ast.expressions)) {
|
|
252
|
+
let text = '';
|
|
253
|
+
for (let i = 0; i < ast.strings.length; i++) {
|
|
254
|
+
text += ast.strings[i];
|
|
255
|
+
if (i < ast.expressions.length) {
|
|
256
|
+
const res = evaluateScalarAst(ast.expressions[i], state);
|
|
257
|
+
if (res.value !== undefined && res.value !== null) {
|
|
258
|
+
text += String(res.value);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let signalName = null;
|
|
264
|
+
let templatePattern = text.trim();
|
|
265
|
+
if (ast.expressions.length === 1) {
|
|
266
|
+
const only = evaluateScalarAst(ast.expressions[0], state);
|
|
267
|
+
signalName = only.signalName;
|
|
268
|
+
const placeholder = buildPipePlaceholder(only.pipes);
|
|
269
|
+
templatePattern = `${ast.strings[0]}${placeholder}${ast.strings[1] ?? ''}`.trim();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return { text: text.trim(), rawValue: text.trim(), signalName, templatePattern };
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const evaluated = evaluateScalarAst(ast, state);
|
|
276
|
+
const rawValue = evaluated.value;
|
|
277
|
+
const text = rawValue !== undefined && rawValue !== null ? String(rawValue) : '';
|
|
278
|
+
return {
|
|
279
|
+
text,
|
|
280
|
+
rawValue,
|
|
281
|
+
signalName: evaluated.signalName,
|
|
282
|
+
templatePattern: buildPipePlaceholder(evaluated.pipes),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function isTruthyCondition(val) {
|
|
287
|
+
return val !== 'false' && val !== '0' && val !== '' && Boolean(val);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function isBranchActive(branch, state) {
|
|
291
|
+
if (!branch.expression) {
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
const evaluated = evaluateScalarAst(branch.expression.ast, state);
|
|
295
|
+
if (evaluated.value === undefined) {
|
|
296
|
+
const ident = extractExpressionIdentifier(branch.expression.ast);
|
|
297
|
+
if (!ident || !(ident in state)) {
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
return isTruthyCondition(state[ident]);
|
|
301
|
+
}
|
|
302
|
+
return isTruthyCondition(evaluated.value);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function normalizeComparisonOp(op) {
|
|
306
|
+
if (op === '===') return '==';
|
|
307
|
+
if (op === '!==') return '!=';
|
|
308
|
+
if (op === '==' || op === '!=' || op === '>' || op === '>=' || op === '<' || op === '<=') {
|
|
309
|
+
return op;
|
|
310
|
+
}
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function invertComparisonOp(op) {
|
|
315
|
+
switch (op) {
|
|
316
|
+
case '==':
|
|
317
|
+
return '!=';
|
|
318
|
+
case '!=':
|
|
319
|
+
return '==';
|
|
320
|
+
case '>':
|
|
321
|
+
return '<=';
|
|
322
|
+
case '<=':
|
|
323
|
+
return '>';
|
|
324
|
+
case '<':
|
|
325
|
+
return '>=';
|
|
326
|
+
case '>=':
|
|
327
|
+
return '<';
|
|
328
|
+
default:
|
|
329
|
+
return '!=';
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function extractReactiveCondition(ast, numericSignals, signalAliases = {}) {
|
|
334
|
+
if (!ast || !numericSignals) return null;
|
|
335
|
+
|
|
336
|
+
if (ast instanceof ParenthesizedExpression) {
|
|
337
|
+
return extractReactiveCondition(ast.expression, numericSignals, signalAliases);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (ast instanceof PrefixNot) {
|
|
341
|
+
const inner = extractReactiveCondition(ast.expression, numericSignals, signalAliases);
|
|
342
|
+
if (!inner) return null;
|
|
343
|
+
return {
|
|
344
|
+
signalName: inner.signalName,
|
|
345
|
+
op: invertComparisonOp(inner.op),
|
|
346
|
+
rhs: inner.rhs,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (typeof ast.operation === 'string' && ast.left !== undefined && ast.right !== undefined) {
|
|
351
|
+
const op = normalizeComparisonOp(ast.operation);
|
|
352
|
+
if (!op) return null;
|
|
353
|
+
const leftChain = collectReceiverChain(ast.left);
|
|
354
|
+
if (!leftChain || leftChain.length !== 1 || !(leftChain[0] in numericSignals)) {
|
|
355
|
+
return null;
|
|
356
|
+
}
|
|
357
|
+
const rightVal = evaluateScalarAst(ast.right, {}).value;
|
|
358
|
+
const signalName = signalAliases[leftChain[0]] || leftChain[0];
|
|
359
|
+
if (typeof rightVal === 'number' && Number.isFinite(rightVal)) {
|
|
360
|
+
return { signalName, op, rhs: rightVal };
|
|
361
|
+
}
|
|
362
|
+
if (typeof rightVal === 'boolean') {
|
|
363
|
+
return { signalName, op, rhs: rightVal ? 1 : 0 };
|
|
364
|
+
}
|
|
365
|
+
return null;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const chain = collectReceiverChain(ast);
|
|
369
|
+
if (chain && chain.length === 1 && chain[0] in numericSignals) {
|
|
370
|
+
return { signalName: signalAliases[chain[0]] || chain[0], op: '!=', rhs: 0 };
|
|
371
|
+
}
|
|
372
|
+
return null;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function resolveCollectionValue(exprAst, state) {
|
|
376
|
+
const evaluated = evaluateScalarAst(exprAst, state);
|
|
377
|
+
const raw = evaluated.value;
|
|
378
|
+
if (Array.isArray(raw)) {
|
|
379
|
+
return raw;
|
|
380
|
+
}
|
|
381
|
+
if (typeof raw === 'string') {
|
|
382
|
+
const trimmed = raw.trim();
|
|
383
|
+
if (!trimmed) return [];
|
|
384
|
+
if (trimmed.startsWith('[')) {
|
|
385
|
+
try {
|
|
386
|
+
const parsed = JSON.parse(trimmed);
|
|
387
|
+
return Array.isArray(parsed) ? parsed : [];
|
|
388
|
+
} catch {
|
|
389
|
+
return [];
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return trimmed
|
|
393
|
+
.split(',')
|
|
394
|
+
.map((s) => s.trim())
|
|
395
|
+
.filter(Boolean);
|
|
396
|
+
}
|
|
397
|
+
return [];
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function makeCommentNode(idState, prefix, text) {
|
|
401
|
+
return {
|
|
402
|
+
id: `${prefix}_${idState.next++}`,
|
|
403
|
+
tag: '#comment',
|
|
404
|
+
isComment: true,
|
|
405
|
+
text,
|
|
406
|
+
title: null,
|
|
407
|
+
classes: [],
|
|
408
|
+
attributes: {},
|
|
409
|
+
reactiveBinding: null,
|
|
410
|
+
reactiveAttrs: [],
|
|
411
|
+
visibilityBinding: null,
|
|
412
|
+
events: {},
|
|
413
|
+
children: [],
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function isControlOrElementChild(child) {
|
|
418
|
+
return (
|
|
419
|
+
typeof child.name === 'string' ||
|
|
420
|
+
Array.isArray(child.branches) ||
|
|
421
|
+
child.triggers !== undefined ||
|
|
422
|
+
Array.isArray(child.definedTriggers) ||
|
|
423
|
+
(child.item !== undefined && child.trackBy !== undefined) ||
|
|
424
|
+
Array.isArray(child.groups) ||
|
|
425
|
+
(child.value !== undefined && typeof child.name === 'string' && child.attributes === undefined)
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Parses an Angular 22 HTML template using `@angular/compiler`'s official `parseTemplate` AST,
|
|
431
|
+
* resolving static attributes, `[property]` / `{{ interpolation | pipe }}` bindings,
|
|
432
|
+
* `(event)` outputs, `@if`, `@for`/`@empty`, `@switch`/`@case`/`@default`, `@let`, and `@defer` blocks
|
|
433
|
+
* into the Native Scene Graph IR.
|
|
434
|
+
*/
|
|
435
|
+
export function parseTemplateHtml(
|
|
436
|
+
html,
|
|
437
|
+
state,
|
|
438
|
+
numericSignals,
|
|
439
|
+
templateUrl = 'app.html',
|
|
440
|
+
componentRegistry = {},
|
|
441
|
+
projectedChildren = null,
|
|
442
|
+
// Shared across nested component expansions so auto-generated ids stay globally unique
|
|
443
|
+
// within one compilation (each expansion used to restart at 1 and collide).
|
|
444
|
+
idState = { next: 1 },
|
|
445
|
+
signalAliases = {},
|
|
446
|
+
) {
|
|
447
|
+
const parsed = parseTemplate(html, templateUrl, {
|
|
448
|
+
preserveWhitespaces: false,
|
|
449
|
+
collectCommentNodes: true,
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
function transformNodes(astNodes, currentState) {
|
|
453
|
+
const scopeState = { ...currentState };
|
|
454
|
+
const out = [];
|
|
455
|
+
|
|
456
|
+
for (const tmplNode of astNodes || []) {
|
|
457
|
+
if (
|
|
458
|
+
tmplNode.value !== undefined &&
|
|
459
|
+
typeof tmplNode.name === 'string' &&
|
|
460
|
+
tmplNode.attributes === undefined
|
|
461
|
+
) {
|
|
462
|
+
const evaluated = evaluateBoundValue(tmplNode.value, scopeState);
|
|
463
|
+
scopeState[tmplNode.name] =
|
|
464
|
+
evaluated.rawValue !== undefined ? evaluated.rawValue : evaluated.text;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (tmplNode.name === 'ng-content') {
|
|
469
|
+
if (projectedChildren && projectedChildren.length > 0) {
|
|
470
|
+
out.push(...projectedChildren);
|
|
471
|
+
}
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
if (Array.isArray(tmplNode.branches)) {
|
|
476
|
+
const firstExpr = tmplNode.branches[0]?.expression?.source?.trim() || 'cond';
|
|
477
|
+
out.push(makeCommentNode(idState, 'anchor', `@if(${firstExpr})`));
|
|
478
|
+
|
|
479
|
+
const firstCond = extractReactiveCondition(
|
|
480
|
+
tmplNode.branches[0]?.expression?.ast,
|
|
481
|
+
numericSignals,
|
|
482
|
+
signalAliases,
|
|
483
|
+
);
|
|
484
|
+
if (firstCond) {
|
|
485
|
+
for (const branch of tmplNode.branches) {
|
|
486
|
+
const branchCond = branch.expression
|
|
487
|
+
? extractReactiveCondition(branch.expression.ast, numericSignals, signalAliases)
|
|
488
|
+
: {
|
|
489
|
+
signalName: firstCond.signalName,
|
|
490
|
+
op: invertComparisonOp(firstCond.op),
|
|
491
|
+
rhs: firstCond.rhs,
|
|
492
|
+
};
|
|
493
|
+
const active = isBranchActive(branch, scopeState);
|
|
494
|
+
const branchNodes = transformNodes(branch.children, scopeState);
|
|
495
|
+
for (const node of branchNodes) {
|
|
496
|
+
if (!node.isComment && branchCond) {
|
|
497
|
+
node.attributes.visible = String(active);
|
|
498
|
+
node.visibilityBinding = branchCond;
|
|
499
|
+
}
|
|
500
|
+
out.push(node);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
} else {
|
|
504
|
+
for (const branch of tmplNode.branches) {
|
|
505
|
+
if (isBranchActive(branch, scopeState)) {
|
|
506
|
+
out.push(...transformNodes(branch.children, scopeState));
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (tmplNode.item !== undefined && tmplNode.trackBy !== undefined) {
|
|
515
|
+
const exprSource = tmplNode.expression?.source?.trim() || 'items';
|
|
516
|
+
const trackSource = tmplNode.trackBy?.source?.trim() || '$index';
|
|
517
|
+
out.push(makeCommentNode(idState, 'for', `@for(${exprSource}; track:${trackSource})`));
|
|
518
|
+
|
|
519
|
+
const items = resolveCollectionValue(tmplNode.expression?.ast, scopeState);
|
|
520
|
+
if (items.length > 0) {
|
|
521
|
+
items.forEach((itemVal, idx) => {
|
|
522
|
+
const itemScope = {
|
|
523
|
+
...scopeState,
|
|
524
|
+
[tmplNode.item.name]: itemVal,
|
|
525
|
+
$index: idx,
|
|
526
|
+
$count: items.length,
|
|
527
|
+
$first: idx === 0,
|
|
528
|
+
$last: idx === items.length - 1,
|
|
529
|
+
$even: idx % 2 === 0,
|
|
530
|
+
$odd: idx % 2 === 1,
|
|
531
|
+
};
|
|
532
|
+
out.push(...transformNodes(tmplNode.children, itemScope));
|
|
533
|
+
});
|
|
534
|
+
} else if (tmplNode.empty?.children) {
|
|
535
|
+
out.push(...transformNodes(tmplNode.empty.children, scopeState));
|
|
536
|
+
}
|
|
537
|
+
continue;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (Array.isArray(tmplNode.groups)) {
|
|
541
|
+
const exprSource = tmplNode.expression?.source?.trim() || 'value';
|
|
542
|
+
out.push(makeCommentNode(idState, 'switch', `@switch(${exprSource})`));
|
|
543
|
+
|
|
544
|
+
const switchEval = evaluateScalarAst(tmplNode.expression?.ast, scopeState);
|
|
545
|
+
const rawSwitchSig = switchEval.signalName;
|
|
546
|
+
const switchSig =
|
|
547
|
+
rawSwitchSig && rawSwitchSig in numericSignals
|
|
548
|
+
? signalAliases[rawSwitchSig] || rawSwitchSig
|
|
549
|
+
: null;
|
|
550
|
+
|
|
551
|
+
const numericCases = [];
|
|
552
|
+
if (switchSig) {
|
|
553
|
+
for (const group of tmplNode.groups) {
|
|
554
|
+
const firstCase = (group.cases || []).find((c) => c.expression);
|
|
555
|
+
if (firstCase) {
|
|
556
|
+
const caseVal = evaluateScalarAst(firstCase.expression.ast, scopeState).value;
|
|
557
|
+
if (typeof caseVal === 'number' && Number.isFinite(caseVal)) {
|
|
558
|
+
numericCases.push(caseVal);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (switchSig && numericCases.length > 0) {
|
|
565
|
+
for (const group of tmplNode.groups) {
|
|
566
|
+
const cases = group.cases || [];
|
|
567
|
+
const firstCase = cases.find((c) => c.expression);
|
|
568
|
+
if (firstCase) {
|
|
569
|
+
const caseVal = evaluateScalarAst(firstCase.expression.ast, scopeState).value;
|
|
570
|
+
if (typeof caseVal !== 'number' || !Number.isFinite(caseVal)) continue;
|
|
571
|
+
const isMatch = Number(caseVal) === Number(switchEval.value);
|
|
572
|
+
const groupNodes = transformNodes(group.children, scopeState);
|
|
573
|
+
for (const node of groupNodes) {
|
|
574
|
+
if (!node.isComment) {
|
|
575
|
+
node.attributes.visible = String(isMatch);
|
|
576
|
+
node.visibilityBinding = {
|
|
577
|
+
signalName: switchSig,
|
|
578
|
+
op: '==',
|
|
579
|
+
rhs: Number(caseVal),
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
out.push(node);
|
|
583
|
+
}
|
|
584
|
+
} else if (cases.some((c) => !c.expression) && numericCases.length === 1) {
|
|
585
|
+
const isMatch = Number(switchEval.value) !== numericCases[0];
|
|
586
|
+
const groupNodes = transformNodes(group.children, scopeState);
|
|
587
|
+
for (const node of groupNodes) {
|
|
588
|
+
if (!node.isComment) {
|
|
589
|
+
node.attributes.visible = String(isMatch);
|
|
590
|
+
node.visibilityBinding = {
|
|
591
|
+
signalName: switchSig,
|
|
592
|
+
op: '!=',
|
|
593
|
+
rhs: numericCases[0],
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
out.push(node);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const switchVal = switchEval.value;
|
|
604
|
+
let matchedGroup = null;
|
|
605
|
+
let defaultGroup = null;
|
|
606
|
+
|
|
607
|
+
for (const group of tmplNode.groups) {
|
|
608
|
+
const cases = group.cases || [];
|
|
609
|
+
const hasDefault = cases.some((c) => !c.expression);
|
|
610
|
+
if (hasDefault && !defaultGroup) {
|
|
611
|
+
defaultGroup = group;
|
|
612
|
+
}
|
|
613
|
+
const matches = cases.some((c) => {
|
|
614
|
+
if (!c.expression) return false;
|
|
615
|
+
const caseVal = evaluateScalarAst(c.expression.ast, scopeState).value;
|
|
616
|
+
return String(caseVal) === String(switchVal);
|
|
617
|
+
});
|
|
618
|
+
if (matches) {
|
|
619
|
+
matchedGroup = group;
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
const chosen = matchedGroup || defaultGroup;
|
|
625
|
+
if (chosen?.children) {
|
|
626
|
+
out.push(...transformNodes(chosen.children, scopeState));
|
|
627
|
+
}
|
|
628
|
+
continue;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
if (tmplNode.triggers !== undefined || Array.isArray(tmplNode.definedTriggers)) {
|
|
632
|
+
const triggers =
|
|
633
|
+
Array.isArray(tmplNode.definedTriggers) && tmplNode.definedTriggers.length > 0
|
|
634
|
+
? tmplNode.definedTriggers.join(',')
|
|
635
|
+
: 'idle';
|
|
636
|
+
const prefetch =
|
|
637
|
+
Array.isArray(tmplNode.definedPrefetchTriggers) &&
|
|
638
|
+
tmplNode.definedPrefetchTriggers.length > 0
|
|
639
|
+
? `; prefetch:${tmplNode.definedPrefetchTriggers.join(',')}`
|
|
640
|
+
: '';
|
|
641
|
+
out.push(makeCommentNode(idState, 'defer', `@defer(${triggers}${prefetch})`));
|
|
642
|
+
if (tmplNode.placeholder?.children) {
|
|
643
|
+
out.push(...transformNodes(tmplNode.placeholder.children, scopeState));
|
|
644
|
+
}
|
|
645
|
+
if (tmplNode.loading?.children) {
|
|
646
|
+
out.push(...transformNodes(tmplNode.loading.children, scopeState));
|
|
647
|
+
}
|
|
648
|
+
if (tmplNode.error?.children) {
|
|
649
|
+
out.push(...transformNodes(tmplNode.error.children, scopeState));
|
|
650
|
+
}
|
|
651
|
+
if (Array.isArray(tmplNode.children)) {
|
|
652
|
+
out.push(...transformNodes(tmplNode.children, scopeState));
|
|
653
|
+
}
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
if (typeof tmplNode.name !== 'string') {
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
if (tmplNode.name in componentRegistry) {
|
|
662
|
+
const compDef = componentRegistry[tmplNode.name];
|
|
663
|
+
const compState = { ...scopeState };
|
|
664
|
+
const childSignalAliases = { ...signalAliases };
|
|
665
|
+
const childNumericSignals = { ...numericSignals };
|
|
666
|
+
for (const attr of tmplNode.attributes || []) {
|
|
667
|
+
compState[attr.name] = attr.value;
|
|
668
|
+
}
|
|
669
|
+
for (const input of tmplNode.inputs || []) {
|
|
670
|
+
const evaluated = evaluateBoundValue(input.value, scopeState);
|
|
671
|
+
compState[input.name] =
|
|
672
|
+
evaluated.rawValue !== undefined ? evaluated.rawValue : evaluated.text;
|
|
673
|
+
if (evaluated.signalName) {
|
|
674
|
+
const resolvedSig = signalAliases[evaluated.signalName] || evaluated.signalName;
|
|
675
|
+
if (resolvedSig in numericSignals) {
|
|
676
|
+
childSignalAliases[input.name] = resolvedSig;
|
|
677
|
+
childNumericSignals[input.name] = numericSignals[resolvedSig];
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const projected = transformNodes(tmplNode.children, scopeState);
|
|
683
|
+
const compNodes = parseTemplateHtml(
|
|
684
|
+
compDef.templateHtml,
|
|
685
|
+
compState,
|
|
686
|
+
childNumericSignals,
|
|
687
|
+
compDef.templateUrl || `${tmplNode.name}.html`,
|
|
688
|
+
componentRegistry,
|
|
689
|
+
projected,
|
|
690
|
+
idState,
|
|
691
|
+
childSignalAliases,
|
|
692
|
+
);
|
|
693
|
+
|
|
694
|
+
const hostId = tmplNode.attributes?.find((a) => a.name === 'id')?.value;
|
|
695
|
+
const hostClasses =
|
|
696
|
+
tmplNode.attributes
|
|
697
|
+
?.find((a) => a.name === 'class')
|
|
698
|
+
?.value?.split(/\s+/)
|
|
699
|
+
.filter(Boolean) || [];
|
|
700
|
+
|
|
701
|
+
if (compNodes.length > 0) {
|
|
702
|
+
if (hostId) compNodes[0].id = hostId;
|
|
703
|
+
if (hostClasses.length > 0) {
|
|
704
|
+
compNodes[0].classes = Array.from(new Set([...compNodes[0].classes, ...hostClasses]));
|
|
705
|
+
}
|
|
706
|
+
for (const output of tmplNode.outputs || []) {
|
|
707
|
+
const handlerName =
|
|
708
|
+
extractExpressionIdentifier(output.handler?.ast) ||
|
|
709
|
+
output.handler?.source?.replace(/\(\)\s*$/, '').trim();
|
|
710
|
+
if (handlerName) {
|
|
711
|
+
const mappedEvent = output.name === 'select' ? 'click' : output.name;
|
|
712
|
+
compNodes[0].events[mappedEvent] = handlerName;
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
out.push(...compNodes);
|
|
718
|
+
continue;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
const attrs = {};
|
|
722
|
+
for (const attr of tmplNode.attributes || []) {
|
|
723
|
+
attrs[attr.name] = attr.value;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
const reactiveAttrs = [];
|
|
727
|
+
let visibilityBinding = null;
|
|
728
|
+
for (const input of tmplNode.inputs || []) {
|
|
729
|
+
const evaluated = evaluateBoundValue(input.value, scopeState);
|
|
730
|
+
attrs[input.name] = evaluated.text;
|
|
731
|
+
if (input.name === 'visible') {
|
|
732
|
+
const cond = extractReactiveCondition(input.value?.ast, numericSignals, signalAliases);
|
|
733
|
+
if (cond) {
|
|
734
|
+
visibilityBinding = cond;
|
|
735
|
+
attrs.visible = String(
|
|
736
|
+
isTruthyCondition(evaluateScalarAst(input.value?.ast, scopeState).value),
|
|
737
|
+
);
|
|
738
|
+
}
|
|
739
|
+
} else if (evaluated.signalName && evaluated.signalName in numericSignals) {
|
|
740
|
+
reactiveAttrs.push({
|
|
741
|
+
attrKey: input.name,
|
|
742
|
+
signalName: signalAliases[evaluated.signalName] || evaluated.signalName,
|
|
743
|
+
template: evaluated.templatePattern,
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
const events = {};
|
|
749
|
+
for (const output of tmplNode.outputs || []) {
|
|
750
|
+
const handlerName =
|
|
751
|
+
extractExpressionIdentifier(output.handler?.ast) ||
|
|
752
|
+
output.handler?.source?.replace(/\(\)\s*$/, '').trim();
|
|
753
|
+
if (handlerName) {
|
|
754
|
+
events[output.name] = handlerName;
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
const customAttrs = {};
|
|
759
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
760
|
+
if (k !== 'id' && k !== 'title' && k !== 'class') {
|
|
761
|
+
customAttrs[k] = v;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
let textContent = null;
|
|
766
|
+
let reactiveBinding = null;
|
|
767
|
+
const childElements = [];
|
|
768
|
+
|
|
769
|
+
for (const child of tmplNode.children || []) {
|
|
770
|
+
if (isControlOrElementChild(child)) {
|
|
771
|
+
childElements.push(...transformNodes([child], scopeState));
|
|
772
|
+
} else if (typeof child.value === 'string') {
|
|
773
|
+
const trimmed = child.value.trim();
|
|
774
|
+
if (trimmed) {
|
|
775
|
+
textContent = trimmed;
|
|
776
|
+
}
|
|
777
|
+
} else if (child.value && typeof child.value === 'object') {
|
|
778
|
+
const evaluated = evaluateBoundValue(child.value, scopeState);
|
|
779
|
+
if (evaluated.text) {
|
|
780
|
+
textContent = evaluated.text;
|
|
781
|
+
}
|
|
782
|
+
if (evaluated.signalName && evaluated.signalName in numericSignals) {
|
|
783
|
+
reactiveBinding = {
|
|
784
|
+
signalName: signalAliases[evaluated.signalName] || evaluated.signalName,
|
|
785
|
+
template: evaluated.templatePattern,
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
out.push({
|
|
792
|
+
id: attrs.id || `node_${idState.next++}`,
|
|
793
|
+
tag: tmplNode.name,
|
|
794
|
+
title: attrs.title || null,
|
|
795
|
+
classes: attrs.class ? attrs.class.split(/\s+/).filter(Boolean) : [],
|
|
796
|
+
attributes: customAttrs,
|
|
797
|
+
text: textContent,
|
|
798
|
+
reactiveBinding,
|
|
799
|
+
reactiveAttrs,
|
|
800
|
+
visibilityBinding,
|
|
801
|
+
events,
|
|
802
|
+
children: childElements,
|
|
803
|
+
});
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
return out;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
return transformNodes(parsed.nodes, state);
|
|
810
|
+
}
|