@adaas/are-html 0.0.22 → 0.0.23
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/dist/browser/index.d.mts +176 -8
- package/dist/browser/index.mjs +661 -235
- package/dist/browser/index.mjs.map +1 -1
- package/dist/node/{AreBinding.attribute-doUvtOjc.d.mts → AreBinding.attribute-BWzEIw6H.d.mts} +45 -0
- package/dist/node/{AreBinding.attribute-Bm5LlOyE.d.ts → AreBinding.attribute-GpT-5Qmf.d.ts} +45 -0
- package/dist/node/attributes/AreBinding.attribute.d.mts +1 -1
- package/dist/node/attributes/AreBinding.attribute.d.ts +1 -1
- package/dist/node/attributes/AreDirective.attribute.d.mts +1 -1
- package/dist/node/attributes/AreDirective.attribute.d.ts +1 -1
- package/dist/node/attributes/AreEvent.attribute.d.mts +1 -1
- package/dist/node/attributes/AreEvent.attribute.d.ts +1 -1
- package/dist/node/attributes/AreStatic.attribute.d.mts +1 -1
- package/dist/node/attributes/AreStatic.attribute.d.ts +1 -1
- package/dist/node/directives/AreDirectiveFor.directive.d.mts +18 -1
- package/dist/node/directives/AreDirectiveFor.directive.d.ts +18 -1
- package/dist/node/directives/AreDirectiveFor.directive.js +57 -9
- package/dist/node/directives/AreDirectiveFor.directive.js.map +1 -1
- package/dist/node/directives/AreDirectiveFor.directive.mjs +57 -9
- package/dist/node/directives/AreDirectiveFor.directive.mjs.map +1 -1
- package/dist/node/directives/AreDirectiveIf.directive.d.mts +1 -1
- package/dist/node/directives/AreDirectiveIf.directive.d.ts +1 -1
- package/dist/node/directives/AreDirectiveShow.directive.d.mts +1 -1
- package/dist/node/directives/AreDirectiveShow.directive.d.ts +1 -1
- package/dist/node/engine/AreHTML.compiler.d.mts +1 -1
- package/dist/node/engine/AreHTML.compiler.d.ts +1 -1
- package/dist/node/engine/AreHTML.compiler.js +4 -0
- package/dist/node/engine/AreHTML.compiler.js.map +1 -1
- package/dist/node/engine/AreHTML.compiler.mjs +4 -0
- package/dist/node/engine/AreHTML.compiler.mjs.map +1 -1
- package/dist/node/engine/AreHTML.constants.d.mts +33 -1
- package/dist/node/engine/AreHTML.constants.d.ts +33 -1
- package/dist/node/engine/AreHTML.constants.js +166 -0
- package/dist/node/engine/AreHTML.constants.js.map +1 -1
- package/dist/node/engine/AreHTML.constants.mjs +165 -1
- package/dist/node/engine/AreHTML.constants.mjs.map +1 -1
- package/dist/node/engine/AreHTML.context.d.mts +66 -0
- package/dist/node/engine/AreHTML.context.d.ts +66 -0
- package/dist/node/engine/AreHTML.context.js +98 -0
- package/dist/node/engine/AreHTML.context.js.map +1 -1
- package/dist/node/engine/AreHTML.context.mjs +98 -0
- package/dist/node/engine/AreHTML.context.mjs.map +1 -1
- package/dist/node/engine/AreHTML.interpreter.d.mts +3 -0
- package/dist/node/engine/AreHTML.interpreter.d.ts +3 -0
- package/dist/node/engine/AreHTML.interpreter.js +66 -10
- package/dist/node/engine/AreHTML.interpreter.js.map +1 -1
- package/dist/node/engine/AreHTML.interpreter.mjs +66 -10
- package/dist/node/engine/AreHTML.interpreter.mjs.map +1 -1
- package/dist/node/engine/AreHTML.lifecycle.d.mts +1 -8
- package/dist/node/engine/AreHTML.lifecycle.d.ts +1 -8
- package/dist/node/engine/AreHTML.lifecycle.js +29 -44
- package/dist/node/engine/AreHTML.lifecycle.js.map +1 -1
- package/dist/node/engine/AreHTML.lifecycle.mjs +29 -44
- package/dist/node/engine/AreHTML.lifecycle.mjs.map +1 -1
- package/dist/node/engine/AreHTML.tokenizer.d.mts +1 -1
- package/dist/node/engine/AreHTML.tokenizer.d.ts +1 -1
- package/dist/node/engine/AreHTML.tokenizer.js +7 -1
- package/dist/node/engine/AreHTML.tokenizer.js.map +1 -1
- package/dist/node/engine/AreHTML.tokenizer.mjs +7 -1
- package/dist/node/engine/AreHTML.tokenizer.mjs.map +1 -1
- package/dist/node/engine/AreHTML.transformer.d.mts +1 -1
- package/dist/node/engine/AreHTML.transformer.d.ts +1 -1
- package/dist/node/index.d.mts +4 -3
- package/dist/node/index.d.ts +4 -3
- package/dist/node/index.js +7 -0
- package/dist/node/index.mjs +1 -0
- package/dist/node/instructions/AddStaticHTML.instruction.d.mts +8 -0
- package/dist/node/instructions/AddStaticHTML.instruction.d.ts +8 -0
- package/dist/node/instructions/AddStaticHTML.instruction.js +31 -0
- package/dist/node/instructions/AddStaticHTML.instruction.js.map +1 -0
- package/dist/node/instructions/AddStaticHTML.instruction.mjs +24 -0
- package/dist/node/instructions/AddStaticHTML.instruction.mjs.map +1 -0
- package/dist/node/instructions/AreHTML.instructions.constants.d.mts +1 -0
- package/dist/node/instructions/AreHTML.instructions.constants.d.ts +1 -0
- package/dist/node/instructions/AreHTML.instructions.constants.js +1 -0
- package/dist/node/instructions/AreHTML.instructions.constants.js.map +1 -1
- package/dist/node/instructions/AreHTML.instructions.constants.mjs +1 -0
- package/dist/node/instructions/AreHTML.instructions.constants.mjs.map +1 -1
- package/dist/node/instructions/AreHTML.instructions.types.d.mts +9 -1
- package/dist/node/instructions/AreHTML.instructions.types.d.ts +9 -1
- package/dist/node/lib/AreDirective/AreDirective.component.d.mts +1 -1
- package/dist/node/lib/AreDirective/AreDirective.component.d.ts +1 -1
- package/dist/node/lib/AreDirective/AreDirective.types.d.mts +1 -1
- package/dist/node/lib/AreDirective/AreDirective.types.d.ts +1 -1
- package/dist/node/lib/AreHTML/AreHTML.tokenizer.d.mts +1 -1
- package/dist/node/lib/AreHTML/AreHTML.tokenizer.d.ts +1 -1
- package/dist/node/lib/AreHTMLAttribute/AreHTML.attribute.d.mts +1 -1
- package/dist/node/lib/AreHTMLAttribute/AreHTML.attribute.d.ts +1 -1
- package/dist/node/lib/AreHTMLNode/AreHTMLNode.d.mts +1 -1
- package/dist/node/lib/AreHTMLNode/AreHTMLNode.d.ts +1 -1
- package/dist/node/lib/AreHTMLNode/AreHTMLNode.js +51 -0
- package/dist/node/lib/AreHTMLNode/AreHTMLNode.js.map +1 -1
- package/dist/node/lib/AreHTMLNode/AreHTMLNode.mjs +51 -0
- package/dist/node/lib/AreHTMLNode/AreHTMLNode.mjs.map +1 -1
- package/dist/node/lib/AreRoot/AreRoot.component.js.map +1 -1
- package/dist/node/lib/AreRoot/AreRoot.component.mjs.map +1 -1
- package/dist/node/nodes/AreComment.d.mts +1 -1
- package/dist/node/nodes/AreComment.d.ts +1 -1
- package/dist/node/nodes/AreComponent.d.mts +1 -1
- package/dist/node/nodes/AreComponent.d.ts +1 -1
- package/dist/node/nodes/AreInterpolation.d.mts +1 -1
- package/dist/node/nodes/AreInterpolation.d.ts +1 -1
- package/dist/node/nodes/AreRoot.d.mts +1 -1
- package/dist/node/nodes/AreRoot.d.ts +1 -1
- package/dist/node/nodes/AreText.d.mts +1 -1
- package/dist/node/nodes/AreText.d.ts +1 -1
- package/examples/dashboard/concept.ts +1 -1
- package/examples/dashboard/dist/index.html +1 -1
- package/examples/dashboard/dist/{mqh9ryml-xat335.js → mqiw5sqa-ypckmj.js} +403 -57
- package/examples/for-perf/dist/index.html +1 -1
- package/examples/for-perf/dist/{mqh9ryfo-6a8d0o.js → mqj1mpf2-z4aokv.js} +558 -117
- package/examples/for-perf/dist/{mqh9ryfq-4pf5cv.js → mqj1mpff-4fr7mw.js} +558 -117
- package/examples/signal-routing/dist/index.html +1 -1
- package/examples/signal-routing/dist/{mqh9ryc9-dkcbkx.js → mqiwo23h-bhcolu.js} +413 -60
- package/package.json +5 -5
- package/src/directives/AreDirectiveFor.directive.ts +99 -16
- package/src/engine/AreHTML.compiler.ts +13 -0
- package/src/engine/AreHTML.constants.ts +142 -0
- package/src/engine/AreHTML.context.ts +112 -0
- package/src/engine/AreHTML.interpreter.ts +114 -13
- package/src/engine/AreHTML.lifecycle.ts +81 -74
- package/src/engine/AreHTML.tokenizer.ts +30 -1
- package/src/index.ts +1 -0
- package/src/instructions/AddStaticHTML.instruction.ts +23 -0
- package/src/instructions/AreHTML.instructions.constants.ts +1 -0
- package/src/instructions/AreHTML.instructions.types.ts +9 -0
- package/src/lib/AreHTMLNode/AreHTMLNode.ts +74 -0
- package/src/lib/AreRoot/AreRoot.component.ts +3 -3
- package/tests/StaticIsland.test.ts +115 -0
- package/examples/for-perf/dist/mqh9ryde-m243t8.js +0 -15223
|
@@ -8,6 +8,7 @@ import { AreHTMLNode } from "@adaas/are-html/node";
|
|
|
8
8
|
import { AreDirectiveContext } from "@adaas/are-html/directive/AreDirective.context";
|
|
9
9
|
import { A_Frame } from "@adaas/a-frame/core";
|
|
10
10
|
import { AreSchedulerHelper } from "@adaas/are-html/helpers/AreScheduler.helper";
|
|
11
|
+
import { AreHTMLEngineContext } from "@adaas/are-html/context";
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
type AreForExpression = {
|
|
@@ -109,7 +110,12 @@ export class AreDirectiveFor extends AreDirective {
|
|
|
109
110
|
* Parse the $for expression and evaluate the source array.
|
|
110
111
|
*/
|
|
111
112
|
const { key, index, arrayExpr } = this.parseExpression(attribute.content);
|
|
112
|
-
|
|
113
|
+
// Item-scoped variables from an enclosing directive (e.g. the `row` of an
|
|
114
|
+
// outer `$for`) so a nested `$for="cell in row.cells"` resolves correctly.
|
|
115
|
+
// Use resolve() (not resolveFlat) so the ENCLOSING item's context — which
|
|
116
|
+
// lives on an ancestor scope, not on this directive's own node — is found.
|
|
117
|
+
const contextScope = attribute.owner.scope.resolve(AreDirectiveContext)?.scope || {};
|
|
118
|
+
const array = this.resolveArray(store, arrayExpr, attribute.content, contextScope);
|
|
113
119
|
|
|
114
120
|
attribute.value = array;
|
|
115
121
|
|
|
@@ -193,9 +199,12 @@ export class AreDirectiveFor extends AreDirective {
|
|
|
193
199
|
* Re-evaluate the source array.
|
|
194
200
|
*/
|
|
195
201
|
const { key, index, arrayExpr, trackExpr } = this.parseExpression(attribute.content);
|
|
196
|
-
const newArray = this.resolveArray(store, arrayExpr, attribute.content);
|
|
197
|
-
|
|
198
202
|
const owner = attribute.owner;
|
|
203
|
+
// Item-scoped variables from an enclosing directive (see transform()).
|
|
204
|
+
// resolve() walks ancestor scopes to find the enclosing item's context.
|
|
205
|
+
const contextScope = owner.scope.resolve(AreDirectiveContext)?.scope || {};
|
|
206
|
+
const newArray = this.resolveArray(store, arrayExpr, attribute.content, contextScope);
|
|
207
|
+
|
|
199
208
|
const currentChildren = [...owner.children] as AreHTMLNode[];
|
|
200
209
|
|
|
201
210
|
attribute.value = newArray;
|
|
@@ -239,15 +248,25 @@ export class AreDirectiveFor extends AreDirective {
|
|
|
239
248
|
// render, so it is deferred into the time-sliced loop below alongside
|
|
240
249
|
// transform/compile/mount. Existing (keyed) children are reconciled in
|
|
241
250
|
// place synchronously — that is cheap and keeps reused rows stable.
|
|
242
|
-
const toCreate: Array<{ item: any; idx: number }> = [];
|
|
251
|
+
const toCreate: Array<{ item: any; idx: number; key: any }> = [];
|
|
252
|
+
|
|
253
|
+
// Final identity → node map covering BOTH reused and newly created item
|
|
254
|
+
// nodes, plus the desired key order. After all items are mounted these
|
|
255
|
+
// drive the DOM reorder pass (step 5) so the rendered order always
|
|
256
|
+
// matches the source array — making prepend / shuffle / arbitrary
|
|
257
|
+
// reorders move existing rows instead of only appending at the end.
|
|
258
|
+
const finalByKey = new Map<any, AreHTMLNode>();
|
|
259
|
+
const orderedKeys: any[] = new Array(newArray.length);
|
|
243
260
|
|
|
244
261
|
for (let i = 0; i < newArray.length; i++) {
|
|
245
262
|
const item = newArray[i];
|
|
246
263
|
const k = computeKey(item, i);
|
|
264
|
+
orderedKeys[i] = k;
|
|
247
265
|
const existing = childByKey.get(k);
|
|
248
266
|
|
|
249
267
|
if (existing) {
|
|
250
268
|
remaining.delete(existing);
|
|
269
|
+
finalByKey.set(k, existing);
|
|
251
270
|
|
|
252
271
|
let directiveContext = existing.scope.resolveFlat(AreDirectiveContext);
|
|
253
272
|
if (!directiveContext) {
|
|
@@ -260,7 +279,7 @@ export class AreDirectiveFor extends AreDirective {
|
|
|
260
279
|
[index || 'index']: i,
|
|
261
280
|
};
|
|
262
281
|
} else {
|
|
263
|
-
toCreate.push({ item, idx: i });
|
|
282
|
+
toCreate.push({ item, idx: i, key: k });
|
|
264
283
|
}
|
|
265
284
|
}
|
|
266
285
|
|
|
@@ -274,12 +293,13 @@ export class AreDirectiveFor extends AreDirective {
|
|
|
274
293
|
}
|
|
275
294
|
|
|
276
295
|
// ── 4. Create + mount the new item nodes. ───────────────────────────
|
|
277
|
-
// `spawnItemNode` appends to `owner.children` immediately
|
|
278
|
-
//
|
|
279
|
-
//
|
|
280
|
-
//
|
|
281
|
-
const createItem = (desc: { item: any; idx: number }) => {
|
|
296
|
+
// `spawnItemNode` appends to `owner.children` immediately; new rows are
|
|
297
|
+
// therefore mounted at the end (just before the anchor comment). The
|
|
298
|
+
// reorder pass (step 5) then moves any out-of-position node so the final
|
|
299
|
+
// DOM order matches the source array.
|
|
300
|
+
const createItem = (desc: { item: any; idx: number; key: any }) => {
|
|
282
301
|
const child = this.spawnItemNode(attribute.template!, owner, key, index, desc.item, desc.idx);
|
|
302
|
+
finalByKey.set(desc.key, child);
|
|
283
303
|
child.transform();
|
|
284
304
|
child.compile();
|
|
285
305
|
// While detached, stop after compile: the item's instructions are
|
|
@@ -292,6 +312,8 @@ export class AreDirectiveFor extends AreDirective {
|
|
|
292
312
|
// Small lists → fully synchronous, identical to the previous behavior.
|
|
293
313
|
if (toCreate.length <= AreDirectiveFor.SYNC_THRESHOLD) {
|
|
294
314
|
for (const desc of toCreate) createItem(desc);
|
|
315
|
+
// ── 5. Reorder live DOM to match the source array order ──────────
|
|
316
|
+
if (attached) this.reconcileOrder(owner, orderedKeys, finalByKey);
|
|
295
317
|
return this.finishUpdate(attribute, store, scene, state);
|
|
296
318
|
}
|
|
297
319
|
|
|
@@ -325,12 +347,55 @@ export class AreDirectiveFor extends AreDirective {
|
|
|
325
347
|
});
|
|
326
348
|
}
|
|
327
349
|
|
|
350
|
+
// ── 5. Reorder live DOM to match the source array order ──────────
|
|
351
|
+
if (attached) this.reconcileOrder(owner, orderedKeys, finalByKey);
|
|
328
352
|
return this.finishUpdate(attribute, store, scene, state);
|
|
329
353
|
};
|
|
330
354
|
|
|
331
355
|
return processChunk();
|
|
332
356
|
}
|
|
333
357
|
|
|
358
|
+
/**
|
|
359
|
+
* Repositions the item nodes' DOM elements so the rendered order matches the
|
|
360
|
+
* source array order. The keyed diff (steps 1–4) reuses existing nodes in
|
|
361
|
+
* place and mounts new ones at the end; without this pass a `prepend` or
|
|
362
|
+
* `shuffle` would leave reused rows where they were and pile new rows at the
|
|
363
|
+
* bottom. We walk the desired order RIGHT-TO-LEFT, keeping a `ref` pointer to
|
|
364
|
+
* the element each item must precede (starting at the `$for` anchor comment),
|
|
365
|
+
* and only call `insertBefore` when an element is not already in position —
|
|
366
|
+
* so a plain `append` (already-correct order) performs ZERO DOM moves.
|
|
367
|
+
*/
|
|
368
|
+
private reconcileOrder(
|
|
369
|
+
owner: AreHTMLNode,
|
|
370
|
+
orderedKeys: any[],
|
|
371
|
+
finalByKey: Map<any, AreHTMLNode>,
|
|
372
|
+
): void {
|
|
373
|
+
const context = owner.scope.resolve<AreHTMLEngineContext>(AreHTMLEngineContext);
|
|
374
|
+
if (!context) return;
|
|
375
|
+
|
|
376
|
+
const anchor = context.getNodeElement(owner);
|
|
377
|
+
if (!anchor || !anchor.parentNode) return;
|
|
378
|
+
|
|
379
|
+
const parent = anchor.parentNode;
|
|
380
|
+
let ref: Node = anchor;
|
|
381
|
+
|
|
382
|
+
for (let i = orderedKeys.length - 1; i >= 0; i--) {
|
|
383
|
+
const node = finalByKey.get(orderedKeys[i]);
|
|
384
|
+
if (!node) continue;
|
|
385
|
+
|
|
386
|
+
const element = context.getNodeElement(node);
|
|
387
|
+
// Element may be missing if the item is still detached/unmounted.
|
|
388
|
+
if (!element || element.parentNode !== parent) continue;
|
|
389
|
+
|
|
390
|
+
// Already immediately before `ref` — no move needed.
|
|
391
|
+
if (element.nextSibling !== ref) {
|
|
392
|
+
parent.insertBefore(element, ref);
|
|
393
|
+
}
|
|
394
|
+
ref = element;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
|
|
334
399
|
/**
|
|
335
400
|
* Completes an update pass. If another update() arrived while a chunked
|
|
336
401
|
* render was streaming, run exactly one more pass now from the latest store
|
|
@@ -441,14 +506,32 @@ export class AreDirectiveFor extends AreDirective {
|
|
|
441
506
|
* Supports both plain key lookups and function-call expressions:
|
|
442
507
|
* items → store.get('items')
|
|
443
508
|
* filter(items) → store.get('filter')(store.get('items'))
|
|
509
|
+
*
|
|
510
|
+
* `contextScope` carries item-scoped variables introduced by an enclosing
|
|
511
|
+
* directive (e.g. the `row` of an outer `$for`). It is consulted BEFORE the
|
|
512
|
+
* store so a nested `$for="cell in row.cells"` resolves `row` from the
|
|
513
|
+
* parent iteration instead of looking for a (non-existent) top-level store
|
|
514
|
+
* key. Leading identifiers not present in the context fall back to the store.
|
|
444
515
|
*/
|
|
445
|
-
private resolveArray(
|
|
516
|
+
private resolveArray(
|
|
517
|
+
store: AreStore,
|
|
518
|
+
arrayExpr: string,
|
|
519
|
+
fullContent: string,
|
|
520
|
+
contextScope: Record<string, any> = {},
|
|
521
|
+
): any[] {
|
|
522
|
+
// Resolve a leading identifier from the directive context first, then
|
|
523
|
+
// the store — mirrors how bindings/interpolations evaluate scoped vars.
|
|
524
|
+
const getRoot = (rawKey: string): any => {
|
|
525
|
+
const k = rawKey.replace(/\?$/, '');
|
|
526
|
+
return (k in contextScope) ? contextScope[k] : store.get(k as any);
|
|
527
|
+
};
|
|
528
|
+
|
|
446
529
|
let result: any;
|
|
447
530
|
const callMatch = arrayExpr.match(/^([^(]+)\((.+)\)$/);
|
|
448
531
|
|
|
449
532
|
if (callMatch) {
|
|
450
533
|
const fnName = callMatch[1].trim();
|
|
451
|
-
const fn =
|
|
534
|
+
const fn = getRoot(fnName);
|
|
452
535
|
|
|
453
536
|
if (typeof fn !== 'function')
|
|
454
537
|
throw new AreCompilerError({
|
|
@@ -465,14 +548,14 @@ export class AreDirectiveFor extends AreDirective {
|
|
|
465
548
|
const stripped = arg.replace(/\?$/, '');
|
|
466
549
|
if (stripped.includes('.')) {
|
|
467
550
|
const parts = stripped.split('.').map(p => p.replace(/\?$/, ''));
|
|
468
|
-
let val: any =
|
|
551
|
+
let val: any = getRoot(parts[0]);
|
|
469
552
|
for (let j = 1; j < parts.length; j++) {
|
|
470
553
|
if (val == null) return undefined;
|
|
471
554
|
val = val[parts[j]];
|
|
472
555
|
}
|
|
473
556
|
return val ?? undefined;
|
|
474
557
|
}
|
|
475
|
-
return
|
|
558
|
+
return getRoot(stripped);
|
|
476
559
|
});
|
|
477
560
|
|
|
478
561
|
result = (fn as Function)(...resolvedArgs);
|
|
@@ -481,13 +564,13 @@ export class AreDirectiveFor extends AreDirective {
|
|
|
481
564
|
// Strip optional-chaining `?` suffix from each segment so that
|
|
482
565
|
// `record?.keywords` resolves the same as `record.keywords`.
|
|
483
566
|
const parts = arrayExpr.split('.').map(p => p.replace(/\?$/, ''));
|
|
484
|
-
result =
|
|
567
|
+
result = getRoot(parts[0]);
|
|
485
568
|
for (let i = 1; i < parts.length; i++) {
|
|
486
569
|
if (result == null) break;
|
|
487
570
|
result = result[parts[i]];
|
|
488
571
|
}
|
|
489
572
|
} else {
|
|
490
|
-
result =
|
|
573
|
+
result = getRoot(arrayExpr);
|
|
491
574
|
}
|
|
492
575
|
|
|
493
576
|
// null / undefined from optional-chaining expressions (e.g. `record?.keywords`)
|
|
@@ -13,6 +13,7 @@ import { AddAttributeInstruction} from "@adaas/are-html/instructions/AddAttribut
|
|
|
13
13
|
import { AddTextInstruction} from "@adaas/are-html/instructions/AddText.instruction";
|
|
14
14
|
import { AddListenerInstruction} from "@adaas/are-html/instructions/AddListener.instruction";
|
|
15
15
|
import { AddStyleInstruction } from "@adaas/are-html/instructions/AddStyle.instruction";
|
|
16
|
+
import { AddStaticHTMLInstruction } from "@adaas/are-html/instructions/AddStaticHTML.instruction";
|
|
16
17
|
import { AreHTMLNode } from "@adaas/are-html/node";
|
|
17
18
|
|
|
18
19
|
|
|
@@ -39,6 +40,18 @@ export class AreHTMLCompiler extends AreCompiler {
|
|
|
39
40
|
): void {
|
|
40
41
|
super.compile(node, scene, logger, ...args);
|
|
41
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Static-island materialisation. When the tokenizer flagged this node as
|
|
45
|
+
* a static island its inner subtree was never exploded into child nodes,
|
|
46
|
+
* so there is nothing for the base compiler to walk. Emit a single
|
|
47
|
+
* AddStaticHTML instruction carrying the captured inner markup; the
|
|
48
|
+
* interpreter injects it onto the host element in one pass (and decodes
|
|
49
|
+
* HTML entities for free).
|
|
50
|
+
*/
|
|
51
|
+
if (node.isStaticIsland && scene.host) {
|
|
52
|
+
scene.plan(new AddStaticHTMLInstruction(scene.host, { html: node.staticInnerHTML! }));
|
|
53
|
+
}
|
|
54
|
+
|
|
42
55
|
if (node.styles?.styles) {
|
|
43
56
|
const host = scene.host;
|
|
44
57
|
if (host) {
|
|
@@ -181,3 +181,145 @@ export function toDOMString(value: any): string {
|
|
|
181
181
|
}
|
|
182
182
|
|
|
183
183
|
|
|
184
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
185
|
+
// ── Static-island detection ──────────────────────────────────────────────────
|
|
186
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Standard HTML element names that are safe to materialise wholesale via
|
|
190
|
+
* `innerHTML` / a cached `<template>` clone.
|
|
191
|
+
*
|
|
192
|
+
* The set is intentionally an allow-list of plain HTML flow/phrasing/table/list
|
|
193
|
+
* /form-display tags. Anything NOT in this set — custom elements, registered
|
|
194
|
+
* ARE components (resolved by PascalCase tag), and SVG/MathML elements — is
|
|
195
|
+
* excluded so those subtrees keep flowing through the normal per-node pipeline
|
|
196
|
+
* (SVG needs createElementNS; components need their own lifecycle).
|
|
197
|
+
*/
|
|
198
|
+
export const STANDARD_HTML_TAGS = new Set<string>([
|
|
199
|
+
// root / sections
|
|
200
|
+
'html', 'body', 'header', 'footer', 'main', 'nav', 'section', 'article',
|
|
201
|
+
'aside', 'address', 'hgroup',
|
|
202
|
+
// headings
|
|
203
|
+
'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
|
|
204
|
+
// grouping
|
|
205
|
+
'div', 'p', 'span', 'pre', 'blockquote', 'figure', 'figcaption',
|
|
206
|
+
'hr', 'br', 'wbr',
|
|
207
|
+
// lists
|
|
208
|
+
'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'menu',
|
|
209
|
+
// text-level / phrasing
|
|
210
|
+
'a', 'b', 'i', 'u', 's', 'em', 'strong', 'small', 'mark', 'abbr', 'cite',
|
|
211
|
+
'q', 'code', 'kbd', 'samp', 'var', 'sub', 'sup', 'time', 'data', 'dfn',
|
|
212
|
+
'bdi', 'bdo', 'ruby', 'rt', 'rp', 'del', 'ins',
|
|
213
|
+
// media / embedded (no special namespace handling needed)
|
|
214
|
+
'img', 'picture', 'source', 'figure', 'audio', 'video', 'track',
|
|
215
|
+
// tables
|
|
216
|
+
'table', 'caption', 'colgroup', 'col', 'thead', 'tbody', 'tfoot',
|
|
217
|
+
'tr', 'th', 'td',
|
|
218
|
+
// forms (display only — these still render fine from innerHTML)
|
|
219
|
+
'label', 'fieldset', 'legend', 'datalist', 'option', 'optgroup', 'output',
|
|
220
|
+
'progress', 'meter',
|
|
221
|
+
// interactive
|
|
222
|
+
'details', 'summary', 'dialog',
|
|
223
|
+
]);
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Detects whether an inner-markup string is a fully *static island* — i.e. it
|
|
227
|
+
* contains no ARE-reactive constructs and therefore can be rendered in one shot
|
|
228
|
+
* (browser-parsed `innerHTML` / cached `<template>` clone) instead of being
|
|
229
|
+
* exploded into one AreNode per element/text/interpolation.
|
|
230
|
+
*
|
|
231
|
+
* A subtree is static iff it contains:
|
|
232
|
+
* 1. no `{{ }}` interpolations, and
|
|
233
|
+
* 2. no dynamic attributes (`$`-directive / `:`-binding / `@`-event), and
|
|
234
|
+
* 3. only standard HTML tags (no custom elements, ARE components or SVG).
|
|
235
|
+
*
|
|
236
|
+
* The scanner is quote-aware so a `:` / `@` / `$` inside an attribute *value*
|
|
237
|
+
* (e.g. `href="http://…"`, `style="color:red"`) is never mistaken for a
|
|
238
|
+
* dynamic-attribute prefix. The detector is deliberately conservative: any
|
|
239
|
+
* ambiguity resolves to `false` (skip the optimisation, keep the safe path).
|
|
240
|
+
*
|
|
241
|
+
* NOTE: pure-text content (no tags at all) is also considered static — this is
|
|
242
|
+
* what lets ` `, `&`, ` ` and friends decode correctly, since the
|
|
243
|
+
* browser HTML parser handles entities that hand-built text nodes do not.
|
|
244
|
+
*/
|
|
245
|
+
export function isStaticMarkup(inner: string): boolean {
|
|
246
|
+
if (!inner) return false;
|
|
247
|
+
// 1. interpolations make the subtree dynamic
|
|
248
|
+
if (inner.indexOf('{{') !== -1) return false;
|
|
249
|
+
|
|
250
|
+
const n = inner.length;
|
|
251
|
+
let i = 0;
|
|
252
|
+
|
|
253
|
+
while (i < n) {
|
|
254
|
+
const lt = inner.indexOf('<', i);
|
|
255
|
+
if (lt === -1) break; // remaining content is plain text — safe
|
|
256
|
+
|
|
257
|
+
// HTML comment — inert, skip over it
|
|
258
|
+
if (inner.startsWith('<!--', lt)) {
|
|
259
|
+
const end = inner.indexOf('-->', lt + 4);
|
|
260
|
+
if (end === -1) return false; // malformed
|
|
261
|
+
i = end + 3;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// closing tag, doctype or processing instruction — skip to its '>'
|
|
266
|
+
if (inner[lt + 1] === '/' || inner[lt + 1] === '!' || inner[lt + 1] === '?') {
|
|
267
|
+
const gt = inner.indexOf('>', lt);
|
|
268
|
+
if (gt === -1) return false;
|
|
269
|
+
i = gt + 1;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// opening tag — extract the tag name
|
|
274
|
+
const nameMatch = /^<([a-zA-Z][a-zA-Z0-9-]*)/.exec(inner.slice(lt));
|
|
275
|
+
if (!nameMatch) { i = lt + 1; continue; }
|
|
276
|
+
|
|
277
|
+
const tag = nameMatch[1].toLowerCase();
|
|
278
|
+
// custom element / ARE component / non-standard (incl. SVG) → not static
|
|
279
|
+
if (tag.indexOf('-') !== -1 || !STANDARD_HTML_TAGS.has(tag)) return false;
|
|
280
|
+
|
|
281
|
+
// walk the opening tag (quote-aware) to find its closing '>' and inspect
|
|
282
|
+
// attribute-name boundaries for dynamic prefixes
|
|
283
|
+
let j = lt + nameMatch[0].length;
|
|
284
|
+
let inSingle = false;
|
|
285
|
+
let inDouble = false;
|
|
286
|
+
let atNameBoundary = true; // true right after whitespace / '/' inside a tag
|
|
287
|
+
let tagEnd = -1;
|
|
288
|
+
|
|
289
|
+
while (j < n) {
|
|
290
|
+
const ch = inner[j];
|
|
291
|
+
|
|
292
|
+
if (inDouble) {
|
|
293
|
+
if (ch === '"') inDouble = false;
|
|
294
|
+
} else if (inSingle) {
|
|
295
|
+
if (ch === "'") inSingle = false;
|
|
296
|
+
} else if (ch === '"') {
|
|
297
|
+
inDouble = true;
|
|
298
|
+
atNameBoundary = false;
|
|
299
|
+
} else if (ch === "'") {
|
|
300
|
+
inSingle = true;
|
|
301
|
+
atNameBoundary = false;
|
|
302
|
+
} else if (ch === '>') {
|
|
303
|
+
tagEnd = j;
|
|
304
|
+
break;
|
|
305
|
+
} else if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r' || ch === '/') {
|
|
306
|
+
atNameBoundary = true;
|
|
307
|
+
} else {
|
|
308
|
+
// a dynamic-attribute prefix only counts when it STARTS an
|
|
309
|
+
// attribute name (i.e. sits at a name boundary, outside quotes)
|
|
310
|
+
if (atNameBoundary && (ch === '$' || ch === ':' || ch === '@')) {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
atNameBoundary = false;
|
|
314
|
+
}
|
|
315
|
+
j++;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (tagEnd === -1) return false; // unterminated tag — bail to safe path
|
|
319
|
+
i = tagEnd + 1;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
|
|
@@ -54,6 +54,38 @@ export class AreHTMLEngineContext extends AreContext {
|
|
|
54
54
|
*/
|
|
55
55
|
protected _container: Document;
|
|
56
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Parsed-fragment cache for static islands (see AddStaticHTMLInstruction).
|
|
59
|
+
*
|
|
60
|
+
* Keyed by `hostTag\u0000markup`, each entry holds a `DocumentFragment` whose
|
|
61
|
+
* children were parsed by the browser exactly once — in the *correct element
|
|
62
|
+
* context* (the host tag), so table fragments (`<tr>`, `<td>`, …) and other
|
|
63
|
+
* context-sensitive content parse correctly. Repeated static islands with
|
|
64
|
+
* identical markup (e.g. list rows, reused components) clone the pre-parsed
|
|
65
|
+
* fragment instead of re-parsing the HTML string on every mount — turning an
|
|
66
|
+
* O(parse) operation into an O(clone) one.
|
|
67
|
+
*/
|
|
68
|
+
protected _staticFragmentCache = new Map<string, DocumentFragment>();
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Live-DOM attachments deferred while a mount pass is batching.
|
|
72
|
+
*
|
|
73
|
+
* A freshly-mounted subtree is built inside a *detached* root element, so
|
|
74
|
+
* every descendant `appendChild`/`insertBefore` happens off-document and
|
|
75
|
+
* triggers zero layout/paint invalidation. The single mutation that actually
|
|
76
|
+
* connects the built subtree to the live document is deferred and collected
|
|
77
|
+
* here, then flushed once when the batch closes — collapsing O(nodes) reflows
|
|
78
|
+
* into O(1) per mount root.
|
|
79
|
+
*/
|
|
80
|
+
protected _pendingAttachments: Array<() => void> = [];
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Depth of the currently open batching scopes. Re-entrant so that nested
|
|
84
|
+
* `beginBatch`/`endBatch` pairs flush exactly once, when the outermost scope
|
|
85
|
+
* closes.
|
|
86
|
+
*/
|
|
87
|
+
protected _batchDepth = 0;
|
|
88
|
+
|
|
57
89
|
|
|
58
90
|
constructor(props: Partial<AreHTMLContextConstructor>) {
|
|
59
91
|
super(props.container?.body.innerHTML || props.source || '');
|
|
@@ -64,6 +96,86 @@ export class AreHTMLEngineContext extends AreContext {
|
|
|
64
96
|
return this._container;
|
|
65
97
|
}
|
|
66
98
|
|
|
99
|
+
/**
|
|
100
|
+
* `true` while a synchronous mount pass is batching live-DOM attachments.
|
|
101
|
+
* Interpreter handlers consult this to decide whether to attach an element
|
|
102
|
+
* immediately or hand the attachment to {@link deferAttach}.
|
|
103
|
+
*/
|
|
104
|
+
get isBatching(): boolean {
|
|
105
|
+
return this._batchDepth > 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Opens a batching scope. Re-entrant: only the outermost matching
|
|
110
|
+
* {@link endBatch} flushes the deferred attachments, so a single mount pass
|
|
111
|
+
* connects its built subtree to the live DOM exactly once.
|
|
112
|
+
*/
|
|
113
|
+
beginBatch(): void {
|
|
114
|
+
this._batchDepth++;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Registers a live-DOM attachment to run when the current batch flushes. If
|
|
119
|
+
* no batch is active the attachment runs immediately, preserving the original
|
|
120
|
+
* synchronous behaviour for updates that mount outside a batch.
|
|
121
|
+
*
|
|
122
|
+
* @param attach the DOM mutation that connects a built subtree to the document
|
|
123
|
+
*/
|
|
124
|
+
deferAttach(attach: () => void): void {
|
|
125
|
+
if (this._batchDepth > 0) {
|
|
126
|
+
this._pendingAttachments.push(attach);
|
|
127
|
+
} else {
|
|
128
|
+
attach();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Closes a batching scope. When the outermost scope closes, every deferred
|
|
134
|
+
* attachment runs in registration (document) order, connecting the built
|
|
135
|
+
* subtrees to the live DOM in a single pass.
|
|
136
|
+
*/
|
|
137
|
+
endBatch(): void {
|
|
138
|
+
if (this._batchDepth === 0) return;
|
|
139
|
+
this._batchDepth--;
|
|
140
|
+
if (this._batchDepth > 0) return;
|
|
141
|
+
|
|
142
|
+
const pending = this._pendingAttachments;
|
|
143
|
+
this._pendingAttachments = [];
|
|
144
|
+
for (let i = 0; i < pending.length; i++) {
|
|
145
|
+
pending[i]();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Returns a `DocumentFragment` containing the parsed form of `html`, parsed
|
|
151
|
+
* once in the context of `hostTag` (so context-sensitive content such as
|
|
152
|
+
* table rows/cells parses correctly) and cached thereafter. Callers should
|
|
153
|
+
* `cloneNode(true)` the returned fragment rather than mutating it, so the
|
|
154
|
+
* cache stays reusable.
|
|
155
|
+
*
|
|
156
|
+
* @param hostTag the tag name of the element the markup will be injected into
|
|
157
|
+
* @param html verbatim static-island inner markup
|
|
158
|
+
*/
|
|
159
|
+
getStaticFragment(hostTag: string, html: string): DocumentFragment {
|
|
160
|
+
const key = `${hostTag}\u0000${html}`;
|
|
161
|
+
let fragment = this._staticFragmentCache.get(key);
|
|
162
|
+
if (!fragment) {
|
|
163
|
+
// Parse in the correct element context: the fragment-parsing
|
|
164
|
+
// algorithm uses the container element's tag to choose the right
|
|
165
|
+
// insertion mode (e.g. `<tbody>` legitimately allows `<tr>`).
|
|
166
|
+
const container = this._container.createElement(hostTag);
|
|
167
|
+
container.innerHTML = html;
|
|
168
|
+
|
|
169
|
+
fragment = this._container.createDocumentFragment();
|
|
170
|
+
while (container.firstChild) {
|
|
171
|
+
fragment.appendChild(container.firstChild);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
this._staticFragmentCache.set(key, fragment);
|
|
175
|
+
}
|
|
176
|
+
return fragment;
|
|
177
|
+
}
|
|
178
|
+
|
|
67
179
|
|
|
68
180
|
/**
|
|
69
181
|
* Retrieves the DOM element associated with a given AreNode. This method looks up the node's ASEID in the nodeToHostElements map to find the corresponding DOM element. If the node is not found, it returns undefined. This allows the engine to efficiently access and manipulate the DOM elements that correspond to specific nodes in the AreNode tree, enabling dynamic updates and interactions based on the application state.
|