@astryxdesign/richtext 0.7.5 → 0.7.6-canary.0bd3b37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/package.json +19 -6
  2. package/src/CodeBlockHeaderPlugin.test.tsx +0 -100
  3. package/src/CodeSyntaxPlugin.test.tsx +0 -60
  4. package/src/MarkdownExtensionsPlugin.test.tsx +0 -279
  5. package/src/RichTextEditor.test.tsx +0 -2258
  6. package/src/TableColumnFloorPlugin.test.tsx +0 -49
  7. package/src/TableScrollRegionPlugin.test.tsx +0 -138
  8. package/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts +0 -2092
  9. package/src/__tests__/MarkdownPlugins.a11y.chromium.spec.ts +0 -483
  10. package/src/__tests__/MarkdownPluginsImportExport.a11y.chromium.spec.ts +0 -152
  11. package/src/__tests__/Playground.a11y.chromium.spec.ts +0 -755
  12. package/src/__tests__/RichTextCodeLanguages.a11y.chromium.spec.ts +0 -61
  13. package/src/__tests__/RichTextEditorToolbar.a11y.chromium.spec.ts +0 -113
  14. package/src/__tests__/RichTextView.a11y.chromium.spec.ts +0 -101
  15. package/src/__tests__/TableCellSelection.a11y.chromium.spec.ts +0 -380
  16. package/src/markdownAngleCodeSpans.test.ts +0 -92
  17. package/src/markdownAngleDestinations.test.ts +0 -332
  18. package/src/markdownCharacterReferences.test.ts +0 -202
  19. package/src/markdownCodeFence.test.ts +0 -195
  20. package/src/markdownCodeSpans.test.ts +0 -399
  21. package/src/markdownEscapes.test.ts +0 -234
  22. package/src/markdownExtensionSelection.test.ts +0 -102
  23. package/src/markdownExtensions.test.ts +0 -765
  24. package/src/markdownHeadingMarkers.test.ts +0 -132
  25. package/src/markdownIndentedFences.test.ts +0 -43
  26. package/src/markdownIndentedTables.test.ts +0 -43
  27. package/src/markdownListContinuation.test.ts +0 -34
  28. package/src/markdownListIndentation.test.ts +0 -235
  29. package/src/markdownListThematicBreak.test.ts +0 -31
  30. package/src/markdownListThematicBreakInside.test.ts +0 -64
  31. package/src/markdownOpenFence.test.ts +0 -35
  32. package/src/markdownOrderedList.test.ts +0 -62
  33. package/src/markdownQuoteMarkers.test.ts +0 -206
  34. package/src/markdownRefusedLinks.test.tsx +0 -193
  35. package/src/markdownSource.test.tsx +0 -750
  36. package/src/markdownSpaceRuns.test.ts +0 -204
  37. package/src/markdownSpacedBlockStarts.test.ts +0 -96
  38. package/src/markdownTabRuns.test.ts +0 -75
  39. package/src/markdownTable.test.tsx +0 -214
  40. package/src/markdownTableLinks.test.ts +0 -69
  41. package/src/markdownTaskList.test.tsx +0 -445
  42. package/src/markdownThematicBreak.test.tsx +0 -185
  43. package/src/publicSurface.test.ts +0 -42
  44. package/src/textSemantics.test.tsx +0 -368
