@cube-drone/marquee-react-renderer 0.9.3 → 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) {
@@ -285,14 +297,12 @@ function turbolink(node, target, levelAttr, ctx, key) {
285
297
  }
286
298
  function directive(node, ctx, key) {
287
299
  const { name, attrs } = node;
288
- // The hooks see the children rendered; built-ins that lay their children
289
- // out themselves (table, stack) render them again, so rewind the aside
290
- // numbering first - or a note inside counts twice.
291
- const saved = { n: ctx.note.n, pending: [...ctx.note.pending] };
292
- const rewind = () => {
293
- ctx.note.n = saved.n;
294
- ctx.note.pending = [...saved.pending];
295
- };
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
+ }
296
306
  const kids = renderChildren(node.children, ctx);
297
307
  const custom = ctx.hooks.directive?.(name, attrs, kids);
298
308
  if (custom !== undefined && custom !== null) {
@@ -339,12 +349,6 @@ function directive(node, ctx, key) {
339
349
  }
340
350
  return h("div", nodeProps(node, ctx, { key, className: "mq-media", style }), ...kids);
341
351
  }
342
- case "table":
343
- rewind();
344
- return renderTable(node, ctx, key);
345
- case "stack":
346
- rewind();
347
- return renderStack(node, ctx, key);
348
352
  case "layer":
349
353
  // Outside a stack a layer is just a section-like container: its style
350
354
  // knobs apply, place/backing mean nothing.
@@ -430,13 +434,13 @@ const PLACES = new Set([
430
434
  "fill", "top-left", "top", "top-right", "left", "center", "right", "bottom-left", "bottom", "bottom-right",
431
435
  ]);
432
436
  /** The `aspect` vocabulary as CSS aspect-ratio values. */
433
- const STACK_ASPECTS = {
437
+ const STACK_ASPECTS = Object.assign(Object.create(null), {
434
438
  "16:9": "16/9",
435
439
  "4:3": "4/3",
436
440
  "3:2": "3/2",
437
441
  "1:1": "1/1",
438
442
  card: "5/7",
439
- };
443
+ });
440
444
  /** :::stack - every direct child is a layer, source order is z-order; the
441
445
  * stylesheet puts every layer in one grid cell (SPEC.md, "Stacks"). Layers
442
446
  * past the cap render in flow below the box. */
@@ -489,6 +493,13 @@ function sizeRung(value, inner, node, ctx, key) {
489
493
  }
490
494
  function span(node, ctx, key) {
491
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
+ }
492
503
  const kids = renderChildren(node.children, ctx);
493
504
  const custom = ctx.hooks.span?.(name, attrs, kids);
494
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.3",
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.3",
38
- "@cube-drone/marquee-parser": "^0.9.3"
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.3",
52
- "@cube-drone/marquee-fonts": "^0.9.3",
53
- "@cube-drone/marquee-emoji": "^0.9.3",
54
- "@cube-drone/marquee-turbolink": "^0.9.3"
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). */
@@ -395,14 +409,12 @@ function turbolink(
395
409
 
396
410
  function directive(node: Node & { type: "directive" }, ctx: Ctx, key: string): ReactNode {
397
411
  const { name, attrs } = node;
398
- // The hooks see the children rendered; built-ins that lay their children
399
- // out themselves (table, stack) render them again, so rewind the aside
400
- // numbering first - or a note inside counts twice.
401
- const saved = { n: ctx.note.n, pending: [...ctx.note.pending] };
402
- const rewind = (): void => {
403
- ctx.note.n = saved.n;
404
- ctx.note.pending = [...saved.pending];
405
- };
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
+ }
406
418
  const kids = renderChildren(node.children, ctx);
407
419
 
408
420
  const custom = ctx.hooks.directive?.(name, attrs, kids);
@@ -463,12 +475,6 @@ function directive(node: Node & { type: "directive" }, ctx: Ctx, key: string): R
463
475
  }
464
476
  return h("div", nodeProps(node, ctx, { key, className: "mq-media", style }), ...kids);
465
477
  }
466
- case "table":
467
- rewind();
468
- return renderTable(node, ctx, key);
469
- case "stack":
470
- rewind();
471
- return renderStack(node, ctx, key);
472
478
  case "layer":
473
479
  // Outside a stack a layer is just a section-like container: its style
474
480
  // knobs apply, place/backing mean nothing.
@@ -578,13 +584,13 @@ const PLACES = new Set([
578
584
  ]);
579
585
 
580
586
  /** The `aspect` vocabulary as CSS aspect-ratio values. */
581
- const STACK_ASPECTS: Record<string, string> = {
587
+ const STACK_ASPECTS: Record<string, string> = Object.assign(Object.create(null), {
582
588
  "16:9": "16/9",
583
589
  "4:3": "4/3",
584
590
  "3:2": "3/2",
585
591
  "1:1": "1/1",
586
592
  card: "5/7",
587
- };
593
+ });
588
594
 
589
595
  /** :::stack - every direct child is a layer, source order is z-order; the
590
596
  * stylesheet puts every layer in one grid cell (SPEC.md, "Stacks"). Layers
@@ -651,6 +657,13 @@ function sizeRung(value: string, inner: ReactNode[], node: Node, ctx: Ctx, key:
651
657
 
652
658
  function span(node: Node & { type: "span" }, ctx: Ctx, key: string): ReactNode {
653
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
+ }
654
667
  const kids = renderChildren(node.children, ctx);
655
668
 
656
669
  const custom = ctx.hooks.span?.(name, attrs, kids);