Repository navigation
fix(web): keep links with url text that touch other text #899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Markgugg
wants to merge
1
commit into
software-mansion:main
Choose a base branch
from
Markgugg:fix/web-autolink-manual-link
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) => `<a href="${HREF}">${text}</a>`; | ||
|
|
||
| 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<EnrichedTextInputInstance>(); | ||
| act(() => { | ||
| root.render(<EnrichedTextInput ref={ref} defaultValue={defaultValue} />); | ||
| }); | ||
| // TipTap emits `create` on a timeout once the view is mounted. | ||
| await act(() => new Promise<void>((resolve) => setTimeout(resolve, 20))); | ||
| return ref.current!; | ||
| } | ||
|
|
||
| const KEPT = [ | ||
| `<html><p>foo${link()}bar</p></html>`, | ||
| `<html><p>(${link()})</p></html>`, | ||
| `<html><p><b>foo</b>${link()}</p></html>`, | ||
| `<html><p>${link()}<i>bar</i></p></html>`, | ||
| `<html><p>${link(`<b>${HREF}</b>`)}bar</p></html>`, | ||
| `<html><p>a ${link()} b</p></html>`, | ||
| `<html><p>${link()}</p></html>`, | ||
| ]; | ||
|
|
||
| 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(`<html><p><b>${link()}</b>bar</p></html>`); | ||
| expect(await input.getHTML()).toBe( | ||
| `<html><p>${link(`<b>${HREF}</b>`)}bar</p></html>` | ||
| ); | ||
| }); | ||
|
|
||
| test('keeps a link set with setLink inside a word after reload', async () => { | ||
| const first = await mount('<html><p>foobar</p></html>'); | ||
| await act(async () => first.setLink(3, 3, HREF, HREF)); | ||
| const saved = await first.getHTML(); | ||
| expect(saved).toBe(`<html><p>foo${link()}bar</p></html>`); | ||
|
|
||
| const second = await mount(saved); | ||
| expect(await second.getHTML()).toBe(saved); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stop at boundaries inside inline wrappers.
For
<p><a href="https://example.com">https://example.com</a><b><code>x</code></b></p>,textContentreturnsx. This incorrectly classifies the link as manual. The normalizer preserves this nesting, butAutolinkPlugintreats code as a word boundary. (raw.githubusercontent.com)If the user then types
(at the paragraph start, the link remains linked instead of losing its automatic mark. Descend through inline wrappers in the requested direction and stop at nested boundaries. Preserve the non-whitespace check for ordinary marked text. (raw.githubusercontent.com)