"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sanitizeUrl = void 0; var constants_1 = require("./constants"); function isRelativeUrlWithoutProtocol(url) { return constants_1.relativeFirstCharacters.indexOf(url[0]) > -1; } // adapted from https://stackoverflow.com/a/29824550/2601552 function decodeHtmlCharacters(str) { var removedNullByte = str.replace(constants_1.ctrlCharactersRegex, ""); return removedNullByte.replace(constants_1.htmlEntitiesRegex, function (match, dec) { return String.fromCharCode(dec); }); } function sanitizeUrl(url) { if (!url) { return constants_1.BLANK_URL; } var sanitizedUrl = decodeHtmlCharacters(url) .replace(constants_1.htmlCtrlEntityRegex, "") .replace(constants_1.ctrlCharactersRegex, "") .trim(); if (!sanitizedUrl) { return constants_1.BLANK_URL; } if (isRelativeUrlWithoutProtocol(sanitizedUrl)) { return sanitizedUrl; } var urlSchemeParseResults = sanitizedUrl.match(constants_1.urlSchemeRegex); if (!urlSchemeParseResults) { return sanitizedUrl; } var urlScheme = urlSchemeParseResults[0]; if (constants_1.invalidProtocolRegex.test(urlScheme)) { return constants_1.BLANK_URL; } return sanitizedUrl; } exports.sanitizeUrl = sanitizeUrl;