@cube-drone/marquee-react-renderer 0.9.2 → 0.10.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/dist/render.js CHANGED
@@ -22,6 +22,16 @@ function stringHook(hook, name, attrs, children, profile) {
22
22
  const childrenHtml = children.map((child) => renderHtml(child, profile)).join("");
23
23
  return hook(name, attrs, childrenHtml);
24
24
  }
25
+ /** Would embedder vocabulary claim this node? A probe with empty children,
26
+ * through whichever hook path would actually run (the React hook wins). */
27
+ function claimed(kind, name, attrs, ctx) {
28
+ const reactHook = ctx.hooks[kind];
29
+ if (reactHook !== undefined) {
30
+ const claim = reactHook(name, attrs, null);
31
+ return claim !== undefined && claim !== null;
32
+ }
33
+ return ctx.profile[kind](name, attrs, "") !== null;
34
+ }
25
35
  /** Effects whose elements the visibility observer watches, and which the
26
36
  * skip gesture stills. Named, closed: the vocabulary decides, not the DOM. */
27
37
  const ANIMATED = new Set([
@@ -47,12 +57,12 @@ function isColorValue(v) {
47
57
  function isToken(v) {
48
58
  return v !== undefined && TOKEN.test(v);
49
59
  }
50
- const MEDIA_SIZE_TOKENS = {
60
+ const MEDIA_SIZE_TOKENS = Object.assign(Object.create(null), {
51
61
  small: "10rem",
52
62
  medium: "20rem",
53
63
  large: "32rem",
54
64
  full: "100%",
55
- };
65
+ });
56
66
  function mediaSize(v) {
57
67
  if (v === undefined) {
58
68
  return null;
@@ -69,8 +79,10 @@ function mediaSize(v) {
69
79
  }
70
80
  /** A URL safe inside a CSS url('...') token: author bytes must not write
71
81
  * CSS, even in their own style attribute. */
82
+ // encodeURI throws on a lone surrogate; an embedder's media() may pass one.
83
+ const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g;
72
84
  function cssUrl(url) {
73
- return encodeURI(url).replaceAll("'", "%27").replaceAll("(", "%28").replaceAll(")", "%29");
85
+ return encodeURI(url.replace(LONE_SURROGATE, "\uFFFD")).replaceAll("'", "%27").replaceAll("(", "%28").replaceAll(")", "%29");
74
86
  }
75
87
  /** Style knobs -> React style object (custom properties are legal keys). */
76
88
  function styleVars(attrs, profile) {
@@ -182,7 +194,7 @@ export function renderNode(node, ctx, key) {
182
194
  case "link": {
183
195
  const inner = renderChildren(node.children, ctx);
184
196
  return ctx.profile.linkAllowed(node.target)
185
- ? h("a", nodeProps(node, ctx, { key, href: node.target }), ...inner)
197
+ ? h("a", nodeProps(node, ctx, { key, href: href(node.target, ctx) }), ...inner)
186
198
  : h("span", nodeProps(node, ctx, { key, className: "mq-blocked" }), ...inner);
187
199
  }
188
200
  case "embed":
@@ -261,6 +273,10 @@ function embed(node, ctx, key) {
261
273
  return h("video", nodeProps(node, ctx, { ...common, ...label, src: media.url, controls: true }));
262
274
  }
263
275
  const TURBOLINK_LEVELS = new Set(["full", "title", "bare"]);
276
+ /** Where an allowed link points: the profile's rewrite, or the target. */
277
+ function href(target, ctx) {
278
+ return ctx.profile.linkTarget?.(target) ?? target;
279
+ }
264
280
  function turbolink(node, target, levelAttr, ctx, key) {
265
281
  if (!ctx.profile.linkAllowed(target)) {
266
282
  return h("p", nodeProps(node, ctx, { key, className: "mq-turbolink" }), target);
@@ -274,21 +290,19 @@ function turbolink(node, target, levelAttr, ctx, key) {
274
290
  if (react !== null || html !== null) {
275
291
  // Enrichment augments, never replaces: the wrapper carries the
276
292
  // original link, so no plugin can eat it - not even by accident.
277
- return h("div", nodeProps(node, ctx, { key, className: "mq-turbolink mq-turbolink-rich" }), react ?? trustedHtml("span", html, "rich"), h("a", { key: "src", className: "mq-turbolink-source", href: target }, target));
293
+ return h("div", nodeProps(node, ctx, { key, className: "mq-turbolink mq-turbolink-rich" }), react ?? trustedHtml("span", html, "rich"), h("a", { key: "src", className: "mq-turbolink-source", href: href(target, ctx) }, target));
278
294
  }
279
295
  }
280
- return h("p", nodeProps(node, ctx, { key, className: "mq-turbolink" }), h("a", { href: target }, target));
296
+ return h("p", nodeProps(node, ctx, { key, className: "mq-turbolink" }), h("a", { href: href(target, ctx) }, target));
281
297
  }
282
298
  function directive(node, ctx, key) {
283
299
  const { name, attrs } = node;
284
- // The hooks see the children rendered; built-ins that lay their children
285
- // out themselves (table, stack) render them again, so rewind the aside
286
- // numbering first - or a note inside counts twice.
287
- const saved = { n: ctx.note.n, pending: [...ctx.note.pending] };
288
- const rewind = () => {
289
- ctx.note.n = saved.n;
290
- ctx.note.pending = [...saved.pending];
291
- };
300
+ // Built-ins that lay their children out themselves (table, stack) probe
301
+ // the hooks instead of rendering the children for them: rendering them
302
+ // twice per level is exponential in nesting, and a note would count twice.
303
+ if ((name === "table" || name === "stack") && !claimed("directive", name, attrs, ctx)) {
304
+ return name === "table" ? renderTable(node, ctx, key) : renderStack(node, ctx, key);
305
+ }
292
306
  const kids = renderChildren(node.children, ctx);
293
307
  const custom = ctx.hooks.directive?.(name, attrs, kids);
294
308
  if (custom !== undefined && custom !== null) {
@@ -335,12 +349,6 @@ function directive(node, ctx, key) {
335
349
  }
336
350
  return h("div", nodeProps(node, ctx, { key, className: "mq-media", style }), ...kids);
337
351
  }
338
- case "table":
339
- rewind();
340
- return renderTable(node, ctx, key);
341
- case "stack":
342
- rewind();
343
- return renderStack(node, ctx, key);
344
352
  case "layer":
345
353
  // Outside a stack a layer is just a section-like container: its style
346
354
  // knobs apply, place/backing mean nothing.
@@ -426,13 +434,13 @@ const PLACES = new Set([
426
434
  "fill", "top-left", "top", "top-right", "left", "center", "right", "bottom-left", "bottom", "bottom-right",
427
435
  ]);
428
436
  /** The `aspect` vocabulary as CSS aspect-ratio values. */
429
- const STACK_ASPECTS = {
437
+ const STACK_ASPECTS = Object.assign(Object.create(null), {
430
438
  "16:9": "16/9",
431
439
  "4:3": "4/3",
432
440
  "3:2": "3/2",
433
441
  "1:1": "1/1",
434
442
  card: "5/7",
435
- };
443
+ });
436
444
  /** :::stack - every direct child is a layer, source order is z-order; the
437
445
  * stylesheet puts every layer in one grid cell (SPEC.md, "Stacks"). Layers
438
446
  * past the cap render in flow below the box. */
@@ -485,6 +493,13 @@ function sizeRung(value, inner, node, ctx, key) {
485
493
  }
486
494
  function span(node, ctx, key) {
487
495
  const { name, attrs } = node;
496
+ // A split effect renders its children itself (splitRender): probe the
497
+ // hooks rather than render them here too, which doubled the work - and
498
+ // every note inside - at each nesting level.
499
+ const look = effectLook(name, attrs, node.children);
500
+ if (look !== null && look.units !== null && !claimed("span", name, attrs, ctx)) {
501
+ return effect(node, ctx, key);
502
+ }
488
503
  const kids = renderChildren(node.children, ctx);
489
504
  const custom = ctx.hooks.span?.(name, attrs, kids);
490
505
  if (custom !== undefined && custom !== null) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cube-drone/marquee-react-renderer",
3
- "version": "0.9.2",
3
+ "version": "0.10.0",
4
4
  "description": "Interactive React renderer for the Marquee markup language: the JS halves of the animation contract (start-on-visible, tap-to-skip) plus source-position scroll sync for side-by-side editors",
5
5
  "license": "MPL-2.0",
6
6
  "repository": {
@@ -34,8 +34,8 @@
34
34
  "demo": "node --conditions=marquee-src scripts/demo.ts"
35
35
  },
36
36
  "dependencies": {
37
- "@cube-drone/marquee-html-renderer": "^0.9.2",
38
- "@cube-drone/marquee-parser": "^0.9.2"
37
+ "@cube-drone/marquee-html-renderer": "^0.10.0",
38
+ "@cube-drone/marquee-parser": "^0.10.0"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "react": ">=18"
@@ -48,9 +48,9 @@
48
48
  "react": "^19.0.0",
49
49
  "react-dom": "^19.0.0",
50
50
  "typescript": "^5.9.0",
51
- "@cube-drone/marquee-css": "^0.9.2",
52
- "@cube-drone/marquee-fonts": "^0.9.2",
53
- "@cube-drone/marquee-emoji": "^0.9.2",
54
- "@cube-drone/marquee-turbolink": "^0.9.2"
51
+ "@cube-drone/marquee-css": "^0.10.0",
52
+ "@cube-drone/marquee-fonts": "^0.10.0",
53
+ "@cube-drone/marquee-emoji": "^0.10.0",
54
+ "@cube-drone/marquee-turbolink": "^0.10.0"
55
55
  }
56
56
  }
package/src/render.ts CHANGED
@@ -41,6 +41,17 @@ function stringHook(
41
41
  return hook(name, attrs, childrenHtml);
42
42
  }
43
43
 
44
+ /** Would embedder vocabulary claim this node? A probe with empty children,
45
+ * through whichever hook path would actually run (the React hook wins). */
46
+ function claimed(kind: "directive" | "span", name: string, attrs: Attrs, ctx: Ctx): boolean {
47
+ const reactHook = ctx.hooks[kind];
48
+ if (reactHook !== undefined) {
49
+ const claim = reactHook(name, attrs, null);
50
+ return claim !== undefined && claim !== null;
51
+ }
52
+ return ctx.profile[kind](name, attrs, "") !== null;
53
+ }
54
+
44
55
  /** React-returning versions of the rendering hooks. When present these win
45
56
  * over the string-returning `Profile` hooks (no innerHTML at all). */
46
57
  export interface ReactHooks {
@@ -89,12 +100,12 @@ function isToken(v: string | undefined): v is string {
89
100
  return v !== undefined && TOKEN.test(v);
90
101
  }
91
102
 
92
- const MEDIA_SIZE_TOKENS: Record<string, string> = {
103
+ const MEDIA_SIZE_TOKENS: Record<string, string> = Object.assign(Object.create(null), {
93
104
  small: "10rem",
94
105
  medium: "20rem",
95
106
  large: "32rem",
96
107
  full: "100%",
97
- };
108
+ });
98
109
 
99
110
  function mediaSize(v: string | undefined): string | null {
100
111
  if (v === undefined) {
@@ -113,8 +124,11 @@ function mediaSize(v: string | undefined): string | null {
113
124
 
114
125
  /** A URL safe inside a CSS url('...') token: author bytes must not write
115
126
  * CSS, even in their own style attribute. */
127
+ // encodeURI throws on a lone surrogate; an embedder's media() may pass one.
128
+ const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g;
129
+
116
130
  function cssUrl(url: string): string {
117
- return encodeURI(url).replaceAll("'", "%27").replaceAll("(", "%28").replaceAll(")", "%29");
131
+ return encodeURI(url.replace(LONE_SURROGATE, "\uFFFD")).replaceAll("'", "%27").replaceAll("(", "%28").replaceAll(")", "%29");
118
132
  }
119
133
 
120
134
  /** Style knobs -> React style object (custom properties are legal keys). */
@@ -254,7 +268,7 @@ export function renderNode(node: Node, ctx: Ctx, key: string): ReactNode {
254
268
  case "link": {
255
269
  const inner = renderChildren(node.children, ctx);
256
270
  return ctx.profile.linkAllowed(node.target)
257
- ? h("a", nodeProps(node, ctx, { key, href: node.target }), ...inner)
271
+ ? h("a", nodeProps(node, ctx, { key, href: href(node.target, ctx) }), ...inner)
258
272
  : h("span", nodeProps(node, ctx, { key, className: "mq-blocked" }), ...inner);
259
273
  }
260
274
  case "embed":
@@ -353,6 +367,11 @@ function embed(node: Node & { type: "embed" }, ctx: Ctx, key: string): ReactNode
353
367
 
354
368
  const TURBOLINK_LEVELS = new Set<TurbolinkLevel>(["full", "title", "bare"]);
355
369
 
370
+ /** Where an allowed link points: the profile's rewrite, or the target. */
371
+ function href(target: string, ctx: Ctx): string {
372
+ return ctx.profile.linkTarget?.(target) ?? target;
373
+ }
374
+
356
375
  function turbolink(
357
376
  node: Node,
358
377
  target: string,
@@ -377,27 +396,25 @@ function turbolink(
377
396
  "div",
378
397
  nodeProps(node, ctx, { key, className: "mq-turbolink mq-turbolink-rich" }),
379
398
  react ?? trustedHtml("span", html!, "rich"),
380
- h("a", { key: "src", className: "mq-turbolink-source", href: target }, target),
399
+ h("a", { key: "src", className: "mq-turbolink-source", href: href(target, ctx) }, target),
381
400
  );
382
401
  }
383
402
  }
384
403
  return h(
385
404
  "p",
386
405
  nodeProps(node, ctx, { key, className: "mq-turbolink" }),
387
- h("a", { href: target }, target),
406
+ h("a", { href: href(target, ctx) }, target),
388
407
  );
389
408
  }
390
409
 
391
410
  function directive(node: Node & { type: "directive" }, ctx: Ctx, key: string): ReactNode {
392
411
  const { name, attrs } = node;
393
- // The hooks see the children rendered; built-ins that lay their children
394
- // out themselves (table, stack) render them again, so rewind the aside
395
- // numbering first - or a note inside counts twice.
396
- const saved = { n: ctx.note.n, pending: [...ctx.note.pending] };
397
- const rewind = (): void => {
398
- ctx.note.n = saved.n;
399
- ctx.note.pending = [...saved.pending];
400
- };
412
+ // Built-ins that lay their children out themselves (table, stack) probe
413
+ // the hooks instead of rendering the children for them: rendering them
414
+ // twice per level is exponential in nesting, and a note would count twice.
415
+ if ((name === "table" || name === "stack") && !claimed("directive", name, attrs, ctx)) {
416
+ return name === "table" ? renderTable(node, ctx, key) : renderStack(node, ctx, key);
417
+ }
401
418
  const kids = renderChildren(node.children, ctx);
402
419
 
403
420
  const custom = ctx.hooks.directive?.(name, attrs, kids);
@@ -458,12 +475,6 @@ function directive(node: Node & { type: "directive" }, ctx: Ctx, key: string): R
458
475
  }
459
476
  return h("div", nodeProps(node, ctx, { key, className: "mq-media", style }), ...kids);
460
477
  }
461
- case "table":
462
- rewind();
463
- return renderTable(node, ctx, key);
464
- case "stack":
465
- rewind();
466
- return renderStack(node, ctx, key);
467
478
  case "layer":
468
479
  // Outside a stack a layer is just a section-like container: its style
469
480
  // knobs apply, place/backing mean nothing.
@@ -573,13 +584,13 @@ const PLACES = new Set([
573
584
  ]);
574
585
 
575
586
  /** The `aspect` vocabulary as CSS aspect-ratio values. */
576
- const STACK_ASPECTS: Record<string, string> = {
587
+ const STACK_ASPECTS: Record<string, string> = Object.assign(Object.create(null), {
577
588
  "16:9": "16/9",
578
589
  "4:3": "4/3",
579
590
  "3:2": "3/2",
580
591
  "1:1": "1/1",
581
592
  card: "5/7",
582
- };
593
+ });
583
594
 
584
595
  /** :::stack - every direct child is a layer, source order is z-order; the
585
596
  * stylesheet puts every layer in one grid cell (SPEC.md, "Stacks"). Layers
@@ -646,6 +657,13 @@ function sizeRung(value: string, inner: ReactNode[], node: Node, ctx: Ctx, key:
646
657
 
647
658
  function span(node: Node & { type: "span" }, ctx: Ctx, key: string): ReactNode {
648
659
  const { name, attrs } = node;
660
+ // A split effect renders its children itself (splitRender): probe the
661
+ // hooks rather than render them here too, which doubled the work - and
662
+ // every note inside - at each nesting level.
663
+ const look = effectLook(name, attrs, node.children);
664
+ if (look !== null && look.units !== null && !claimed("span", name, attrs, ctx)) {
665
+ return effect(node, ctx, key);
666
+ }
649
667
  const kids = renderChildren(node.children, ctx);
650
668
 
651
669
  const custom = ctx.hooks.span?.(name, attrs, kids);