An open source design system that's fully customizable and agent ready
TypeScript
fix(richtext): read angle-bracket destination edges as core does, and write brackets with spaces (#7073)
Cindy Zhang0d75803c047a
diff --git a/packages/richtext/src/markdownAngleDestinations.test.ts b/packages/richtext/src/markdownAngleDestinations.test.tsindex af0b88e34e..3a7db24f5c 100644--- a/packages/richtext/src/markdownAngleDestinations.test.ts+++ b/packages/richtext/src/markdownAngleDestinations.test.ts@@ -132,3 +132,43 @@ describe('angle-bracket link destinations (CommonMark 0.31 §6.3)', () => {expect(richLinks('[x](<data: text/html,hi>)')).toEqual([]);});});++describe('angle-bracket destination edges', () => {+ it('reads `<b>c>` as a destination as it stands, as core does', () => {+ const markdown = '[a](<b>c>)';+ expect(richLinks(markdown).map(({url}) => url)).toEqual(coreUrls(markdown));+ });++ it('reads an escaped bracket inside the angle brackets as part of the address', () => {+ expect(richLinks('[a](<a \\<b>)')).toEqual([{url: 'a <b', title: null}]);+ });++ it('writes an address holding a space and brackets so it reads back', () => {+ const editor = newEditor();+ editor.update(+ () => {+ $getRoot()+ .clear()+ .append(+ $createParagraphNode().append(+ $createLinkNode('a <b c>').append($createTextNode('x')),+ ),+ );+ },+ {discrete: true},+ );+ const exported = editor+ .getEditorState()+ .read(() => $exportMarkdownKeepingSource([...DEFAULT_TRANSFORMERS]));+ expect(exported).toBe('[x](<a \\<b c\\>>)');+ expect(richLinks(exported)).toEqual([{url: 'a <b c>', title: null}]);+ });++ it.each([+ '[x](<javascript:x \\<y>)',+ '[x](<data: text/html \\<b\\>>)',+ '[x](<JavaScript:a b>)',+ ])('still refuses the unsafe address in %j', markdown => {+ expect(richLinks(markdown)).toEqual([]);+ });+});diff --git a/packages/richtext/src/markdownCharacterReferences.ts b/packages/richtext/src/markdownCharacterReferences.tsindex 0261489d74..2fc59d37fa 100644--- a/packages/richtext/src/markdownCharacterReferences.ts+++ b/packages/richtext/src/markdownCharacterReferences.ts@@ -339,10 +339,14 @@ function coreRefusesDestination(destination: string): boolean {* A URL that cannot be written back is refused.*/export function coreRefusesUrl(url: string): boolean {- if (/[<>\n\r]/.test(url) && /\s/.test(url)) {+ if (/[\n\r]/.test(url)) {return true;}- const escaped = url.replace(/[\\()<>&[\]"']/g, character => `\\${character}`);+ // Angle brackets never decide a scheme, so the probe percent-encodes them:+ // the URL then fits one angle-bracket destination even with spaces in it.+ const escaped = url+ .replace(/[<>]/g, character => (character === '<' ? '%3C' : '%3E'))+ .replace(/[\\()&[\]"']/g, character => `\\${character}`);return coreRefusesDestination(/\s/.test(url) ? `<${escaped}>` : escaped);}diff --git a/packages/richtext/src/markdownLink.ts b/packages/richtext/src/markdownLink.tsindex 915ceab236..62c60b3817 100644--- a/packages/richtext/src/markdownLink.ts+++ b/packages/richtext/src/markdownLink.ts@@ -57,7 +57,9 @@ export const LINK_KEEPING_DESTINATIONS: TextMatchTransformer = {importRegExp: LINK_WITH_ANGLE_DESTINATION,replace: (textNode, match) => {const destination = match[2];- if (destination?.startsWith('<') !== true) {+ // Only a whole angle-bracket destination drops its brackets; `<b>c>`+ // is a destination as it stands, as core reads it.+ if (destination == null || !/^<[^<>\n]*>$/.test(destination)) {return LINK.replace?.(textNode, match);}// The link's address is what the angle brackets hold.