@native-sdk/cli 0.7.2 → 0.8.1

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.
Files changed (61) hide show
  1. package/build/app.zig +265 -65
  2. package/build/ts_run.mjs +6 -8
  3. package/build.zig +292 -349
  4. package/package.json +13 -11
  5. package/packages/core/compile-surface/core.ts +559 -0
  6. package/packages/core/package-lock.json +375 -12
  7. package/packages/core/package.json +9 -6
  8. package/packages/core/scripts/compiler_typecheck.mjs +58 -0
  9. package/packages/core/scripts/gen_case_tables.mjs +130 -0
  10. package/packages/core/scripts/gen_declarations.mjs +97 -0
  11. package/packages/core/scripts/run_external_core_compiler.mjs +108 -0
  12. package/packages/core/scripts/stage_external_core.mjs +148 -0
  13. package/packages/core/sdk/core.d.ts +363 -0
  14. package/packages/core/sdk/events.d.ts +65 -0
  15. package/packages/core/sdk/text.d.ts +51 -0
  16. package/packages/core/src/checker.ts +3 -3
  17. package/packages/core/src/cli.ts +40 -38
  18. package/packages/core/src/contract.ts +749 -0
  19. package/packages/core/src/devhost.ts +4 -3
  20. package/packages/core/src/diagnostics.ts +5 -7
  21. package/packages/core/src/{transpile.ts → frontend.ts} +38 -50
  22. package/packages/core/src/infer.ts +1 -1
  23. package/packages/core/src/typed_ast.ts +16 -15
  24. package/packages/core/src/wyhash.ts +79 -0
  25. package/skill-data/native-ui/SKILL.md +3 -2
  26. package/skill-data/ts-core/SKILL.md +29 -29
  27. package/src/platform/macos/appkit_host.h +4 -4
  28. package/src/platform/macos/appkit_host.m +24 -4
  29. package/src/platform/macos/cef_host.mm +22 -6
  30. package/src/platform/macos/root.zig +2 -2
  31. package/src/primitives/canvas/markdown.zig +2010 -57
  32. package/src/primitives/canvas/markdown_hostile_tests.zig +40 -0
  33. package/src/primitives/canvas/markdown_tests.zig +778 -3
  34. package/src/primitives/canvas/ui_markup.zig +9 -1
  35. package/src/primitives/canvas/ui_markup_compiled.zig +29 -0
  36. package/src/primitives/canvas/ui_markup_contract.zig +11 -0
  37. package/src/primitives/canvas/ui_markup_view.zig +20 -0
  38. package/src/primitives/canvas/ui_markup_view_tests.zig +13 -1
  39. package/src/primitives/canvas/ui_schema.zig +3 -0
  40. package/src/primitives/canvas/ui_schema_tests.zig +4 -3
  41. package/src/primitives/canvas/widget_layout.zig +26 -3
  42. package/src/primitives/canvas/widget_metrics.zig +21 -3
  43. package/src/primitives/canvas/widget_render.zig +6 -1
  44. package/src/runtime/effects.zig +9 -0
  45. package/src/runtime/effects_fetch_tests.zig +21 -0
  46. package/src/runtime/ts_core_host.zig +38 -38
  47. package/src/runtime/ts_ui_app.zig +12 -12
  48. package/src/tooling/manifest.zig +32 -0
  49. package/src/tooling/raw_manifest.zig +4 -0
  50. package/src/tooling/templates.zig +25 -20
  51. package/src/tooling/ts_core.zig +55 -34
  52. package/src/tooling/verbs.zig +1 -1
  53. package/tools/corewire/core_abi.zig +169 -0
  54. package/tools/corewire/emit.zig +2418 -0
  55. package/tools/corewire/emit_facade.zig +3984 -0
  56. package/tools/corewire/emit_profile.zig +501 -0
  57. package/tools/corewire/main.zig +561 -0
  58. package/tools/corewire/shim_rt.zig +632 -0
  59. package/tools/corewire/sidecar.zig +2507 -0
  60. package/packages/core/rt/rt.zig +0 -1986
  61. package/packages/core/src/emitter.ts +0 -11898
@@ -56,6 +56,22 @@ fn findKind(widget: canvas.Widget, kind: canvas.WidgetKind) ?canvas.Widget {
56
56
  return null;
57
57
  }
58
58
 
