Skip to content

Commit

Permalink
fix(shared): check link with protocol correctly (#1542)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope authored Apr 16, 2024
1 parent 1825636 commit 8b1ab67
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/shared/src/utils/isLinkWithProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
* Determine a link has protocol or not
*/
export const isLinkWithProtocol = (link: string): boolean =>
/^[a-z][a-z0-9+.-]*:/.test(link)
/^[a-z][a-z0-9+.-]*:/.test(link) || link.startsWith('//')
11 changes: 10 additions & 1 deletion packages/shared/tests/isLinkWithProtocol.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ import { expect, it } from 'vitest'
import { isLinkWithProtocol } from '../src/index.js'

const testCases: [string, ReturnType<typeof isLinkWithProtocol>][] = [
// with protocol
['ftp://foobar.com', true],
['ms-windows-store://home', true],
['mailto:foobar', true],
['tel:foobar', true],
['https://foobar.com', true],
['http://foobar.com', true],
['//foobar.com', true],

// hostname
['foobar.com', false],

// pathname
['/foo/bar', false],

// relative path
['../foo/bar', false],
['//foobar.com', false],
['./foo/bar', false],
['foo/bar', false],
]

testCases.forEach(([source, expected]) => {
Expand Down

0 comments on commit 8b1ab67

Please sign in to comment.