Skip to content

Commit 7de880e

Browse files
committed
fix(v1-components): isURL predicate now returns true for relative URLs without protocol
1 parent 9caeeb5 commit 7de880e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
export const isURL = (href: string): boolean => {
1+
export const isURL = (href: string, loose = true): boolean => {
22
try {
3-
const normalizedHref = href.includes('://') ? href : `http://${href}`
3+
const url = new URL(href)
44

5-
const url = new URL(normalizedHref)
5+
return /^(https?:\/\/)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(\S*)$/.test(url.href)
6+
} catch {
7+
if (loose) {
8+
if (!href.startsWith('/')) return false
69

7-
const urlPattern = /^(https?:\/\/)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(\S*)$/
10+
return [
11+
href.includes('://') ? href : `https://${href}`,
12+
`https://example.com${href}`,
13+
].some(href => isURL(href, false))
14+
}
815

9-
return urlPattern.test(url.href)
10-
} catch {
1116
return false
1217
}
1318
}

0 commit comments

Comments
 (0)