An open source design system that's fully customizable and agent ready
TypeScript
test(richtext): keep a table cell's link through an edit of the table (#7052)
Cindy Zhangd6fa1d66038a
diff --git a/packages/richtext/src/markdownTableLinks.test.ts b/packages/richtext/src/markdownTableLinks.test.tsnew file mode 100644index 0000000000..645a7533a7--- /dev/null+++ b/packages/richtext/src/markdownTableLinks.test.ts@@ -0,0 +1,69 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, expect, it} from 'vitest';+import {createHeadlessEditor} from '@lexical/headless';+import {$getRoot, $isTextNode} from 'lexical';+import {DEFAULT_NODES} from './editorNodes';+import {DEFAULT_TRANSFORMERS} from './markdownTable';+import {+ $exportMarkdownKeepingSource,+ importMarkdownKeepingSource,+} from './markdownSource';+import {markdownToEditorStateJSON} from './markdownSerializers';++type Json = Record<string, unknown>;++/** Each link's destination and text, in document order. */+function linksOf(markdown: string): Array<[string, string]> {+ const found: Array<[string, string]> = [];+ const visit = (node: Json) => {+ if (node.type === 'link') {+ found.push([+ node.url as string,+ ((node.children as Array<Json>) ?? [])+ .map(child => child.text as string)+ .join(''),+ ]);+ }+ ((node.children as Array<Json>) ?? []).forEach(visit);+ };+ visit((JSON.parse(markdownToEditorStateJSON(markdown)) as {root: Json}).root);+ return found;+}++describe('links in table cells (spec:AST-062 FR3)', () => {+ it('keep a destination with unbalanced parentheses through an edit of the table', () => {+ const markdown = [+ '| Name | Link |',+ '| --- | --- |',+ '| Ada | [docs](https://e.com/a\\(b) |',+ '',+ ].join('\n');+ expect(linksOf(markdown)).toEqual([['https://e.com/a(b', 'docs']]);+ const editor = createHeadlessEditor({+ nodes: [...DEFAULT_NODES],+ onError(error) {+ throw error;+ },+ });+ importMarkdownKeepingSource(editor, markdown, [...DEFAULT_TRANSFORMERS]);+ // Editing another cell makes the table export from the editor's nodes.+ editor.update(+ () => {+ const name = $getRoot()+ .getAllTextNodes()+ .find(node => node.getTextContent() === 'Ada');+ if (!$isTextNode(name)) {+ throw new Error('No Ada cell');+ }+ name.setTextContent('Grace');+ },+ {discrete: true},+ );+ const exported = editor+ .getEditorState()+ .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));+ expect(exported).toContain('Grace');+ expect(linksOf(exported)).toEqual([['https://e.com/a(b', 'docs']]);+ });+});