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