Skip to content

Commit 47836ea

Browse files
authored
fix: protocols in domain fields (#856)
Some accounts such as the issuers of NFTs put fully formatted urls in the domain field. Since _"https://"_ is prepended to the value of that field, an invalid URI is created. The change adds _"https://"_ only if a protocol is not detected. Closes #714
1 parent 31d9529 commit 47836ea

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/containers/shared/components/DomainLink.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,27 @@ export interface Props {
77
domain: string
88
}
99

10+
// Matches a protocol (e.g. 'http://' or 'https://') at the start of a string.
11+
const PROTOCOL_REGEX = /^([a-z][a-z0-9+\-.]*):\/\//
12+
1013
const DomainLink = (props: Props) => {
1114
const { className, decode = false, domain } = props
15+
16+
// If decode is true, decode the domain
17+
const decodedDomain = decode ? decodeHex(domain) : domain
18+
19+
// Use the test method to check for the protocol
20+
const domainHasProtocol = PROTOCOL_REGEX.test(decodedDomain)
21+
22+
// If decoded domain does not have a protocol, add one ; otherwise, don't
23+
const href = domainHasProtocol ? decodedDomain : `https://${decodedDomain}`
24+
1225
return (
1326
<a
14-
className={classnames(`domain`, className)}
27+
className={classnames('domain', className)}
1528
rel="noopener noreferrer"
1629
target="_blank"
17-
href={`https://${decode ? decodeHex(domain) : domain}`}
30+
href={href}
1831
>
1932
{decode ? decodeHex(domain) : domain}
2033
</a>

src/containers/shared/components/test/DomainLink.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,13 @@ describe('DomainLink', () => {
3838
expect(wrapper.find('a').props().href).toEqual('https://sologenic.com')
3939
wrapper.unmount()
4040
})
41+
it('handles domain link with domain provided in HEX-encoded format', () => {
42+
const url = 'https://example.com'
43+
const urlInHex = '68747470733A2F2F6578616D706C652E636F6D'
44+
const wrapper = mount(<DomainLink decode domain={urlInHex} />)
45+
expect(wrapper.find('a').props().className).toEqual('domain')
46+
expect(wrapper.find('a').text()).toEqual(url)
47+
expect(wrapper.find('a').props().href).toEqual(url)
48+
wrapper.unmount()
49+
})
4150
})

0 commit comments

Comments
 (0)