-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(richtext-lexical): improved regex matchers for absolute and relat…
…ive URLs to make autolinking more reliable (#10725) Fixes an issue where pasting text over a selection will automatically add it as a link instead of replacing the text. This is caused by poor regex matching in the case of relative URLs. Added tests and strengthened both absolute and relative URL matchers
- Loading branch information
Showing
2 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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,88 @@ | ||
import { jest } from '@jest/globals' | ||
import { absoluteRegExp, relativeOrAnchorRegExp } from './url.js' | ||
|
||
describe('Lexical URL Regex Matchers', () => { | ||
describe('relative URLs', () => { | ||
it('validation for links it should match', async () => { | ||
const shouldMatch = [ | ||
'/path/to/resource', | ||
'/file-name.html', | ||
'/', | ||
'/dir/', | ||
'/path.with.dots/', | ||
'#anchor', | ||
'#section-title', | ||
'/path#fragment', | ||
] | ||
|
||
shouldMatch.forEach((testCase) => { | ||
expect(relativeOrAnchorRegExp.test(testCase)).toBe(true) | ||
}) | ||
}) | ||
|
||
it('validation for links it should not match', async () => { | ||
const shouldNotMatch = [ | ||
'match', | ||
'http://example.com', | ||
'relative/path', | ||
'file.html', | ||
'some#fragment', | ||
'#', | ||
'/#', | ||
'/path/with spaces', | ||
'', | ||
'ftp://example.com', | ||
] | ||
|
||
shouldNotMatch.forEach((testCase) => { | ||
expect(relativeOrAnchorRegExp.test(testCase)).not.toBe(true) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('absolute URLs', () => { | ||
it('validation for links it should match', async () => { | ||
const shouldMatch = [ | ||
'http://example.com', | ||
'https://example.com', | ||
'ftp://files.example.com', | ||
'http://example.com/resource', | ||
'https://example.com/resource?key=value', | ||
'http://example.com/resource#anchor', | ||
'http://www.example.com', | ||
'https://sub.example.com/path/file', | ||
'mailto:[email protected]', | ||
'tel:+1234567890', | ||
'http://user:[email protected]', | ||
'www.example.com', | ||
'www.example.com/resource', | ||
'www.example.com/resource?query=1', | ||
'www.example.com#fragment', | ||
] | ||
|
||
shouldMatch.forEach((testCase) => { | ||
expect(absoluteRegExp.test(testCase)).toBe(true) | ||
}) | ||
}) | ||
|
||
it('validation for links it should not match', async () => { | ||
const shouldNotMatch = [ | ||
'/relative/path', | ||
'#anchor', | ||
'example.com', | ||
'://missing.scheme', | ||
'http://', | ||
'http:/example.com', | ||
'ftp://example .com', | ||
'http://example', | ||
'not-a-url', | ||
'http//example.com', | ||
'https://example.com/ spaces', | ||
] | ||
|
||
shouldNotMatch.forEach((testCase) => { | ||
expect(absoluteRegExp.test(testCase)).not.toBe(true) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains 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