Skip to content

Commit

Permalink
Refactor isRemoteImageURL
Browse files Browse the repository at this point in the history
  • Loading branch information
muliswilliam committed May 31, 2022
1 parent bc389b2 commit 70ee68d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { create } from 'ethereum-blockies'
import { BraveWallet, SupportedTestNetworks } from '../../../constants/types'

// Utils
import { stripERC20TokenImageURL, isRemoteImageURL, isValidIconExtension } from '../../../utils/string-utils'
import { stripERC20TokenImageURL, isFromDifferentOrigin, isValidIconExtension } from '../../../utils/string-utils'

// Styled components
import { IconWrapper, Placeholder, NetworkIcon } from './style'
Expand All @@ -31,7 +31,7 @@ function CreateNetworkIcon (props: Props) {
)

const networkImageURL = stripERC20TokenImageURL(network?.iconUrls[0])
const isRemoteURL = isRemoteImageURL(networkImageURL)
const isRemoteURL = isFromDifferentOrigin(networkImageURL)
const isDataURL = network.iconUrls[0]?.startsWith('chrome://erc-token-images/')
const isStorybook = network.iconUrls[0]?.startsWith('static/media/components/brave_wallet_ui/')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { background } from 'ethereum-blockies'
import { BraveWallet } from '../../../constants/types'

// Utils
import { stripERC20TokenImageURL, isRemoteImageURL, isValidIconExtension, httpifyIpfsUrl } from '../../../utils/string-utils'
import { stripERC20TokenImageURL, isFromDifferentOrigin, isValidIconExtension, httpifyIpfsUrl } from '../../../utils/string-utils'

// Styled components
import { IconWrapper, PlaceholderText } from './style'
Expand Down Expand Up @@ -49,7 +49,7 @@ function withPlaceholderIcon (WrappedComponent: React.ComponentType<any>, config
)

const tokenImageURL = stripERC20TokenImageURL(asset.logo)
const isRemoteURL = isRemoteImageURL(tokenImageURL)
const isRemoteURL = isFromDifferentOrigin(tokenImageURL)
const isDataURL = asset.logo.startsWith('chrome://erc-token-images/')
const isStorybook = asset.logo.startsWith('static/media/components/brave_wallet_ui/')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
import { Store } from '../../common/async/types'
import { getLocale } from '../../../common/locale'
import getWalletPanelApiProxy from '../wallet_panel_api_proxy'
import { isRemoteImageURL } from '../../utils/string-utils'
import { isFromDifferentOrigin } from '../../utils/string-utils'
import { HardwareVendor } from 'components/brave_wallet_ui/common/api/hardware_keyrings'

const handler = new AsyncActionHandler()
Expand Down Expand Up @@ -123,7 +123,7 @@ async function getPendingAddSuggestTokenRequest () {
(await braveWalletService.getPendingAddSuggestTokenRequests()).requests
if (requests && requests.length) {
const logo = requests[0].token.logo
if (logo !== '' && !isRemoteImageURL(logo)) {
if (logo !== '' && !isFromDifferentOrigin(logo)) {
requests[0].token.logo = `chrome://erc-token-images/${logo}`
}
return requests[0]
Expand Down
12 changes: 6 additions & 6 deletions components/brave_wallet_ui/utils/string-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { isRemoteImageURL, isValidIconExtension } from './string-utils'
import { isFromDifferentOrigin, isValidIconExtension } from './string-utils'

describe('Checking URL is remote image or not', () => {
test('HTTP URL should return true', () => {
expect(isRemoteImageURL('http://test.com/test.png')).toEqual(true)
expect(isFromDifferentOrigin('http://test.com/test.png')).toEqual(true)
})

test('HTTPS URL should return true', () => {
expect(isRemoteImageURL('https://test.com/test.png')).toEqual(true)
expect(isFromDifferentOrigin('https://test.com/test.png')).toEqual(true)
})

test('Data URL image should return true', () => {
expect(isRemoteImageURL('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mP4z8AAAAMBAQD3A0FDAAAAAElFTkSuQmCC')).toEqual(true)
expect(isFromDifferentOrigin('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mP4z8AAAAMBAQD3A0FDAAAAAElFTkSuQmCC')).toEqual(true)
})

test('local path should return false', () => {
expect(isRemoteImageURL('bat.png')).toEqual(false)
expect(isFromDifferentOrigin('bat.png')).toEqual(false)
})

test('undefined input should return undefined', () => {
expect(isRemoteImageURL(undefined)).toEqual(undefined)
expect(isFromDifferentOrigin(undefined)).toEqual(undefined)
})
})

Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet_ui/utils/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const toProperCase = (value: string) =>
value.replace(/\w\S*/g,
(txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase())

export const isRemoteImageURL = (url?: string) =>
export const isFromDifferentOrigin = (url?: string) =>
url?.startsWith('http://') || url?.startsWith('https://') || url?.startsWith('data:image/') || url?.startsWith('ipfs://')

export const isValidIconExtension = (url?: string) =>
Expand Down

0 comments on commit 70ee68d

Please sign in to comment.