/*global Autolinker */ /** * @class Autolinker.match.Url * @extends Autolinker.match.Match * * Represents a Url match found in an input string which should be Autolinked. * * See this class's superclass ({@link Autolinker.match.Match}) for more details. */ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, { /** * @cfg {String} url (required) * * The url that was matched. */ /** * @cfg {Boolean} protocolUrlMatch (required) * * `true` if the URL is a match which already has a protocol (i.e. 'http://'), `false` if the match was from a 'www' or * known TLD match. */ /** * @cfg {Boolean} protocolRelativeMatch (required) * * `true` if the URL is a protocol-relative match. A protocol-relative match is a URL that starts with '//', * and will be either http:// or https:// based on the protocol that the site is loaded under. */ /** * @cfg {Boolean} stripPrefix (required) * @inheritdoc Autolinker#stripPrefix */ /** * @private * @property {RegExp} urlPrefixRegex * * A regular expression used to remove the 'http://' or 'https://' and/or the 'www.' from URLs. */ urlPrefixRegex: /^(https?:\/\/)?(www\.)?/i, /** * @private * @property {RegExp} protocolRelativeRegex * * The regular expression used to remove the protocol-relative '//' from the {@link #url} string, for purposes * of {@link #getAnchorText}. A protocol-relative URL is, for example, "//yahoo.com" */ protocolRelativeRegex : /^\/\//, /** * @private * @property {Boolean} protocolPrepended * * Will be set to `true` if the 'http://' protocol has been prepended to the {@link #url} (because the * {@link #url} did not have a protocol) */ protocolPrepended : false, /** * Returns a string name for the type of match that this class represents. * * @return {String} */ getType : function() { return 'url'; }, /** * Returns the url that was matched, assuming the protocol to be 'http://' if the original * match was missing a protocol. * * @return {String} */ getUrl : function() { var url = this.url; // if the url string doesn't begin with a protocol, assume 'http://' if( !this.protocolRelativeMatch && !this.protocolUrlMatch && !this.protocolPrepended ) { url = this.url = 'http://' + url; this.protocolPrepended = true; } return url; }, /** * Returns the anchor href that should be generated for the match. * * @return {String} */ getAnchorHref : function() { var url = this.getUrl(); return url.replace( /&/g, '&' ); // any &'s in the URL should be converted back to '&' if they were displayed as & in the source html }, /** * Returns the anchor text that should be generated for the match. * * @return {String} */ getAnchorText : function() { var anchorText = this.getUrl(); if( this.protocolRelativeMatch ) { // Strip off any protocol-relative '//' from the anchor text anchorText = this.stripProtocolRelativePrefix( anchorText ); } if( this.stripPrefix ) { anchorText = this.stripUrlPrefix( anchorText ); } anchorText = this.removeTrailingSlash( anchorText ); // remove trailing slash, if there is one return anchorText; }, // --------------------------------------- // Utility Functionality /** * Strips the URL prefix (such as "http://" or "https://") from the given text. * * @private * @param {String} text The text of the anchor that is being generated, for which to strip off the * url prefix (such as stripping off "http://") * @return {String} The `anchorText`, with the prefix stripped. */ stripUrlPrefix : function( text ) { return text.replace( this.urlPrefixRegex, '' ); }, /** * Strips any protocol-relative '//' from the anchor text. * * @private * @param {String} text The text of the anchor that is being generated, for which to strip off the * protocol-relative prefix (such as stripping off "//") * @return {String} The `anchorText`, with the protocol-relative prefix stripped. */ stripProtocolRelativePrefix : function( text ) { return text.replace( this.protocolRelativeRegex, '' ); }, /** * Removes any trailing slash from the given `anchorText`, in preparation for the text to be displayed. * * @private * @param {String} anchorText The text of the anchor that is being generated, for which to remove any trailing * slash ('/') that may exist. * @return {String} The `anchorText`, with the trailing slash removed. */ removeTrailingSlash : function( anchorText ) { if( anchorText.charAt( anchorText.length - 1 ) === '/' ) { anchorText = anchorText.slice( 0, -1 ); } return anchorText; } } );