@@ -137,7 +137,7 @@ var Autolinker = function( cfg ) {
137137 this . hashtag = cfg . hashtag || false ;
138138 this . mention = cfg . mention || false ;
139139 this . newWindow = typeof cfg . newWindow === 'boolean' ? cfg . newWindow : true ;
140- this . stripPrefix = typeof cfg . stripPrefix === 'boolean' ? cfg . stripPrefix : true ;
140+ this . stripPrefix = this . normalizeStripPrefixCfg ( cfg . stripPrefix ) ;
141141 this . stripTrailingSlash = typeof cfg . stripTrailingSlash === 'boolean' ? cfg . stripTrailingSlash : true ;
142142
143143 // Validate the value of the `mention` cfg
@@ -207,23 +207,36 @@ Autolinker.prototype = {
207207 constructor : Autolinker , // fix constructor property
208208
209209 /**
210- * @cfg {Boolean/Object} [urls=true ]
210+ * @cfg {Boolean/Object} [urls]
211211 *
212212 * `true` if URLs should be automatically linked, `false` if they should not
213- * be.
213+ * be. Defaults to `true`.
214214 *
215- * This option also accepts an Object form with 3 properties, to allow for
216- * more customization of what exactly gets linked. All default to `true`:
215+ * Examples:
217216 *
218- * @param {Boolean } schemeMatches `true` to match URLs found prefixed with a
219- * scheme, i.e. `http://google.com`, or `other+scheme://google.com`,
217+ * urls: true
218+ *
219+ * // or
220+ *
221+ * urls: {
222+ * schemeMatches : true,
223+ * wwwMatches : true,
224+ * tldMatches : true
225+ * }
226+ *
227+ * As shown above, this option also accepts an Object form with 3 properties
228+ * to allow for more customization of what exactly gets linked. All default
229+ * to `true`:
230+ *
231+ * @cfg {Boolean} [urls.schemeMatches] `true` to match URLs found prefixed
232+ * with a scheme, i.e. `http://google.com`, or `other+scheme://google.com`,
220233 * `false` to prevent these types of matches.
221- * @param {Boolean } wwwMatches `true` to match urls found prefixed with
234+ * @cfg {Boolean} [urls. wwwMatches] `true` to match urls found prefixed with
222235 * `'www.'`, i.e. `www.google.com`. `false` to prevent these types of
223236 * matches. Note that if the URL had a prefixed scheme, and
224237 * `schemeMatches` is true, it will still be linked.
225- * @param {Boolean } tldMatches `true` to match URLs with known top level
226- * domains (.com, .net, etc.) that are not prefixed with a scheme or
238+ * @cfg {Boolean} [urls. tldMatches] `true` to match URLs with known top
239+ * level domains (.com, .net, etc.) that are not prefixed with a scheme or
227240 * `'www.'`. This option attempts to match anything that looks like a URL
228241 * in the given text. Ex: `google.com`, `asdf.org/?page=1`, etc. `false`
229242 * to prevent these types of matches.
@@ -275,10 +288,37 @@ Autolinker.prototype = {
275288 */
276289
277290 /**
278- * @cfg {Boolean} [stripPrefix=true]
291+ * @cfg {Boolean/Object} [stripPrefix]
292+ *
293+ * `true` if 'http://' (or 'https://') and/or the 'www.' should be stripped
294+ * from the beginning of URL links' text, `false` otherwise. Defaults to
295+ * `true`.
296+ *
297+ * Examples:
279298 *
280- * `true` if 'http://' or 'https://' and/or the 'www.' should be stripped
281- * from the beginning of URL links' text, `false` otherwise.
299+ * stripPrefix: true
300+ *
301+ * // or
302+ *
303+ * stripPrefix: {
304+ * scheme : true,
305+ * www : true
306+ * }
307+ *
308+ * As shown above, this option also accepts an Object form with 2 properties
309+ * to allow for more customization of what exactly is prevented from being
310+ * displayed. Both default to `true`:
311+ *
312+ * @cfg {Boolean} [stripPrefix.scheme] `true` to prevent the scheme part of
313+ * a URL match from being displayed to the user. Example:
314+ * `'http://google.com'` will be displayed as `'google.com'`. `false` to
315+ * not strip the scheme. NOTE: Only an `'http://'` or `'https://'` scheme
316+ * will be removed, so as not to remove a potentially dangerous scheme
317+ * (such as `'file://'` or `'javascript:'`)
318+ * @cfg {Boolean} [stripPrefix.www] www (Boolean): `true` to prevent the
319+ * `'www.'` part of a URL match from being displayed to the user. Ex:
320+ * `'www.google.com'` will be displayed as `'google.com'`. `false` to not
321+ * strip the `'www'`.
282322 */
283323
284324 /**
@@ -445,6 +485,31 @@ Autolinker.prototype = {
445485 } ,
446486
447487
488+ /**
489+ * Normalizes the {@link #stripPrefix} config into an Object with 2
490+ * properties: `scheme`, and `www` - both Booleans.
491+ *
492+ * See {@link #stripPrefix} config for details.
493+ *
494+ * @private
495+ * @param {Boolean/Object } stripPrefix
496+ * @return {Object }
497+ */
498+ normalizeStripPrefixCfg : function ( stripPrefix ) {
499+ if ( stripPrefix == null ) stripPrefix = true ; // default to `true`
500+
501+ if ( typeof stripPrefix === 'boolean' ) {
502+ return { scheme : stripPrefix , www : stripPrefix } ;
503+
504+ } else { // object form
505+ return {
506+ scheme : typeof stripPrefix . scheme === 'boolean' ? stripPrefix . scheme : true ,
507+ www : typeof stripPrefix . www === 'boolean' ? stripPrefix . www : true
508+ } ;
509+ }
510+ } ,
511+
512+
448513 /**
449514 * Normalizes the {@link #truncate} config into an Object with 2 properties:
450515 * `length` (Number), and `location` (String).
@@ -2749,8 +2814,9 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
27492814 */
27502815
27512816 /**
2752- * @cfg {Boolean} stripPrefix (required)
2753- * @inheritdoc Autolinker#cfg-stripPrefix
2817+ * @cfg {Object} stripPrefix (required)
2818+ *
2819+ * The Object form of {@link Autolinker#cfg-stripPrefix}.
27542820 */
27552821
27562822 /**
@@ -2785,12 +2851,20 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
27852851
27862852 /**
27872853 * @private
2788- * @property {RegExp } urlPrefixRegex
2854+ * @property {RegExp } schemePrefixRegex
2855+ *
2856+ * A regular expression used to remove the 'http://' or 'https://' from
2857+ * URLs.
2858+ */
2859+ schemePrefixRegex : / ^ ( h t t p s ? : \/ \/ ) ? / i,
2860+
2861+ /**
2862+ * @private
2863+ * @property {RegExp } wwwPrefixRegex
27892864 *
2790- * A regular expression used to remove the 'http://' or 'https://' and/or
2791- * the 'www.' from URLs.
2865+ * A regular expression used to remove the 'www.' from URLs.
27922866 */
2793- urlPrefixRegex : / ^ ( h t t p s ? : \/ \/ ) ? ( w w w \. ) ? / i,
2867+ wwwPrefixRegex : / ^ ( h t t p s ? : \/ \/ ) ? ( w w w \. ) ? / i,
27942868
27952869 /**
27962870 * @private
@@ -2881,8 +2955,11 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
28812955 // Strip off any protocol-relative '//' from the anchor text
28822956 anchorText = this . stripProtocolRelativePrefix ( anchorText ) ;
28832957 }
2884- if ( this . stripPrefix ) {
2885- anchorText = this . stripUrlPrefix ( anchorText ) ;
2958+ if ( this . stripPrefix . scheme ) {
2959+ anchorText = this . stripSchemePrefix ( anchorText ) ;
2960+ }
2961+ if ( this . stripPrefix . www ) {
2962+ anchorText = this . stripWwwPrefix ( anchorText ) ;
28862963 }
28872964 if ( this . stripTrailingSlash ) {
28882965 anchorText = this . removeTrailingSlash ( anchorText ) ; // remove trailing slash, if there is one
@@ -2897,15 +2974,29 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
28972974 // Utility Functionality
28982975
28992976 /**
2900- * Strips the URL prefix (such as "http://" or "https://") from the given text.
2977+ * Strips the scheme prefix (such as "http://" or "https://") from the given
2978+ * `url`.
29012979 *
29022980 * @private
2903- * @param {String } text The text of the anchor that is being generated, for which to strip off the
2904- * url prefix (such as stripping off "http://")
2905- * @return {String } The `anchorText`, with the prefix stripped.
2981+ * @param {String } url The text of the anchor that is being generated, for
2982+ * which to strip off the url scheme.
2983+ * @return {String } The `url`, with the scheme stripped.
2984+ */
2985+ stripSchemePrefix : function ( url ) {
2986+ return url . replace ( this . schemePrefixRegex , '' ) ;
2987+ } ,
2988+
2989+
2990+ /**
2991+ * Strips the 'www' prefix from the given `url`.
2992+ *
2993+ * @private
2994+ * @param {String } url The text of the anchor that is being generated, for
2995+ * which to strip off the 'www' if it exists.
2996+ * @return {String } The `url`, with the 'www' stripped.
29062997 */
2907- stripUrlPrefix : function ( text ) {
2908- return text . replace ( this . urlPrefixRegex , '' ) ;
2998+ stripWwwPrefix : function ( url ) {
2999+ return url . replace ( this . wwwPrefixRegex , '$1 ' ) ; // leave any scheme ($1), it one exists
29093000 } ,
29103001
29113002
@@ -3279,8 +3370,9 @@ Autolinker.matcher.Mention = Autolinker.Util.extend( Autolinker.matcher.Matcher,
32793370Autolinker . matcher . Url = Autolinker . Util . extend ( Autolinker . matcher . Matcher , {
32803371
32813372 /**
3282- * @cfg {Boolean} stripPrefix (required)
3283- * @inheritdoc Autolinker#stripPrefix
3373+ * @cfg {Object} stripPrefix (required)
3374+ *
3375+ * The Object form of {@link Autolinker#cfg-stripPrefix}.
32843376 */
32853377
32863378 /**
0 commit comments