-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* ♻️ extract functions that manage URLs * ✨ add simple url polyfill * 🔥 url-polyfill dependency * ♿ tweaks for IE * 👌 use URL when available
- Loading branch information
Showing
13 changed files
with
189 additions
and
76 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
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
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
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
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,68 @@ | ||
import { getLinkElementOrigin, getLocationOrigin } from './utils' | ||
|
||
export function normalizeUrl(url: string) { | ||
return buildUrl(url, getLocationOrigin()).href | ||
} | ||
|
||
export function isValidUrl(url: string) { | ||
try { | ||
return !!buildUrl(url) | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
export function haveSameOrigin(url1: string, url2: string) { | ||
return getOrigin(url1) === getOrigin(url2) | ||
} | ||
|
||
export function getOrigin(url: string) { | ||
return getLinkElementOrigin(buildUrl(url)) | ||
} | ||
|
||
export function getPathName(url: string) { | ||
const pathname = buildUrl(url).pathname | ||
return pathname[0] === '/' ? pathname : `/${pathname}` | ||
} | ||
|
||
export function getSearch(url: string) { | ||
return buildUrl(url).search | ||
} | ||
|
||
export function getHash(url: string) { | ||
return buildUrl(url).hash | ||
} | ||
|
||
function buildUrl(url: string, base?: string) { | ||
if (checkURLSupported()) { | ||
return new URL(url, base) | ||
} | ||
if (base === undefined && !/:/.test(url)) { | ||
throw new Error(`Invalid URL: '${url}'`) | ||
} | ||
let doc = document | ||
const anchorElement = doc.createElement('a') | ||
if (base !== undefined) { | ||
doc = document.implementation.createHTMLDocument('') | ||
const baseElement = doc.createElement('base') | ||
baseElement.href = base | ||
doc.head.appendChild(baseElement) | ||
doc.body.appendChild(anchorElement) | ||
} | ||
anchorElement.href = url | ||
return anchorElement | ||
} | ||
|
||
let isURLSupported: boolean | undefined | ||
function checkURLSupported() { | ||
if (isURLSupported !== undefined) { | ||
return isURLSupported | ||
} | ||
try { | ||
const url = new URL('http://test') | ||
return url.href === 'http://test' | ||
} catch { | ||
isURLSupported = false | ||
} | ||
return isURLSupported | ||
} |
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
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
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,70 @@ | ||
import { | ||
getHash, | ||
getLocationOrigin, | ||
getOrigin, | ||
getPathName, | ||
getSearch, | ||
isFirefox, | ||
isValidUrl, | ||
normalizeUrl, | ||
} from '../src' | ||
|
||
describe('normalize url', () => { | ||
it('should add origin to relative path', () => { | ||
expect(normalizeUrl('/my/path')).toEqual(`${getLocationOrigin()}/my/path`) | ||
}) | ||
|
||
it('should add protocol to relative url', () => { | ||
expect(normalizeUrl('//foo.com:9876/my/path')).toEqual('http://foo.com:9876/my/path') | ||
}) | ||
|
||
it('should keep full url unchanged', () => { | ||
expect(normalizeUrl('https://foo.com/my/path')).toEqual('https://foo.com/my/path') | ||
}) | ||
|
||
it('should keep non http url unchanged', () => { | ||
if (isFirefox()) { | ||
pending('https://bugzilla.mozilla.org/show_bug.cgi?id=1578787') | ||
} | ||
expect(normalizeUrl('file://foo.com/my/path')).toEqual('file://foo.com/my/path') | ||
}) | ||
}) | ||
|
||
describe('isValidUrl', () => { | ||
it('should ensure url is valid', () => { | ||
expect(isValidUrl('http://www.datadoghq.com')).toBe(true) | ||
expect(isValidUrl('http://www.datadoghq.com/foo/bar?a=b#hello')).toBe(true) | ||
expect(isValidUrl('file://www.datadoghq.com')).toBe(true) | ||
expect(isValidUrl('/plop')).toBe(false) | ||
expect(isValidUrl('')).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('getOrigin', () => { | ||
it('should retrieve url origin', () => { | ||
expect(getOrigin('http://www.datadoghq.com')).toBe('http://www.datadoghq.com') | ||
expect(getOrigin('http://www.datadoghq.com/foo/bar?a=b#hello')).toBe('http://www.datadoghq.com') | ||
expect(getOrigin('http://localhost:8080')).toBe('http://localhost:8080') | ||
}) | ||
}) | ||
|
||
describe('getPathName', () => { | ||
it('should retrieve url path name', () => { | ||
expect(getPathName('http://www.datadoghq.com')).toBe('/') | ||
expect(getPathName('http://www.datadoghq.com/foo/bar?a=b#hello')).toBe('/foo/bar') | ||
}) | ||
}) | ||
|
||
describe('getSearch', () => { | ||
it('should retrieve url search', () => { | ||
expect(getSearch('http://www.datadoghq.com')).toBe('') | ||
expect(getSearch('http://www.datadoghq.com/foo/bar?a=b#hello')).toBe('?a=b') | ||
}) | ||
}) | ||
|
||
describe('getHash', () => { | ||
it('should retrieve url hash', () => { | ||
expect(getHash('http://www.datadoghq.com')).toBe('') | ||
expect(getHash('http://www.datadoghq.com/foo/bar?a=b#hello')).toBe('#hello') | ||
}) | ||
}) |
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
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
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
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 |
---|---|---|
|
@@ -2,26 +2,25 @@ | |
# yarn lockfile v1 | ||
|
||
|
||
"@datadog/browser-core@1.4.1", "@datadog/browser-core@file:../../packages/core": | ||
version "1.4.1" | ||
"@datadog/browser-core@1.7.0", "@datadog/browser-core@file:../../packages/core": | ||
version "1.7.0" | ||
dependencies: | ||
lodash.assign "4.2.0" | ||
lodash.merge "4.6.2" | ||
tslib "1.10.0" | ||
url-polyfill "1.1.7" | ||
|
||
"@datadog/browser-logs@file:../../packages/logs": | ||
version "1.4.1" | ||
version "1.7.0" | ||
dependencies: | ||
"@datadog/browser-core" "1.4.1" | ||
"@datadog/browser-core" "1.7.0" | ||
lodash.assign "4.2.0" | ||
lodash.merge "4.6.2" | ||
tslib "1.10.0" | ||
|
||
"@datadog/browser-rum@file:../../packages/rum": | ||
version "1.4.1" | ||
version "1.7.0" | ||
dependencies: | ||
"@datadog/browser-core" "1.4.1" | ||
"@datadog/browser-core" "1.7.0" | ||
lodash.assign "4.2.0" | ||
lodash.merge "4.6.2" | ||
tslib "1.10.0" | ||
|
@@ -2236,11 +2235,6 @@ urix@^0.1.0: | |
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72" | ||
integrity sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI= | ||
|
||
[email protected]: | ||
version "1.1.7" | ||
resolved "https://registry.yarnpkg.com/url-polyfill/-/url-polyfill-1.1.7.tgz#402ee84360eb549bbeb585f4c7971e79a31de9e3" | ||
integrity sha512-ZrAxYWCREjmMtL8gSbSiKKLZZticgihCvVBtrFbUVpyoETt8GQJeG2okMWA8XryDAaHMjJfhnc+rnhXRbI4DXA== | ||
|
||
url@^0.11.0: | ||
version "0.11.0" | ||
resolved "https://registry.yarnpkg.com/url/-/url-0.11.0.tgz#3838e97cfc60521eb73c525a8e55bfdd9e2e28f1" | ||
|
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 |
---|---|---|
|
@@ -9713,11 +9713,6 @@ urix@^0.1.0: | |
resolved "https://registry.yarnpkg.com/urix/-/urix-0.1.0.tgz#da937f7a62e21fec1fd18d49b35c2935067a6c72" | ||
integrity sha1-2pN/emLiH+wf0Y1Js1wpNQZ6bHI= | ||
|
||
[email protected]: | ||
version "1.1.7" | ||
resolved "https://registry.yarnpkg.com/url-polyfill/-/url-polyfill-1.1.7.tgz#402ee84360eb549bbeb585f4c7971e79a31de9e3" | ||
integrity sha512-ZrAxYWCREjmMtL8gSbSiKKLZZticgihCvVBtrFbUVpyoETt8GQJeG2okMWA8XryDAaHMjJfhnc+rnhXRbI4DXA== | ||
|
||
url@^0.11.0: | ||
version "0.11.0" | ||
resolved "https://registry.yarnpkg.com/url/-/url-0.11.0.tgz#3838e97cfc60521eb73c525a8e55bfdd9e2e28f1" | ||
|