59
+ fn findImageId(widget: canvas.Widget, image_id: canvas.ImageId) ?canvas.Widget {
60
+ if (widget.kind == .image and widget.image_id == image_id) return widget;
61
+ for (widget.children) |child| {
62
+ if (findImageId(child, image_id)) |found| return found;
63
+ }
64
+ return null;
65
+ }
66
+
67
+ fn findCellContainingImage(widget: canvas.Widget, image_id: canvas.ImageId) ?canvas.Widget {
68
+ if (widget.kind == .data_cell and findImageId(widget, image_id) != null) return widget;
69
+ for (widget.children) |child| {
70
+ if (findCellContainingImage(child, image_id)) |found| return found;
71
+ }
72
+ return null;
73
+ }
74
+
59
75
  fn appendParagraphText(widget: canvas.Widget, out: *std.ArrayListUnmanaged(u8), allocator: std.mem.Allocator) !void {
60
76
  if (widget.kind == .text and widget.spans.len > 0) {
61
77
  try out.appendSlice(allocator, widget.text);
@@ -74,6 +90,16 @@ fn hasSpan(widget: canvas.Widget, text: []const u8, color: ?canvas.TextSpanColor
74
90
  return false;
75
91
  }
76
92
 
93
+ fn findSpan(widget: canvas.Widget, text: []const u8) ?canvas.TextSpan {
94
+ for (widget.spans) |span| {
95
+ if (std.mem.eql(u8, span.text, text)) return span;
96
+ }
97
+ for (widget.children) |child| {
98
+ if (findSpan(child, text)) |span| return span;
99
+ }
100
+ return null;
101
+ }
102
+
77
103
  fn allSpansMonospace(widget: canvas.Widget) bool {
78
104
  for (widget.spans) |span| {
79
105
  if (!span.monospace) return false;
@@ -108,6 +134,19 @@ fn findKindLabel(widget: canvas.Widget, kind: canvas.WidgetKind, label: []const
108
134
  return null;
109
135
  }
110
136
 
137
+ fn countKindLabel(widget: canvas.Widget, kind: canvas.WidgetKind, label: []const u8) usize {
138
+ var count: usize = if (widget.kind == kind and
139
+ (std.mem.eql(u8, widget.semantics.label, label) or std.mem.eql(u8, widget.text, label))) 1 else 0;
140
+ for (widget.children) |child| count += countKindLabel(child, kind, label);
141
+ return count;
142
+ }
143
+
144
+ fn countRole(widget: canvas.Widget, role: canvas.WidgetRole) usize {
145
+ var count: usize = if (widget.semantics.role == role) 1 else 0;
146
+ for (widget.children) |child| count += countRole(child, role);
147
+ return count;
148
+ }
149
+
111
150
  fn findRowWithDirectParagraph(widget: canvas.Widget, fragment: []const u8) ?canvas.Widget {
112
151
  if (widget.kind == .row) {
113
152
  for (widget.children) |child| {
@@ -156,6 +195,464 @@ test "markdown maps headings, paragraphs, and inline styles onto spans" {
156
195
  try testing.expectEqualStrings("https://example.com", msg.open_url);
157
196
  }
158
197
 
198
+ test "safe GitHub-style inline HTML maps onto native spans" {
199
+ var doc = TestDoc.init();
200
+ defer doc.deinit();
201
+ const tree = try doc.build(
202
+ \\Plain <strong>bold <em>and italic</em></strong>, <code>code</code>, <del>gone</del>, <ins>new</ins>, <mark>marked</mark>, and <small>small</small>.
203
+ \\<A HREF=https://example.com/a/b>a link</A>.<BR />Next <IMG src="ignored.png" onerror="boom()" alt='diagram'> &amp; &#x2713;.
204
+ , .{ .on_link = Ui.linkMsg(.open_url) });
205
+
206
+ const paragraph = findParagraphContaining(tree.root, "Plain").?;
207
+ try testing.expectEqualStrings(
208
+ "Plain bold and italic, code, gone, new, marked, and small. a link.\nNext diagram & ✓.",
209
+ paragraph.text,
210
+ );
211
+
212
+ try testing.expectEqual(canvas.TextSpanWeight.bold, findSpan(paragraph, "bold ").?.weight);
213
+ const nested = findSpan(paragraph, "and italic").?;
214
+ try testing.expectEqual(canvas.TextSpanWeight.bold, nested.weight);
215
+ try testing.expect(nested.italic);
216
+ try testing.expect(findSpan(paragraph, "code").?.monospace);
217
+ try testing.expect(findSpan(paragraph, "gone").?.strikethrough);
218
+ try testing.expect(findSpan(paragraph, "new").?.underline);
219
+ try testing.expectEqual(canvas.TextSpanColor.surface_pressed, findSpan(paragraph, "marked").?.background.?);
220
+ try testing.expectEqual(@as(f32, 0.875), findSpan(paragraph, "small").?.scale);
221
+
222
+ const link = findSpan(paragraph, "a link").?;
223
+ try testing.expectEqualStrings("https://example.com/a/b", link.link);
224
+ const link_widget = findRoleLabel(tree.root, .link, "a link").?;
225
+ const msg = tree.msgForPointer(link_widget.id, .up).?;
226
+ try testing.expectEqualStrings("https://example.com/a/b", msg.open_url);
227
+ }
228
+
229
+ test "safe HTML decodes entities in link and image attributes" {
230
+ var doc = TestDoc.init();
231
+ defer doc.deinit();
232
+ const tree = try doc.build(
233
+ \\<a href="https://example.com/search?a=1&amp;b=2">query</a> <img alt="A &amp; B">.
234
+ , .{ .on_link = Ui.linkMsg(.open_url) });
235
+
236
+ const paragraph = findParagraphContaining(tree.root, "query").?;
237
+ try testing.expectEqualStrings("query A & B.", paragraph.text);
238
+ const link = findSpan(paragraph, "query").?;
239
+ try testing.expectEqualStrings("https://example.com/search?a=1&b=2", link.link);
240
+ const link_widget = findRoleLabel(tree.root, .link, "query").?;
241
+ const msg = tree.msgForPointer(link_widget.id, .up).?;
242
+ try testing.expectEqualStrings("https://example.com/search?a=1&b=2", msg.open_url);
243
+ }
244
+
245
+ test "empty HTML anchors do not create accessibility links" {
246
+ var doc = TestDoc.init();
247
+ defer doc.deinit();
248
+ const tree = try doc.build(
249
+ \\Before<a href="https://empty.example"><sup><img alt="" /></sup></a>After.
250
+ \\<a href="https://diagram.example"><img alt="diagram" /></a>
251
+ , .{ .on_link = Ui.linkMsg(.open_url) });
252
+
253
+ try testing.expectEqual(@as(usize, 1), countRole(tree.root, .link));
254
+ try testing.expect(findRoleLabel(tree.root, .link, "") == null);
255
+ const diagram = findRoleLabel(tree.root, .link, "diagram").?;
256
+ try testing.expectEqualStrings("https://diagram.example", tree.msgForPointer(diagram.id, .up).?.open_url);
257
+ }
258
+
259
+ test "link reference definitions stay hidden without resolving references" {
260
+ var doc = TestDoc.init();
261
+ defer doc.deinit();
262
+ const tree = try doc.build(
263
+ \\[vc]: #hash:payload
264
+ \\ [docs]: <https://example.com/a b> "A title"
265
+ \\The update leaves [vc] and [docs][] literal.
266
+ \\
267
+ \\Paragraph text
268
+ \\[not-interrupting]: /url
269
+ \\
270
+ \\[broken]:
271
+ \\[junk]: /url trailing words
272
+ \\[joined-title]: <https://example.com>"title"
273
+ \\ [too-indented]: /url
274
+ , .{});
275
+
276
+ try testing.expect(findParagraphContaining(tree.root, "#hash:payload") == null);
277
+ try testing.expect(findParagraphContaining(tree.root, "A title") == null);
278
+ try testing.expectEqualStrings(
279
+ "The update leaves [vc] and [docs][] literal.",
280
+ findParagraphContaining(tree.root, "The update").?.text,
281
+ );
282
+ try testing.expectEqualStrings(
283
+ "Paragraph text [not-interrupting]: /url",
284
+ findParagraphContaining(tree.root, "Paragraph text").?.text,
285
+ );
286
+ try testing.expect(findParagraphContaining(tree.root, "[broken]:") != null);
287
+ try testing.expect(findParagraphContaining(tree.root, "[junk]: /url trailing words") != null);
288
+ try testing.expect(findParagraphContaining(tree.root, "[joined-title]") != null);
289
+ try testing.expect(findParagraphContaining(tree.root, "[too-indented]: /url") != null);
290
+ }
291
+
292
+ test "Vercel deployment comments hide metadata and retain table links" {
293
+ var doc = TestDoc.init();
294
+ defer doc.deinit();
295
+ const tree = try doc.build(
296
+ \\[vc]: #H4enDGKDtR2Lb2lsv4EbfoqtEIHUz0WJNjyO4zsNHm8=:eyJpc01vbm9yZXBvIjp0cnVlLCJ0eXBlIjoiZ2l0aHViIn0=
297
+ \\The latest updates on your projects. Learn more about [Vercel for GitHub](https://vercel.link/github-learn-more).
298
+ \\
299
+ \\| Project | Deployment | Actions | Updated (UTC) |
300
+ \\| :--- | :----- | :------ | :------ |
301
+ \\| <a href="https://vercel.com/vercel-labs/native-sdk"><sup><img src="avatar" width="16" height="16" alt="" /></sup></a> [native-sdk](https://vercel.com/vercel-labs/native-sdk) | ![Ready](ready.svg) [Ready](https://vercel.com/deployment) | [Preview](https://preview.example) | Aug 4, 2026 8:14pm |
302
+ , .{ .on_link = Ui.linkMsg(.open_url) });
303
+
304
+ try testing.expect(findParagraphContaining(tree.root, "[vc]") == null);
305
+ try testing.expectEqualStrings(
306
+ "The latest updates on your projects. Learn more about Vercel for GitHub.",
307
+ findParagraphContaining(tree.root, "The latest updates").?.text,
308
+ );
309
+ try testing.expectEqual(@as(usize, 1), countKind(tree.root, .table));
310
+ try testing.expectEqual(@as(usize, 4), countRole(tree.root, .link));
311
+ try testing.expect(findRoleLabel(tree.root, .link, "") == null);
312
+ try testing.expect(findRoleLabel(tree.root, .link, "Vercel for GitHub") != null);
313
+ try testing.expect(findRoleLabel(tree.root, .link, "native-sdk") != null);
314
+ try testing.expect(findRoleLabel(tree.root, .link, "Ready") != null);
315
+ try testing.expect(findRoleLabel(tree.root, .link, "Preview") != null);
316
+ }
317
+
318
+ test "GitHub-style HTML blocks lower onto native document structure" {
319
+ var doc = TestDoc.init();
320
+ defer doc.deinit();
321
+ const tree = try doc.build(
322
+ \\<div align="center">
323
+ \\<h2>Centered <em>heading</em></h2>
324
+ \\<p>Copy <strong>here</strong>.</p>
325
+ \\</div>
326
+ \\<h3>
327
+ \\Multiline heading
328
+ \\</h3>
329
+ \\
330
+ \\<blockquote>Quoted <code>value</code>.</blockquote>
331
+ \\<blockquote>
332
+ \\Nested <strong>quote</strong>.
333
+ \\</blockquote>
334
+ \\<hr class="ignored">
335
+ \\<ul>
336
+ \\<li>First</li>
337
+ \\<li>Second <b>bold</b></li>
338
+ \\</ul>
339
+ \\<pre>
340
+ \\<code>
341
+ \\const answer = 42;
342
+ \\</code>
343
+ \\</pre>
344
+ , .{});
345
+
346
+ const heading = findParagraphContaining(tree.root, "Centered heading").?;
347
+ try testing.expectEqual(canvas.TextAlign.center, heading.text_alignment);
348
+ try testing.expectEqual(markdown.heading_scales[1], findSpan(heading, "Centered ").?.scale);
349
+ const heading_emphasis = findSpan(heading, "heading").?;
350
+ try testing.expectEqual(canvas.TextSpanWeight.bold, heading_emphasis.weight);
351
+ try testing.expect(heading_emphasis.italic);
352
+
353
+ const centered_copy = findParagraphContaining(tree.root, "Copy here.").?;
354
+ try testing.expectEqual(canvas.TextAlign.center, centered_copy.text_alignment);
355
+ try testing.expectEqual(canvas.TextSpanWeight.bold, findSpan(centered_copy, "here").?.weight);
356
+
357
+ const multiline_heading = findParagraphContaining(tree.root, "Multiline heading").?;
358
+ try testing.expectEqual(canvas.TextSpanWeight.bold, multiline_heading.spans[0].weight);
359
+ try testing.expectEqual(markdown.heading_scales[2], multiline_heading.spans[0].scale);
360
+
361
+ const quote = findParagraphContaining(tree.root, "Quoted value.").?;
362
+ try testing.expectEqual(canvas.TextAlign.start, quote.text_alignment);
363
+ try testing.expect(findSpan(quote, "value").?.monospace);
364
+ try testing.expectEqual(canvas.TextSpanWeight.bold, findSpan(tree.root, "quote").?.weight);
365
+ try testing.expectEqual(@as(usize, 3), countKind(tree.root, .separator));
366
+ try testing.expect(findRowWithDirectParagraph(tree.root, "First") != null);
367
+ try testing.expect(findRowWithDirectParagraph(tree.root, "Second bold") != null);
368
+ const preformatted = findParagraphContaining(tree.root, "const answer = 42;").?;
369
+ try testing.expect(allSpansMonospace(preformatted));
370
+ }
371
+
372
+ test "HTML block closers may follow content without absorbing later blocks" {
373
+ var doc = TestDoc.init();
374
+ defer doc.deinit();
375
+ const tree = try doc.build(
376
+ \\<div align="center">
377
+ \\Centered</div>
378
+ \\
379
+ \\Outside
380
+ \\<blockquote>
381
+ \\Quoted</blockquote>After quote
382
+ \\<pre>
383
+ \\<code>&lt;tag&gt; &amp; &#x2713;</code></pre>After pre
384
+ , .{});
385
+
386
+ try testing.expectEqual(@as(usize, 6), tree.root.children.len);
387
+ try testing.expectEqual(canvas.TextAlign.center, tree.root.children[0].text_alignment);
388
+ try testing.expectEqualStrings("Outside", tree.root.children[1].text);
389
+ try testing.expectEqual(canvas.TextAlign.start, tree.root.children[1].text_alignment);
390
+ try testing.expect(findParagraphContaining(tree.root.children[2], "Quoted") != null);
391
+ try testing.expectEqualStrings("After quote", tree.root.children[3].text);
392
+ try testing.expect(findParagraphContaining(tree.root.children[4], "<tag> & ✓") != null);
393
+ try testing.expectEqualStrings("After pre", tree.root.children[5].text);
394
+ }
395
+
396
+ test "multiline HTML blocks keep semantics when content follows the opener" {
397
+ var doc = TestDoc.init();
398
+ defer doc.deinit();
399
+ const tree = try doc.build(
400
+ \\<blockquote>First line
401
+ \\Second line</blockquote>After quote
402
+ \\<pre><code>first
403
+ \\second</code></pre>After pre
404
+ , .{});
405
+
406
+ try testing.expectEqual(@as(usize, 4), tree.root.children.len);
407
+ try testing.expectEqual(@as(usize, 1), countKind(tree.root.children[0], .separator));
408
+ try testing.expect(findParagraphContaining(tree.root.children[0], "First line Second line") != null);
409
+ try testing.expectEqualStrings("After quote", tree.root.children[1].text);
410
+ const code = findParagraphContaining(tree.root.children[2], "first\nsecond").?;
411
+ try testing.expect(allSpansMonospace(code));
412
+ try testing.expectEqualStrings("After pre", tree.root.children[3].text);
413
+ }
414
+
415
+ test "multiline HTML list items retain their native marker" {
416
+ var doc = TestDoc.init();
417
+ defer doc.deinit();
418
+ const tree = try doc.build(
419
+ \\<ul>
420
+ \\<li>
421
+ \\First <strong>bold</strong>
422
+ \\</li>
423
+ \\</ul>
424
+ , .{});
425
+
426
+ const item = findRowWithDirectParagraph(tree.root, "First bold").?;
427
+ try testing.expect(findKindLabel(item, .text, "•") != null);
428
+ try testing.expectEqual(canvas.TextSpanWeight.bold, findSpan(item, "bold").?.weight);
429
+ }
430
+
431
+ test "multiline HTML headings honor align" {
432
+ var doc = TestDoc.init();
433
+ defer doc.deinit();
434
+ const tree = try doc.build(
435
+ \\<h2 align="center">
436
+ \\Heading
437
+ \\</h2>
438
+ \\Outside
439
+ , .{});
440
+
441
+ const heading = findParagraphContaining(tree.root, "Heading").?;
442
+ const outside = findParagraphContaining(tree.root, "Outside").?;
443
+ try testing.expectEqual(canvas.TextAlign.center, heading.text_alignment);
444
+ try testing.expectEqual(markdown.heading_scales[1], heading.spans[0].scale);
445
+ try testing.expectEqual(canvas.TextAlign.start, outside.text_alignment);
446
+ }
447
+
448
+ test "HTML blockquotes contain malformed child presentation scopes" {
449
+ var doc = TestDoc.init();
450
+ defer doc.deinit();
451
+ const tree = try doc.build(
452
+ \\<blockquote>
453
+ \\<div align="center">
454
+ \\Inside
455
+ \\</blockquote>
456
+ \\Outside
457
+ , .{});
458
+
459
+ const inside = findParagraphContaining(tree.root.children[0], "Inside").?;
460
+ const outside = findParagraphContaining(tree.root.children[1], "Outside").?;
461
+ try testing.expectEqual(canvas.TextAlign.center, inside.text_alignment);
462
+ try testing.expectEqual(canvas.TextAlign.start, outside.text_alignment);
463
+ }
464
+
465
+ test "self-closing HTML wrappers do not leak block presentation" {
466
+ var doc = TestDoc.init();
467
+ defer doc.deinit();
468
+ const tree = try doc.build(
469
+ \\<div align="center" />
470
+ \\Plain
471
+ \\<h1 />
472
+ \\Still plain
473
+ , .{});
474
+
475
+ const plain = findParagraphContaining(tree.root, "Plain").?;
476
+ const still_plain = findParagraphContaining(tree.root, "Still plain").?;
477
+ try testing.expectEqual(canvas.TextAlign.start, plain.text_alignment);
478
+ try testing.expectEqual(canvas.TextAlign.start, still_plain.text_alignment);
479
+ try testing.expectEqual(canvas.TextSpanWeight.regular, plain.spans[0].weight);
480
+ try testing.expectEqual(canvas.TextSpanWeight.regular, still_plain.spans[0].weight);
481
+ try testing.expectEqual(@as(f32, 0), plain.spans[0].scale);
482
+ try testing.expectEqual(@as(f32, 0), still_plain.spans[0].scale);
483
+ }
484
+
485
+ test "HTML comments hide content while unsupported and malformed tags stay literal" {
486
+ var doc = TestDoc.init();
487
+ defer doc.deinit();
488
+ const tree = try doc.build(
489
+ \\Visible <!-- hidden **secret** --> text. <script><strong>alert</strong>&amp;</script> <span onclick="boom()">safe</span>.
490
+ \\
491
+ \\Malformed <strong never closes.
492
+ , .{});
493
+
494
+ const visible = findParagraphContaining(tree.root, "Visible").?;
495
+ try testing.expect(std.mem.indexOf(u8, visible.text, "hidden") == null);
496
+ try testing.expect(std.mem.indexOf(u8, visible.text, "secret") == null);
497
+ try testing.expect(std.mem.indexOf(u8, visible.text, "<script><strong>alert</strong>&amp;</script>") != null);
498
+ try testing.expect(std.mem.indexOf(u8, visible.text, "onclick") == null);
499
+ try testing.expect(std.mem.indexOf(u8, visible.text, "safe") != null);
500
+
501
+ const malformed = findParagraphContaining(tree.root, "Malformed").?;
502
+ try testing.expect(std.mem.indexOf(u8, malformed.text, "<strong never closes.") != null);
503
+ }
504
+
505
+ test "multiline unsupported HTML stays opaque across block boundaries" {
506
+ var doc = TestDoc.init();
507
+ defer doc.deinit();
508
+ const tree = try doc.build(
509
+ \\<div align="center">
510
+ \\<script>
511
+ \\raw script text
512
+ \\
513
+ \\<strong>alert</strong> &amp;
514
+ \\</div>
515
+ \\</script>
516
+ \\Still centered
517
+ \\</div>
518
+ \\Outside
519
+ , .{});
520
+
521
+ const script = findParagraphContaining(tree.root, "<script>").?;
522
+ try testing.expect(std.mem.indexOf(u8, script.text, "<strong>alert</strong> &amp;") != null);
523
+ try testing.expect(std.mem.startsWith(u8, script.spans[0].text, "<script>"));
524
+ try testing.expect(std.mem.indexOf(u8, script.spans[0].text, "<strong>alert</strong> &amp;") != null);
525
+ try testing.expectEqual(canvas.TextSpanWeight.regular, script.spans[0].weight);
526
+
527
+ const centered = findParagraphContaining(tree.root, "Still centered").?;
528
+ const outside = findParagraphContaining(tree.root, "Outside").?;
529
+ try testing.expectEqual(canvas.TextAlign.center, centered.text_alignment);
530
+ try testing.expectEqual(canvas.TextAlign.start, outside.text_alignment);
531
+ }
532
+
533
+ test "HTML element bodies truncate at the markdown paragraph budget" {
534
+ var source_buffer: [markdown.max_markdown_paragraph_bytes + 256]u8 = undefined;
535
+ var stream = std.Io.Writer.fixed(&source_buffer);
536
+ try stream.writeAll("<p>");
537
+ for (0..markdown.max_markdown_paragraph_bytes + 128) |_| try stream.writeAll("x");
538
+ try stream.writeAll("</p>After");
539
+
540
+ var doc = TestDoc.init();
541
+ defer doc.deinit();
542
+ const tree = try doc.build(stream.buffered(), .{});
543
+ try testing.expectEqual(@as(usize, 2), tree.root.children.len);
544
+ try testing.expectEqual(markdown.max_markdown_paragraph_bytes, tree.root.children[0].text.len);
545
+ try testing.expectEqualStrings("After", tree.root.children[1].text);
546
+ }
547
+
548
+ test "multiline HTML comments stay hidden across blank lines" {
549
+ var doc = TestDoc.init();
550
+ defer doc.deinit();
551
+ const tree = try doc.build(
552
+ \\Before
553
+ \\<!-- hidden
554
+ \\
555
+ \\still hidden -->
556
+ \\After
557
+ , .{});
558
+
559
+ try testing.expectEqual(@as(usize, 2), tree.root.children.len);
560
+ try testing.expectEqualStrings("Before", tree.root.children[0].text);
561
+ try testing.expectEqualStrings("After", tree.root.children[1].text);
562
+ }
563
+
564
+ test "unclosed safe HTML blocks and inline tags remain literal" {
565
+ var doc = TestDoc.init();
566
+ defer doc.deinit();
567
+ const tree = try doc.build(
568
+ \\<h2>Unclosed heading
569
+ \\
570
+ \\- following item
571
+ \\
572
+ \\Malformed <strong title="**">never closes and stray </em>.
573
+ \\
574
+ \\</h3>
575
+ , .{});
576
+
577
+ const heading = findParagraphContaining(tree.root, "Unclosed heading").?;
578
+ try testing.expectEqualStrings("<h2>Unclosed heading", heading.text);
579
+ try testing.expectEqual(canvas.TextSpanWeight.regular, heading.spans[0].weight);
580
+ try testing.expectEqual(@as(f32, 0), heading.spans[0].scale);
581
+ try testing.expect(findRowWithDirectParagraph(tree.root, "following item") != null);
582
+
583
+ const malformed = findParagraphContaining(tree.root, "Malformed").?;
584
+ try testing.expectEqualStrings(
585
+ "Malformed <strong title=\"**\">never closes and stray </em>.",
586
+ malformed.text,
587
+ );
588
+ for (malformed.spans) |span| try testing.expectEqual(canvas.TextSpanWeight.regular, span.weight);
589
+ try testing.expectEqualStrings("</h3>", findParagraphContaining(tree.root, "</h3>").?.text);
590
+ }
591
+
592
+ test "HTML wrapper closers inside Markdown code spans stay literal" {
593
+ var doc = TestDoc.init();
594
+ defer doc.deinit();
595
+ const tree = try doc.build(
596
+ \\<div align="center">
597
+ \\Use `</div>` literally.
598
+ \\
599
+ \\Still centered.
600
+ \\</div>
601
+ \\Outside.
602
+ , .{});
603
+
604
+ const code_line = findParagraphContaining(tree.root, "Use </div> literally.").?;
605
+ const centered = findParagraphContaining(tree.root, "Still centered.").?;
606
+ const outside = findParagraphContaining(tree.root, "Outside.").?;
607
+ try testing.expect(findSpan(code_line, "</div>").?.monospace);
608
+ try testing.expectEqual(canvas.TextAlign.center, code_line.text_alignment);
609
+ try testing.expectEqual(canvas.TextAlign.center, centered.text_alignment);
610
+ try testing.expectEqual(canvas.TextAlign.start, outside.text_alignment);
611
+ }
612
+
613
+ test "multiline comments stay opaque while collecting HTML blocks" {
614
+ var doc = TestDoc.init();
615
+ defer doc.deinit();
616
+ const tree = try doc.build(
617
+ \\<blockquote>
618
+ \\Before.
619
+ \\<!-- hidden
620
+ \\</blockquote>
621
+ \\still hidden -->
622
+ \\After.
623
+ \\</blockquote>
624
+ \\Outside.
625
+ , .{});
626
+
627
+ try testing.expectEqual(@as(usize, 2), tree.root.children.len);
628
+ try testing.expect(findParagraphContaining(tree.root.children[0], "Before.") != null);
629
+ try testing.expect(findParagraphContaining(tree.root.children[0], "After.") != null);
630
+ try testing.expect(findParagraphContaining(tree.root.children[0], "hidden") == null);
631
+ try testing.expectEqualStrings("Outside.", tree.root.children[1].text);
632
+ }
633
+
634
+ test "HTML ordered and definition lists retain their native semantics" {
635
+ var doc = TestDoc.init();
636
+ defer doc.deinit();
637
+ const tree = try doc.build(
638
+ \\<ol>
639
+ \\<li>First</li>
640
+ \\<li>Second</li>
641
+ \\</ol>
642
+ \\<ol><li>Compact one</li><li>Compact two</li></ol>
643
+ \\<dl>
644
+ \\<dt>Term</dt>
645
+ \\<dd>Definition</dd>
646
+ \\</dl>
647
+ , .{});
648
+
649
+ try testing.expectEqual(@as(usize, 2), countKindLabel(tree.root, .text, "1."));
650
+ try testing.expectEqual(@as(usize, 2), countKindLabel(tree.root, .text, "2."));
651
+ try testing.expectEqual(@as(usize, 0), countKindLabel(tree.root, .text, "•"));
652
+ try testing.expectEqual(canvas.TextSpanWeight.bold, findSpan(tree.root, "Term").?.weight);
653
+ try testing.expect(findParagraphContaining(tree.root, "Definition") != null);
654
+ }
655
+
159
656
  test "markdown maps lists, task lists, code fences, quotes, and rules" {
160
657
  var doc = TestDoc.init();
161
658
  defer doc.deinit();
@@ -324,6 +821,30 @@ test "details blocks are caller-controlled collapsibles" {
324
821
  try testing.expectEqual(@as(?bool, true), open_header.state.expanded);
325
822
  }
326
823
 
824
+ test "details summaries lower safe inline HTML" {
825
+ const source =
826
+ \\<details>
827
+ \\<summary><strong>More</strong> &amp; <a href="https://example.com">docs</a></summary>
828
+ \\Body.
829
+ \\</details>
830
+ ;
831
+
832
+ var doc = TestDoc.init();
833
+ defer doc.deinit();
834
+ const tree = try doc.build(source, .{
835
+ .on_link = Ui.linkMsg(.open_url),
836
+ .on_details = Md.detailsMsg(.toggle_details),
837
+ });
838
+
839
+ const summary = findParagraphContaining(tree.root, "More & docs").?;
840
+ try testing.expectEqual(canvas.TextSpanWeight.bold, findSpan(summary, "More").?.weight);
841
+ try testing.expectEqualStrings("https://example.com", findSpan(summary, "docs").?.link);
842
+
843
+ const header = findKindLabel(tree.root, .list_item, "▸ More & docs").?;
844
+ const toggle = tree.msgForPointer(header.id, .up).?;
845
+ try testing.expectEqual(@as(usize, 0), toggle.toggle_details);
846
+ }
847
+
327
848
  test "malformed markdown degrades to literal text and never fails" {
328
849
  var doc = TestDoc.init();
329
850
  defer doc.deinit();
@@ -398,6 +919,259 @@ test "pipe tables map onto table/data_row/data_cell with alignment and header st
398
919
  try testing.expectEqualStrings("https://example.com/logs", msg.open_url);
399
920
  }
400
921
 
922
+ test "resolved leading table images render as native image leaves with alt fallback" {
923
+ const avatar_url = "https://vercel.com/api/www/avatar?projectId=project&teamId=team&s=32";
924
+ const ready_url = "https://vercel.com/static/status/ready.svg";
925
+ const images = [_]markdown.ResolvedImage{
926
+ .{ .source = avatar_url, .image = 41, .width = 32, .height = 32 },
927
+ .{ .source = ready_url, .image = 42, .width = 10, .height = 10 },
928
+ };
929
+
930
+ var doc = TestDoc.init();
931
+ defer doc.deinit();
932
+ const tree = try doc.build(
933
+ \\| Project | Deployment |
934
+ \\| :--- | :--- |
935
+ \\| <a href="https://vercel.com/vercel-labs/native-sdk"><sup><img src="https://vercel.com/api/www/avatar?projectId=project&teamId=team&s=32" width="16" height="16" align="middle" alt="" /></sup></a> [native-sdk](https://vercel.com/vercel-labs/native-sdk) | ![Ready](https://vercel.com/static/status/ready.svg) [Ready](https://vercel.com/deploy) |
936
+ , .{ .on_link = Ui.linkMsg(.open_url), .images = &images });
937
+
938
+ try testing.expectEqual(@as(usize, 2), countKind(tree.root, .image));
939
+ const avatar = findImageId(tree.root, 41).?;
940
+ try testing.expectEqual(@as(f32, 16), avatar.layout.min_size.width);
941
+ try testing.expectEqual(@as(f32, 16), avatar.layout.min_size.height);
942
+ try testing.expectEqual(canvas.WidgetRole.link, avatar.semantics.role);
943
+ try testing.expectEqualStrings("https://vercel.com/vercel-labs/native-sdk", tree.msgForPointer(avatar.id, .up).?.open_url);
944
+
945
+ const ready = findImageId(tree.root, 42).?;
946
+ try testing.expectEqual(@as(f32, 10), ready.layout.min_size.width);
947
+ try testing.expectEqual(@as(f32, 10), ready.layout.min_size.height);
948
+ try testing.expectEqual(canvas.WidgetRole.image, ready.semantics.role);
949
+ try testing.expectEqualStrings("Ready", ready.semantics.label);
950
+ try testing.expect(findParagraphContaining(tree.root, "native-sdk") != null);
951
+ try testing.expect(findParagraphContaining(tree.root, "Ready") != null);
952
+
953
+ var fallback_doc = TestDoc.init();
954
+ defer fallback_doc.deinit();
955
+ const fallback = try fallback_doc.build(
956
+ \\| Deployment |
957
+ \\| --- |
958
+ \\| ![Ready](https://vercel.com/static/status/ready.svg) |
959
+ , .{});
960
+ try testing.expectEqual(@as(usize, 0), countKind(fallback.root, .image));
961
+ try testing.expect(findCellContaining(fallback.root, "Ready") != null);
962
+ }
963
+
964
+ test "entity-normalized discovered image sources match resolved HTML images" {
965
+ const source =
966
+ \\<img src="https://example.com/avatar?a=1&amp;b=2" alt="Avatar" />
967
+ ;
968
+ var collected_storage: [1]markdown.CollectedImageSource = undefined;
969
+ const collected = markdown.collectImageSources(source, &collected_storage);
970
+ try testing.expectEqual(@as(usize, 1), collected.len);
971
+ try testing.expectEqualStrings("https://example.com/avatar?a=1&b=2", collected[0].value());
972
+
973
+ const images = [_]markdown.ResolvedImage{.{
974
+ .source = collected[0].value(),
975
+ .image = 75,
976
+ .width = 32,
977
+ .height = 32,
978
+ }};
979
+ var doc = TestDoc.init();
980
+ defer doc.deinit();
981
+ const tree = try doc.build(source, .{ .images = &images });
982
+ try testing.expect(findImageId(tree.root, 75) != null);
983
+ }
984
+
985
+ test "table text cells vertically center beside resolved images" {
986
+ const source = "https://example.com/tall.png";
987
+ const images = [_]markdown.ResolvedImage{
988
+ .{ .source = source, .image = 76, .width = 40, .height = 40 },
989
+ };
990
+
991
+ var doc = TestDoc.init();
992
+ defer doc.deinit();
993
+ const tree = try doc.build(
994
+ \\| Project | Action |
995
+ \\| --- | --- |
996
+ \\| ![Logo](https://example.com/tall.png) Native | [Preview](https://example.com) |
997
+ , .{ .on_link = Ui.linkMsg(.open_url), .images = &images });
998
+
999
+ const image = findImageId(tree.root, 76).?;
1000
+ const preview = findRoleLabel(tree.root, .link, "Preview").?;
1001
+ var nodes: [64]canvas.WidgetLayoutNode = undefined;
1002
+ const layout = try canvas.layoutWidgetTreeWithTokens(
1003
+ tree.root,
1004
+ geometry.RectF.init(0, 0, 360, 160),
1005
+ .{},
1006
+ &nodes,
1007
+ );
1008
+ const image_frame = layout.findById(image.id).?.frame;
1009
+ const preview_frame = layout.findById(preview.id).?.frame;
1010
+ try testing.expectApproxEqAbs(
1011
+ image_frame.y + image_frame.height * 0.5,
1012
+ preview_frame.y + preview_frame.height * 0.5,
1013
+ 0.01,
1014
+ );
1015
+ }
1016
+
1017
+ test "resolved image composites honor center and end alignment" {
1018
+ const source = "https://example.com/centered.png";
1019
+ const images = [_]markdown.ResolvedImage{
1020
+ .{ .source = source, .image = 78, .width = 40, .height = 20 },
1021
+ .{ .source = source, .image = 79, .width = 40, .height = 20 },
1022
+ .{ .source = source, .image = 81, .width = 40, .height = 20 },
1023
+ };
1024
+
1025
+ var paragraph_doc = TestDoc.init();
1026
+ defer paragraph_doc.deinit();
1027
+ const paragraph_tree = try paragraph_doc.build(
1028
+ \\<p align="center"><img src="https://example.com/centered.png" alt="Centered" /></p>
1029
+ , .{ .images = images[0..1] });
1030
+ const paragraph_image = findImageId(paragraph_tree.root, 78).?;
1031
+ var paragraph_nodes: [32]canvas.WidgetLayoutNode = undefined;
1032
+ const paragraph_layout = try canvas.layoutWidgetTreeWithTokens(
1033
+ paragraph_tree.root,
1034
+ geometry.RectF.init(0, 0, 400, 100),
1035
+ .{},
1036
+ &paragraph_nodes,
1037
+ );
1038
+ const paragraph_frame = paragraph_layout.findById(paragraph_image.id).?.frame;
1039
+ try testing.expectApproxEqAbs(@as(f32, 200), paragraph_frame.x + paragraph_frame.width * 0.5, 0.01);
1040
+
1041
+ var table_doc = TestDoc.init();
1042
+ defer table_doc.deinit();
1043
+ const table_tree = try table_doc.build(
1044
+ \\| Centered |
1045
+ \\| :---: |
1046
+ \\| ![Centered](https://example.com/centered.png) |
1047
+ , .{ .images = images[1..2] });
1048
+ const table_image = findImageId(table_tree.root, 79).?;
1049
+ const table_cell = findCellContainingImage(table_tree.root, 79).?;
1050
+ var table_nodes: [32]canvas.WidgetLayoutNode = undefined;
1051
+ const table_layout = try canvas.layoutWidgetTreeWithTokens(
1052
+ table_tree.root,
1053
+ geometry.RectF.init(0, 0, 400, 120),
1054
+ .{},
1055
+ &table_nodes,
1056
+ );
1057
+ const image_frame = table_layout.findById(table_image.id).?.frame;
1058
+ const cell_frame = table_layout.findById(table_cell.id).?.frame;
1059
+ try testing.expectApproxEqAbs(
1060
+ cell_frame.x + cell_frame.width * 0.5,
1061
+ image_frame.x + image_frame.width * 0.5,
1062
+ 0.01,
1063
+ );
1064
+
1065
+ var end_doc = TestDoc.init();
1066
+ defer end_doc.deinit();
1067
+ const end_tree = try end_doc.build(
1068
+ \\<p align="right"><img src="https://example.com/centered.png" alt="End" /></p>
1069
+ , .{ .images = images[2..3] });
1070
+ const end_image = findImageId(end_tree.root, 81).?;
1071
+ var end_nodes: [32]canvas.WidgetLayoutNode = undefined;
1072
+ const end_layout = try canvas.layoutWidgetTreeWithTokens(
1073
+ end_tree.root,
1074
+ geometry.RectF.init(0, 0, 400, 100),
1075
+ .{},
1076
+ &end_nodes,
1077
+ );
1078
+ const end_frame = end_layout.findById(end_image.id).?.frame;
1079
+ try testing.expectApproxEqAbs(@as(f32, 400), end_frame.x + end_frame.width, 0.01);
1080
+ }
1081
+
1082
+ test "resolved leading paragraph images compose with trailing inline text" {
1083
+ const source = "https://example.com/diagram.png";
1084
+ const images = [_]markdown.ResolvedImage{
1085
+ .{ .source = source, .image = 77, .width = 80, .height = 40 },
1086
+ };
1087
+
1088
+ var doc = TestDoc.init();
1089
+ defer doc.deinit();
1090
+ const tree = try doc.build(
1091
+ \\![Architecture](https://example.com/diagram.png) **Current** design
1092
+ \\Text before ![inline](https://example.com/inline.png) stays an alt fallback.
1093
+ , .{ .images = &images });
1094
+
1095
+ const image = findImageId(tree.root, 77).?;
1096
+ try testing.expectEqualStrings("Architecture", image.semantics.label);
1097
+ try testing.expect(findParagraphContaining(tree.root, "Current design") != null);
1098
+ try testing.expect(findParagraphContaining(tree.root, "Text before inline stays an alt fallback.") != null);
1099
+ }
1100
+
1101
+ test "resolved image bounds preserve aspect ratio" {
1102
+ const source = "https://example.com/wide.png";
1103
+ const images = [_]markdown.ResolvedImage{
1104
+ .{ .source = source, .image = 80, .width = 1024, .height = 256 },
1105
+ };
1106
+
1107
+ var doc = TestDoc.init();
1108
+ defer doc.deinit();
1109
+ const tree = try doc.build(
1110
+ \\![Wide](https://example.com/wide.png)
1111
+ , .{ .images = &images });
1112
+ const image = findImageId(tree.root, 80).?;
1113
+ try testing.expectEqual(@as(f32, 512), image.layout.min_size.width);
1114
+ try testing.expectEqual(@as(f32, 128), image.layout.min_size.height);
1115
+ }
1116
+
1117
+ test "image source collection is bounded, distinct, and keeps code inert" {
1118
+ var storage: [4]markdown.CollectedImageSource = undefined;
1119
+ const sources = markdown.collectImageSources(
1120
+ \\![first](https://example.com/first.png)
1121
+ \\Text before ![inline](https://example.com/inline.png)
1122
+ \\`![code](https://example.com/code.png)`
1123
+ \\| Image | Copy |
1124
+ \\| --- | --- |
1125
+ \\| <a href="https://example.com"><sup><img src="https://example.com/second.svg?a=1&amp;b=2" alt="second" /></sup></a> label | <!-- <img src="https://example.com/comment.png" /> --> |
1126
+ \\![again](https://example.com/first.png)
1127
+ \\```
1128
+ \\![fenced](https://example.com/fenced.png)
1129
+ \\```
1130
+ , &storage);
1131
+
1132
+ try testing.expectEqual(@as(usize, 2), sources.len);
1133
+ try testing.expectEqualStrings("https://example.com/first.png", sources[0].value());
1134
+ try testing.expectEqualStrings("https://example.com/second.svg?a=1&b=2", sources[1].value());
1135
+ }
1136
+
1137
+ test "image source collection mirrors visible block starts and keeps opaque HTML inert" {
1138
+ var storage: [10]markdown.CollectedImageSource = undefined;
1139
+ const sources = markdown.collectImageSources(
1140
+ \\Opening prose
1141
+ \\![joined fallback](https://example.com/joined.png)
1142
+ \\
1143
+ \\# ![heading](https://example.com/heading.png)
1144
+ \\- ![list](https://example.com/list.png)
1145
+ \\> ![quote](https://example.com/quote.png)
1146
+ \\<p><img src="https://example.com/html.png" alt="html" /></p>
1147
+ \\<!--
1148
+ \\<img src="https://tracker.example/comment.png" />
1149
+ \\-->
1150
+ \\![after comment](https://example.com/after-comment.png)
1151
+ \\
1152
+ \\<script>
1153
+ \\<img src="https://tracker.example/script.png" />
1154
+ \\</script>
1155
+ \\
1156
+ \\<pre>
1157
+ \\<img src="https://tracker.example/pre.png" />
1158
+ \\</pre>
1159
+ \\![after pre](https://example.com/after-pre.png)
1160
+ \\
1161
+ \\<code>
1162
+ \\<img src="https://tracker.example/code.png" />
1163
+ \\</code>
1164
+ , &storage);
1165
+
1166
+ try testing.expectEqual(@as(usize, 6), sources.len);
1167
+ try testing.expectEqualStrings("https://example.com/heading.png", sources[0].value());
1168
+ try testing.expectEqualStrings("https://example.com/list.png", sources[1].value());
1169
+ try testing.expectEqualStrings("https://example.com/quote.png", sources[2].value());
1170
+ try testing.expectEqualStrings("https://example.com/html.png", sources[3].value());
1171
+ try testing.expectEqualStrings("https://example.com/after-comment.png", sources[4].value());
1172
+ try testing.expectEqualStrings("https://example.com/after-pre.png", sources[5].value());
1173
+ }
1174
+
401
1175
  test "table rows pad short rows, drop extra cells, and stop at blank or pipeless lines" {
402
1176
  var doc = TestDoc.init();
403
1177
  defer doc.deinit();
@@ -548,11 +1322,12 @@ test "the README-shaped fixture renders through the mapper and the reference ren
548
1322
  // scales, wrapped bullets and em-dash spacing at the face's real
549
1323
  // advances, real sans and mono outlines (fixed-pitch runs sit in their
550
1324
  // 0.6 em cells), GFM tables as borderless cells on hairline row
551
- // separators, bare fenced code with preserved source indentation and
552
- // language-token colors, and near-black underlined links.
1325
+ // separators with vertically centered cell content, bare fenced code
1326
+ // with preserved source indentation and language-token colors, and
1327
+ // near-black underlined links.
553
1328
  // Update deliberately when markdown rendering changes, reviewing the
554
1329
  // rendered pixels first (see reference_tests.zig conventions).
555
- const markdown_document_reference_signature: u64 = 5787917808851547223;
1330
+ const markdown_document_reference_signature: u64 = 17200107192111546862;
556
1331
 
557
1332
  fn markdownGoldenDumpRequested() bool {
558
1333
  if (comptime !@import("builtin").link_libc) return false;