@mdsvex/render 1.0.0-next.1 → 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.
@@ -1,703 +0,0 @@
1
- import { describe, it, expect } from 'vitest';
2
- import { PFMParser } from '@mdsvex/parse';
3
- import { TreeBuilder } from '@mdsvex/parse/tree-builder';
4
- import {
5
- CursorHTMLRenderer,
6
- NONE,
7
- _emit,
8
- _out_offsets,
9
- _resolve_mappings,
10
- } from '../src/html_cursor';
11
- import type { Mapping, MappingData, MappingRole } from '../src/mappings';
12
- import {
13
- CI_TEXT,
14
- CI_CODE,
15
- CI_SVELTE,
16
- CI_STRUCTURE,
17
- MapSink,
18
- P_CODE,
19
- P_STRUCTURE,
20
- P_SVELTE,
21
- P_TEXT,
22
- R_CLOSE_SYNTAX,
23
- R_CONTENT,
24
- R_NODE,
25
- R_OPEN_SYNTAX,
26
- data_text,
27
- data_code,
28
- data_svelte,
29
- data_structure,
30
- record_code,
31
- record_data,
32
- } from '../src/mappings';
33
- import {
34
- build_line_starts,
35
- offset_to_position,
36
- mappings_to_v3,
37
- records_to_v3,
38
- } from '../src/sourcemap';
39
-
40
- // ── vlq decoder (for testing round-trips) ──
41
-
42
- const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
43
- const B64_LOOKUP = new Uint8Array(128);
44
- for (let i = 0; i < B64.length; i++) B64_LOOKUP[B64.charCodeAt(i)] = i;
45
-
46
- function decodeVLQMappings(mappings: string): number[][][] {
47
- const lines: number[][][] = [];
48
- let line: number[][] = [];
49
- let i = 0;
50
- let gen_col = 0,
51
- src_idx = 0,
52
- src_line = 0,
53
- src_col = 0;
54
-
55
- while (i < mappings.length) {
56
- const ch = mappings[i];
57
- if (ch === ';') {
58
- lines.push(line);
59
- line = [];
60
- gen_col = 0;
61
- i++;
62
- } else if (ch === ',') {
63
- i++;
64
- } else {
65
- const seg: number[] = [];
66
- for (
67
- let f = 0;
68
- f < 5 &&
69
- i < mappings.length &&
70
- mappings[i] !== ',' &&
71
- mappings[i] !== ';';
72
- f++
73
- ) {
74
- let value = 0,
75
- shift = 0,
76
- digit: number;
77
- do {
78
- digit = B64_LOOKUP[mappings.charCodeAt(i++)];
79
- value |= (digit & 0x1f) << shift;
80
- shift += 5;
81
- } while (digit & 0x20);
82
- seg.push(value & 1 ? -(value >>> 1) : value >>> 1);
83
- }
84
- if (seg.length >= 1) seg[0] = gen_col += seg[0];
85
- if (seg.length >= 2) seg[1] = src_idx += seg[1];
86
- if (seg.length >= 3) seg[2] = src_line += seg[2];
87
- if (seg.length >= 4) seg[3] = src_col += seg[3];
88
- line.push(seg);
89
- }
90
- }
91
- lines.push(line);
92
- return lines;
93
- }
94
-
95
- // ── helpers ──
96
-
97
- function renderMapped(source: string) {
98
- const tree = new TreeBuilder(source.length >> 3 || 128);
99
- const parser = new PFMParser(tree);
100
- parser.parse(source);
101
- const renderer = new CursorHTMLRenderer({ cache: false });
102
- const { mappings } = renderer.update_mapped(tree.get_buffer(), source);
103
- return { html: renderer.html, mappings, source };
104
- }
105
-
106
- function byRole(mappings: Mapping<MappingData>[], role: MappingRole) {
107
- return mappings.filter((m) => m.data.role === role);
108
- }
109
-
110
- function srcSlice(source: string, m: Mapping<MappingData>) {
111
- return source.slice(m.sourceOffsets[0], m.sourceOffsets[0] + m.lengths[0]);
112
- }
113
-
114
- function genSlice(html: string, m: Mapping<MappingData>) {
115
- const len = m.generatedLengths ? m.generatedLengths[0] : m.lengths[0];
116
- return html.slice(m.generatedOffsets[0], m.generatedOffsets[0] + len);
117
- }
118
-
119
- /** verify every mapping has valid offsets. */
120
- function assertMappingsValid(
121
- source: string,
122
- html: string,
123
- mappings: Mapping<MappingData>[]
124
- ) {
125
- for (const m of mappings) {
126
- for (let i = 0; i < m.sourceOffsets.length; i++) {
127
- const srcOff = m.sourceOffsets[i];
128
- const srcLen = m.lengths[i];
129
- const genOff = m.generatedOffsets[i];
130
- const genLen = m.generatedLengths ? m.generatedLengths[i] : m.lengths[i];
131
-
132
- expect(srcOff).toBeGreaterThanOrEqual(0);
133
- expect(srcOff + srcLen).toBeLessThanOrEqual(source.length);
134
- expect(genOff).toBeGreaterThanOrEqual(0);
135
- expect(genOff + genLen).toBeLessThanOrEqual(html.length);
136
- }
137
- }
138
- }
139
-
140
- // ── mapping collection tests ──
141
-
142
- describe('Mapping collection', () => {
143
- it('paragraph emits node + content mappings', () => {
144
- const { html, mappings, source } = renderMapped('hello world\n');
145
- assertMappingsValid(source, html, mappings);
146
-
147
- const nodes = byRole(mappings, 'node');
148
- const contents = byRole(mappings, 'content');
149
- expect(nodes.length).toBeGreaterThanOrEqual(1);
150
- expect(contents.length).toBe(1);
151
-
152
- // content maps text accurately
153
- expect(srcSlice(source, contents[0])).toBe('hello world');
154
- expect(genSlice(html, contents[0])).toBe('hello world');
155
-
156
- // node span covers full <p>...</p>
157
- const pNode = nodes.find((m) => genSlice(html, m).startsWith('<p'));
158
- expect(pNode).toBeDefined();
159
- });
160
-
161
- it('heading emits node + open_syntax + close_syntax + content', () => {
162
- const { html, mappings, source } = renderMapped('### hello\n');
163
- assertMappingsValid(source, html, mappings);
164
-
165
- const nodes = byRole(mappings, 'node');
166
- const opens = byRole(mappings, 'open_syntax');
167
- const closes = byRole(mappings, 'close_syntax');
168
- const contents = byRole(mappings, 'content');
169
-
170
- expect(nodes.length).toBeGreaterThanOrEqual(1);
171
- expect(opens.length).toBeGreaterThanOrEqual(1);
172
- expect(contents.length).toBe(1);
173
-
174
- // content: hello → hello
175
- expect(srcSlice(source, contents[0])).toBe('hello');
176
-
177
- // open_syntax: <h3> maps to ### (source marker)
178
- const headingOpen = opens.find((m) => genSlice(html, m).startsWith('<h'));
179
- expect(headingOpen).toBeDefined();
180
- expect(srcSlice(source, headingOpen!)).toBe('### ');
181
-
182
- // node span covers full <h3>hello</h3>
183
- const headingNode = nodes.find((m) => genSlice(html, m).includes('<h3>'));
184
- expect(headingNode).toBeDefined();
185
- });
186
-
187
- it('emphasis emits open/close syntax mapping to markers', () => {
188
- const { html, mappings, source } = renderMapped('_hello_\n');
189
- assertMappingsValid(source, html, mappings);
190
-
191
- const opens = byRole(mappings, 'open_syntax');
192
- const closes = byRole(mappings, 'close_syntax');
193
-
194
- // find the emphasis open_syntax (maps <em> → _)
195
- const emOpen = opens.find((m) => genSlice(html, m).includes('<em'));
196
- expect(emOpen).toBeDefined();
197
- expect(srcSlice(source, emOpen!)).toBe('_');
198
-
199
- const emClose = closes.find((m) => genSlice(html, m).includes('</em>'));
200
- expect(emClose).toBeDefined();
201
- expect(srcSlice(source, emClose!)).toBe('_');
202
- });
203
-
204
- it('code span emits content with CODE capabilities', () => {
205
- const { html, mappings, source } = renderMapped('use `code` here\n');
206
- assertMappingsValid(source, html, mappings);
207
-
208
- const contents = byRole(mappings, 'content');
209
- const codeContent = contents.find((m) => srcSlice(source, m) === 'code');
210
- expect(codeContent).toBeDefined();
211
- expect(codeContent!.data.semantic).toBe(true);
212
- expect(codeContent!.data.verification).toBeUndefined();
213
- });
214
-
215
- it('code fence emits content + node + syntax', () => {
216
- const { html, mappings, source } = renderMapped(
217
- '```js\nconst x = 1;\n```\n'
218
- );
219
- assertMappingsValid(source, html, mappings);
220
-
221
- const contents = byRole(mappings, 'content');
222
- const codeContent = contents.find((m) =>
223
- srcSlice(source, m).includes('const x')
224
- );
225
- expect(codeContent).toBeDefined();
226
-
227
- const opens = byRole(mappings, 'open_syntax');
228
- expect(opens.length).toBeGreaterThanOrEqual(1);
229
- });
230
-
231
- it('uses generatedLengths when escape changes length', () => {
232
- const { html, mappings, source } = renderMapped('a & b\n');
233
- assertMappingsValid(source, html, mappings);
234
-
235
- const contents = byRole(mappings, 'content');
236
- const text_content = contents[0];
237
- expect(text_content.generatedLengths).toBeDefined();
238
- expect(text_content.generatedLengths![0]).toBeGreaterThan(
239
- text_content.lengths[0]
240
- );
241
- });
242
-
243
- it('no generatedLengths when escape does not change length', () => {
244
- const { html, mappings, source } = renderMapped('hello\n');
245
- assertMappingsValid(source, html, mappings);
246
-
247
- const contents = byRole(mappings, 'content');
248
- expect(contents[0].generatedLengths).toBeUndefined();
249
- });
250
-
251
- it('nodeIndex is stable and matches cursor index', () => {
252
- const { mappings } = renderMapped('# Title\n\npara\n');
253
-
254
- // all mappings for the same node should share nodeIndex
255
- const byNode = new Map<number, MappingData[]>();
256
- for (const m of mappings) {
257
- const arr = byNode.get(m.data.nodeIndex) ?? [];
258
- arr.push(m.data);
259
- byNode.set(m.data.nodeIndex, arr);
260
- }
261
-
262
- // each node should have at most one of each role
263
- for (const [, entries] of byNode) {
264
- const roles = entries.map((e) => e.role);
265
- const unique = new Set(roles);
266
- expect(unique.size).toBe(roles.length);
267
- }
268
- });
269
-
270
- it('close_syntax source range does not span entire document for container nodes', () => {
271
- const { html, mappings, source } = renderMapped(
272
- '# Title\n\nparagraph text\n'
273
- );
274
- assertMappingsValid(source, html, mappings);
275
-
276
- const closes = byRole(mappings, 'close_syntax');
277
- for (const m of closes) {
278
- // no close_syntax source range should be more than half the document —
279
- // a bogus range from offset 0 to the node end would be caught here
280
- expect(m.lengths[0]).toBeLessThan(source.length / 2);
281
- }
282
- });
283
-
284
- it('paragraph close_syntax is zero-width (no markdown close marker)', () => {
285
- const { html, mappings, source } = renderMapped('hello world\n');
286
- assertMappingsValid(source, html, mappings);
287
-
288
- const closes = byRole(mappings, 'close_syntax');
289
- const paraClose = closes.find((m) => genSlice(html, m).includes('</p>'));
290
- expect(paraClose).toBeDefined();
291
- // paragraphs have no closing syntax in markdown — source length should be 0
292
- expect(paraClose!.lengths[0]).toBe(0);
293
- });
294
-
295
- it('paragraph open_syntax is zero-width (no markdown open marker)', () => {
296
- const { html, mappings, source } = renderMapped('hello world\n');
297
- assertMappingsValid(source, html, mappings);
298
-
299
- const opens = byRole(mappings, 'open_syntax');
300
- const paraOpen = opens.find((m) => genSlice(html, m).includes('<p'));
301
- expect(paraOpen).toBeDefined();
302
- expect(paraOpen!.lengths[0]).toBe(0);
303
- });
304
-
305
- it('heading open_syntax maps to marker, close_syntax is zero-width', () => {
306
- const { html, mappings, source } = renderMapped('### hello\n');
307
- assertMappingsValid(source, html, mappings);
308
-
309
- const opens = byRole(mappings, 'open_syntax');
310
- const headingOpen = opens.find((m) => genSlice(html, m).startsWith('<h'));
311
- expect(headingOpen).toBeDefined();
312
- expect(srcSlice(source, headingOpen!)).toBe('### ');
313
-
314
- const closes = byRole(mappings, 'close_syntax');
315
- const headingClose = closes.find((m) => genSlice(html, m).includes('</h'));
316
- expect(headingClose).toBeDefined();
317
- expect(headingClose!.lengths[0]).toBe(0);
318
- });
319
-
320
- it('code fence open_syntax maps to opening fence, close_syntax to closing fence', () => {
321
- const src = '```js\ncode\n```\n';
322
- const { html, mappings, source } = renderMapped(src);
323
- assertMappingsValid(source, html, mappings);
324
-
325
- const opens = byRole(mappings, 'open_syntax');
326
- const fenceOpen = opens.find((m) => genSlice(html, m).includes('<pre>'));
327
- expect(fenceOpen).toBeDefined();
328
- expect(srcSlice(source, fenceOpen!)).toContain('```js');
329
-
330
- const closes = byRole(mappings, 'close_syntax');
331
- const fenceClose = closes.find((m) =>
332
- genSlice(html, m).includes('</code></pre>')
333
- );
334
- expect(fenceClose).toBeDefined();
335
- expect(srcSlice(source, fenceClose!)).toContain('```');
336
- });
337
-
338
- it('emphasis open/close syntax maps to single marker chars', () => {
339
- const { html, mappings, source } = renderMapped('_hello_\n');
340
- assertMappingsValid(source, html, mappings);
341
-
342
- const opens = byRole(mappings, 'open_syntax');
343
- const emOpen = opens.find((m) => genSlice(html, m).includes('<em'));
344
- expect(emOpen).toBeDefined();
345
- expect(srcSlice(source, emOpen!)).toBe('_');
346
- expect(emOpen!.lengths[0]).toBe(1);
347
-
348
- const closes = byRole(mappings, 'close_syntax');
349
- const emClose = closes.find((m) => genSlice(html, m).includes('</em>'));
350
- expect(emClose).toBeDefined();
351
- expect(srcSlice(source, emClose!)).toBe('_');
352
- expect(emClose!.lengths[0]).toBe(1);
353
- });
354
-
355
- it('mustache emits content + node + syntax', () => {
356
- const { html, mappings, source } = renderMapped('{name}\n');
357
- assertMappingsValid(source, html, mappings);
358
-
359
- const contents = byRole(mappings, 'content');
360
- const svelte = contents.filter((m) => m.data.completion === true);
361
- expect(svelte.length).toBeGreaterThanOrEqual(1);
362
- });
363
-
364
- it('unmapped render path is unaffected', () => {
365
- const tree = new TreeBuilder(128);
366
- const parser = new PFMParser(tree);
367
- parser.parse('hello world\n');
368
- const renderer = new CursorHTMLRenderer({ cache: false });
369
- renderer.update(tree.get_buffer(), 'hello world\n');
370
- expect(renderer.html).toBe('<p>hello world</p>');
371
- });
372
- });
373
-
374
- // ── line starts tests ──
375
-
376
- describe('build_line_starts', () => {
377
- it('single line', () => {
378
- const ls = build_line_starts('hello');
379
- expect(Array.from(ls)).toEqual([0]);
380
- });
381
-
382
- it('multiple lines', () => {
383
- const ls = build_line_starts('a\nb\nc\n');
384
- expect(Array.from(ls)).toEqual([0, 2, 4, 6]);
385
- });
386
-
387
- it('empty string', () => {
388
- const ls = build_line_starts('');
389
- expect(Array.from(ls)).toEqual([0]);
390
- });
391
-
392
- it('crlf and bare cr', () => {
393
- const ls = build_line_starts('a\r\nb\rc\n\r');
394
- expect(Array.from(ls)).toEqual([0, 3, 5, 7, 8]);
395
- });
396
-
397
- it('many lines', () => {
398
- const ls = build_line_starts('x\n'.repeat(1000));
399
- expect(ls.length).toBe(1001);
400
- expect(ls[1000]).toBe(2000);
401
- });
402
- });
403
-
404
- describe('offset_to_position', () => {
405
- const ls = build_line_starts('ab\ncd\nef\n');
406
-
407
- it('start of first line', () => {
408
- expect(offset_to_position(ls, 0)).toEqual([0, 0]);
409
- });
410
-
411
- it('middle of first line', () => {
412
- expect(offset_to_position(ls, 1)).toEqual([0, 1]);
413
- });
414
-
415
- it('start of second line', () => {
416
- expect(offset_to_position(ls, 3)).toEqual([1, 0]);
417
- });
418
-
419
- it('middle of third line', () => {
420
- expect(offset_to_position(ls, 7)).toEqual([2, 1]);
421
- });
422
- });
423
-
424
- // ── v3 conversion tests ──
425
-
426
- describe('mappings_to_v3', () => {
427
- it('produces valid v3 structure', () => {
428
- const { html, mappings, source } = renderMapped('hello\n');
429
- const v3 = mappings_to_v3(mappings, source, html, 'test.md');
430
-
431
- expect(v3.version).toBe(3);
432
- expect(v3.file).toBe('test.md');
433
- expect(v3.sources).toEqual(['test.md']);
434
- expect(v3.sourcesContent).toEqual([source]);
435
- expect(v3.names).toEqual([]);
436
- expect(typeof v3.mappings).toBe('string');
437
- expect(v3.mappings.length).toBeGreaterThan(0);
438
- });
439
-
440
- it('uses basename for sources', () => {
441
- const { html, mappings, source } = renderMapped('hello\n');
442
- const v3 = mappings_to_v3(mappings, source, html, '/path/to/file.md');
443
-
444
- expect(v3.file).toBe('file.md');
445
- expect(v3.sources).toEqual(['file.md']);
446
- });
447
-
448
- it('v3 excludes syntax spans', () => {
449
- const { html, mappings, source } = renderMapped('### hello\n');
450
-
451
- // enriched mappings have syntax spans
452
- const opens = byRole(mappings, 'open_syntax');
453
- expect(opens.length).toBeGreaterThan(0);
454
-
455
- // v3 should only have content + node anchors
456
- const v3 = mappings_to_v3(mappings, source, html);
457
- expect(v3.mappings).toMatch(/^[A-Za-z0-9+/,;]*$/);
458
- });
459
-
460
- it('vlq decodes correctly and maps to right positions', () => {
461
- const { html, mappings, source } = renderMapped('hello world\n');
462
- const v3 = mappings_to_v3(mappings, source, html);
463
-
464
- const decoded = decodeVLQMappings(v3.mappings);
465
- expect(decoded.length).toBeGreaterThan(0);
466
- expect(decoded[0].length).toBeGreaterThan(0);
467
-
468
- // find the segment that maps the text content
469
- const textSeg = decoded[0].find(
470
- (s) => s.length >= 1 && html.slice(s[0], s[0] + 5) === 'hello'
471
- );
472
- expect(textSeg).toBeDefined();
473
- expect(textSeg!.length).toBe(4);
474
-
475
- // source position should point to 'hello' in source
476
- const src_line = textSeg![2];
477
- const src_col = textSeg![3];
478
- const src_lines = source.split('\n');
479
- expect(src_lines[src_line].slice(src_col, src_col + 5)).toBe('hello');
480
- });
481
-
482
- it('maps every character of long and multi-line runs listed before their node', () => {
483
- const line = 'abcdefghijklmnopqrstuvwxyz0123456789';
484
- const source = `${line}\n${line}\n`;
485
- const html = `<p>${line}\n${line}</p>`;
486
- const content: Mapping<MappingData> = {
487
- sourceOffsets: [0],
488
- generatedOffsets: [3],
489
- lengths: [source.length - 1],
490
- data: { role: 'content' } as MappingData,
491
- };
492
- const node: Mapping<MappingData> = {
493
- sourceOffsets: [0],
494
- generatedOffsets: [0],
495
- lengths: [source.length],
496
- data: { role: 'node' } as MappingData,
497
- };
498
- const v3 = mappings_to_v3([content, node], source, html);
499
- const decoded = decodeVLQMappings(v3.mappings);
500
- const src_lines = source.split('\n');
501
- const gen_lines = html.split('\n');
502
- let chars = 0;
503
- for (let l = 0; l < decoded.length; l++) {
504
- for (const [gen_col, , src_line, src_col] of decoded[l]) {
505
- if (l === 0 && gen_col === 0) continue;
506
- expect(gen_lines[l][gen_col]).toBe(src_lines[src_line][src_col]);
507
- chars++;
508
- }
509
- }
510
- expect(decoded[0][0]).toEqual([0, 0, 0, 0]);
511
- expect(chars).toBe(source.length - 1);
512
- });
513
-
514
- it('does not depend on the order of the mappings', () => {
515
- const source = 'ab'.repeat(3000);
516
- const sorted: Mapping<MappingData>[] = [];
517
- for (let i = 0; i < 3000; i++) {
518
- sorted.push({
519
- sourceOffsets: [i * 2],
520
- generatedOffsets: [i * 2],
521
- lengths: [1],
522
- data: { role: 'content' } as MappingData,
523
- });
524
- }
525
- const reversed = sorted.slice().reverse();
526
- expect(mappings_to_v3(reversed, source, source).mappings).toBe(
527
- mappings_to_v3(sorted, source, source).mappings
528
- );
529
- });
530
-
531
- it('survives svelte compiler remapping', () => {
532
- const svelte = require('svelte/compiler');
533
- // single-element output — Svelte generates per-element mappings for these
534
- const src = 'hello world\n';
535
- const { html, mappings, source } = renderMapped(src);
536
- const v3 = mappings_to_v3(mappings, source, html, 'test.md');
537
-
538
- const result = svelte.compile(html, {
539
- filename: 'test.md',
540
- generate: 'client',
541
- sourcemap: v3,
542
- });
543
-
544
- expect(result.js.map).toBeDefined();
545
- expect(result.js.map.mappings.length).toBeGreaterThan(0);
546
- expect(result.js.map.sources).toContain('test.md');
547
- });
548
- });
549
-
550
- describe('mapping data factories', () => {
551
- it('match the CI presets, key order included', () => {
552
- const pairs = [
553
- [data_text, CI_TEXT],
554
- [data_code, CI_CODE],
555
- [data_svelte, CI_SVELTE],
556
- [data_structure, CI_STRUCTURE],
557
- ] as const;
558
- for (const [make, preset] of pairs) {
559
- const data = make(7, 'content');
560
- const spread = { ...preset, nodeIndex: 7, role: 'content' };
561
- expect(data).toEqual(spread);
562
- expect(Object.keys(data)).toEqual(Object.keys(spread));
563
- }
564
- });
565
- });
566
-
567
- const RECORD_DOCS = [
568
- 'hello\n',
569
- '# Heading *em* **strong**\n\npara with `code` and <b>html</b>\n',
570
- '- one\n- two\n\n1. a\n2. b\n\n> quote\n',
571
- '```js\nlet x = 1 < 2;\n```\n\n| a | b |\n| :- | -: |\n| 1 | {x} |\n',
572
- '<script>\nlet a = 1;\n</script>\n\n{#if a}\n_y_ {@html z}\n{:else}\nno\n{/if}\n\n<Foo bar={1} />\n',
573
- '![alt *x*](src "t") [link](href) <!-- c --> a & b < c "q"\n\n---\n',
574
- '',
575
- ];
576
-
577
- describe('record_data', () => {
578
- it('builds the data objects the presets build', () => {
579
- const presets = [data_text, data_code, data_svelte, data_structure];
580
- const roles: MappingRole[] = [
581
- 'node',
582
- 'content',
583
- 'open_syntax',
584
- 'close_syntax',
585
- ];
586
- for (let p = 0; p < presets.length; p++) {
587
- for (let r = 0; r < roles.length; r++) {
588
- const data = record_data(record_code(p, r), 3);
589
- const want = presets[p](3, roles[r]);
590
- expect(data).toEqual(want);
591
- expect(Object.keys(data)).toEqual(Object.keys(want));
592
- }
593
- }
594
- });
595
- });
596
-
597
- describe('update_v3', () => {
598
- it.each(RECORD_DOCS)(
599
- 'equals mappings_to_v3 over update_mapped: %j',
600
- (source) => {
601
- const tree = new TreeBuilder(source.length >> 3 || 128);
602
- new PFMParser(tree).parse(source);
603
- const renderer = new CursorHTMLRenderer({ cache: false });
604
- const { mappings } = renderer.update_mapped(tree.get_buffer(), source);
605
- const html = renderer.html;
606
- const want = mappings_to_v3(mappings, source, html, '/a/doc.svx');
607
-
608
- const fused = new CursorHTMLRenderer({ cache: false });
609
- const got = fused.update_v3(
610
- tree.get_buffer(),
611
- source,
612
- source,
613
- '/a/doc.svx'
614
- );
615
- expect(fused.html).toBe(html);
616
- expect(JSON.stringify(got)).toBe(JSON.stringify(want));
617
- }
618
- );
619
-
620
- it('leaves update_mapped unchanged after a fused render', () => {
621
- const source = RECORD_DOCS[1];
622
- const tree = new TreeBuilder(128);
623
- new PFMParser(tree).parse(source);
624
- const renderer = new CursorHTMLRenderer({ cache: false });
625
- const before = renderer.update_mapped(tree.get_buffer(), source).mappings;
626
- renderer.update_v3(tree.get_buffer(), source, source);
627
- const after = renderer.update_mapped(tree.get_buffer(), source).mappings;
628
- expect(after).toEqual(before);
629
- expect(byRole(after, 'open_syntax').length).toBeGreaterThan(0);
630
- });
631
- });
632
-
633
- describe('records_to_v3', () => {
634
- const source = 'abcdef\nghijkl\nmnopqr\n';
635
-
636
- function both(out: string[], fill: (sink: MapSink) => void) {
637
- const sink = new MapSink();
638
- sink.begin(true);
639
- fill(sink);
640
- const generated = out.join('');
641
- const want = mappings_to_v3(
642
- _resolve_mappings(out, sink),
643
- source,
644
- generated,
645
- 'x.md'
646
- );
647
- const got = records_to_v3(
648
- sink,
649
- _out_offsets(out),
650
- source,
651
- generated,
652
- 'x.md'
653
- );
654
- expect(JSON.stringify(got)).toBe(JSON.stringify(want));
655
- return got;
656
- }
657
-
658
- it('encodes identity runs, anchors and nodes like the object path', () => {
659
- const out = ['<p>', 'abcdef', '</p>\n', 'gh&amp;', '<br />'];
660
- const map = both(out, (sink) => {
661
- _emit(sink, 0, 3, 0, 14, 0, record_code(P_TEXT, R_NODE));
662
- _emit(sink, 1, 2, 0, 6, 1, record_code(P_TEXT, R_CONTENT));
663
- _emit(sink, 3, 4, 7, 10, 2, record_code(P_TEXT, R_CONTENT));
664
- _emit(sink, 0, 1, 0, 0, 0, record_code(P_STRUCTURE, R_OPEN_SYNTAX));
665
- _emit(sink, 2, 3, 6, 7, 0, record_code(P_STRUCTURE, R_CLOSE_SYNTAX));
666
- });
667
- expect(map.mappings.length).toBeGreaterThan(0);
668
- });
669
-
670
- it('falls back to single characters when runs overlap', () => {
671
- both(['abc', 'def', 'X'], (sink) => {
672
- _emit(sink, 0, 2, 0, 6, 0, record_code(P_TEXT, R_CONTENT));
673
- _emit(sink, 1, 2, 8, 9, 1, record_code(P_CODE, R_CONTENT));
674
- _emit(sink, 2, 3, 14, 15, 2, record_code(P_SVELTE, R_NODE));
675
- });
676
- });
677
-
678
- it('skips empty ranges, missing sources and empty identity runs', () => {
679
- both(['', 'ab', '', 'c'], (sink) => {
680
- _emit(sink, 0, 1, 0, 0, 0, record_code(P_TEXT, R_CONTENT));
681
- _emit(sink, 1, 1, 0, 2, 0, record_code(P_TEXT, R_CONTENT));
682
- _emit(sink, 1, 2, NONE, 2, 0, record_code(P_TEXT, R_CONTENT));
683
- _emit(sink, 1, 2, 0, 2, 0, record_code(P_TEXT, R_CONTENT));
684
- _emit(sink, 2, 3, 3, 3, -1, record_code(P_STRUCTURE, R_NODE));
685
- _emit(sink, 3, 4, 7, 8, -1, record_code(P_SVELTE, R_CONTENT));
686
- });
687
- });
688
-
689
- it('grows the sink past its first buffer', () => {
690
- const out: string[] = [];
691
- both(out, (sink) => {
692
- for (let i = 0; i < 2000; i++) {
693
- out.push(i % 3 === 0 ? '&amp;' : 'a');
694
- const s = i % 20;
695
- _emit(sink, i, i + 1, s, s + 1, i, record_code(P_TEXT, R_CONTENT));
696
- }
697
- });
698
- });
699
-
700
- it('gives the empty map for no records', () => {
701
- expect(both(['<hr />'], () => {}).mappings).toBe('');
702
- });
703
- });