diff --git a/.playwright/tests/links.spec.ts b/.playwright/tests/links.spec.ts index 12329a338..246734686 100644 --- a/.playwright/tests/links.spec.ts +++ b/.playwright/tests/links.spec.ts @@ -523,6 +523,40 @@ test.describe('test-links autolink', () => { .poll(async () => getTestLinksSerializedHtml(page)) .not.toContain(' { + await gotoTestLinks(page); + await page.locator(sel.linkRegexMode).selectOption('default'); + + const html = + '

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, + '

https://example.com

' + ); + + 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 = createRef(); + act(() => { + root.render(); + }); + // TipTap emits `create` on a timeout once the view is mounted. + await act(() => new Promise((resolve) => setTimeout(resolve, 20))); + return ref.current!; + } + + const KEPT = [ + `

foo${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,
, , code, mentions) ends the word there. +const WORD_INLINE_TAGS = new Set(['A', 'B', 'I', 'U', 'S', 'SPAN']); + +function adjacentChar(el: Element, side: 'before' | 'after'): string | null { + let node: Node = el; + while (true) { + let sibling = side === 'before' ? node.previousSibling : node.nextSibling; + while (sibling) { + const isText = sibling.nodeType === Node.TEXT_NODE; + if (!isText && !WORD_INLINE_TAGS.has(sibling.nodeName)) return null; + const text = sibling.textContent ?? ''; + if (text.length > 0) { + return side === 'before' ? text[text.length - 1]! : text[0]!; + } + sibling = + side === 'before' ? sibling.previousSibling : sibling.nextSibling; + } + const parent = node.parentElement; + if (!parent || !WORD_INLINE_TAGS.has(parent.nodeName)) return null; + node = parent; + } +} + +// AutolinkPlugin removes auto links that are not a whole word, so a link +// touching other text has to be a manual one, or it is lost on load. +function isWholeWord(el: Element): boolean { + return [adjacentChar(el, 'before'), adjacentChar(el, 'after')].every( + (char) => char === null || /\s/.test(char) + ); +} + export const EnrichedLink = Link.extend< LinkOptions & { getLinkRegex: () => RegExp | null | undefined } >({ @@ -46,7 +78,7 @@ export const EnrichedLink = Link.extend< const linkRegex = this.options.getLinkRegex(); if (linkRegex === null) return false; - return isFullLinkMatch(href, linkRegex); + return isFullLinkMatch(href, linkRegex) && isWholeWord(el); }, renderHTML: () => { return {};