| 1 | /*global Autolinker */ |
| 2 | /** |
| 3 | * @class Autolinker.matcher.Url |
| 4 | * @extends Autolinker.matcher.Matcher |
| 5 | * |
| 6 | * Matcher to find URL matches in an input string. |
| 7 | * |
| 8 | * See this class's superclass ({@link Autolinker.matcher.Matcher}) for more details. |
| 9 | */ |
| 10 | Autolinker.matcher.Url = Autolinker.Util.extend( Autolinker.matcher.Matcher, { |
| 11 | |
| 12 | /** |
| 13 | * @cfg {Object} stripPrefix (required) |
| 14 | * |
| 15 | * The Object form of {@link Autolinker#cfg-stripPrefix}. |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * @cfg {Boolean} stripTrailingSlash (required) |
| 20 | * @inheritdoc Autolinker#stripTrailingSlash |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * @cfg {Boolean} decodePercentEncoding (required) |
| 25 | * @inheritdoc Autolinker#decodePercentEncoding |
| 26 | */ |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * @private |
| 31 | * @property {RegExp} matcherRegex |
| 32 | * |
| 33 | * The regular expression to match URLs with an optional scheme, port |
| 34 | * number, path, query string, and hash anchor. |
| 35 | * |
| 36 | * Example matches: |
| 37 | * |
| 38 | * http://google.com |
| 39 | * www.google.com |
| 40 | * google.com/path/to/file?q1=1&q2=2#myAnchor |
| 41 | * |
| 42 | * |
| 43 | * This regular expression will have the following capturing groups: |
| 44 | * |
| 45 | * 1. Group that matches a scheme-prefixed URL (i.e. 'http://google.com'). |
| 46 | * This is used to match scheme URLs with just a single word, such as |
| 47 | * 'http://localhost', where we won't double check that the domain name |
| 48 | * has at least one dot ('.') in it. |
| 49 | * 2. Group that matches a 'www.' prefixed URL. This is only matched if the |
| 50 | * 'www.' text was not prefixed by a scheme (i.e.: not prefixed by |
| 51 | * 'http://', 'ftp:', etc.) |
| 52 | * 3. A protocol-relative ('//') match for the case of a 'www.' prefixed |
| 53 | * URL. Will be an empty string if it is not a protocol-relative match. |
| 54 | * We need to know the character before the '//' in order to determine |
| 55 | * if it is a valid match or the // was in a string we don't want to |
| 56 | * auto-link. |
| 57 | * 4. Group that matches a known TLD (top level domain), when a scheme |
| 58 | * or 'www.'-prefixed domain is not matched. |
| 59 | * 5. A protocol-relative ('//') match for the case of a known TLD prefixed |
| 60 | * URL. Will be an empty string if it is not a protocol-relative match. |
| 61 | * See #3 for more info. |
| 62 | */ |
| 63 | matcherRegex : (function() { |
| 64 | var schemeRegex = /(?:[A-Za-z][-.+A-Za-z0-9]{0,63}:(?![A-Za-z][-.+A-Za-z0-9]{0,63}:\/\/)(?!\d+\/?)(?:\/\/)?)/, // match protocol, allow in format "http://" or "mailto:". However, do not match the first part of something like 'link:http://www.google.com' (i.e. don't match "link:"). Also, make sure we don't interpret 'google.com:8000' as if 'google.com' was a protocol here (i.e. ignore a trailing port number in this regex) |
| 65 | wwwRegex = /(?:www\.)/, // starting with 'www.' |
| 66 | getDomainNameStr = Autolinker.RegexLib.getDomainNameStr, |
| 67 | tldRegex = Autolinker.tldRegex, // match our known top level domains (TLDs) |
| 68 | alphaNumericCharsStr = Autolinker.RegexLib.alphaNumericCharsStr, |
| 69 | |
| 70 | // Allow optional path, query string, and hash anchor, not ending in the following characters: "?!:,.;" |
| 71 | // http://blog.codinghorror.com/the-problem-with-urls/ |
| 72 | urlSuffixRegex = new RegExp( '[/?#](?:[' + alphaNumericCharsStr + '\\-+&@#/%=~_()|\'$*\\[\\]?!:,.;\u2713]*[' + alphaNumericCharsStr + '\\-+&@#/%=~_()|\'$*\\[\\]\u2713])?' ); |
| 73 | |
| 74 | return new RegExp( [ |
| 75 | '(?:', // parens to cover match for scheme (optional), and domain |
| 76 | '(', // *** Capturing group $1, for a scheme-prefixed url (ex: http://google.com) |
| 77 | schemeRegex.source, |
| 78 | getDomainNameStr(2), |
| 79 | ')', |
| 80 | |
| 81 | '|', |
| 82 | |
| 83 | '(', // *** Capturing group $4 for a 'www.' prefixed url (ex: www.google.com) |
| 84 | '(//)?', // *** Capturing group $5 for an optional protocol-relative URL. Must be at the beginning of the string or start with a non-word character (handled later) |
| 85 | wwwRegex.source, |
| 86 | getDomainNameStr(6), |
| 87 | ')', |
| 88 | |
| 89 | '|', |
| 90 | |
| 91 | '(', // *** Capturing group $8, for known a TLD url (ex: google.com) |
| 92 | '(//)?', // *** Capturing group $9 for an optional protocol-relative URL. Must be at the beginning of the string or start with a non-word character (handled later) |
| 93 | getDomainNameStr(10) + '\\.', |
| 94 | tldRegex.source, |
| 95 | '(?![-' + alphaNumericCharsStr + '])', // TLD not followed by a letter, behaves like unicode-aware \b |
| 96 | ')', |
| 97 | ')', |
| 98 | |
| 99 | '(?::[0-9]+)?', // port |
| 100 | |
| 101 | '(?:' + urlSuffixRegex.source + ')?' // match for path, query string, and/or hash anchor - optional |
| 102 | ].join( "" ), 'gi' ); |
| 103 | } )(), |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * A regular expression to use to check the character before a protocol-relative |
| 108 | * URL match. We don't want to match a protocol-relative URL if it is part |
| 109 | * of another word. |
| 110 | * |
| 111 | * For example, we want to match something like "Go to: //google.com", |
| 112 | * but we don't want to match something like "abc//google.com" |
| 113 | * |
| 114 | * This regular expression is used to test the character before the '//'. |
| 115 | * |
| 116 | * @private |
| 117 | * @type {RegExp} wordCharRegExp |
| 118 | */ |
| 119 | wordCharRegExp : new RegExp( '[' + Autolinker.RegexLib.alphaNumericCharsStr + ']' ), |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * The regular expression to match opening parenthesis in a URL match. |
| 124 | * |
| 125 | * This is to determine if we have unbalanced parenthesis in the URL, and to |
| 126 | * drop the final parenthesis that was matched if so. |
| 127 | * |
| 128 | * Ex: The text "(check out: wikipedia.com/something_(disambiguation))" |
| 129 | * should only autolink the inner "wikipedia.com/something_(disambiguation)" |
| 130 | * part, so if we find that we have unbalanced parenthesis, we will drop the |
| 131 | * last one for the match. |
| 132 | * |
| 133 | * @private |
| 134 | * @property {RegExp} |
| 135 | */ |
| 136 | openParensRe : /\(/g, |
| 137 | |
| 138 | /** |
| 139 | * The regular expression to match closing parenthesis in a URL match. See |
| 140 | * {@link #openParensRe} for more information. |
| 141 | * |
| 142 | * @private |
| 143 | * @property {RegExp} |
| 144 | */ |
| 145 | closeParensRe : /\)/g, |
| 146 | |
| 147 | |
| 148 | /** |
| 149 | * @constructor |
| 150 | * @param {Object} cfg The configuration properties for the Match instance, |
| 151 | * specified in an Object (map). |
| 152 | */ |
| 153 | constructor : function( cfg ) { |
| 154 | Autolinker.matcher.Matcher.prototype.constructor.call( this, cfg ); |
| 155 | |
| 156 | // @if DEBUG |
| 157 | if( cfg.stripPrefix == null ) throw new Error( '`stripPrefix` cfg required' ); |
| 158 | if( cfg.stripTrailingSlash == null ) throw new Error( '`stripTrailingSlash` cfg required' ); |
| 159 | // @endif |
| 160 | |
| 161 | this.stripPrefix = cfg.stripPrefix; |
| 162 | this.stripTrailingSlash = cfg.stripTrailingSlash; |
| 163 | this.decodePercentEncoding = cfg.decodePercentEncoding; |
| 164 | }, |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * @inheritdoc |
| 169 | */ |
| 170 | parseMatches : function( text ) { |
| 171 | var matcherRegex = this.matcherRegex, |
| 172 | stripPrefix = this.stripPrefix, |
| 173 | stripTrailingSlash = this.stripTrailingSlash, |
| 174 | decodePercentEncoding = this.decodePercentEncoding, |
| 175 | tagBuilder = this.tagBuilder, |
| 176 | matches = [], |
| 177 | match; |
| 178 | |
| 179 | while( ( match = matcherRegex.exec( text ) ) !== null ) { |
| 180 | var matchStr = match[ 0 ], |
| 181 | schemeUrlMatch = match[ 1 ], |
| 182 | wwwUrlMatch = match[ 4 ], |
| 183 | wwwProtocolRelativeMatch = match[ 5 ], |
| 184 | //tldUrlMatch = match[ 8 ], -- not needed at the moment |
| 185 | tldProtocolRelativeMatch = match[ 9 ], |
| 186 | offset = match.index, |
| 187 | protocolRelativeMatch = wwwProtocolRelativeMatch || tldProtocolRelativeMatch, |
| 188 | prevChar = text.charAt( offset - 1 ); |
| 189 | |
| 190 | if( !Autolinker.matcher.UrlMatchValidator.isValid( matchStr, schemeUrlMatch ) ) { |
| 191 | continue; |
| 192 | } |
| 193 | |
| 194 | // If the match is preceded by an '@' character, then it is either |
| 195 | // an email address or a username. Skip these types of matches. |
| 196 | if( offset > 0 && prevChar === '@' ) { |
| 197 | continue; |
| 198 | } |
| 199 | |
| 200 | // If it's a protocol-relative '//' match, but the character before the '//' |
| 201 | // was a word character (i.e. a letter/number), then we found the '//' in the |
| 202 | // middle of another word (such as "asdf//asdf.com"). In this case, skip the |
| 203 | // match. |
| 204 | if( offset > 0 && protocolRelativeMatch && this.wordCharRegExp.test( prevChar ) ) { |
| 205 | continue; |
| 206 | } |
| 207 | |
| 208 | if( /\?$/.test(matchStr) ) { |
| 209 | matchStr = matchStr.substr(0, matchStr.length-1); |
| 210 | } |
| 211 | |
| 212 | // Handle a closing parenthesis at the end of the match, and exclude |
| 213 | // it if there is not a matching open parenthesis in the match |
| 214 | // itself. |
| 215 | if( this.matchHasUnbalancedClosingParen( matchStr ) ) { |
| 216 | matchStr = matchStr.substr( 0, matchStr.length - 1 ); // remove the trailing ")" |
| 217 | } else { |
| 218 | // Handle an invalid character after the TLD |
| 219 | var pos = this.matchHasInvalidCharAfterTld( matchStr, schemeUrlMatch ); |
| 220 | if( pos > -1 ) { |
| 221 | matchStr = matchStr.substr( 0, pos ); // remove the trailing invalid chars |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | var urlMatchType = schemeUrlMatch ? 'scheme' : ( wwwUrlMatch ? 'www' : 'tld' ), |
| 226 | protocolUrlMatch = !!schemeUrlMatch; |
| 227 | |
| 228 | matches.push( new Autolinker.match.Url( { |
| 229 | tagBuilder : tagBuilder, |
| 230 | matchedText : matchStr, |
| 231 | offset : offset, |
| 232 | urlMatchType : urlMatchType, |
| 233 | url : matchStr, |
| 234 | protocolUrlMatch : protocolUrlMatch, |
| 235 | protocolRelativeMatch : !!protocolRelativeMatch, |
| 236 | stripPrefix : stripPrefix, |
| 237 | stripTrailingSlash : stripTrailingSlash, |
| 238 | decodePercentEncoding : decodePercentEncoding, |
| 239 | } ) ); |
| 240 | } |
| 241 | |
| 242 | return matches; |
| 243 | }, |
| 244 | |
| 245 | |
| 246 | /** |
| 247 | * Determines if a match found has an unmatched closing parenthesis. If so, |
| 248 | * this parenthesis will be removed from the match itself, and appended |
| 249 | * after the generated anchor tag. |
| 250 | * |
| 251 | * A match may have an extra closing parenthesis at the end of the match |
| 252 | * because the regular expression must include parenthesis for URLs such as |
| 253 | * "wikipedia.com/something_(disambiguation)", which should be auto-linked. |
| 254 | * |
| 255 | * However, an extra parenthesis *will* be included when the URL itself is |
| 256 | * wrapped in parenthesis, such as in the case of "(wikipedia.com/something_(disambiguation))". |
| 257 | * In this case, the last closing parenthesis should *not* be part of the |
| 258 | * URL itself, and this method will return `true`. |
| 259 | * |
| 260 | * @private |
| 261 | * @param {String} matchStr The full match string from the {@link #matcherRegex}. |
| 262 | * @return {Boolean} `true` if there is an unbalanced closing parenthesis at |
| 263 | * the end of the `matchStr`, `false` otherwise. |
| 264 | */ |
| 265 | matchHasUnbalancedClosingParen : function( matchStr ) { |
| 266 | var lastChar = matchStr.charAt( matchStr.length - 1 ); |
| 267 | |
| 268 | if( lastChar === ')' ) { |
| 269 | var openParensMatch = matchStr.match( this.openParensRe ), |
| 270 | closeParensMatch = matchStr.match( this.closeParensRe ), |
| 271 | numOpenParens = ( openParensMatch && openParensMatch.length ) || 0, |
| 272 | numCloseParens = ( closeParensMatch && closeParensMatch.length ) || 0; |
| 273 | |
| 274 | if( numOpenParens < numCloseParens ) { |
| 275 | return true; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return false; |
| 280 | }, |
| 281 | |
| 282 | |
| 283 | /** |
| 284 | * Determine if there's an invalid character after the TLD in a URL. Valid |
| 285 | * characters after TLD are ':/?#'. Exclude scheme matched URLs from this |
| 286 | * check. |
| 287 | * |
| 288 | * @private |
| 289 | * @param {String} urlMatch The matched URL, if there was one. Will be an |
| 290 | * empty string if the match is not a URL match. |
| 291 | * @param {String} schemeUrlMatch The match URL string for a scheme |
| 292 | * match. Ex: 'http://yahoo.com'. This is used to match something like |
| 293 | * 'http://localhost', where we won't double check that the domain name |
| 294 | * has at least one '.' in it. |
| 295 | * @return {Number} the position where the invalid character was found. If |
| 296 | * no such character was found, returns -1 |
| 297 | */ |
| 298 | matchHasInvalidCharAfterTld : function( urlMatch, schemeUrlMatch ) { |
| 299 | if( !urlMatch ) { |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | var offset = 0; |
| 304 | if ( schemeUrlMatch ) { |
| 305 | offset = urlMatch.indexOf(':'); |
| 306 | urlMatch = urlMatch.slice(offset); |
| 307 | } |
| 308 | |
| 309 | var alphaNumeric = Autolinker.RegexLib.alphaNumericCharsStr; |
| 310 | |
| 311 | var re = new RegExp("^((.?\/\/)?[-." + alphaNumeric + "]*[-" + alphaNumeric + "]\\.[-" + alphaNumeric + "]+)"); |
| 312 | var res = re.exec( urlMatch ); |
| 313 | if ( res === null ) { |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | offset += res[1].length; |
| 318 | urlMatch = urlMatch.slice(res[1].length); |
| 319 | if (/^[^-.A-Za-z0-9:\/?#]/.test(urlMatch)) { |
| 320 | return offset; |
| 321 | } |
| 322 | |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | } ); |