source: node_modules/autolinker/dist/commonjs/match/url-match.js@ d24f17c

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

Initial commit

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