@mdsvex/render 1.0.0-next.2 → 1.0.0-next.4

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.
@@ -12,7 +12,13 @@
12
12
  */
13
13
 
14
14
  import { Cursor } from '@mdsvex/parse/cursor';
15
- import { is_raw_text_tag } from '@mdsvex/parse/utils';
15
+ import {
16
+ is_raw_text_tag,
17
+ spans_have,
18
+ spans_text,
19
+ unread_spans,
20
+ } from '@mdsvex/parse/utils';
21
+ import type { AttrSpans } from '@mdsvex/parse/utils';
16
22
  import {
17
23
  buf_code,
18
24
  footnote_id,
@@ -38,14 +44,24 @@ import {
38
44
  trace_take,
39
45
  } from './sourcemap.js';
40
46
  import type { MapTrace, SourceMapV3 } from './sourcemap.js';
41
- import { ComponentScope, component_imports } from './scope.js';
42
- import type { ComponentImport, ReplaceWarning } from './scope.js';
47
+ import {
48
+ ComponentScope,
49
+ component_imports,
50
+ shell_key,
51
+ side_effect_import,
52
+ } from './scope.js';
53
+ import type {
54
+ ComponentImport,
55
+ ReplaceWarning,
56
+ Shell,
57
+ ShellTable,
58
+ } from './scope.js';
43
59
  import { is_entity_name } from './entities.js';
44
60
  import { top_block } from './component.js';
45
61
 
46
62
  export type { Mapping, CodeInformation, MappingData } from './mappings.js';
47
63
  export { MapSink } from './mappings.js';
48
- export { ComponentScope, component_imports } from './scope.js';
64
+ export { ComponentScope, component_imports, shell_key } from './scope.js';
49
65
  /** @internal a pass over the tree reads references as the renderer does */
50
66
  export { is_entity_name } from './entities.js';
51
67
  export type {
@@ -53,6 +69,8 @@ export type {
53
69
  ComponentSource,
54
70
  DefaultImport,
55
71
  ReplaceWarning,
72
+ Shell,
73
+ ShellTable,
56
74
  } from './scope.js';
57
75
 
58
76
  /**
@@ -130,8 +148,16 @@ export interface PreMeta {
130
148
  pre_props(meta: string, start: number): string;
131
149
  }
132
150
 
133
- /** a string is escaped as a url of the source is, '' leaves the attribute out, an expression goes in braces */
134
- export type UrlSource = string | { type: 'expression'; value: string };
151
+ /**
152
+ * a string is escaped as a url of the source is, '' leaves the attribute out,
153
+ * an expression goes in braces, a picture names an imagetools Picture, a
154
+ * markdown image renders it as a picture element and a replacement gets it
155
+ * as its src
156
+ */
157
+ export type UrlSource =
158
+ | string
159
+ | { type: 'expression'; value: string }
160
+ | { type: 'picture'; value: string };
135
161
 
136
162
  /** what replaces the src of an image or the href of a link, url is never empty, null or undefined keeps it */
137
163
  export type UrlLookup = (
@@ -999,6 +1025,15 @@ let comp_scope: ComponentScope | null = null;
999
1025
  let dir_scope: ComponentScope | null = null;
1000
1026
  /** a directive no scope replaces throws, otherwise it renders as its children */
1001
1027
  let dir_strict = false;
1028
+ /** the shells of static directives, null renders every directive live */
1029
+ let dir_shells: ShellTable | null = null;
1030
+ /** comp_scan met a directive written as its shell, which imports nothing */
1031
+ let dir_shelled = false;
1032
+ /** the import lines of the shells comp_scan met, after those of the replacements */
1033
+ let shell_lines = '';
1034
+ /** each import line once, the specifiers of the modules a shelled directive comes from */
1035
+ const shell_seen = new Set<string>();
1036
+ const shell_specs: string[] = [];
1002
1037
  let comp_mode: number = CM.FOLD;
1003
1038
  /** component_mode all, typed lowercase elements are replaced too */
1004
1039
  let comp_all = false;
@@ -1046,7 +1081,8 @@ function hoist_reset(): void {
1046
1081
  */
1047
1082
  function hoist_begin(c: Cursor, buf: NodeBuffer, extra: boolean): void {
1048
1083
  const child = hoist_imports(buf);
1049
- if (hoist_nodes.length === 0 && !has_components && !extra) return;
1084
+ // comp_lines is empty for a document whose only replacements are shells
1085
+ if (hoist_nodes.length === 0 && comp_lines === '' && !extra) return;
1050
1086
  if (extra) comp_lines += extra_lines;
1051
1087
  hoist_script_from(c, buf, child);
1052
1088
  // no script and no import to start one, the render starts with one
@@ -1128,7 +1164,9 @@ function wrap_hoist_begin(c: Cursor, buf: NodeBuffer): void {
1128
1164
  component_imports(comp_used, {
1129
1165
  specifier: wrap_spec,
1130
1166
  local: WRAP_LOCAL,
1131
- }) + extra_lines;
1167
+ }) +
1168
+ shell_lines +
1169
+ extra_lines;
1132
1170
  if (declare) comp_lines += 'let ' + WRAP_PROPS + ' = $props();\n';
1133
1171
  wrap_open =
1134
1172
  '<' +
@@ -1156,11 +1194,13 @@ function module_begin(buf: NodeBuffer, code: string): void {
1156
1194
  if ((n[b] & 0xff) === K.HTML) {
1157
1195
  const slot = n[b + W.meta];
1158
1196
  const meta = slot === 0 ? undefined : buf._meta[slot - 1];
1159
- const attrs = meta?.attributes as Record<string, unknown> | undefined;
1197
+ const script =
1198
+ meta !== undefined && meta.tag === 'script' && !meta.self_closing;
1199
+ const attrs = script
1200
+ ? (meta.attributes as Record<string, unknown> | undefined)
1201
+ : undefined;
1160
1202
  if (
1161
- meta !== undefined &&
1162
- meta.tag === 'script' &&
1163
- !meta.self_closing &&
1203
+ script &&
1164
1204
  attrs !== undefined &&
1165
1205
  attrs.src === undefined &&
1166
1206
  (attrs.module !== undefined || attrs.context === 'module')
@@ -1648,12 +1688,11 @@ function render_node(c: Cursor, sink?: MapSink): void {
1648
1688
  case K.IMAGE: {
1649
1689
  const pre = mo.length;
1650
1690
  const meta = c.meta();
1651
- let s = '<img';
1652
- if (meta?.src) s += url_attr(meta.src as string, true);
1691
+ let s = img_head(meta?.src);
1653
1692
  s += ' alt="' + escape_text_html(_children_raw(c)) + '"';
1654
1693
  if (meta?.title)
1655
1694
  s += ' title="' + escape_text_html(meta.title as string) + '"';
1656
- mo = mo + s + _attrs(c, IMAGE_HANDLED) + ' />';
1695
+ mo = mo + s + _attrs(c, IMAGE_HANDLED) + FOLD_STR[img_close];
1657
1696
  // the syntax spans are empty, only the node is recorded
1658
1697
  if (sink) put_record(sink, pre, mo.length, c.start, c.end, c.index, 0);
1659
1698
  break;
@@ -2182,6 +2221,7 @@ const S_IMG = fold_base('<img');
2182
2221
  const S_SRC = fold_base(' src="');
2183
2222
  const S_ALT = fold_base(' alt="');
2184
2223
  const S_SELF_CLOSE = fold_base(' />');
2224
+ const S_PICTURE_CLOSE = fold_base(' /></picture>');
2185
2225
  const S_OL_START = fold_base('<ol start="');
2186
2226
  const S_OL = fold_base('<ol');
2187
2227
  const S_OL_OPEN = fold_base('<ol>\n');
@@ -2684,8 +2724,14 @@ function fold_link(c: Cursor, p: number): number {
2684
2724
 
2685
2725
  function fold_image(c: Cursor, p: number): number {
2686
2726
  const meta = c.meta();
2687
- p = push_static(p, S_IMG);
2688
- if (meta?.src) p = fold_url(p, meta.src as string, true);
2727
+ let close = S_SELF_CLOSE;
2728
+ if (ul === null) {
2729
+ p = push_static(p, S_IMG);
2730
+ if (meta?.src) p = fold_url(p, meta.src as string, true);
2731
+ } else {
2732
+ p = push_dyn(p, img_head(meta?.src));
2733
+ close = img_close;
2734
+ }
2689
2735
  p = push_static(p, S_ALT);
2690
2736
  p = push_dyn(p, escape_text_html(_children_raw(c)));
2691
2737
  p = push_static(p, S_QUOTE);
@@ -2695,7 +2741,7 @@ function fold_image(c: Cursor, p: number): number {
2695
2741
  p = push_static(p, S_QUOTE);
2696
2742
  }
2697
2743
  p = fold_attrs(meta, p, IMAGE_HANDLED);
2698
- return push_static(p, S_SELF_CLOSE);
2744
+ return push_static(p, close);
2699
2745
  }
2700
2746
 
2701
2747
  function fold_list(c: Cursor, p: number): number {
@@ -2746,9 +2792,14 @@ function fold_html(c: Cursor, p: number): number {
2746
2792
  const meta = c.meta();
2747
2793
  const tag = meta?.tag as string;
2748
2794
  if (is_embed_script(c, tag)) return push_dyn(p, embed_html(c));
2749
- const html_attrs = meta?.attributes as
2750
- | Record<string, string | boolean>
2751
- | undefined;
2795
+ // an unread tag written as fold writes it is its own attribute text
2796
+ const spans = c.synthetic ? undefined : unread_spans(meta);
2797
+ const text =
2798
+ spans !== undefined && spans.canonical ? spans_text(spans) : undefined;
2799
+ const html_attrs =
2800
+ text === undefined
2801
+ ? (meta?.attributes as Record<string, string | boolean> | undefined)
2802
+ : undefined;
2752
2803
 
2753
2804
  if (meta?.self_closing) {
2754
2805
  // source passthrough as in _node, decided before any push because a
@@ -2758,13 +2809,15 @@ function fold_html(c: Cursor, p: number): number {
2758
2809
  if (passthrough) return push_dyn(p, passthrough);
2759
2810
  p = push_static(p, S_LT);
2760
2811
  p = push_dyn(p, tag);
2761
- if (html_attrs) p = fold_html_attrs(html_attrs, !c.synthetic, p);
2812
+ if (text !== undefined) p = push_dyn(p, text);
2813
+ else if (html_attrs) p = fold_html_attrs(html_attrs, !c.synthetic, p);
2762
2814
  return push_static(p, S_SELF_CLOSE);
2763
2815
  }
2764
2816
 
2765
2817
  p = push_static(p, S_LT);
2766
2818
  p = push_dyn(p, tag);
2767
- if (html_attrs) {
2819
+ if (text !== undefined) p = push_dyn(p, text);
2820
+ else if (html_attrs) {
2768
2821
  p = fold_html_attrs(html_attrs, !c.synthetic, p);
2769
2822
  if (meta!.repeated !== undefined)
2770
2823
  p = push_dyn(
@@ -3230,15 +3283,14 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
3230
3283
  const pre = mo.length + FOLD_LEN[p];
3231
3284
  if (p !== 0) mo += FOLD_STR[p];
3232
3285
  const meta = c.meta();
3233
- let s = '<img';
3234
- if (meta?.src) s += url_attr(meta.src as string, true);
3286
+ let s = img_head(meta?.src);
3235
3287
  s += ' alt="' + escape_text_html(_children_raw(c)) + '"';
3236
3288
  if (meta?.title)
3237
3289
  s += ' title="' + escape_text_html(meta.title as string) + '"';
3238
3290
  mo = mo + s + _attrs(c, IMAGE_HANDLED);
3239
3291
  // the syntax spans are empty, only the node is recorded
3240
3292
  tr_at(sink, pre, c);
3241
- return S_SELF_CLOSE;
3293
+ return img_close;
3242
3294
  }
3243
3295
 
3244
3296
  case K.HTML_COMMENT: {
@@ -3768,23 +3820,23 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3768
3820
  const pre = mo.length + FOLD_LEN[p];
3769
3821
  if (p !== 0) mo += FOLD_STR[p];
3770
3822
  const meta = c.meta();
3771
- let s = '<img';
3772
- if (meta?.src) s += url_attr(meta.src as string, true);
3823
+ let s = img_head(meta?.src);
3773
3824
  s += ' alt="' + escape_text_html(_children_raw(c)) + '"';
3774
3825
  if (meta?.title)
3775
3826
  s += ' title="' + escape_text_html(meta.title as string) + '"';
3776
3827
  mo = mo + s + _attrs(c, IMAGE_HANDLED);
3828
+ const close = img_close;
3777
3829
  // the syntax spans are empty, only the node is recorded
3778
3830
  put_record(
3779
3831
  sink,
3780
3832
  pre,
3781
- mo.length + FOLD_LEN[S_SELF_CLOSE],
3833
+ mo.length + FOLD_LEN[close],
3782
3834
  c.start,
3783
3835
  c.end,
3784
3836
  c.index,
3785
3837
  0
3786
3838
  );
3787
- return S_SELF_CLOSE;
3839
+ return close;
3788
3840
  }
3789
3841
 
3790
3842
  case K.HTML_COMMENT: {
@@ -4144,7 +4196,7 @@ function mp_table_cells_ext(
4144
4196
  /** flat so the caller never pays for a rope */
4145
4197
  function render_folded(c: Cursor): string {
4146
4198
  fold_out = comp_prefix;
4147
- const p = fold_node(c, 0);
4199
+ const p = ws_on ? ws_pass(c, undefined, 0, CM.FOLD, false) : fold_node(c, 0);
4148
4200
  let html = fold_out;
4149
4201
  fold_out = '';
4150
4202
  if (p !== 0) html += FOLD_STR[p];
@@ -4291,6 +4343,23 @@ function comp_use_html(c: Cursor, name: string): void {
4291
4343
  else comp_warnings.push({ tag: name, directive, start: c.start });
4292
4344
  }
4293
4345
 
4346
+ /** a shelled directive imports what its shell names and runs its module for its styles */
4347
+ function note_shell(ref: ComponentImport, shell: Shell): void {
4348
+ dir_shelled = true;
4349
+ if (!shell_seen.has(ref.specifier)) {
4350
+ shell_seen.add(ref.specifier);
4351
+ shell_specs.push(ref.specifier);
4352
+ }
4353
+ const imports = shell.imports;
4354
+ if (imports === undefined) return;
4355
+ for (let i = 0; i < imports.length; i++) {
4356
+ const line = imports[i];
4357
+ if (shell_seen.has(line)) continue;
4358
+ shell_seen.add(line);
4359
+ shell_lines += line;
4360
+ }
4361
+ }
4362
+
4294
4363
  /** walk what the render walks below c and note each replacement it will use */
4295
4364
  function comp_scan(c: Cursor): void {
4296
4365
  if (!c.goto_first_child()) return;
@@ -4299,7 +4368,12 @@ function comp_scan(c: Cursor): void {
4299
4368
  if (k === K.TEXT || k === K.LINE_BREAK) continue;
4300
4369
  if (k >= K.DIRECTIVE_INLINE && k <= K.DIRECTIVE_CONTAINER) {
4301
4370
  const ref = dir_scope!.get(dir_name(c));
4302
- if (ref !== undefined) note_use(ref);
4371
+ // a directive written as its shell imports no component
4372
+ if (ref !== undefined) {
4373
+ const shell = ref.static === true ? dir_shell_of(c, ref) : undefined;
4374
+ if (shell === undefined) note_use(ref);
4375
+ else note_shell(ref, shell);
4376
+ }
4303
4377
  } else if (k === K.FOOTNOTE_DEFINITION) {
4304
4378
  // a definition is in the tree where it was written, the section holds it
4305
4379
  comp_use('footnotes');
@@ -4371,30 +4445,60 @@ function comp_begin(
4371
4445
  scope: ComponentScope | null,
4372
4446
  directives: ComponentScope | null,
4373
4447
  mode: number,
4374
- all: boolean
4448
+ all: boolean,
4449
+ shells: ShellTable | undefined
4375
4450
  ): readonly ReplaceWarning[] {
4376
4451
  comp_scope = scope ?? NO_SCOPE;
4377
4452
  dir_scope = directives ?? NO_SCOPE;
4453
+ if (shells !== undefined && dir_scope.has_static) dir_shells = shells;
4378
4454
  comp_mode = mode;
4379
4455
  comp_all = all;
4380
4456
  comp_scan(c);
4381
4457
  const warnings =
4382
4458
  comp_warnings.length === 0 ? NO_WARNINGS : comp_warnings.splice(0);
4383
- if (comp_used.length === 0) return warnings;
4459
+ // the walks route a shell through the replacement code too
4460
+ if (comp_used.length === 0 && !dir_shelled) return warnings;
4384
4461
  has_components = true;
4385
4462
  for (let i = 0; i < TABLE_PARTS.length; i++) {
4386
4463
  const ref = comp_scope.get(TABLE_PARTS[i]);
4387
4464
  if (ref !== undefined && comp_seen.has(ref)) comp_table = true;
4388
4465
  }
4389
- comp_lines = component_imports(comp_used);
4466
+ if (dir_shelled) shell_lines = shell_module_lines() + shell_lines;
4467
+ comp_lines = component_imports(comp_used) + shell_lines;
4390
4468
  return warnings;
4391
4469
  }
4392
4470
 
4471
+ /**
4472
+ * the modules of the shelled directives that no replacement already
4473
+ * imports from, run for their side effects, the styles a barrel imports
4474
+ */
4475
+ function shell_module_lines(): string {
4476
+ let s = '';
4477
+ for (let i = 0; i < shell_specs.length; i++) {
4478
+ const spec = shell_specs[i];
4479
+ let imported = false;
4480
+ for (let j = 0; j < comp_used.length; j++)
4481
+ if (comp_used[j].specifier === spec) {
4482
+ imported = true;
4483
+ break;
4484
+ }
4485
+ if (!imported) s += side_effect_import(spec);
4486
+ }
4487
+ return s;
4488
+ }
4489
+
4393
4490
  function comp_end(): void {
4394
4491
  has_components = false;
4395
4492
  comp_table = false;
4396
4493
  comp_scope = null;
4397
4494
  dir_scope = null;
4495
+ dir_shells = null;
4496
+ dir_shelled = false;
4497
+ if (shell_specs.length !== 0) {
4498
+ shell_specs.length = 0;
4499
+ shell_seen.clear();
4500
+ shell_lines = '';
4501
+ }
4398
4502
  comp_mode = CM.FOLD;
4399
4503
  comp_all = false;
4400
4504
  comp_lines = '';
@@ -4574,17 +4678,27 @@ function open_tag_end(
4574
4678
  /** the per kind extras of a typed element, those the author wrote are kept */
4575
4679
  function typed_extras(
4576
4680
  tag: string,
4577
- attrs: Record<string, unknown> | undefined
4681
+ attrs: Record<string, unknown> | undefined,
4682
+ spans: AttrSpans | undefined
4578
4683
  ): string {
4579
4684
  if (tag.length === 2 && tag.charCodeAt(0) === 104) {
4580
4685
  const d = tag.charCodeAt(1) - 48;
4581
- if (d >= 1 && d <= 6 && (attrs === undefined || !('level' in attrs)))
4686
+ if (d >= 1 && d <= 6 && !writes(attrs, spans, 'level'))
4582
4687
  return ' level={' + d + '}';
4583
- } else if (tag === 'ol' && (attrs === undefined || !('start' in attrs)))
4688
+ } else if (tag === 'ol' && !writes(attrs, spans, 'start'))
4584
4689
  return ' start={1}';
4585
4690
  return '';
4586
4691
  }
4587
4692
 
4693
+ function writes(
4694
+ attrs: Record<string, unknown> | undefined,
4695
+ spans: AttrSpans | undefined,
4696
+ name: string
4697
+ ): boolean {
4698
+ if (spans !== undefined) return spans_have(spans, name);
4699
+ return attrs !== undefined && name in attrs;
4700
+ }
4701
+
4588
4702
  /**
4589
4703
  * the attributes of a typed tag, copied from source, and what follows them up
4590
4704
  * to > or />, which maps as identity only when it is as long
@@ -4796,9 +4910,18 @@ function comp_node(
4796
4910
  case K.HTML: {
4797
4911
  const meta = c.meta()!;
4798
4912
  const tag = meta.tag as string;
4799
- const attrs = meta.attributes as Record<string, unknown> | undefined;
4800
4913
  const typed = !c.synthetic;
4801
- const end = typed ? open_tag_end(c, tag, attrs) : -1;
4914
+ // attributes nobody read spell the source, so they need no check
4915
+ const spans = typed ? unread_spans(meta) : undefined;
4916
+ const unread = spans !== undefined && spans.name_start === c.start + 1;
4917
+ const attrs = unread
4918
+ ? undefined
4919
+ : (meta.attributes as Record<string, unknown> | undefined);
4920
+ let end = -1;
4921
+ if (unread) {
4922
+ end = spans!.open_end;
4923
+ tag_attrs_end = spans!.attrs_end;
4924
+ } else if (typed) end = open_tag_end(c, tag, attrs);
4802
4925
  // a typed tag keeps its attributes as written, they map as identity
4803
4926
  const from = c.start + 1 + tag.length;
4804
4927
  const at = pre + open.length;
@@ -4814,7 +4937,7 @@ function comp_node(
4814
4937
  if (meta.repeated !== undefined)
4815
4938
  open += repeated_attrs(meta.repeated as [string, unknown][], typed);
4816
4939
  }
4817
- if (typed) open += typed_extras(tag, attrs);
4940
+ if (typed) open += typed_extras(tag, attrs, unread ? spans : undefined);
4818
4941
  const shut = meta.self_closing ? ' />' : '>';
4819
4942
  if (end !== -1)
4820
4943
  cm_tag(
@@ -4995,6 +5118,10 @@ function dir_node(
4995
5118
  p: number,
4996
5119
  ref: ComponentImport
4997
5120
  ): number {
5121
+ if (ref.static === true && dir_shells !== null) {
5122
+ const shell = dir_shell_of(c, ref);
5123
+ if (shell !== undefined) return dir_shell(c, sink, p, shell);
5124
+ }
4998
5125
  const pre = mo.length;
4999
5126
  const kind = c.kind as number;
5000
5127
  const local = ref.local;
@@ -5034,6 +5161,148 @@ function dir_node(
5034
5161
  return p;
5035
5162
  }
5036
5163
 
5164
+ /** which snippets the component of the directive at c is given */
5165
+ function dir_snippets(c: Cursor): { label: number; children: boolean } {
5166
+ const kind = c.kind as number;
5167
+ if (kind === K.DIRECTIVE_INLINE)
5168
+ return {
5169
+ label: Slot.NONE,
5170
+ children: c.words[c.index * W.stride + W.first_child] !== Slot.NONE,
5171
+ };
5172
+ return {
5173
+ label: dir_label_of(c),
5174
+ children: kind === K.DIRECTIVE_CONTAINER && dir_has_body(c),
5175
+ };
5176
+ }
5177
+
5178
+ /** the key of the shell the static directive at c renders with */
5179
+ function dir_shell_key(c: Cursor, ref: ComponentImport): string {
5180
+ const kind = c.kind as number;
5181
+ // throws for an arg the component takes as a snippet, as a live render does
5182
+ dir_props(c, kind);
5183
+ const { label, children } = dir_snippets(c);
5184
+ return shell_key(
5185
+ ref.specifier,
5186
+ ref.name,
5187
+ c.meta()?.args as Record<string, string> | undefined,
5188
+ label !== Slot.NONE,
5189
+ children
5190
+ );
5191
+ }
5192
+
5193
+ function dir_shell_of(c: Cursor, ref: ComponentImport): Shell | undefined {
5194
+ return dir_shells === null
5195
+ ? undefined
5196
+ : dir_shells.get(dir_shell_key(c, ref));
5197
+ }
5198
+
5199
+ /**
5200
+ * a static directive as the html of its shell, the label and the body
5201
+ * rendered into its holes as the walks render them
5202
+ */
5203
+ function dir_shell(
5204
+ c: Cursor,
5205
+ sink: MapSink | undefined,
5206
+ p: number,
5207
+ shell: Shell
5208
+ ): number {
5209
+ const pre = mo.length;
5210
+ const kind = c.kind as number;
5211
+ const { html, holes } = shell;
5212
+ p = cm_put(p, html[0]);
5213
+ if (holes.length === 0) {
5214
+ cm_void(c, sink, pre, Preset.STRUCTURE);
5215
+ return p;
5216
+ }
5217
+ const ao = mo.length;
5218
+ for (let i = 0; i < holes.length; i++) {
5219
+ if (holes[i] === 'label') {
5220
+ const at = c.index;
5221
+ c.move_to(dir_label_of(c));
5222
+ p = cm_children(c, sink, p);
5223
+ c.move_to(at);
5224
+ } else {
5225
+ // no line breaks around the body, outside a snippet svelte keeps them as a space
5226
+ p = cm_children(c, sink, p);
5227
+ }
5228
+ if (i === holes.length - 1) {
5229
+ const bc = mo.length;
5230
+ p = cm_put(p, html[i + 1]);
5231
+ cm_spans(
5232
+ c,
5233
+ sink,
5234
+ pre,
5235
+ ao,
5236
+ bc,
5237
+ mo.length,
5238
+ kind === K.DIRECTIVE_INLINE ? Preset.TEXT : Preset.STRUCTURE
5239
+ );
5240
+ } else p = cm_put(p, html[i + 1]);
5241
+ }
5242
+ return p;
5243
+ }
5244
+
5245
+ /** one use of a static directive with no shell yet */
5246
+ export interface ShellRequest {
5247
+ key: string;
5248
+ /** the module of the component, emitted verbatim as an import specifier */
5249
+ specifier: string;
5250
+ /** its export name, the directive name */
5251
+ name: string;
5252
+ args: Record<string, string>;
5253
+ label: boolean;
5254
+ children: boolean;
5255
+ /** the offset of the directive in the source */
5256
+ start: number;
5257
+ }
5258
+
5259
+ /**
5260
+ * every static directive of the document in directives that shells has no
5261
+ * shell for, one request per key in document order, empty when every use
5262
+ * has one
5263
+ */
5264
+ export function shell_requests(
5265
+ buf: NodeBuffer,
5266
+ source: string,
5267
+ directives: ComponentScope,
5268
+ shells: ShellTable | undefined
5269
+ ): ShellRequest[] {
5270
+ const out: ShellRequest[] = [];
5271
+ if (!directives.has_static) return out;
5272
+ const c = new Cursor(buf, source);
5273
+ const seen = new Set<string>();
5274
+ const walk = (): void => {
5275
+ if (!c.goto_first_child()) return;
5276
+ do {
5277
+ const k = c.kind as number;
5278
+ if (k >= K.DIRECTIVE_INLINE && k <= K.DIRECTIVE_CONTAINER) {
5279
+ const ref = directives.get(dir_name(c));
5280
+ if (ref !== undefined && ref.static === true) {
5281
+ const key = dir_shell_key(c, ref);
5282
+ if (!seen.has(key) && shells?.get(key) === undefined) {
5283
+ seen.add(key);
5284
+ const { label, children } = dir_snippets(c);
5285
+ const args = c.meta()?.args as Record<string, string> | undefined;
5286
+ out.push({
5287
+ key,
5288
+ specifier: ref.specifier,
5289
+ name: ref.name,
5290
+ args: args === undefined ? {} : { ...args },
5291
+ label: label !== Slot.NONE,
5292
+ children,
5293
+ start: c.start,
5294
+ });
5295
+ }
5296
+ }
5297
+ }
5298
+ if (k !== K.CODE_SPAN && k !== K.CODE_FENCE) walk();
5299
+ } while (c.goto_next_sibling());
5300
+ c.goto_parent();
5301
+ };
5302
+ walk();
5303
+ return out;
5304
+ }
5305
+
5037
5306
  /** table content where a part may be replaced */
5038
5307
  function cm_table(c: Cursor, sink: MapSink | undefined, p: number): number {
5039
5308
  const meta = c.meta();
@@ -5496,7 +5765,9 @@ function walk_child(
5496
5765
  function wrap_root(c: Cursor, sink: MapSink | undefined, p: number): number {
5497
5766
  p = wrap_pass(c, sink, p, true);
5498
5767
  p = cm_wrap(p, wrap_open);
5499
- p = wrap_pass(c, sink, p, false);
5768
+ p = ws_on
5769
+ ? ws_pass(c, sink, p, wrap_mode, true)
5770
+ : wrap_pass(c, sink, p, false);
5500
5771
  if (fn !== null) p = fn_section(c, sink, p, wrap_mode);
5501
5772
  return cm_wrap(p, '</' + WRAP_LOCAL + '>');
5502
5773
  }
@@ -5607,6 +5878,16 @@ export class CursorHTMLRenderer {
5607
5878
  * references of a document as its labels are first met
5608
5879
  */
5609
5880
  declare footnotes?: FootnoteRender;
5881
+ /**
5882
+ * true wraps each run of two or more static root blocks in a display
5883
+ * contents div, the cached render of update ignores it
5884
+ */
5885
+ declare wrap_static?: boolean;
5886
+ /**
5887
+ * the shells of the static directives of the next renders, a static
5888
+ * directive with none renders live, the cached render of update ignores it
5889
+ */
5890
+ declare shells?: ShellTable;
5610
5891
  /** the cached render met a footnote, so each update numbers them again */
5611
5892
  private noted = false;
5612
5893
 
@@ -5661,10 +5942,12 @@ export class CursorHTMLRenderer {
5661
5942
  scope,
5662
5943
  directives,
5663
5944
  CM.FOLD,
5664
- this.replace_typed
5945
+ this.replace_typed,
5946
+ this.shells
5665
5947
  );
5666
5948
  if (extra) extra_begin(imports);
5667
5949
  if (this.strict_directives) dir_strict = true;
5950
+ if (this.wrap_static === true) ws_on = true;
5668
5951
  hoist_begin(c, buf, extra);
5669
5952
  if (code) module_begin_fold(buf, code);
5670
5953
  prebuilt_begin(buf);
@@ -5680,6 +5963,7 @@ export class CursorHTMLRenderer {
5680
5963
  esc_prebuilt = true;
5681
5964
  esc_bits = null;
5682
5965
  if (dir_strict) dir_strict = false;
5966
+ if (ws_on) ws_on = false;
5683
5967
  if (comp_scope !== null) comp_end();
5684
5968
  if (extra) extra_end();
5685
5969
  if (code) module_end();
@@ -5796,10 +6080,12 @@ export class CursorHTMLRenderer {
5796
6080
  scope,
5797
6081
  directives,
5798
6082
  trace ? CM.TRACE : CM.MAPPED,
5799
- this.replace_typed
6083
+ this.replace_typed,
6084
+ this.shells
5800
6085
  );
5801
6086
  if (extra) extra_begin(imports);
5802
6087
  if (this.strict_directives) dir_strict = true;
6088
+ if (this.wrap_static === true) ws_on = true;
5803
6089
  hoist_begin(c, buf, extra);
5804
6090
  mo = comp_prefix;
5805
6091
  if (code) {
@@ -5808,7 +6094,8 @@ export class CursorHTMLRenderer {
5808
6094
  mo += module_script();
5809
6095
  }
5810
6096
  prebuilt_begin(buf);
5811
- if (trace) p = tr_node(c, sink, 0);
6097
+ if (ws_on) p = ws_pass(c, sink, 0, trace ? CM.TRACE : CM.MAPPED, false);
6098
+ else if (trace) p = tr_node(c, sink, 0);
5812
6099
  else p = mp_node(c, sink, 0);
5813
6100
  if (fn !== null) p = fn_section(c, sink, p, trace ? CM.TRACE : CM.MAPPED);
5814
6101
  } finally {
@@ -5821,6 +6108,7 @@ export class CursorHTMLRenderer {
5821
6108
  esc_prebuilt = true;
5822
6109
  esc_bits = null;
5823
6110
  if (dir_strict) dir_strict = false;
6111
+ if (ws_on) ws_on = false;
5824
6112
  if (comp_scope !== null) comp_end();
5825
6113
  if (extra) extra_end();
5826
6114
  if (code) module_end();
@@ -5856,9 +6144,17 @@ export class CursorHTMLRenderer {
5856
6144
  (scope !== null && scope.size !== 0) ||
5857
6145
  (directives !== null && directives.size !== 0)
5858
6146
  )
5859
- comp_last = comp_begin(c, scope, directives, mode, this.replace_typed);
6147
+ comp_last = comp_begin(
6148
+ c,
6149
+ scope,
6150
+ directives,
6151
+ mode,
6152
+ this.replace_typed,
6153
+ this.shells
6154
+ );
5860
6155
  if (extra) extra_begin(imports);
5861
6156
  if (this.strict_directives) dir_strict = true;
6157
+ if (this.wrap_static === true) ws_on = true;
5862
6158
  wrap_begin(this.template!, mode);
5863
6159
  wrap_hoist_begin(c, buf);
5864
6160
  if (code) {
@@ -5891,6 +6187,7 @@ export class CursorHTMLRenderer {
5891
6187
  esc_prebuilt = true;
5892
6188
  esc_bits = null;
5893
6189
  if (dir_strict) dir_strict = false;
6190
+ if (ws_on) ws_on = false;
5894
6191
  if (comp_scope !== null) comp_end();
5895
6192
  if (extra) extra_end();
5896
6193
  wrap_end();
@@ -6034,6 +6331,8 @@ export class CursorHTMLRenderer {
6034
6331
  if (this.smart_punctuation !== undefined)
6035
6332
  this.smart_punctuation = undefined;
6036
6333
  if (this.footnotes !== undefined) this.footnotes = undefined;
6334
+ if (this.wrap_static !== undefined) this.wrap_static = undefined;
6335
+ if (this.shells !== undefined) this.shells = undefined;
6037
6336
  this.noted = false;
6038
6337
  // the escape index is module state and would keep the source alive,
6039
6338
  // every render resets it, a zero length clamps any text of '' to empty
@@ -6120,14 +6419,51 @@ function fold_url(p: number, url: string, image: boolean): number {
6120
6419
 
6121
6420
  /** url_attr of a render with a lookup */
6122
6421
  function url_swap(url: string, image: boolean): string {
6123
- const name = image ? ' src=' : ' href=';
6124
- let to = ul!(url, image);
6422
+ return url_value(image ? ' src=' : ' href=', url, ul!(url, image));
6423
+ }
6424
+
6425
+ /** the attribute for what the lookup gave, a picture is the src of a replacement */
6426
+ function url_value(
6427
+ name: string,
6428
+ url: string,
6429
+ to: UrlSource | null | undefined
6430
+ ): string {
6125
6431
  if (to == null) to = url;
6126
6432
  if (typeof to !== 'string') return name + '{' + to.value + '}';
6127
6433
  // an empty url of the source has no attribute either
6128
6434
  return to === '' ? '' : name + '"' + escape_text_html(to) + '"';
6129
6435
  }
6130
6436
 
6437
+ /** the static that closes what img_head opened last */
6438
+ let img_close = S_SELF_CLOSE;
6439
+
6440
+ /**
6441
+ * a markdown image up to its alt, the img and its src, or for a picture the
6442
+ * markup enhanced-img gives an image it imports, one source per format and
6443
+ * the img with the fallback and its size
6444
+ */
6445
+ function img_head(src: unknown): string {
6446
+ img_close = S_SELF_CLOSE;
6447
+ if (!src) return '<img';
6448
+ if (ul === null) return '<img src="' + escape_text_html(src as string) + '"';
6449
+ const to = ul(src as string, true);
6450
+ if (to == null || typeof to === 'string' || to.type !== 'picture')
6451
+ return '<img' + url_value(' src=', src as string, to);
6452
+ img_close = S_PICTURE_CLOSE;
6453
+ const v = to.value;
6454
+ return (
6455
+ '<picture>{#each Object.entries(' +
6456
+ v +
6457
+ ".sources) as [format, srcset]}<source {srcset} type={'image/' + format} />{/each}<img src={" +
6458
+ v +
6459
+ '.img.src} width={' +
6460
+ v +
6461
+ '.img.w} height={' +
6462
+ v +
6463
+ '.img.h}'
6464
+ );
6465
+ }
6466
+
6131
6467
  /** the fences of the render in progress that take their code from elsewhere, null reads every one from the source */
6132
6468
  let fs: FenceSources | null = null;
6133
6469
 
@@ -7426,3 +7762,175 @@ function fn_section_folded(c: Cursor): string {
7426
7762
  if (p !== 0) html += FOLD_STR[p];
7427
7763
  return html;
7428
7764
  }
7765
+
7766
+ // static runs
7767
+
7768
+ // hydration steps over the siblings before each node svelte owns one at a
7769
+ // time, a run of blocks in one wrapper is one step
7770
+
7771
+ /** set while a render wraps runs of static blocks, only the root walks read it */
7772
+ let ws_on = false;
7773
+
7774
+ const WS_OPEN = '<div style="display: contents">';
7775
+ const WS_CLOSE = '</div>';
7776
+
7777
+ /**
7778
+ * the root children as wrap_pass walks them, each block renders into an
7779
+ * output of its own and its records move when it joins its run and when the
7780
+ * run joins the document, so the html is never cut and a record moves twice
7781
+ * at most
7782
+ */
7783
+ function ws_pass(
7784
+ c: Cursor,
7785
+ sink: MapSink | undefined,
7786
+ p: number,
7787
+ mode: number,
7788
+ skip_hoisted: boolean
7789
+ ): number {
7790
+ const fold = mode === CM.FOLD;
7791
+ const stride = mode === CM.TRACE ? Trace.SIZE : Rec.SIZE;
7792
+ let doc = fold ? fold_out : mo;
7793
+ if (p !== 0) doc += FOLD_STR[p];
7794
+ // the static blocks not yet in doc, their records count from the start of run
7795
+ let run = '';
7796
+ let count = 0;
7797
+ let run_rec = 0;
7798
+ const n = c.words;
7799
+ let child = n[W.first_child];
7800
+ while (child !== Slot.NONE) {
7801
+ const b = child * W.stride;
7802
+ if (n[b + W.parent] !== 0) break;
7803
+ const k = n[b] & 0xff;
7804
+ if (k !== K.LINE_BREAK) {
7805
+ c.move_to(child);
7806
+ if (!skip_hoisted || !wrap_hoists(c, k)) {
7807
+ const rec = sink === undefined ? 0 : sink.n;
7808
+ if (fold) fold_out = '';
7809
+ else mo = '';
7810
+ p = walk_child(c, sink, 0, mode, child, k);
7811
+ let out = fold ? fold_out : mo;
7812
+ if (p !== 0) out += FOLD_STR[p];
7813
+ if (out.length === 0 && count === 0) {
7814
+ // renders nothing outside a run, an import or a definition
7815
+ ws_shift(sink, rec, stride, doc.length);
7816
+ } else if (out.length === 0 || ws_static(out)) {
7817
+ // a block that renders nothing inside a run neither ends it nor counts
7818
+ if (count === 0) run_rec = rec;
7819
+ ws_shift(sink, rec, stride, run.length);
7820
+ if (out.length !== 0) {
7821
+ run += out;
7822
+ count++;
7823
+ }
7824
+ } else {
7825
+ doc = ws_join(doc, run, count, sink, run_rec, rec, stride);
7826
+ run = '';
7827
+ count = 0;
7828
+ ws_shift(sink, rec, stride, doc.length);
7829
+ doc += out;
7830
+ }
7831
+ }
7832
+ }
7833
+ child = n[b + W.next];
7834
+ }
7835
+ const end = sink === undefined ? 0 : sink.n;
7836
+ doc = ws_join(doc, run, count, sink, run_rec, end, stride);
7837
+ if (fold) fold_out = doc;
7838
+ else mo = doc;
7839
+ c.move_to(0);
7840
+ return 0;
7841
+ }
7842
+
7843
+ /** doc and the run after it, inside a wrapper when the run holds two blocks or more */
7844
+ function ws_join(
7845
+ doc: string,
7846
+ run: string,
7847
+ count: number,
7848
+ sink: MapSink | undefined,
7849
+ from: number,
7850
+ to: number,
7851
+ stride: number
7852
+ ): string {
7853
+ if (count === 0) return doc;
7854
+ if (count === 1) {
7855
+ ws_move(sink, from, to, stride, doc.length);
7856
+ return doc + run;
7857
+ }
7858
+ ws_move(sink, from, to, stride, doc.length + WS_OPEN.length);
7859
+ return doc + WS_OPEN + run + WS_CLOSE;
7860
+ }
7861
+
7862
+ function ws_shift(
7863
+ sink: MapSink | undefined,
7864
+ from: number,
7865
+ stride: number,
7866
+ by: number
7867
+ ): void {
7868
+ if (sink !== undefined) ws_move(sink, from, sink.n, stride, by);
7869
+ }
7870
+
7871
+ function ws_move(
7872
+ sink: MapSink | undefined,
7873
+ from: number,
7874
+ to: number,
7875
+ stride: number,
7876
+ by: number
7877
+ ): void {
7878
+ if (sink === undefined || by === 0) return;
7879
+ // grow keeps every index, so the records sit where they were written
7880
+ const rec = sink.rec;
7881
+ for (let i = from; i < to; i += stride) rec[i] += by;
7882
+ }
7883
+
7884
+ /**
7885
+ * true when the markup holds nothing svelte finds while hydrating, a block
7886
+ * it wrongly takes is still correct inside a wrapper
7887
+ */
7888
+ function ws_static(s: string): boolean {
7889
+ const len = s.length;
7890
+ // the next brace from i, found again only once the scan passes it
7891
+ let brace = s.indexOf('{');
7892
+ let i = 0;
7893
+ for (;;) {
7894
+ const lt = s.indexOf('<', i);
7895
+ // a brace in text is an expression or a block
7896
+ if (brace !== -1 && (lt === -1 || brace < lt)) return false;
7897
+ if (lt === -1) return true;
7898
+ let ch = s.charCodeAt(lt + 1);
7899
+ if (ch === 33) {
7900
+ // a comment, svelte drops it, braces and all
7901
+ const e = s.indexOf('-->', lt + 4);
7902
+ if (e === -1) return false;
7903
+ i = e + 3;
7904
+ if (brace !== -1 && brace < i) brace = s.indexOf('{', i);
7905
+ continue;
7906
+ }
7907
+ let j = lt + 1;
7908
+ if (ch === 47) {
7909
+ // an end tag, its name was read at the start tag
7910
+ i = lt + 2;
7911
+ continue;
7912
+ }
7913
+ // a capital, a dot or a colon in the name is a component or a svelte element
7914
+ for (; j < len; j++) {
7915
+ ch = s.charCodeAt(j);
7916
+ if ((ch >= 65 && ch <= 90) || ch === 46 || ch === 58) return false;
7917
+ if (!((ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57) || ch === 45))
7918
+ break;
7919
+ }
7920
+ if (j === lt + 1) return false;
7921
+ const name = s.slice(lt + 1, j);
7922
+ if (name === 'script' || name === 'style' || name === 'slot') return false;
7923
+ // the attributes, a colon outside a value is a directive, a brace anywhere an expression
7924
+ let quote = 0;
7925
+ for (; j < len; j++) {
7926
+ ch = s.charCodeAt(j);
7927
+ if (ch === 123) return false;
7928
+ if (quote !== 0) {
7929
+ if (ch === quote) quote = 0;
7930
+ } else if (ch === 34 || ch === 39) quote = ch;
7931
+ else if (ch === 58) return false;
7932
+ else if (ch === 62) break;
7933
+ }
7934
+ i = j;
7935
+ }
7936
+ }