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

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdsvex/render",
3
- "version": "1.0.0-next.2",
3
+ "version": "1.0.0-next.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/pngwn/MDsveX.git",
@@ -52,7 +52,7 @@
52
52
  }
53
53
  },
54
54
  "dependencies": {
55
- "@mdsvex/parse": "1.0.0-next.2"
55
+ "@mdsvex/parse": "1.0.0-next.3"
56
56
  },
57
57
  "author": "pngwn <hello@pngwn.io>",
58
58
  "license": "MIT",
@@ -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,
@@ -130,8 +136,16 @@ export interface PreMeta {
130
136
  pre_props(meta: string, start: number): string;
131
137
  }
132
138
 
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 };
139
+ /**
140
+ * a string is escaped as a url of the source is, '' leaves the attribute out,
141
+ * an expression goes in braces, a picture names an imagetools Picture, a
142
+ * markdown image renders it as a picture element and a replacement gets it
143
+ * as its src
144
+ */
145
+ export type UrlSource =
146
+ | string
147
+ | { type: 'expression'; value: string }
148
+ | { type: 'picture'; value: string };
135
149
 
136
150
  /** what replaces the src of an image or the href of a link, url is never empty, null or undefined keeps it */
137
151
  export type UrlLookup = (
@@ -1156,11 +1170,13 @@ function module_begin(buf: NodeBuffer, code: string): void {
1156
1170
  if ((n[b] & 0xff) === K.HTML) {
1157
1171
  const slot = n[b + W.meta];
1158
1172
  const meta = slot === 0 ? undefined : buf._meta[slot - 1];
1159
- const attrs = meta?.attributes as Record<string, unknown> | undefined;
1173
+ const script =
1174
+ meta !== undefined && meta.tag === 'script' && !meta.self_closing;
1175
+ const attrs = script
1176
+ ? (meta.attributes as Record<string, unknown> | undefined)
1177
+ : undefined;
1160
1178
  if (
1161
- meta !== undefined &&
1162
- meta.tag === 'script' &&
1163
- !meta.self_closing &&
1179
+ script &&
1164
1180
  attrs !== undefined &&
1165
1181
  attrs.src === undefined &&
1166
1182
  (attrs.module !== undefined || attrs.context === 'module')
@@ -1648,12 +1664,11 @@ function render_node(c: Cursor, sink?: MapSink): void {
1648
1664
  case K.IMAGE: {
1649
1665
  const pre = mo.length;
1650
1666
  const meta = c.meta();
1651
- let s = '<img';
1652
- if (meta?.src) s += url_attr(meta.src as string, true);
1667
+ let s = img_head(meta?.src);
1653
1668
  s += ' alt="' + escape_text_html(_children_raw(c)) + '"';
1654
1669
  if (meta?.title)
1655
1670
  s += ' title="' + escape_text_html(meta.title as string) + '"';
1656
- mo = mo + s + _attrs(c, IMAGE_HANDLED) + ' />';
1671
+ mo = mo + s + _attrs(c, IMAGE_HANDLED) + FOLD_STR[img_close];
1657
1672
  // the syntax spans are empty, only the node is recorded
1658
1673
  if (sink) put_record(sink, pre, mo.length, c.start, c.end, c.index, 0);
1659
1674
  break;
@@ -2182,6 +2197,7 @@ const S_IMG = fold_base('<img');
2182
2197
  const S_SRC = fold_base(' src="');
2183
2198
  const S_ALT = fold_base(' alt="');
2184
2199
  const S_SELF_CLOSE = fold_base(' />');
2200
+ const S_PICTURE_CLOSE = fold_base(' /></picture>');
2185
2201
  const S_OL_START = fold_base('<ol start="');
2186
2202
  const S_OL = fold_base('<ol');
2187
2203
  const S_OL_OPEN = fold_base('<ol>\n');
@@ -2684,8 +2700,14 @@ function fold_link(c: Cursor, p: number): number {
2684
2700
 
2685
2701
  function fold_image(c: Cursor, p: number): number {
2686
2702
  const meta = c.meta();
2687
- p = push_static(p, S_IMG);
2688
- if (meta?.src) p = fold_url(p, meta.src as string, true);
2703
+ let close = S_SELF_CLOSE;
2704
+ if (ul === null) {
2705
+ p = push_static(p, S_IMG);
2706
+ if (meta?.src) p = fold_url(p, meta.src as string, true);
2707
+ } else {
2708
+ p = push_dyn(p, img_head(meta?.src));
2709
+ close = img_close;
2710
+ }
2689
2711
  p = push_static(p, S_ALT);
2690
2712
  p = push_dyn(p, escape_text_html(_children_raw(c)));
2691
2713
  p = push_static(p, S_QUOTE);
@@ -2695,7 +2717,7 @@ function fold_image(c: Cursor, p: number): number {
2695
2717
  p = push_static(p, S_QUOTE);
2696
2718
  }
2697
2719
  p = fold_attrs(meta, p, IMAGE_HANDLED);
2698
- return push_static(p, S_SELF_CLOSE);
2720
+ return push_static(p, close);
2699
2721
  }
2700
2722
 
2701
2723
  function fold_list(c: Cursor, p: number): number {
@@ -2746,9 +2768,14 @@ function fold_html(c: Cursor, p: number): number {
2746
2768
  const meta = c.meta();
2747
2769
  const tag = meta?.tag as string;
2748
2770
  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;
2771
+ // an unread tag written as fold writes it is its own attribute text
2772
+ const spans = c.synthetic ? undefined : unread_spans(meta);
2773
+ const text =
2774
+ spans !== undefined && spans.canonical ? spans_text(spans) : undefined;
2775
+ const html_attrs =
2776
+ text === undefined
2777
+ ? (meta?.attributes as Record<string, string | boolean> | undefined)
2778
+ : undefined;
2752
2779
 
2753
2780
  if (meta?.self_closing) {
2754
2781
  // source passthrough as in _node, decided before any push because a
@@ -2758,13 +2785,15 @@ function fold_html(c: Cursor, p: number): number {
2758
2785
  if (passthrough) return push_dyn(p, passthrough);
2759
2786
  p = push_static(p, S_LT);
2760
2787
  p = push_dyn(p, tag);
2761
- if (html_attrs) p = fold_html_attrs(html_attrs, !c.synthetic, p);
2788
+ if (text !== undefined) p = push_dyn(p, text);
2789
+ else if (html_attrs) p = fold_html_attrs(html_attrs, !c.synthetic, p);
2762
2790
  return push_static(p, S_SELF_CLOSE);
2763
2791
  }
2764
2792
 
2765
2793
  p = push_static(p, S_LT);
2766
2794
  p = push_dyn(p, tag);
2767
- if (html_attrs) {
2795
+ if (text !== undefined) p = push_dyn(p, text);
2796
+ else if (html_attrs) {
2768
2797
  p = fold_html_attrs(html_attrs, !c.synthetic, p);
2769
2798
  if (meta!.repeated !== undefined)
2770
2799
  p = push_dyn(
@@ -3230,15 +3259,14 @@ function tr_node(c: Cursor, sink: MapSink, p: number): number {
3230
3259
  const pre = mo.length + FOLD_LEN[p];
3231
3260
  if (p !== 0) mo += FOLD_STR[p];
3232
3261
  const meta = c.meta();
3233
- let s = '<img';
3234
- if (meta?.src) s += url_attr(meta.src as string, true);
3262
+ let s = img_head(meta?.src);
3235
3263
  s += ' alt="' + escape_text_html(_children_raw(c)) + '"';
3236
3264
  if (meta?.title)
3237
3265
  s += ' title="' + escape_text_html(meta.title as string) + '"';
3238
3266
  mo = mo + s + _attrs(c, IMAGE_HANDLED);
3239
3267
  // the syntax spans are empty, only the node is recorded
3240
3268
  tr_at(sink, pre, c);
3241
- return S_SELF_CLOSE;
3269
+ return img_close;
3242
3270
  }
3243
3271
 
3244
3272
  case K.HTML_COMMENT: {
@@ -3768,23 +3796,23 @@ function mp_node(c: Cursor, sink: MapSink, p: number): number {
3768
3796
  const pre = mo.length + FOLD_LEN[p];
3769
3797
  if (p !== 0) mo += FOLD_STR[p];
3770
3798
  const meta = c.meta();
3771
- let s = '<img';
3772
- if (meta?.src) s += url_attr(meta.src as string, true);
3799
+ let s = img_head(meta?.src);
3773
3800
  s += ' alt="' + escape_text_html(_children_raw(c)) + '"';
3774
3801
  if (meta?.title)
3775
3802
  s += ' title="' + escape_text_html(meta.title as string) + '"';
3776
3803
  mo = mo + s + _attrs(c, IMAGE_HANDLED);
3804
+ const close = img_close;
3777
3805
  // the syntax spans are empty, only the node is recorded
3778
3806
  put_record(
3779
3807
  sink,
3780
3808
  pre,
3781
- mo.length + FOLD_LEN[S_SELF_CLOSE],
3809
+ mo.length + FOLD_LEN[close],
3782
3810
  c.start,
3783
3811
  c.end,
3784
3812
  c.index,
3785
3813
  0
3786
3814
  );
3787
- return S_SELF_CLOSE;
3815
+ return close;
3788
3816
  }
3789
3817
 
3790
3818
  case K.HTML_COMMENT: {
@@ -4144,7 +4172,7 @@ function mp_table_cells_ext(
4144
4172
  /** flat so the caller never pays for a rope */
4145
4173
  function render_folded(c: Cursor): string {
4146
4174
  fold_out = comp_prefix;
4147
- const p = fold_node(c, 0);
4175
+ const p = ws_on ? ws_pass(c, undefined, 0, CM.FOLD, false) : fold_node(c, 0);
4148
4176
  let html = fold_out;
4149
4177
  fold_out = '';
4150
4178
  if (p !== 0) html += FOLD_STR[p];
@@ -4574,17 +4602,27 @@ function open_tag_end(
4574
4602
  /** the per kind extras of a typed element, those the author wrote are kept */
4575
4603
  function typed_extras(
4576
4604
  tag: string,
4577
- attrs: Record<string, unknown> | undefined
4605
+ attrs: Record<string, unknown> | undefined,
4606
+ spans: AttrSpans | undefined
4578
4607
  ): string {
4579
4608
  if (tag.length === 2 && tag.charCodeAt(0) === 104) {
4580
4609
  const d = tag.charCodeAt(1) - 48;
4581
- if (d >= 1 && d <= 6 && (attrs === undefined || !('level' in attrs)))
4610
+ if (d >= 1 && d <= 6 && !writes(attrs, spans, 'level'))
4582
4611
  return ' level={' + d + '}';
4583
- } else if (tag === 'ol' && (attrs === undefined || !('start' in attrs)))
4612
+ } else if (tag === 'ol' && !writes(attrs, spans, 'start'))
4584
4613
  return ' start={1}';
4585
4614
  return '';
4586
4615
  }
4587
4616
 
4617
+ function writes(
4618
+ attrs: Record<string, unknown> | undefined,
4619
+ spans: AttrSpans | undefined,
4620
+ name: string
4621
+ ): boolean {
4622
+ if (spans !== undefined) return spans_have(spans, name);
4623
+ return attrs !== undefined && name in attrs;
4624
+ }
4625
+
4588
4626
  /**
4589
4627
  * the attributes of a typed tag, copied from source, and what follows them up
4590
4628
  * to > or />, which maps as identity only when it is as long
@@ -4796,9 +4834,18 @@ function comp_node(
4796
4834
  case K.HTML: {
4797
4835
  const meta = c.meta()!;
4798
4836
  const tag = meta.tag as string;
4799
- const attrs = meta.attributes as Record<string, unknown> | undefined;
4800
4837
  const typed = !c.synthetic;
4801
- const end = typed ? open_tag_end(c, tag, attrs) : -1;
4838
+ // attributes nobody read spell the source, so they need no check
4839
+ const spans = typed ? unread_spans(meta) : undefined;
4840
+ const unread = spans !== undefined && spans.name_start === c.start + 1;
4841
+ const attrs = unread
4842
+ ? undefined
4843
+ : (meta.attributes as Record<string, unknown> | undefined);
4844
+ let end = -1;
4845
+ if (unread) {
4846
+ end = spans!.open_end;
4847
+ tag_attrs_end = spans!.attrs_end;
4848
+ } else if (typed) end = open_tag_end(c, tag, attrs);
4802
4849
  // a typed tag keeps its attributes as written, they map as identity
4803
4850
  const from = c.start + 1 + tag.length;
4804
4851
  const at = pre + open.length;
@@ -4814,7 +4861,7 @@ function comp_node(
4814
4861
  if (meta.repeated !== undefined)
4815
4862
  open += repeated_attrs(meta.repeated as [string, unknown][], typed);
4816
4863
  }
4817
- if (typed) open += typed_extras(tag, attrs);
4864
+ if (typed) open += typed_extras(tag, attrs, unread ? spans : undefined);
4818
4865
  const shut = meta.self_closing ? ' />' : '>';
4819
4866
  if (end !== -1)
4820
4867
  cm_tag(
@@ -5496,7 +5543,9 @@ function walk_child(
5496
5543
  function wrap_root(c: Cursor, sink: MapSink | undefined, p: number): number {
5497
5544
  p = wrap_pass(c, sink, p, true);
5498
5545
  p = cm_wrap(p, wrap_open);
5499
- p = wrap_pass(c, sink, p, false);
5546
+ p = ws_on
5547
+ ? ws_pass(c, sink, p, wrap_mode, true)
5548
+ : wrap_pass(c, sink, p, false);
5500
5549
  if (fn !== null) p = fn_section(c, sink, p, wrap_mode);
5501
5550
  return cm_wrap(p, '</' + WRAP_LOCAL + '>');
5502
5551
  }
@@ -5607,6 +5656,11 @@ export class CursorHTMLRenderer {
5607
5656
  * references of a document as its labels are first met
5608
5657
  */
5609
5658
  declare footnotes?: FootnoteRender;
5659
+ /**
5660
+ * true wraps each run of two or more static root blocks in a display
5661
+ * contents div, the cached render of update ignores it
5662
+ */
5663
+ declare wrap_static?: boolean;
5610
5664
  /** the cached render met a footnote, so each update numbers them again */
5611
5665
  private noted = false;
5612
5666
 
@@ -5665,6 +5719,7 @@ export class CursorHTMLRenderer {
5665
5719
  );
5666
5720
  if (extra) extra_begin(imports);
5667
5721
  if (this.strict_directives) dir_strict = true;
5722
+ if (this.wrap_static === true) ws_on = true;
5668
5723
  hoist_begin(c, buf, extra);
5669
5724
  if (code) module_begin_fold(buf, code);
5670
5725
  prebuilt_begin(buf);
@@ -5680,6 +5735,7 @@ export class CursorHTMLRenderer {
5680
5735
  esc_prebuilt = true;
5681
5736
  esc_bits = null;
5682
5737
  if (dir_strict) dir_strict = false;
5738
+ if (ws_on) ws_on = false;
5683
5739
  if (comp_scope !== null) comp_end();
5684
5740
  if (extra) extra_end();
5685
5741
  if (code) module_end();
@@ -5800,6 +5856,7 @@ export class CursorHTMLRenderer {
5800
5856
  );
5801
5857
  if (extra) extra_begin(imports);
5802
5858
  if (this.strict_directives) dir_strict = true;
5859
+ if (this.wrap_static === true) ws_on = true;
5803
5860
  hoist_begin(c, buf, extra);
5804
5861
  mo = comp_prefix;
5805
5862
  if (code) {
@@ -5808,7 +5865,8 @@ export class CursorHTMLRenderer {
5808
5865
  mo += module_script();
5809
5866
  }
5810
5867
  prebuilt_begin(buf);
5811
- if (trace) p = tr_node(c, sink, 0);
5868
+ if (ws_on) p = ws_pass(c, sink, 0, trace ? CM.TRACE : CM.MAPPED, false);
5869
+ else if (trace) p = tr_node(c, sink, 0);
5812
5870
  else p = mp_node(c, sink, 0);
5813
5871
  if (fn !== null) p = fn_section(c, sink, p, trace ? CM.TRACE : CM.MAPPED);
5814
5872
  } finally {
@@ -5821,6 +5879,7 @@ export class CursorHTMLRenderer {
5821
5879
  esc_prebuilt = true;
5822
5880
  esc_bits = null;
5823
5881
  if (dir_strict) dir_strict = false;
5882
+ if (ws_on) ws_on = false;
5824
5883
  if (comp_scope !== null) comp_end();
5825
5884
  if (extra) extra_end();
5826
5885
  if (code) module_end();
@@ -5859,6 +5918,7 @@ export class CursorHTMLRenderer {
5859
5918
  comp_last = comp_begin(c, scope, directives, mode, this.replace_typed);
5860
5919
  if (extra) extra_begin(imports);
5861
5920
  if (this.strict_directives) dir_strict = true;
5921
+ if (this.wrap_static === true) ws_on = true;
5862
5922
  wrap_begin(this.template!, mode);
5863
5923
  wrap_hoist_begin(c, buf);
5864
5924
  if (code) {
@@ -5891,6 +5951,7 @@ export class CursorHTMLRenderer {
5891
5951
  esc_prebuilt = true;
5892
5952
  esc_bits = null;
5893
5953
  if (dir_strict) dir_strict = false;
5954
+ if (ws_on) ws_on = false;
5894
5955
  if (comp_scope !== null) comp_end();
5895
5956
  if (extra) extra_end();
5896
5957
  wrap_end();
@@ -6034,6 +6095,7 @@ export class CursorHTMLRenderer {
6034
6095
  if (this.smart_punctuation !== undefined)
6035
6096
  this.smart_punctuation = undefined;
6036
6097
  if (this.footnotes !== undefined) this.footnotes = undefined;
6098
+ if (this.wrap_static !== undefined) this.wrap_static = undefined;
6037
6099
  this.noted = false;
6038
6100
  // the escape index is module state and would keep the source alive,
6039
6101
  // every render resets it, a zero length clamps any text of '' to empty
@@ -6120,14 +6182,51 @@ function fold_url(p: number, url: string, image: boolean): number {
6120
6182
 
6121
6183
  /** url_attr of a render with a lookup */
6122
6184
  function url_swap(url: string, image: boolean): string {
6123
- const name = image ? ' src=' : ' href=';
6124
- let to = ul!(url, image);
6185
+ return url_value(image ? ' src=' : ' href=', url, ul!(url, image));
6186
+ }
6187
+
6188
+ /** the attribute for what the lookup gave, a picture is the src of a replacement */
6189
+ function url_value(
6190
+ name: string,
6191
+ url: string,
6192
+ to: UrlSource | null | undefined
6193
+ ): string {
6125
6194
  if (to == null) to = url;
6126
6195
  if (typeof to !== 'string') return name + '{' + to.value + '}';
6127
6196
  // an empty url of the source has no attribute either
6128
6197
  return to === '' ? '' : name + '"' + escape_text_html(to) + '"';
6129
6198
  }
6130
6199
 
6200
+ /** the static that closes what img_head opened last */
6201
+ let img_close = S_SELF_CLOSE;
6202
+
6203
+ /**
6204
+ * a markdown image up to its alt, the img and its src, or for a picture the
6205
+ * markup enhanced-img gives an image it imports, one source per format and
6206
+ * the img with the fallback and its size
6207
+ */
6208
+ function img_head(src: unknown): string {
6209
+ img_close = S_SELF_CLOSE;
6210
+ if (!src) return '<img';
6211
+ if (ul === null) return '<img src="' + escape_text_html(src as string) + '"';
6212
+ const to = ul(src as string, true);
6213
+ if (to == null || typeof to === 'string' || to.type !== 'picture')
6214
+ return '<img' + url_value(' src=', src as string, to);
6215
+ img_close = S_PICTURE_CLOSE;
6216
+ const v = to.value;
6217
+ return (
6218
+ '<picture>{#each Object.entries(' +
6219
+ v +
6220
+ ".sources) as [format, srcset]}<source {srcset} type={'image/' + format} />{/each}<img src={" +
6221
+ v +
6222
+ '.img.src} width={' +
6223
+ v +
6224
+ '.img.w} height={' +
6225
+ v +
6226
+ '.img.h}'
6227
+ );
6228
+ }
6229
+
6131
6230
  /** the fences of the render in progress that take their code from elsewhere, null reads every one from the source */
6132
6231
  let fs: FenceSources | null = null;
6133
6232
 
@@ -7426,3 +7525,175 @@ function fn_section_folded(c: Cursor): string {
7426
7525
  if (p !== 0) html += FOLD_STR[p];
7427
7526
  return html;
7428
7527
  }
7528
+
7529
+ // static runs
7530
+
7531
+ // hydration steps over the siblings before each node svelte owns one at a
7532
+ // time, a run of blocks in one wrapper is one step
7533
+
7534
+ /** set while a render wraps runs of static blocks, only the root walks read it */
7535
+ let ws_on = false;
7536
+
7537
+ const WS_OPEN = '<div style="display: contents">';
7538
+ const WS_CLOSE = '</div>';
7539
+
7540
+ /**
7541
+ * the root children as wrap_pass walks them, each block renders into an
7542
+ * output of its own and its records move when it joins its run and when the
7543
+ * run joins the document, so the html is never cut and a record moves twice
7544
+ * at most
7545
+ */
7546
+ function ws_pass(
7547
+ c: Cursor,
7548
+ sink: MapSink | undefined,
7549
+ p: number,
7550
+ mode: number,
7551
+ skip_hoisted: boolean
7552
+ ): number {
7553
+ const fold = mode === CM.FOLD;
7554
+ const stride = mode === CM.TRACE ? Trace.SIZE : Rec.SIZE;
7555
+ let doc = fold ? fold_out : mo;
7556
+ if (p !== 0) doc += FOLD_STR[p];
7557
+ // the static blocks not yet in doc, their records count from the start of run
7558
+ let run = '';
7559
+ let count = 0;
7560
+ let run_rec = 0;
7561
+ const n = c.words;
7562
+ let child = n[W.first_child];
7563
+ while (child !== Slot.NONE) {
7564
+ const b = child * W.stride;
7565
+ if (n[b + W.parent] !== 0) break;
7566
+ const k = n[b] & 0xff;
7567
+ if (k !== K.LINE_BREAK) {
7568
+ c.move_to(child);
7569
+ if (!skip_hoisted || !wrap_hoists(c, k)) {
7570
+ const rec = sink === undefined ? 0 : sink.n;
7571
+ if (fold) fold_out = '';
7572
+ else mo = '';
7573
+ p = walk_child(c, sink, 0, mode, child, k);
7574
+ let out = fold ? fold_out : mo;
7575
+ if (p !== 0) out += FOLD_STR[p];
7576
+ if (out.length === 0 && count === 0) {
7577
+ // renders nothing outside a run, an import or a definition
7578
+ ws_shift(sink, rec, stride, doc.length);
7579
+ } else if (out.length === 0 || ws_static(out)) {
7580
+ // a block that renders nothing inside a run neither ends it nor counts
7581
+ if (count === 0) run_rec = rec;
7582
+ ws_shift(sink, rec, stride, run.length);
7583
+ if (out.length !== 0) {
7584
+ run += out;
7585
+ count++;
7586
+ }
7587
+ } else {
7588
+ doc = ws_join(doc, run, count, sink, run_rec, rec, stride);
7589
+ run = '';
7590
+ count = 0;
7591
+ ws_shift(sink, rec, stride, doc.length);
7592
+ doc += out;
7593
+ }
7594
+ }
7595
+ }
7596
+ child = n[b + W.next];
7597
+ }
7598
+ const end = sink === undefined ? 0 : sink.n;
7599
+ doc = ws_join(doc, run, count, sink, run_rec, end, stride);
7600
+ if (fold) fold_out = doc;
7601
+ else mo = doc;
7602
+ c.move_to(0);
7603
+ return 0;
7604
+ }
7605
+
7606
+ /** doc and the run after it, inside a wrapper when the run holds two blocks or more */
7607
+ function ws_join(
7608
+ doc: string,
7609
+ run: string,
7610
+ count: number,
7611
+ sink: MapSink | undefined,
7612
+ from: number,
7613
+ to: number,
7614
+ stride: number
7615
+ ): string {
7616
+ if (count === 0) return doc;
7617
+ if (count === 1) {
7618
+ ws_move(sink, from, to, stride, doc.length);
7619
+ return doc + run;
7620
+ }
7621
+ ws_move(sink, from, to, stride, doc.length + WS_OPEN.length);
7622
+ return doc + WS_OPEN + run + WS_CLOSE;
7623
+ }
7624
+
7625
+ function ws_shift(
7626
+ sink: MapSink | undefined,
7627
+ from: number,
7628
+ stride: number,
7629
+ by: number
7630
+ ): void {
7631
+ if (sink !== undefined) ws_move(sink, from, sink.n, stride, by);
7632
+ }
7633
+
7634
+ function ws_move(
7635
+ sink: MapSink | undefined,
7636
+ from: number,
7637
+ to: number,
7638
+ stride: number,
7639
+ by: number
7640
+ ): void {
7641
+ if (sink === undefined || by === 0) return;
7642
+ // grow keeps every index, so the records sit where they were written
7643
+ const rec = sink.rec;
7644
+ for (let i = from; i < to; i += stride) rec[i] += by;
7645
+ }
7646
+
7647
+ /**
7648
+ * true when the markup holds nothing svelte finds while hydrating, a block
7649
+ * it wrongly takes is still correct inside a wrapper
7650
+ */
7651
+ function ws_static(s: string): boolean {
7652
+ const len = s.length;
7653
+ // the next brace from i, found again only once the scan passes it
7654
+ let brace = s.indexOf('{');
7655
+ let i = 0;
7656
+ for (;;) {
7657
+ const lt = s.indexOf('<', i);
7658
+ // a brace in text is an expression or a block
7659
+ if (brace !== -1 && (lt === -1 || brace < lt)) return false;
7660
+ if (lt === -1) return true;
7661
+ let ch = s.charCodeAt(lt + 1);
7662
+ if (ch === 33) {
7663
+ // a comment, svelte drops it, braces and all
7664
+ const e = s.indexOf('-->', lt + 4);
7665
+ if (e === -1) return false;
7666
+ i = e + 3;
7667
+ if (brace !== -1 && brace < i) brace = s.indexOf('{', i);
7668
+ continue;
7669
+ }
7670
+ let j = lt + 1;
7671
+ if (ch === 47) {
7672
+ // an end tag, its name was read at the start tag
7673
+ i = lt + 2;
7674
+ continue;
7675
+ }
7676
+ // a capital, a dot or a colon in the name is a component or a svelte element
7677
+ for (; j < len; j++) {
7678
+ ch = s.charCodeAt(j);
7679
+ if ((ch >= 65 && ch <= 90) || ch === 46 || ch === 58) return false;
7680
+ if (!((ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57) || ch === 45))
7681
+ break;
7682
+ }
7683
+ if (j === lt + 1) return false;
7684
+ const name = s.slice(lt + 1, j);
7685
+ if (name === 'script' || name === 'style' || name === 'slot') return false;
7686
+ // the attributes, a colon outside a value is a directive, a brace anywhere an expression
7687
+ let quote = 0;
7688
+ for (; j < len; j++) {
7689
+ ch = s.charCodeAt(j);
7690
+ if (ch === 123) return false;
7691
+ if (quote !== 0) {
7692
+ if (ch === quote) quote = 0;
7693
+ } else if (ch === 34 || ch === 39) quote = ch;
7694
+ else if (ch === 58) return false;
7695
+ else if (ch === 62) break;
7696
+ }
7697
+ i = j;
7698
+ }
7699
+ }