foohttps://example.combar
'; + await setTestLinksEditorHtml(page, html); + + await expect.poll(async () => getTestLinksSerializedHtml(page)).toBe(html); + }); + + test('removes a loaded autolink when it stops being a whole word', async ({ + page, + }) => { + await gotoTestLinks(page); + await page.locator(sel.linkRegexMode).selectOption('default'); + await setTestLinksEditorHtml( + page, + '' + ); + + const editor = page.locator(sel.editorInner); + await editor.click(); + await expect(editor.locator('.ProseMirror')).toBeFocused(); + await page.keyboard.press('Home'); + await page.keyboard.type('('); + + await expect + .poll(async () => getTestLinksSerializedHtml(page)) + .toBe('(https://example.com
'); + }); }); test.describe('test-links copy-paste', () => { diff --git a/src/web/__tests__/manualLinkReload.test.tsx b/src/web/__tests__/manualLinkReload.test.tsx new file mode 100644 index 000000000..8535942ed --- /dev/null +++ b/src/web/__tests__/manualLinkReload.test.tsx @@ -0,0 +1,74 @@ +import { act, createRef } from 'react'; +import { createRoot, type Root } from 'react-dom/client'; +import { EnrichedTextInput } from '../EnrichedTextInput'; +import type { EnrichedTextInputInstance } from '../../types'; + +jest.mock('../EnrichedText.css', () => ({})); + +const HREF = 'https://example.com'; +const link = (text = HREF) => `${text}`; + +describe('EnrichedTextInput links whose text equals the url', () => { + let container: HTMLDivElement; + let root: Root; + + beforeEach(() => { + (globalThis as any).IS_REACT_ACT_ENVIRONMENT = true; + container = document.createElement('div'); + document.body.appendChild(container); + root = createRoot(container); + }); + + afterEach(() => { + act(() => root.unmount()); + container.remove(); + }); + + async function mount(defaultValue?: string) { + const ref = createReffoo${link()}bar
`, + `(${link()})
`, + `foo${link()}
`, + `${link()}bar
`, + `${link(`${HREF}`)}bar
`, + `a ${link()} b
`, + `${link()}
`, + ]; + + test.each(KEPT)('keeps the link from defaultValue: %s', async (html) => { + const input = await mount(html); + expect(await input.getHTML()).toBe(html); + }); + + test.each(KEPT)('keeps the link from setValue: %s', async (html) => { + const input = await mount(); + await act(async () => input.setValue(html)); + expect(await input.getHTML()).toBe(html); + }); + + test('keeps a link nested in an inline tag that touches other text', async () => { + const input = await mount(`${link()}bar
`); + expect(await input.getHTML()).toBe( + `${link(`${HREF}`)}bar
` + ); + }); + + test('keeps a link set with setLink inside a word after reload', async () => { + const first = await mount('foobar
'); + await act(async () => first.setLink(3, 3, HREF, HREF)); + const saved = await first.getHTML(); + expect(saved).toBe(`foo${link()}bar
`); + + const second = await mount(saved); + expect(await second.getHTML()).toBe(saved); + }); +}); diff --git a/src/web/formats/EnrichedLink.ts b/src/web/formats/EnrichedLink.ts index e4b9920d9..be64d842e 100644 --- a/src/web/formats/EnrichedLink.ts +++ b/src/web/formats/EnrichedLink.ts @@ -14,6 +14,38 @@ function isFullLinkMatch(text: string, linkRegex: RegExp | undefined): boolean { return ranges.some((r) => r.start === 0 && r.endExclusive === text.length); } +// Elements whose text AutolinkPlugin treats as part of the surrounding word. +// Anything else (blocks,