-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from curbengh/whatwg-url
refactor: whatwg url api
- Loading branch information
Showing
8 changed files
with
65 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,33 @@ | ||
'use strict'; | ||
|
||
const { parse, format } = require('url'); | ||
const regexNonUrl = /^(data|javascript|mailto|vbscript)/i; | ||
const { URL } = require('url'); | ||
|
||
function encodeURL(str) { | ||
const parsed = parse(str); | ||
if (parsed.slashes) { | ||
const obj = Object.assign({}, { | ||
auth: parsed.auth, | ||
protocol: parsed.protocol, | ||
host: parsed.host, | ||
pathname: encodeURI(safeDecodeURI(parsed.pathname)) | ||
}); | ||
|
||
if (parsed.hash) { | ||
Object.assign(obj, { hash: encodeURI(safeDecodeURI(parsed.hash)) }); | ||
} | ||
|
||
if (parsed.search) { | ||
Object.assign(obj, { search: encodeURI(safeDecodeURI(parsed.search)) }); | ||
} | ||
|
||
return format(obj); | ||
const urlObj = (str) => { | ||
try { | ||
return new URL(str); | ||
} catch (err) { | ||
return str; | ||
} | ||
}; | ||
|
||
if (str.match(regexNonUrl)) return str; | ||
|
||
return encodeURI(safeDecodeURI(str)); | ||
} | ||
|
||
function safeDecodeURI(str) { | ||
const safeDecodeURI = (str) => { | ||
try { | ||
return decodeURI(str); | ||
} catch (err) { | ||
return str; | ||
} | ||
} | ||
}; | ||
|
||
const encodeURL = (str) => { | ||
const parsed = urlObj(str); | ||
if (typeof parsed === 'object') { | ||
if (parsed.origin === 'null') return str; | ||
|
||
parsed.search = encodeURI(safeDecodeURI(parsed.search)); | ||
return parsed.toString(); | ||
} | ||
|
||
return encodeURI(safeDecodeURI(str)); | ||
}; | ||
|
||
module.exports = encodeURL; |
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
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