| 1 |
import { __extends } from "tslib"; |
| 2 |
import { Match } from './match'; |
| 3 |
/** |
| 4 |
* @class Autolinker.match.Url |
| 5 |
* @extends Autolinker.match.Match |
| 6 |
* |
| 7 |
* Represents a Url match found in an input string which should be Autolinked. |
| 8 |
* |
| 9 |
* See this class's superclass ({@link Autolinker.match.Match}) for more details. |
| 10 |
*/ |
| 11 |
var UrlMatch = /** @class */ (function (_super) { |
| 12 |
__extends(UrlMatch, _super); |
| 13 |
/** |
| 14 |
* @method constructor |
| 15 |
* @param {Object} cfg The configuration properties for the Match |
| 16 |
* instance, specified in an Object (map). |
| 17 |
*/ |
| 18 |
function UrlMatch(cfg) { |
| 19 |
var _this = _super.call(this, cfg) || this; |
| 20 |
/** |
| 21 |
* @cfg {String} url (required) |
| 22 |
* |
| 23 |
* The url that was matched. |
| 24 |
*/ |
| 25 |
_this.url = ''; // default value just to get the above doc comment in the ES5 output and documentation generator |
| 26 |
/** |
| 27 |
* @cfg {"scheme"/"www"/"tld"} urlMatchType (required) |
| 28 |
* |
| 29 |
* The type of URL match that this class represents. This helps to determine |
| 30 |
* if the match was made in the original text with a prefixed scheme (ex: |
| 31 |
* 'http://www.google.com'), a prefixed 'www' (ex: 'www.google.com'), or |
| 32 |
* was matched by a known top-level domain (ex: 'google.com'). |
| 33 |
*/ |
| 34 |
_this.urlMatchType = 'scheme'; // default value just to get the above doc comment in the ES5 output and documentation generator |
| 35 |
/** |
| 36 |
* @cfg {Boolean} protocolUrlMatch (required) |
| 37 |
* |
| 38 |
* `true` if the URL is a match which already has a protocol (i.e. |
| 39 |
* 'http://'), `false` if the match was from a 'www' or known TLD match. |
| 40 |
*/ |
| 41 |
_this.protocolUrlMatch = false; // default value just to get the above doc comment in the ES5 output and documentation generator |
| 42 |
/** |
| 43 |
* @cfg {Boolean} protocolRelativeMatch (required) |
| 44 |
* |
| 45 |
* `true` if the URL is a protocol-relative match. A protocol-relative match |
| 46 |
* is a URL that starts with '//', and will be either http:// or https:// |
| 47 |
* based on the protocol that the site is loaded under. |
| 48 |
*/ |
| 49 |
_this.protocolRelativeMatch = false; // default value just to get the above doc comment in the ES5 output and documentation generator |
| 50 |
/** |
| 51 |
* @cfg {Object} stripPrefix (required) |
| 52 |
* |
| 53 |
* The Object form of {@link Autolinker#cfg-stripPrefix}. |
| 54 |
*/ |
| 55 |
_this.stripPrefix = { |
| 56 |
scheme: true, |
| 57 |
www: true, |
| 58 |
}; // default value just to get the above doc comment in the ES5 output and documentation generator |
| 59 |
/** |
| 60 |
* @cfg {Boolean} stripTrailingSlash (required) |
| 61 |
* @inheritdoc Autolinker#cfg-stripTrailingSlash |
| 62 |
*/ |
| 63 |
_this.stripTrailingSlash = true; // default value just to get the above doc comment in the ES5 output and documentation generator |
| 64 |
/** |
| 65 |
* @cfg {Boolean} decodePercentEncoding (required) |
| 66 |
* @inheritdoc Autolinker#cfg-decodePercentEncoding |
| 67 |
*/ |
| 68 |
_this.decodePercentEncoding = true; // default value just to get the above doc comment in the ES5 output and documentation generator |
| 69 |
/** |
| 70 |
* @private |
| 71 |
* @property {RegExp} schemePrefixRegex |
| 72 |
* |
| 73 |
* A regular expression used to remove the 'http://' or 'https://' from |
| 74 |
* URLs. |
| 75 |
*/ |
| 76 |
_this.schemePrefixRegex = /^(https?:\/\/)?/i; |
| 77 |
/** |
| 78 |
* @private |
| 79 |
* @property {RegExp} wwwPrefixRegex |
| 80 |
* |
| 81 |
* A regular expression used to remove the 'www.' from URLs. |
| 82 |
*/ |
| 83 |
_this.wwwPrefixRegex = /^(https?:\/\/)?(www\.)?/i; |
| 84 |
/** |
| 85 |
* @private |
| 86 |
* @property {RegExp} protocolRelativeRegex |
| 87 |
* |
| 88 |
* The regular expression used to remove the protocol-relative '//' from the {@link #url} string, for purposes |
| 89 |
* of {@link #getAnchorText}. A protocol-relative URL is, for example, "//yahoo.com" |
| 90 |
*/ |
| 91 |
_this.protocolRelativeRegex = /^\/\//; |
| 92 |
/** |
| 93 |
* @private |
| 94 |
* @property {Boolean} protocolPrepended |
| 95 |
* |
| 96 |
* Will be set to `true` if the 'http://' protocol has been prepended to the {@link #url} (because the |
| 97 |
* {@link #url} did not have a protocol) |
| 98 |
*/ |
| 99 |
_this.protocolPrepended = false; |
| 100 |
_this.urlMatchType = cfg.urlMatchType; |
| 101 |
_this.url = cfg.url; |
| 102 |
_this.protocolUrlMatch = cfg.protocolUrlMatch; |
| 103 |
_this.protocolRelativeMatch = cfg.protocolRelativeMatch; |
| 104 |
_this.stripPrefix = cfg.stripPrefix; |
| 105 |
_this.stripTrailingSlash = cfg.stripTrailingSlash; |
| 106 |
_this.decodePercentEncoding = cfg.decodePercentEncoding; |
| 107 |
return _this; |
| 108 |
} |
| 109 |
/** |
| 110 |
* Returns a string name for the type of match that this class represents. |
| 111 |
* For the case of UrlMatch, returns 'url'. |
| 112 |
* |
| 113 |
* @return {String} |
| 114 |
*/ |
| 115 |
UrlMatch.prototype.getType = function () { |
| 116 |
return 'url'; |
| 117 |
}; |
| 118 |
/** |
| 119 |
* Returns a string name for the type of URL match that this class |
| 120 |
* represents. |
| 121 |
* |
| 122 |
* This helps to determine if the match was made in the original text with a |
| 123 |
* prefixed scheme (ex: 'http://www.google.com'), a prefixed 'www' (ex: |
| 124 |
* 'www.google.com'), or was matched by a known top-level domain (ex: |
| 125 |
* 'google.com'). |
| 126 |
* |
| 127 |
* @return {"scheme"/"www"/"tld"} |
| 128 |
*/ |
| 129 |
UrlMatch.prototype.getUrlMatchType = function () { |
| 130 |
return this.urlMatchType; |
| 131 |
}; |
| 132 |
/** |
| 133 |
* Returns the url that was matched, assuming the protocol to be 'http://' if the original |
| 134 |
* match was missing a protocol. |
| 135 |
* |
| 136 |
* @return {String} |
| 137 |
*/ |
| 138 |
UrlMatch.prototype.getUrl = function () { |
| 139 |
var url = this.url; |
| 140 |
// if the url string doesn't begin with a protocol, assume 'http://' |
| 141 |
if (!this.protocolRelativeMatch && !this.protocolUrlMatch && !this.protocolPrepended) { |
| 142 |
url = this.url = 'http://' + url; |
| 143 |
this.protocolPrepended = true; |
| 144 |
} |
| 145 |
return url; |
| 146 |
}; |
| 147 |
/** |
| 148 |
* Returns the anchor href that should be generated for the match. |
| 149 |
* |
| 150 |
* @return {String} |
| 151 |
*/ |
| 152 |
UrlMatch.prototype.getAnchorHref = function () { |
| 153 |
var url = this.getUrl(); |
| 154 |
return url.replace(/&/g, '&'); // any &'s in the URL should be converted back to '&' if they were displayed as & in the source html |
| 155 |
}; |
| 156 |
/** |
| 157 |
* Returns the anchor text that should be generated for the match. |
| 158 |
* |
| 159 |
* @return {String} |
| 160 |
*/ |
| 161 |
UrlMatch.prototype.getAnchorText = function () { |
| 162 |
var anchorText = this.getMatchedText(); |
| 163 |
if (this.protocolRelativeMatch) { |
| 164 |
// Strip off any protocol-relative '//' from the anchor text |
| 165 |
anchorText = this.stripProtocolRelativePrefix(anchorText); |
| 166 |
} |
| 167 |
if (this.stripPrefix.scheme) { |
| 168 |
anchorText = this.stripSchemePrefix(anchorText); |
| 169 |
} |
| 170 |
if (this.stripPrefix.www) { |
| 171 |
anchorText = this.stripWwwPrefix(anchorText); |
| 172 |
} |
| 173 |
if (this.stripTrailingSlash) { |
| 174 |
anchorText = this.removeTrailingSlash(anchorText); // remove trailing slash, if there is one |
| 175 |
} |
| 176 |
if (this.decodePercentEncoding) { |
| 177 |
anchorText = this.removePercentEncoding(anchorText); |
| 178 |
} |
| 179 |
return anchorText; |
| 180 |
}; |
| 181 |
// --------------------------------------- |
| 182 |
// Utility Functionality |
| 183 |
/** |
| 184 |
* Strips the scheme prefix (such as "http://" or "https://") from the given |
| 185 |
* `url`. |
| 186 |
* |
| 187 |
* @private |
| 188 |
* @param {String} url The text of the anchor that is being generated, for |
| 189 |
* which to strip off the url scheme. |
| 190 |
* @return {String} The `url`, with the scheme stripped. |
| 191 |
*/ |
| 192 |
UrlMatch.prototype.stripSchemePrefix = function (url) { |
| 193 |
return url.replace(this.schemePrefixRegex, ''); |
| 194 |
}; |
| 195 |
/** |
| 196 |
* Strips the 'www' prefix from the given `url`. |
| 197 |
* |
| 198 |
* @private |
| 199 |
* @param {String} url The text of the anchor that is being generated, for |
| 200 |
* which to strip off the 'www' if it exists. |
| 201 |
* @return {String} The `url`, with the 'www' stripped. |
| 202 |
*/ |
| 203 |
UrlMatch.prototype.stripWwwPrefix = function (url) { |
| 204 |
return url.replace(this.wwwPrefixRegex, '$1'); // leave any scheme ($1), it one exists |
| 205 |
}; |
| 206 |
/** |
| 207 |
* Strips any protocol-relative '//' from the anchor text. |
| 208 |
* |
| 209 |
* @private |
| 210 |
* @param {String} text The text of the anchor that is being generated, for which to strip off the |
| 211 |
* protocol-relative prefix (such as stripping off "//") |
| 212 |
* @return {String} The `anchorText`, with the protocol-relative prefix stripped. |
| 213 |
*/ |
| 214 |
UrlMatch.prototype.stripProtocolRelativePrefix = function (text) { |
| 215 |
return text.replace(this.protocolRelativeRegex, ''); |
| 216 |
}; |
| 217 |
/** |
| 218 |
* Removes any trailing slash from the given `anchorText`, in preparation for the text to be displayed. |
| 219 |
* |
| 220 |
* @private |
| 221 |
* @param {String} anchorText The text of the anchor that is being generated, for which to remove any trailing |
| 222 |
* slash ('/') that may exist. |
| 223 |
* @return {String} The `anchorText`, with the trailing slash removed. |
| 224 |
*/ |
| 225 |
UrlMatch.prototype.removeTrailingSlash = function (anchorText) { |
| 226 |
if (anchorText.charAt(anchorText.length - 1) === '/') { |
| 227 |
anchorText = anchorText.slice(0, -1); |
| 228 |
} |
| 229 |
return anchorText; |
| 230 |
}; |
| 231 |
/** |
| 232 |
* Decodes percent-encoded characters from the given `anchorText`, in |
| 233 |
* preparation for the text to be displayed. |
| 234 |
* |
| 235 |
* @private |
| 236 |
* @param {String} anchorText The text of the anchor that is being |
| 237 |
* generated, for which to decode any percent-encoded characters. |
| 238 |
* @return {String} The `anchorText`, with the percent-encoded characters |
| 239 |
* decoded. |
| 240 |
*/ |
| 241 |
UrlMatch.prototype.removePercentEncoding = function (anchorText) { |
| 242 |
// First, convert a few of the known % encodings to the corresponding |
| 243 |
// HTML entities that could accidentally be interpretted as special |
| 244 |
// HTML characters |
| 245 |
var preProcessedEntityAnchorText = anchorText |
| 246 |
.replace(/%22/gi, '"') // " char |
| 247 |
.replace(/%26/gi, '&') // & char |
| 248 |
.replace(/%27/gi, ''') // ' char |
| 249 |
.replace(/%3C/gi, '<') // < char |
| 250 |
.replace(/%3E/gi, '>'); // > char |
| 251 |
try { |
| 252 |
// Now attempt to decode the rest of the anchor text |
| 253 |
return decodeURIComponent(preProcessedEntityAnchorText); |
| 254 |
} |
| 255 |
catch (e) { |
| 256 |
// Invalid % escape sequence in the anchor text |
| 257 |
return preProcessedEntityAnchorText; |
| 258 |
} |
| 259 |
}; |
| 260 |
return UrlMatch; |
| 261 |
}(Match)); |
| 262 |
export { UrlMatch }; |
| 263 |
//# sourceMappingURL=url-match.js.map |