source: node_modules/@braintree/sanitize-url/dist/index.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.3 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.sanitizeUrl = void 0;
4var constants_1 = require("./constants");
5function isRelativeUrlWithoutProtocol(url) {
6 return constants_1.relativeFirstCharacters.indexOf(url[0]) > -1;
7}
8// adapted from https://stackoverflow.com/a/29824550/2601552
9function decodeHtmlCharacters(str) {
10 var removedNullByte = str.replace(constants_1.ctrlCharactersRegex, "");
11 return removedNullByte.replace(constants_1.htmlEntitiesRegex, function (match, dec) {
12 return String.fromCharCode(dec);
13 });
14}
15function sanitizeUrl(url) {
16 if (!url) {
17 return constants_1.BLANK_URL;
18 }
19 var sanitizedUrl = decodeHtmlCharacters(url)
20 .replace(constants_1.htmlCtrlEntityRegex, "")
21 .replace(constants_1.ctrlCharactersRegex, "")
22 .trim();
23 if (!sanitizedUrl) {
24 return constants_1.BLANK_URL;
25 }
26 if (isRelativeUrlWithoutProtocol(sanitizedUrl)) {
27 return sanitizedUrl;
28 }
29 var urlSchemeParseResults = sanitizedUrl.match(constants_1.urlSchemeRegex);
30 if (!urlSchemeParseResults) {
31 return sanitizedUrl;
32 }
33 var urlScheme = urlSchemeParseResults[0];
34 if (constants_1.invalidProtocolRegex.test(urlScheme)) {
35 return constants_1.BLANK_URL;
36 }
37 return sanitizedUrl;
38}
39exports.sanitizeUrl = sanitizeUrl;
Note: See TracBrowser for help on using the repository browser.