@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,640 +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 { Cursor } from '@mdsvex/parse/cursor';
5
- import { ComponentScope, CursorHTMLRenderer } from '../src/html_cursor';
6
-
7
- // Helpers ��
8
-
9
- function render(source: string): string {
10
- const tree = new TreeBuilder(source.length >> 3 || 128);
11
- const parser = new PFMParser(tree);
12
- parser.parse(source);
13
- const renderer = new CursorHTMLRenderer({ cache: false });
14
- renderer.update(tree.get_buffer(), source);
15
- return renderer.html;
16
- }
17
-
18
- // Tests ��
19
-
20
- describe('CursorHTMLRenderer', () => {
21
- it('simple paragraph', () => {
22
- expect(render('hello world\n')).toBe('<p>hello world</p>');
23
- });
24
-
25
- it('multiple paragraphs', () => {
26
- const html = render('first\n\nsecond\n');
27
- expect(html).toContain('<p>first</p>');
28
- expect(html).toContain('<p>second</p>');
29
- });
30
-
31
- it('h1', () => expect(render('# Hello\n')).toBe('<h1>Hello</h1>'));
32
- it('h2', () => expect(render('## Title\n')).toBe('<h2>Title</h2>'));
33
- it('h3-h6', () => {
34
- expect(render('### H3\n')).toBe('<h3>H3</h3>');
35
- expect(render('#### H4\n')).toBe('<h4>H4</h4>');
36
- expect(render('##### H5\n')).toBe('<h5>H5</h5>');
37
- expect(render('###### H6\n')).toBe('<h6>H6</h6>');
38
- });
39
-
40
- it('emphasis', () => expect(render('_hello_\n')).toContain('<em>hello</em>'));
41
- it('strong', () =>
42
- expect(render('*bold*\n')).toContain('<strong>bold</strong>'));
43
- it('mixed emphasis', () => {
44
- const html = render('before _middle_ after\n');
45
- expect(html).toContain('before ');
46
- expect(html).toContain('<em>middle</em>');
47
- expect(html).toContain(' after');
48
- });
49
- it('nested emphasis', () => {
50
- const html = render('_em *strong* em_\n');
51
- expect(html).toContain('<em>');
52
- expect(html).toContain('<strong>strong</strong>');
53
- });
54
-
55
- it('code span', () =>
56
- expect(render('use `code` here\n')).toContain('<code>code</code>'));
57
- it('code fence with info', () => {
58
- const html = render('```js\nconst x = 1;\n```\n');
59
- expect(html).toContain('class="language-js"');
60
- expect(html).toContain('const x = 1;');
61
- });
62
- it('code fence no info', () => {
63
- expect(render('```\nhello\n```\n')).toContain(
64
- '<pre><code>hello</code></pre>'
65
- );
66
- });
67
-
68
- it('block quote', () => {
69
- const html = render('> quoted\n');
70
- expect(html).toContain('<blockquote>');
71
- expect(html).toContain('<p>quoted</p>');
72
- });
73
-
74
- it('link', () => {
75
- expect(render('[click](https://example.com)\n')).toContain(
76
- '<a href="https://example.com">click</a>'
77
- );
78
- });
79
- it('link with title', () => {
80
- expect(render('[text](url "title")\n')).toContain('title="title"');
81
- });
82
- it('image', () => {
83
- expect(render('![alt text](image.png)\n')).toContain(
84
- '<img src="image.png" alt="alt text"'
85
- );
86
- });
87
-
88
- it('unordered list (tight)', () => {
89
- const html = render('- one\n- two\n');
90
- expect(html).toContain('<ul>');
91
- expect(html).toContain('<li>one</li>');
92
- expect(html).toContain('<li>two</li>');
93
- expect(html).not.toContain('<p>');
94
- });
95
- it('ordered list (tight)', () => {
96
- const html = render('1. one\n2. two\n');
97
- expect(html).toContain('<ol>');
98
- expect(html).toContain('<li>one</li>');
99
- expect(html).toContain('<li>two</li>');
100
- });
101
-
102
- it('thematic break', () => expect(render('---\n')).toContain('<hr />'));
103
- it('strikethrough', () =>
104
- expect(render('~~deleted~~\n')).toContain('<del>deleted</del>'));
105
- it('superscript', () =>
106
- expect(render('^super^\n')).toContain('<sup>super</sup>'));
107
-
108
- it('table', () => {
109
- const html = render('| foo | bar |\n| --- | --- |\n| baz | bim |\n');
110
- expect(html).toContain('<table>');
111
- expect(html).toContain('<th>foo</th>');
112
- expect(html).toContain('<td>baz</td>');
113
- });
114
- it('table with alignment', () => {
115
- const html = render(
116
- '| left | center | right |\n| :--- | :---: | ---: |\n| a | b | c |\n'
117
- );
118
- expect(html).toContain('align="left"');
119
- expect(html).toContain('align="center"');
120
- expect(html).toContain('align="right"');
121
- });
122
-
123
- it('html self-closing', () =>
124
- expect(render('text <br /> more\n')).toContain('<br />'));
125
- it('html paired', () =>
126
- expect(render('text <span>inside</span> end\n')).toContain(
127
- '<span>inside</span>'
128
- ));
129
- it('html block', () => {
130
- const html = render('<section>\n\n# Heading\n\nParagraph.\n\n</section>\n');
131
- expect(html).toContain('<section>');
132
- expect(html).toContain('<h1>Heading</h1>');
133
- expect(html).toContain('<p>Paragraph.</p>');
134
- });
135
- it('html comment', () =>
136
- expect(render('text <!-- hidden --> more\n')).toContain('<!-- hidden -->'));
137
-
138
- it('complex document', () => {
139
- const html = render(
140
- '# Title\n\n' +
141
- 'A paragraph with _emphasis_ and *bold*.\n\n' +
142
- '> A block quote with `code`\n\n' +
143
- '```js\nconst x = 1;\n```\n\n' +
144
- '- item one\n- item two\n\n' +
145
- '| a | b |\n| --- | --- |\n| 1 | 2 |\n\n' +
146
- '[link](url) and ![img](src)\n\n' +
147
- '---\n'
148
- );
149
- expect(html).toContain('<h1>Title</h1>');
150
- expect(html).toContain('<em>emphasis</em>');
151
- expect(html).toContain('<strong>bold</strong>');
152
- expect(html).toContain('<blockquote>');
153
- expect(html).toContain('<code>code</code>');
154
- expect(html).toContain('class="language-js"');
155
- expect(html).toContain('<li>item one</li>');
156
- expect(html).toContain('<a href="url">link</a>');
157
- expect(html).toContain('<img src="src" alt="img"');
158
- expect(html).toContain('<hr />');
159
- });
160
-
161
- it('escapes HTML entities in text', () => {
162
- expect(render('1 < 2 and 3 > 1\n')).toContain('&lt;');
163
- });
164
-
165
- it('escapes HTML in code spans', () => {
166
- expect(render('`<div>`\n')).toContain('<code>&lt;div&gt;</code>');
167
- });
168
-
169
- // Svelte syntax
170
-
171
- it('mustache expression', () => {
172
- expect(render('hello {name} world')).toBe('<p>hello {name} world</p>');
173
- });
174
-
175
- it('svelte void tag {@html}', () => {
176
- expect(render('{@html "<b>bold</b>"}')).toBe('{@html "<b>bold</b>"}');
177
- });
178
-
179
- it('svelte void tag {@debug}', () => {
180
- expect(render('{@debug myVar}')).toBe('{@debug myVar}');
181
- });
182
-
183
- it('svelte void tag {@const} no expression', () => {
184
- expect(render('{@debug}')).toBe('{@debug}');
185
- });
186
-
187
- it('svelte void tag with text keeps the paragraph', () => {
188
- expect(render('{@html raw} after')).toBe('<p>{@html raw} after</p>');
189
- });
190
-
191
- it('simple if block', () => {
192
- const html = render('{#if show}\nhello\n{/if}');
193
- expect(html).toBe('{#if show}\n<p>hello</p>{/if}');
194
- });
195
-
196
- it('if/else block', () => {
197
- const html = render('{#if a}\nfirst\n{:else}\nsecond\n{/if}');
198
- expect(html).toBe('{#if a}\n<p>first</p>{:else}\n<p>second</p>{/if}');
199
- });
200
-
201
- it('if/else if/else block', () => {
202
- const html = render(
203
- '{#if a}\none\n{:else if b}\ntwo\n{:else}\nthree\n{/if}'
204
- );
205
- expect(html).toBe(
206
- '{#if a}\n<p>one</p>{:else if b}\n<p>two</p>{:else}\n<p>three</p>{/if}'
207
- );
208
- });
209
-
210
- it('each block', () => {
211
- const html = render('{#each items as item}\n{item}\n{/each}');
212
- expect(html).toBe('{#each items as item}\n<p>{item}</p>{/each}');
213
- });
214
-
215
- it('await/then/catch block', () => {
216
- const html = render(
217
- '{#await promise}\nloading\n{:then value}\ndone\n{:catch error}\nfail\n{/await}'
218
- );
219
- expect(html).toBe(
220
- '{#await promise}\n<p>loading</p>{:then value}\n<p>done</p>{:catch error}\n<p>fail</p>{/await}'
221
- );
222
- });
223
-
224
- it('nested blocks', () => {
225
- const html = render('{#if a}\n{#if b}\ninner\n{/if}\n{/if}');
226
- expect(html).toBe('{#if a}\n{#if b}\n<p>inner</p>{/if}{/if}');
227
- });
228
-
229
- it('block with markdown content', () => {
230
- const html = render('{#if show}\n# Title\n{/if}');
231
- expect(html).toBe('{#if show}\n<h1>Title</h1>{/if}');
232
- });
233
- });
234
-
235
- describe('static chunk fold', () => {
236
- function parse(source: string) {
237
- const tree = new TreeBuilder(source.length >> 3 || 128);
238
- new PFMParser(tree).parse(source);
239
- return tree.get_buffer();
240
- }
241
-
242
- it('keeps output past the composite length cap', () => {
243
- const html = render('x\n\n' + '***\n\n'.repeat(60));
244
- expect(html).toBe('<p>x</p>' + '<hr />'.repeat(60));
245
- });
246
-
247
- it('matches the mapped render, which does not fold', () => {
248
- const source =
249
- '# Title\n\n> quote\n\n- one\n- two\n\n| a | b |\n|:--|--:|\n| 1 | 2 |\n\n' +
250
- '[link](/x "t") and ![img](/i.png) and `code`\n\n---\n\n```js\nx\n```\n\n' +
251
- '<div class="a">b</div>\n\n<br />\n\n{#if a}\nyes\n{:else}\nno\n{/if}\n\n{@html x}\n';
252
- const buf = parse(source);
253
- const folded = new CursorHTMLRenderer({ cache: false });
254
- folded.update(buf, source);
255
- const mapped = new CursorHTMLRenderer({ cache: false });
256
- mapped.update_mapped(buf, source);
257
- expect(folded.html).toBe(mapped.html);
258
- const cached = new CursorHTMLRenderer();
259
- cached.update(buf, source);
260
- expect(cached.html).toBe(mapped.html);
261
- });
262
- });
263
-
264
- describe('raw html attribute values', () => {
265
- const cases: [string, string][] = [
266
- ['<div title="a &amp; b">x</div>\n', '<div title="a &amp; b">x</div>'],
267
- ['<div title="a & <b>">x</div>\n', '<div title="a & <b>">x</div>'],
268
- [
269
- `<div title='"a" &amp; b'>x</div>\n`,
270
- '<div title="&quot;a&quot; &amp; b">x</div>',
271
- ],
272
- [
273
- 'text <span title="&lt;&gt;">x</span>\n',
274
- '<p>text <span title="&lt;&gt;">x</span></p>',
275
- ],
276
- ];
277
-
278
- for (const [input, expected] of cases) {
279
- it(JSON.stringify(input), () => {
280
- const tree = new TreeBuilder(128);
281
- new PFMParser(tree).parse(input);
282
- const buf = tree.get_buffer();
283
- const folded = new CursorHTMLRenderer({ cache: false });
284
- folded.update(buf, input);
285
- expect(folded.html).toBe(expected);
286
- const mapped = new CursorHTMLRenderer({ cache: false });
287
- mapped.update_mapped(buf, input);
288
- expect(mapped.html).toBe(expected);
289
- const cached = new CursorHTMLRenderer();
290
- cached.update(buf, input);
291
- expect(cached.html).toBe(expected);
292
- });
293
- }
294
- });
295
-
296
- describe('character references', () => {
297
- const cases: [string, string][] = [
298
- ['a &lt; b &copy;\n', '<p>a &lt; b &copy;</p>'],
299
- ['<div>a &mdash; b</div>\n', '<div>a &mdash; b</div>'],
300
- [
301
- '&#123; &#x7D; &MadeUp; &copy & \\&amp; `&amp; x`\n',
302
- '<p>&#123; &#x7D; &amp;MadeUp; &amp;copy &amp; &amp;amp; <code>&amp;amp; x</code></p>',
303
- ],
304
- [
305
- '> ```\n> &lt; &copy;\n> ```\n',
306
- '<blockquote>\n<pre><code>&amp;lt; &amp;copy;</code></pre>\n</blockquote>',
307
- ],
308
- [
309
- '[a &amp; b](/x?a&amp;b "&quot;t&quot;") ![&copy;](/i?a&b)\n',
310
- '<p><a href="/x?a&amp;b" title="&quot;t&quot;">a &amp; b</a> <img src="/i?a&amp;b" alt="&copy;" /></p>',
311
- ],
312
- ];
313
-
314
- for (const [input, expected] of cases) {
315
- it(JSON.stringify(input), () => {
316
- const tree = new TreeBuilder(128);
317
- new PFMParser(tree).parse(input);
318
- const buf = tree.get_buffer();
319
- const folded = new CursorHTMLRenderer({ cache: false });
320
- folded.update(buf, input);
321
- expect(folded.html).toBe(expected);
322
- const mapped = new CursorHTMLRenderer({ cache: false });
323
- mapped.update_mapped(buf, input);
324
- expect(mapped.html).toBe(expected);
325
- const traced = new CursorHTMLRenderer({ cache: false });
326
- traced.update_trace(buf, input);
327
- expect(traced.html).toBe(expected);
328
- const cached = new CursorHTMLRenderer();
329
- cached.update(buf, input);
330
- expect(cached.html).toBe(expected);
331
- });
332
- }
333
- });
334
-
335
- describe('braces in code', () => {
336
- const cases: [string, string][] = [
337
- [
338
- 'Use `{ a: 1 }` here.\n',
339
- '<p>Use <code>&#123; a: 1 &#125;</code> here.</p>',
340
- ],
341
- [
342
- '```js {1}\nconst o = { a: 1 };\n```\n',
343
- '<pre><code class="language-js">const o = &#123; a: 1 &#125;;</code></pre>',
344
- ],
345
- [
346
- '> ```\n> {a} &lt;\n> ```\n',
347
- '<blockquote>\n<pre><code>&#123;a&#125; &amp;lt;</code></pre>\n</blockquote>',
348
- ],
349
- [
350
- '```sh title="shell" {1}\nls\n```\n',
351
- '<pre><code class="language-sh">ls</code></pre>',
352
- ],
353
- ];
354
-
355
- for (const [input, expected] of cases) {
356
- it(JSON.stringify(input), () => {
357
- const tree = new TreeBuilder(128);
358
- new PFMParser(tree).parse(input);
359
- const buf = tree.get_buffer();
360
- const folded = new CursorHTMLRenderer({ cache: false });
361
- folded.update(buf, input);
362
- expect(folded.html).toBe(expected);
363
- const mapped = new CursorHTMLRenderer({ cache: false });
364
- mapped.update_mapped(buf, input);
365
- expect(mapped.html).toBe(expected);
366
- const traced = new CursorHTMLRenderer({ cache: false });
367
- traced.update_trace(buf, input);
368
- expect(traced.html).toBe(expected);
369
- const cached = new CursorHTMLRenderer();
370
- cached.update(buf, input);
371
- expect(cached.html).toBe(expected);
372
- });
373
- }
374
- });
375
-
376
- describe('paragraphs around tags and components', () => {
377
- const cases: [string, string][] = [
378
- ['<X />\n', '<X />'],
379
- ['<X /> <Y />\n', '<X /> <Y />'],
380
- ['<X />\n<Y />\n', '<X />\n<Y />'],
381
- ['<X>child</X>\n', '<X>child</X>'],
382
- ['<img src="a.png">\n', '<img src="a.png">'],
383
- ['<!-- note -->\n', '<!-- note -->'],
384
- ['<X /> hello\n', '<p><X /> hello</p>'],
385
- ['<X />\nhello *world*\n', '<p><X />\nhello <strong>world</strong></p>'],
386
- ['hello\n<X />\n', '<p>hello\n<X /></p>'],
387
- ['a <X />\n<Y />\n', '<p>a <X />\n<Y /></p>'],
388
- ['<X>child</X> after\n', '<p><X>child</X> after</p>'],
389
- ['{title}\n', '<p>{title}</p>'],
390
- ['<div><p>hi</p></div>\n', '<div><p>hi</p></div>'],
391
- ['<div>a</div>\n', '<div>a</div>'],
392
- ['<div>*a*</div>\n', '<div><strong>a</strong></div>'],
393
- ['<div>a</div> b\n', '<div>a</div> b'],
394
- ['<div>\n# hi\n</div>\n', '<div><h1>hi</h1></div>'],
395
- [
396
- '<details><summary>Title</summary>\n\nBody\n\n</details>\n',
397
- '<details><summary>Title</summary><p>Body</p></details>',
398
- ],
399
- ['<p>\ntext\n</p>\n', '<p>text</p>'],
400
- ['<span>\ntext\n</span>\n', '<span>text</span>'],
401
- ];
402
-
403
- for (const [input, expected] of cases) {
404
- it(JSON.stringify(input), () => {
405
- expect(render(input).replace(/\n+$/, '')).toBe(expected);
406
- });
407
- }
408
- });
409
-
410
- describe('directives no component replaces', () => {
411
- const cases: [string, string][] = [
412
- ['::x[a *b*]\n', ''],
413
- [':::x[a *b*]\nbody\n:::\n', '<p>body</p>'],
414
- ['a\n\n::x[`l` :y[i]](k=v)\n\nc\n', '<p>a</p><p>c</p>'],
415
- ];
416
- it.each(cases)('every walk leaves out the label of %j', (source, html) => {
417
- const parse = () => {
418
- const tree = new TreeBuilder(64);
419
- new PFMParser(tree).parse(source);
420
- return tree.get_buffer();
421
- };
422
- expect(render(source)).toBe(html);
423
- const cached = new CursorHTMLRenderer();
424
- cached.update(parse(), source);
425
- expect(cached.html).toBe(html);
426
- const mapped = new CursorHTMLRenderer({ cache: false });
427
- mapped.update_mapped(parse(), source);
428
- expect(mapped.html).toBe(html);
429
- const traced = new CursorHTMLRenderer({ cache: false });
430
- traced.update_trace(parse(), source);
431
- expect(traced.html).toBe(html);
432
- });
433
- });
434
-
435
- describe('code fences inside block quotes', () => {
436
- const cases: [string, string, string][] = [
437
- [
438
- 'depth 1',
439
- '> ```js\n> a\n> b\n> ```\n',
440
- '<blockquote>\n<pre><code class="language-js">a\nb</code></pre>\n</blockquote>',
441
- ],
442
- [
443
- 'depth 2',
444
- '> > ```\n> > a\n> > b\n> > ```\n',
445
- '<blockquote>\n<blockquote>\n<pre><code>a\nb</code></pre>\n</blockquote>\n</blockquote>',
446
- ],
447
- [
448
- 'a blank > line',
449
- '> ```\n> a\n>\n> b\n> ```\n',
450
- '<blockquote>\n<pre><code>a\n\nb</code></pre>\n</blockquote>',
451
- ],
452
- [
453
- 'indented content',
454
- '> ```\n> a\n> b\n> ```\n',
455
- '<blockquote>\n<pre><code>a\n b</code></pre>\n</blockquote>',
456
- ],
457
- [
458
- 'markers without a space',
459
- '>```\n>a\n>>b\n>```\n',
460
- '<blockquote>\n<pre><code>a\n&gt;b</code></pre>\n</blockquote>',
461
- ],
462
- ];
463
- it.each(cases)('every walk strips the markers, %s', (_, source, html) => {
464
- const parse = () => {
465
- const tree = new TreeBuilder(64);
466
- new PFMParser(tree).parse(source);
467
- return tree.get_buffer();
468
- };
469
- expect(render(source)).toBe(html);
470
- const cached = new CursorHTMLRenderer();
471
- cached.update(parse(), source);
472
- expect(cached.html).toBe(html);
473
- const mapped = new CursorHTMLRenderer({ cache: false });
474
- mapped.update_mapped(parse(), source);
475
- expect(mapped.html).toBe(html);
476
- const traced = new CursorHTMLRenderer({ cache: false });
477
- traced.update_trace(parse(), source);
478
- expect(traced.html).toBe(html);
479
- });
480
- });
481
-
482
- describe('extended tables', () => {
483
- function parse(source: string) {
484
- const tree = new TreeBuilder(source.length >> 3 || 128);
485
- new PFMParser(tree).parse(source);
486
- return tree.get_buffer();
487
- }
488
-
489
- /** the html every walk renders, the cached one only without replacements */
490
- function every_walk(source: string, scope?: ComponentScope): string {
491
- const walks: string[] = [];
492
- const folded = new CursorHTMLRenderer({ cache: false });
493
- const mapped = new CursorHTMLRenderer({ cache: false });
494
- const traced = new CursorHTMLRenderer({ cache: false });
495
- if (scope) folded.scope = mapped.scope = traced.scope = scope;
496
- folded.update(parse(source), source);
497
- mapped.update_mapped(parse(source), source);
498
- traced.update_trace(parse(source), source);
499
- walks.push(folded.html, mapped.html, traced.html);
500
- if (!scope) {
501
- const cached = new CursorHTMLRenderer();
502
- cached.update(parse(source), source);
503
- walks.push(cached.html);
504
- }
505
- for (const html of walks) expect(html).toBe(walks[0]);
506
- return walks[0];
507
- }
508
-
509
- const table = (head: string, body: string) =>
510
- `<table>\n<thead>\n<tr>\n${head}</tr>\n</thead>\n<tbody>\n${body}</tbody>\n</table>`;
511
-
512
- const cases: [string, string, string][] = [
513
- [
514
- 'left header columns',
515
- '| h || a | b |\n|---||---|---|\n| 1 || 2 | 3 |\n',
516
- table(
517
- '<th>h</th>\n<th>a</th>\n<th>b</th>\n',
518
- '<tr>\n<th scope="row">1</th>\n<td>2</td>\n<td>3</td>\n</tr>\n'
519
- ),
520
- ],
521
- [
522
- 'right header columns',
523
- '| a | b || r |\n|---|---||---|\n| 1 | 2 || 3 |\n',
524
- table(
525
- '<th>a</th>\n<th>b</th>\n<th>r</th>\n',
526
- '<tr>\n<td>1</td>\n<td>2</td>\n<th scope="row">3</th>\n</tr>\n'
527
- ),
528
- ],
529
- [
530
- 'header columns on both sides',
531
- '| l || a || r |\n|:--||---||--:|\n| 1 || 2 || 3 |\n',
532
- table(
533
- '<th align="left">l</th>\n<th>a</th>\n<th align="right">r</th>\n',
534
- '<tr>\n<th scope="row" align="left">1</th>\n<td>2</td>\n<th scope="row" align="right">3</th>\n</tr>\n'
535
- ),
536
- ],
537
- [
538
- 'colspan in the header row',
539
- '| a | b |> | c |\n|---|---|---|---|\n| 1 | 2 | 3 | 4 |\n',
540
- table(
541
- '<th>a</th>\n<th colspan="2">b</th>\n<th>c</th>\n',
542
- '<tr>\n<td>1</td>\n<td>2</td>\n<td>3</td>\n<td>4</td>\n</tr>\n'
543
- ),
544
- ],
545
- [
546
- 'colspan in body rows',
547
- '| a | b | c |\n|---|---|---|\n| 1 | 2 |> |\n| x |> |> |\n',
548
- table(
549
- '<th>a</th>\n<th>b</th>\n<th>c</th>\n',
550
- '<tr>\n<td>1</td>\n<td colspan="2">2</td>\n</tr>\n' +
551
- '<tr>\n<td colspan="3">x</td>\n</tr>\n'
552
- ),
553
- ],
554
- [
555
- 'rowspan',
556
- '| a | b |\n|---|---|\n| 1 | 2 |\n|^ | 3 |\n|^ | 4 |\n',
557
- table(
558
- '<th>a</th>\n<th>b</th>\n',
559
- '<tr>\n<td rowspan="3">1</td>\n<td>2</td>\n</tr>\n' +
560
- '<tr>\n<td>3</td>\n</tr>\n<tr>\n<td>4</td>\n</tr>\n'
561
- ),
562
- ],
563
- [
564
- 'a merged rectangle',
565
- '| a | b | c |\n|---|---|---|\n| x |> | 1 |\n|^ |^ | 2 |\n',
566
- table(
567
- '<th>a</th>\n<th>b</th>\n<th>c</th>\n',
568
- '<tr>\n<td colspan="2" rowspan="2">x</td>\n<td>1</td>\n</tr>\n' +
569
- '<tr>\n<td>2</td>\n</tr>\n'
570
- ),
571
- ],
572
- [
573
- 'cells after a merged cell keep their column alignment',
574
- '| a | b | c | d |\n|:-:|---|--:|:--|\n| x |> | r | l |\n|^ |^ | s | m |\n',
575
- table(
576
- '<th align="center">a</th>\n<th>b</th>\n<th align="right">c</th>\n<th align="left">d</th>\n',
577
- '<tr>\n<td align="center" colspan="2" rowspan="2">x</td>\n<td align="right">r</td>\n<td align="left">l</td>\n</tr>\n' +
578
- '<tr>\n<td align="right">s</td>\n<td align="left">m</td>\n</tr>\n'
579
- ),
580
- ],
581
- [
582
- 'spans next to header columns',
583
- '| h || a | b |\n|---||---|---|\n| 1 || x |> |\n|^ || y | z |\n',
584
- table(
585
- '<th>h</th>\n<th>a</th>\n<th>b</th>\n',
586
- '<tr>\n<th scope="row" rowspan="2">1</th>\n<td colspan="2">x</td>\n</tr>\n' +
587
- '<tr>\n<td>y</td>\n<td>z</td>\n</tr>\n'
588
- ),
589
- ],
590
- ];
591
-
592
- it.each(cases)('%s', (_, source, html) => {
593
- expect(every_walk(source)).toBe(html);
594
- });
595
-
596
- it('markers with nothing to merge into stay text in a plain table', () => {
597
- expect(every_walk('| a | b |\n|:--|--:|\n| > | ^ |\n| 1 | 2 |\n')).toBe(
598
- table(
599
- '<th align="left">a</th>\n<th align="right">b</th>\n',
600
- '<tr>\n<td align="left">&gt;</td>\n<td align="right">^</td>\n</tr>\n' +
601
- '<tr>\n<td align="left">1</td>\n<td align="right">2</td>\n</tr>\n'
602
- )
603
- );
604
- });
605
-
606
- it('replaced th and td take scope and spans as they take align', () => {
607
- const scope = new ComponentScope(
608
- [{ specifier: 'm', names: ['th', 'td'] }],
609
- 'G'
610
- );
611
- const html = every_walk(
612
- '| h || a | b |\n|---||:-:|---|\n| 1 || x |> |\n|^ || y | z |\n',
613
- scope
614
- );
615
- expect(html.replace(/^<script>[^]*?<\/script>/, '')).toBe(
616
- table(
617
- '<Th_MDSVEX_G>h</Th_MDSVEX_G>\n<Th_MDSVEX_G align="center">a</Th_MDSVEX_G>\n<Th_MDSVEX_G>b</Th_MDSVEX_G>\n',
618
- '<tr>\n<Th_MDSVEX_G scope="row" rowspan="2">1</Th_MDSVEX_G>\n<Td_MDSVEX_G align="center" colspan="2">x</Td_MDSVEX_G>\n</tr>\n' +
619
- '<tr>\n<Td_MDSVEX_G align="center">y</Td_MDSVEX_G>\n<Td_MDSVEX_G>z</Td_MDSVEX_G>\n</tr>\n'
620
- )
621
- );
622
- });
623
-
624
- it('a row header uses a replaced th even when no header cell is replaced', () => {
625
- const scope = new ComponentScope([{ specifier: 'm', names: ['th'] }], 'G');
626
- const html = every_walk('| a || b |\n|---||---|\n| 1 || 2 |\n', scope);
627
- expect(html).toContain("import { th as Th_MDSVEX_G } from 'm';");
628
- expect(html).toContain(
629
- '<Th_MDSVEX_G scope="row">1</Th_MDSVEX_G>\n<td>2</td>'
630
- );
631
- const only_td = new ComponentScope(
632
- [{ specifier: 'm', names: ['td'] }],
633
- 'G'
634
- );
635
- const td_html = every_walk('| a || b |\n|---||---|\n| 1 || 2 |\n', only_td);
636
- expect(td_html).toContain(
637
- '<th scope="row">1</th>\n<Td_MDSVEX_G>2</Td_MDSVEX_G>'
638
- );
639
- });
640
- });