@@ -1,92 +0,0 @@
1
- // Copyright (c) Meta Platforms, Inc. and affiliates.
2
-
3
- import {describe, expect, it} from 'vitest';
4
- import {createHeadlessEditor} from '@lexical/headless';
5
- import {$createLinkNode} from '@lexical/link';
6
- import {$createParagraphNode, $createTextNode, $getRoot} from 'lexical';
7
- import {parseInlineAst} from '@astryxdesign/core/Markdown/parser';
8
- import {DEFAULT_NODES} from './editorNodes';
9
- import {DEFAULT_TRANSFORMERS} from './markdownTable';
10
- import {$exportMarkdownKeepingSource} from './markdownSource';
11
- import {markdownToEditorStateJSON} from './markdownSerializers';
12
-
13
- type Json = {type: string; url?: string; children?: Json[]};
14
-
15
- const urlsIn = (nodes: ReadonlyArray<Json>): string[] =>
16
- nodes.flatMap(node => [
17
- ...(node.type === 'link' ? [node.url ?? ''] : []),
18
- ...urlsIn(node.children ?? []),
19
- ]);
20
-
21
- const richUrls = (markdown: string): string[] =>
22
- urlsIn([
23
- (JSON.parse(markdownToEditorStateJSON(markdown)) as {root: Json}).root,
24
- ]);
25
-
26
- const coreUrls = (markdown: string): string[] =>
27
- urlsIn(parseInlineAst(markdown) as unknown as Json[]);
28
-
29
- /** A link made in the editor to `url`, exported. */
30
- function exportLinkTo(url: string): string {
31
- const editor = createHeadlessEditor({
32
- nodes: [...DEFAULT_NODES],
33
- onError(error) {
34
- throw error;
35
- },
36
- });
37
- editor.update(
38
- () => {
39
- $getRoot()
40
- .clear()
41
- .append(
42
- $createParagraphNode().append(
43
- $createLinkNode(url).append($createTextNode('x')),
44
- ),
45
- );
46
- },
47
- {discrete: true},
48
- );
49
- return editor
50
- .getEditorState()
51
- .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));
52
- }
53
-
54
- describe('backticks inside an angle-bracket destination with a parenthesis', () => {
55
- it.each([
56
- '[x](<https://e.com/* ([b`\\<`>)',
57
- '[a](<b ([c`d`>)',
58
- '`x` [a](<b (`c`>)',
59
- ])('stay in the address of %j, as core reads it', markdown => {
60
- expect(richUrls(markdown)).toEqual(coreUrls(markdown));
61
- expect(richUrls(markdown)).toHaveLength(1);
62
- });
63
-
64
- it('read back from a link made in the editor', () => {
65
- const url = 'https://e.com/* ([b`<`';
66
- expect(richUrls(exportLinkTo(url))).toEqual([url]);
67
- });
68
-
69
- it('read back across 4,000 addresses made in the editor', () => {
70
- let state = 7050;
71
- const random = () => {
72
- state = (state * 1103515245 + 12345) % 2147483648;
73
- return state / 2147483648;
74
- };
75
- // Backslashes outside angle brackets are a separate gap (#7098).
76
- const alphabet = [...'ab/. ()<>"\'#:_*[]&;`'];
77
- const lost: string[] = [];
78
- for (let round = 0; round < 4000; round++) {
79
- let tail = '';
80
- const length = 1 + Math.floor(random() * 8);
81
- for (let index = 0; index < length; index++) {
82
- tail += alphabet[Math.floor(random() * alphabet.length)];
83
- }
84
- const url = `https://e.com/${tail}`;
85
- const read = richUrls(exportLinkTo(url));
86
- if (read.length !== 1 || read[0] !== url) {
87
- lost.push(url);
88
- }
89
- }
90
- expect(lost).toEqual([]);
91
- });
92
- });
@@ -1,332 +0,0 @@
1
- // Copyright (c) Meta Platforms, Inc. and affiliates.
2
-
3
- import {describe, expect, it} from 'vitest';
4
- import {createHeadlessEditor} from '@lexical/headless';
5
- import {$createLinkNode} from '@lexical/link';
6
- import {
7
- $createParagraphNode,
8
- $createTextNode,
9
- $getRoot,
10
- $isTextNode,
11
- } from 'lexical';
12
- import {parseInlineAst} from '@astryxdesign/core/Markdown/parser';
13
- import {DEFAULT_NODES} from './editorNodes';
14
- import {coreRefusesUrl} from './markdownCharacterReferences';
15
- import {DEFAULT_TRANSFORMERS} from './markdownTable';
16
- import {
17
- $exportMarkdownKeepingSource,
18
- importMarkdownKeepingSource,
19
- } from './markdownSource';
20
- import {
21
- editorStateJSONToMarkdown,
22
- markdownToEditorStateJSON,
23
- } from './markdownSerializers';
24
-
25
- type Json = Record<string, unknown>;
26
-
27
- /** Each link's URL and title, as RichText imports it. */
28
- function richLinks(
29
- markdown: string,
30
- ): Array<{url: string; title: string | null}> {
31
- const links: Array<{url: string; title: string | null}> = [];
32
- const visit = (node: Json) => {
33
- if (node.type === 'link') {
34
- links.push({
35
- url: node.url as string,
36
- title: (node.title as string | null | undefined) ?? null,
37
- });
38
- }
39
- ((node.children as Json[]) ?? []).forEach(visit);
40
- };
41
- visit((JSON.parse(markdownToEditorStateJSON(markdown)) as {root: Json}).root);
42
- return links;
43
- }
44
-
45
- /** Each link's URL, as core reads it. */
46
- function coreUrls(markdown: string): string[] {
47
- const urls: string[] = [];
48
- const visit = (node: Json) => {
49
- if (node.type === 'link') {
50
- urls.push(node.url as string);
51
- }
52
- ((node.children as Json[]) ?? []).forEach(visit);
53
- };
54
- (parseInlineAst(markdown) as unknown as Json[]).forEach(visit);
55
- return urls;
56
- }
57
-
58
- function newEditor() {
59
- return createHeadlessEditor({
60
- nodes: [...DEFAULT_NODES],
61
- onError(error) {
62
- throw error;
63
- },
64
- });
65
- }
66
-
67
- describe('angle-bracket link destinations (CommonMark 0.31 §6.3)', () => {
68
- it.each([
69
- 'See [c](<https://e.com/a b>) here',
70
- 'See [c](<https://e.com/a b> "t") here',
71
- 'See [c](<https://e.com/x>) here',
72
- 'See [c](<a b>) and [d](https://e.com) here',
73
- ])('reads %j with the address core reads, and round-trips it', markdown => {
74
- expect(richLinks(markdown).map(({url}) => url)).toEqual(coreUrls(markdown));
75
- expect(
76
- editorStateJSONToMarkdown(markdownToEditorStateJSON(`${markdown}\n`)),
77
- ).toBe(`${markdown}\n`);
78
- });
79
-
80
- it('keeps the title of an angle-bracket destination', () => {
81
- expect(richLinks('[c](<a b> "t")')).toEqual([{url: 'a b', title: 't'}]);
82
- });
83
-
84
- it('writes an address holding a space in angle brackets after an edit, so it reads back', () => {
85
- const editor = newEditor();
86
- importMarkdownKeepingSource(editor, 'See [c](<https://e.com/a b>) here\n', [
87
- ...DEFAULT_TRANSFORMERS,
88
- ]);
89
- editor.update(
90
- () => {
91
- const last = $getRoot().getAllTextNodes().at(-1);
92
- if (!$isTextNode(last)) {
93
- throw new Error('No text');
94
- }
95
- last.setTextContent(' there');
96
- },
97
- {discrete: true},
98
- );
99
- const exported = editor
100
- .getEditorState()
101
- .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));
102
- expect(exported).toBe('See [c](<https://e.com/a b>) there\n');
103
- expect(richLinks(exported)).toEqual([
104
- {url: 'https://e.com/a b', title: null},
105
- ]);
106
- });
107
-
108
- it('writes a link made in the editor with a space in its address so it reads back', () => {
109
- const editor = newEditor();
110
- editor.update(
111
- () => {
112
- $getRoot()
113
- .clear()
114
- .append(
115
- $createParagraphNode().append(
116
- $createLinkNode('https://e.com/a b').append($createTextNode('c')),
117
- ),
118
- );
119
- },
120
- {discrete: true},
121
- );
122
- const exported = editor
123
- .getEditorState()
124
- .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));
125
- expect(exported).toBe('[c](<https://e.com/a b>)');
126
- expect(richLinks(exported)).toEqual([
127
- {url: 'https://e.com/a b', title: null},
128
- ]);
129
- });
130
-
131
- it('still refuses an unsafe address in angle brackets', () => {
132
- expect(richLinks('[x](<javascript:alert(1)>)')).toEqual([]);
133
- expect(richLinks('[x](<data: text/html,hi>)')).toEqual([]);
134
- });
135
- });
136
-
137
- describe('angle-bracket destination edges', () => {
138
- it.each(['[a](<b>c>)', '[link](<foo\\>)', '[a](<b)', '[a](<b<c>)'])(
139
- 'does not link %j, which is no angle-bracket destination, as core does',
140
- markdown => {
141
- expect(richLinks(markdown)).toEqual([]);
142
- expect(coreUrls(markdown)).toEqual([]);
143
- },
144
- );
145
-
146
- it('writes a link made in the editor whose address opens with `<` so it reads back', () => {
147
- const editor = newEditor();
148
- editor.update(
149
- () => {
150
- $getRoot()
151
- .clear()
152
- .append(
153
- $createParagraphNode().append(
154
- $createLinkNode('<b').append($createTextNode('x')),
155
- ),
156
- );
157
- },
158
- {discrete: true},
159
- );
160
- const exported = editor
161
- .getEditorState()
162
- .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));
163
- expect(exported).toBe('[x](<\\<b>)');
164
- expect(richLinks(exported)).toEqual([{url: '<b', title: null}]);
165
- });
166
-
167
- it('writes a reference-like `&` in an angle destination and its title so it reads back', () => {
168
- const editor = newEditor();
169
- editor.update(
170
- () => {
171
- $getRoot()
172
- .clear()
173
- .append(
174
- $createParagraphNode().append(
175
- $createLinkNode('https://e.com/a b&amp;c', {
176
- title: 'x &copy; y',
177
- }).append($createTextNode('x')),
178
- ),
179
- );
180
- },
181
- {discrete: true},
182
- );
183
- const exported = editor
184
- .getEditorState()
185
- .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));
186
- // Import decodes character references in a destination and a title, so
187
- // the `&` that would start one is written as a reference itself.
188
- expect(exported).toBe(
189
- '[x](<https://e.com/a b&#38;amp;c> "x &#38;copy; y")',
190
- );
191
- expect(richLinks(exported)).toEqual([
192
- {url: 'https://e.com/a b&amp;c', title: 'x &copy; y'},
193
- ]);
194
- });
195
-
196
- it('stores no unsafe address, however an angle destination is written', () => {
197
- let state = 40;
198
- const random = () => {
199
- state = (state * 1103515245 + 12345) % 2147483648;
200
- return state / 2147483648;
201
- };
202
- const pick = <T>(items: ReadonlyArray<T>): T =>
203
- items[Math.floor(random() * items.length)];
204
- const schemes = [
205
- 'javascript:',
206
- 'JaVaScRiPt:',
207
- '&#106;avascript:',
208
- '&#x6A;avascript:',
209
- 'java\\script:',
210
- 'vbscript:',
211
- 'data:text/html,',
212
- 'data: text/html,',
213
- ];
214
- for (let round = 0; round < 300; round++) {
215
- const body = pick(['x', 'a(b)', 'a\\>b', 'a>b', 'a<b', ' x ', 'a\\', '']);
216
- const title = pick(['', ' "t"']);
217
- const markdown = `[a](<${pick(schemes)}${body}>${title})`;
218
- for (const {url} of richLinks(markdown)) {
219
- expect(coreRefusesUrl(url), markdown).toBe(false);
220
- }
221
- }
222
- });
223
-
224
- it('reads an escaped bracket inside the angle brackets as part of the address', () => {
225
- expect(richLinks('[a](<a \\<b>)')).toEqual([{url: 'a <b', title: null}]);
226
- });
227
-
228
- it('writes an address holding a space and brackets so it reads back', () => {
229
- const editor = newEditor();
230
- editor.update(
231
- () => {
232
- $getRoot()
233
- .clear()
234
- .append(
235
- $createParagraphNode().append(
236
- $createLinkNode('a <b c>').append($createTextNode('x')),
237
- ),
238
- );
239
- },
240
- {discrete: true},
241
- );
242
- const exported = editor
243
- .getEditorState()
244
- .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));
245
- expect(exported).toBe('[x](<a \\<b c\\>>)');
246
- expect(richLinks(exported)).toEqual([{url: 'a <b c>', title: null}]);
247
- });
248
-
249
- it.each([
250
- '[x](<javascript:x \\<y>)',
251
- '[x](<data: text/html \\<b\\>>)',
252
- '[x](<JavaScript:a b>)',
253
- ])('still refuses the unsafe address in %j', markdown => {
254
- expect(richLinks(markdown)).toEqual([]);
255
- });
256
- });
257
-
258
- /** A link made in the editor to `url`, exported. */
259
- function exportLinkTo(url: string): string {
260
- const editor = newEditor();
261
- editor.update(
262
- () => {
263
- $getRoot()
264
- .clear()
265
- .append(
266
- $createParagraphNode().append(
267
- $createLinkNode(url).append($createTextNode('x')),
268
- ),
269
- );
270
- },
271
- {discrete: true},
272
- );
273
- return editor
274
- .getEditorState()
275
- .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));
276
- }
277
-
278
- describe('angle-bracket destinations holding parentheses and backslashes', () => {
279
- it('links `[a](<b)c>)` to `b)c`, as core does', () => {
280
- const markdown = '[a](<b)c>)';
281
- expect(richLinks(markdown)).toEqual([{url: 'b)c', title: null}]);
282
- expect(coreUrls(markdown)).toEqual(['b)c']);
283
- });
284
-
285
- it.each([
286
- 'https://example.com/a b)',
287
- 'https://example.com/a (b',
288
- 'https://e.com/a\\b c',
289
- 'https://e.com/a\\> b',
290
- 'https://e.com/a b\\',
291
- ])('writes a link made in the editor to %j so it reads back', url => {
292
- expect(richLinks(exportLinkTo(url))).toEqual([{url, title: null}]);
293
- });
294
-
295
- it.each([
296
- 'https://e.com/a\\*b',
297
- 'https://e.com/a\\',
298
- 'https://e.com/a\\(b',
299
- 'https://e.com/a\\b',
300
- 'https://e.com/a\\\\b',
301
- ])(
302
- 'writes a link made in the editor to %j, backslashes and all, so it reads back',
303
- url => {
304
- expect(richLinks(exportLinkTo(url))).toEqual([{url, title: null}]);
305
- },
306
- );
307
-
308
- it('reads back every link made in the editor across 4,000 addresses', () => {
309
- let state = 4000;
310
- const random = () => {
311
- state = (state * 1103515245 + 12345) % 2147483648;
312
- return state / 2147483648;
313
- };
314
- // Backticks are left out: inside an angle-bracket destination they can
315
- // still pair as code, a separate gap.
316
- const alphabet = [...'ab/. ()<>"\'#:_*[]&;\\'];
317
- const lost: string[] = [];
318
- for (let round = 0; round < 4000; round++) {
319
- let tail = '';
320
- const length = 1 + Math.floor(random() * 8);
321
- for (let index = 0; index < length; index++) {
322
- tail += alphabet[Math.floor(random() * alphabet.length)];
323
- }
324
- const url = `https://e.com/${tail}`;
325
- const read = richLinks(exportLinkTo(url));
326
- if (read.length !== 1 || read[0].url !== url) {
327
- lost.push(url);
328
- }
329
- }
330
- expect(lost).toEqual([]);
331
- });
332
- });
@@ -1,202 +0,0 @@
1
- // Copyright (c) Meta Platforms, Inc. and affiliates.
2
-
3
- import {describe, it, expect} from 'vitest';
4
- import {createHeadlessEditor} from '@lexical/headless';
5
- import {$getRoot} from 'lexical';
6
- import {parseInline, parseMarkdown} from '@astryxdesign/core/Markdown/parser';
7
- import {DEFAULT_NODES} from './editorNodes';
8
- import {
9
- editorStateJSONToMarkdown,
10
- markdownToEditorStateJSON,
11
- } from './markdownSerializers';
12
-
13
- interface SerializedNode {
14
- readonly type: string;
15
- readonly text?: string;
16
- readonly children?: ReadonlyArray<SerializedNode>;
17
- }
18
-
19
- /** The text RichText imports for a one-paragraph document. */
20
- function richTextText(markdown: string): string {
21
- const {root} = JSON.parse(markdownToEditorStateJSON(markdown)) as {
22
- root: SerializedNode;
23
- };
24
- const texts: Array<string> = [];
25
- const visit = (node: SerializedNode) => {
26
- if (node.text != null) {
27
- texts.push(node.text);
28
- }
29
- node.children?.forEach(visit);
30
- };
31
- visit(root);
32
- return texts.join('');
33
- }
34
-
35
- /** The text core Markdown renders for the same paragraph. */
36
- function markdownText(markdown: string): string {
37
- return parseInline(markdown)
38
- .map(node =>
39
- 'content' in node
40
- ? String(node.content)
41
- : 'children' in node && Array.isArray(node.children)
42
- ? node.children
43
- .map(child => ('content' in child ? String(child.content) : ''))
44
- .join('')
45
- : '',
46
- )
47
- .join('');
48
- }
49
-
50
- // One table for both surfaces (spec:AST-061 DEC-5).
51
- const CASES = [
52
- '&copy;',
53
- '&#169;',
54
- '&#xA9; and &#XA9;',
55
- 'Fish &amp; chips',
56
- '&amp;copy;',
57
- '&NotEqualTilde;',
58
- '&unknown;',
59
- '&copy without a semicolon',
60
- '& copy;',
61
- '&#0; &#xD800; &#x110000;',
62
- '&#12345678;',
63
- 'a&nbsp;b',
64
- '\\&copy; is escaped',
65
- // Backslashes inside a code span are literal and close nothing early.
66
- 'Paths like `C:\\` &amp; `D:\\` work',
67
- 'Path `C:\\` then &#12345678; and &copy;',
68
- // An escaped opener, and spans of one, two, and three backticks.
69
- '\\`&amp;` stays text',
70
- '``a ` &amp; b`` and &amp;',
71
- 'Then ```a `` &amp; b``` and &copy;',
72
- // An unclosed run is literal.
73
- '`unclosed &amp; text',
74
- '\\&#169; is escaped too',
75
- '\\\\&copy; follows an escaped backslash',
76
- 'code `&copy;` stays',
77
- ];
78
-
79
- describe('character references (spec:AST-061 FR7, DEC-5)', () => {
80
- it('import exactly as core Markdown renders them', () => {
81
- for (const source of CASES) {
82
- expect(richTextText(`${source}\n`), source).toBe(markdownText(source));
83
- }
84
- });
85
-
86
- it('decode the same way in headings, lists, quotes, links, and table cells', () => {
87
- const {root} = JSON.parse(
88
- markdownToEditorStateJSON(
89
- [
90
- '# Caf&eacute; &#x2014; menu',
91
- '',
92
- '- Fish &amp; chips',
93
- '> &copy; 2026',
94
- '',
95
- '[Terms &amp; conditions](https://example.com/?a=1&amp;b=2 "T&amp;C")',
96
- '',
97
- '| Item &amp; size | Price |',
98
- '| --- | --- |',
99
- '| Tea \\| &frac12; `&copy;` **&#169;** | &#12345678; \\&copy; |',
100
- '',
101
- ].join('\n'),
102
- ),
103
- ) as {root: SerializedNode & {children: Array<SerializedNode>}};
104
- const texts: Array<string> = [];
105
- const links: Array<{url?: unknown; title?: unknown}> = [];
106
- const visit = (node: SerializedNode & {url?: unknown; title?: unknown}) => {
107
- if (node.text != null) {
108
- texts.push(node.text);
109
- }
110
- if (node.type === 'link') {
111
- links.push({url: node.url, title: node.title});
112
- }
113
- (node.children as Array<typeof node> | undefined)?.forEach(visit);
114
- };
115
- visit(root);
116
- const all = texts.join('|');
117
- expect(all).toContain('Café — menu');
118
- expect(all).toContain('Fish & chips');
119
- expect(all).toContain('© 2026');
120
- expect(all).toContain('Terms & conditions');
121
- expect(all).toContain('Item & size');
122
- expect(all).toContain('½');
123
- // Inline code keeps the reference; strong text decodes it.
124
- expect(texts).toContain('&copy;');
125
- expect(texts).toContain('©');
126
- // Not a reference, and an escaped one: literal.
127
- expect(all).toContain('&#12345678;');
128
- expect(all).toContain('&copy;');
129
- expect(links).toEqual([
130
- {url: 'https://example.com/?a=1&b=2', title: 'T&C'},
131
- ]);
132
- });
133
-
134
- it('decode thousands of references in one pass, without extra nodes', () => {
135
- for (const count of [2000, 6000, 10000]) {
136
- const unit = '&copy; &#169; &#12345678; \\&amp; &nope; ';
137
- const markdown = `${unit.repeat(count / 5)}\n`;
138
- const started = performance.now();
139
- const {root} = JSON.parse(markdownToEditorStateJSON(markdown)) as {
140
- root: SerializedNode;
141
- };
142
- const elapsed = performance.now() - started;
143
- const paragraph = root.children?.[0];
144
- // One paragraph, one text node: nothing split per reference.
145
- expect(paragraph?.children, String(count)).toHaveLength(1);
146
- const expected = '© © &#12345678; &amp; &nope; '.repeat(count / 5);
147
- // Compared as a flag, so a mismatch does not print the whole text.
148
- expect(paragraph?.children?.[0].text === expected, String(count)).toBe(
149
- true,
150
- );
151
- expect(elapsed, String(count)).toBeLessThan(5000);
152
- }
153
- });
154
-
155
- it('find code spans the CommonMark way: only a run of the same length closes one', () => {
156
- // A double run inside a single-backtick span neither closes it nor opens
157
- // one, so the whole is code and the reference stays as written.
158
- expect(richTextText('`one`` &amp; two`\n')).toBe('one`` &amp; two');
159
- // Fenced code and CRLF: the reference after the fence decodes.
160
- expect(richTextText('```\r\n&copy;\r\n```\r\n\r\n&copy;\r\n')).toBe(
161
- '&copy;©',
162
- );
163
- });
164
-
165
- it('stay literal in fenced code, as core Markdown keeps them', () => {
166
- const markdown = '```\n&copy; &#169;\n```\n';
167
- expect(richTextText(markdown)).toBe('&copy; &#169;');
168
- expect(parseMarkdown(markdown)[0]).toMatchObject({
169
- content: '&copy; &#169;',
170
- });
171
- });
172
-
173
- it('keep an untouched paragraph exact and write decoded text back so it reads the same (spec:AST-062)', () => {
174
- const markdown = 'A &copy; B &amp;copy; C &#169;\n';
175
- expect(editorStateJSONToMarkdown(markdownToEditorStateJSON(markdown))).toBe(
176
- markdown,
177
- );
178
- const editor = createHeadlessEditor({
179
- namespace: 'astryx-character-reference-test',
180
- nodes: [...DEFAULT_NODES],
181
- onError(error: Error) {
182
- throw error;
183
- },
184
- });
185
- editor.setEditorState(
186
- editor.parseEditorState(markdownToEditorStateJSON(markdown)),
187
- );
188
- editor.update(
189
- () => {
190
- const text = $getRoot().getAllTextNodes()[0];
191
- text?.setTextContent(`${text.getTextContent()}!`);
192
- },
193
- {discrete: true},
194
- );
195
- const exported = editorStateJSONToMarkdown(
196
- JSON.stringify(editor.getEditorState().toJSON()),
197
- );
198
- // The decoded `&copy;` text is escaped, so it does not decode again.
199
- expect(exported).toBe('A © B \\&copy; C ©!\n');
200
- expect(richTextText(exported)).toBe('A © B &copy; C ©!');
201
- });
202
- });