@@ -137,7 +137,7 @@ var Autolinker = function( cfg ) {
137
137
this . hashtag = cfg . hashtag || false ;
138
138
this . mention = cfg . mention || false ;
139
139
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 ) ;
141
141
this . stripTrailingSlash = typeof cfg . stripTrailingSlash === 'boolean' ? cfg . stripTrailingSlash : true ;
142
142
143
143
// Validate the value of the `mention` cfg
@@ -207,23 +207,36 @@ Autolinker.prototype = {
207
207
constructor : Autolinker , // fix constructor property
208
208
209
209
/**
210
- * @cfg {Boolean/Object} [urls=true ]
210
+ * @cfg {Boolean/Object} [urls]
211
211
*
212
212
* `true` if URLs should be automatically linked, `false` if they should not
213
- * be.
213
+ * be. Defaults to `true`.
214
214
*
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:
217
216
*
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`,
220
233
* `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
222
235
* `'www.'`, i.e. `www.google.com`. `false` to prevent these types of
223
236
* matches. Note that if the URL had a prefixed scheme, and
224
237
* `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
227
240
* `'www.'`. This option attempts to match anything that looks like a URL
228
241
* in the given text. Ex: `google.com`, `asdf.org/?page=1`, etc. `false`
229
242
* to prevent these types of matches.
@@ -275,10 +288,37 @@ Autolinker.prototype = {
275
288
*/
276
289
277
290
/**
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:
279
298
*
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'`.
282
322
*/
283
323
284
324
/**
@@ -445,6 +485,31 @@ Autolinker.prototype = {
445
485
} ,
446
486
447
487
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
+
448
513
/**
449
514
* Normalizes the {@link #truncate} config into an Object with 2 properties:
450
515
* `length` (Number), and `location` (String).
@@ -2749,8 +2814,9 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
2749
2814
*/
2750
2815
2751
2816
/**
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}.
2754
2820
*/
2755
2821
2756
2822
/**
@@ -2785,12 +2851,20 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
2785
2851
2786
2852
/**
2787
2853
* @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
2789
2864
*
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.
2792
2866
*/
2793
- urlPrefixRegex : / ^ ( h t t p s ? : \/ \/ ) ? ( w w w \. ) ? / i,
2867
+ wwwPrefixRegex : / ^ ( h t t p s ? : \/ \/ ) ? ( w w w \. ) ? / i,
2794
2868
2795
2869
/**
2796
2870
* @private
@@ -2881,8 +2955,11 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
2881
2955
// Strip off any protocol-relative '//' from the anchor text
2882
2956
anchorText = this . stripProtocolRelativePrefix ( anchorText ) ;
2883
2957
}
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 ) ;
2886
2963
}
2887
2964
if ( this . stripTrailingSlash ) {
2888
2965
anchorText = this . removeTrailingSlash ( anchorText ) ; // remove trailing slash, if there is one
@@ -2897,15 +2974,29 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
2897
2974
// Utility Functionality
2898
2975
2899
2976
/**
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`.
2901
2979
*
2902
2980
* @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.
2906
2997
*/
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
2909
3000
} ,
2910
3001
2911
3002
@@ -3279,8 +3370,9 @@ Autolinker.matcher.Mention = Autolinker.Util.extend( Autolinker.matcher.Matcher,
3279
3370
Autolinker . matcher . Url = Autolinker . Util . extend ( Autolinker . matcher . Matcher , {
3280
3371
3281
3372
/**
3282
- * @cfg {Boolean} stripPrefix (required)
3283
- * @inheritdoc Autolinker#stripPrefix
3373
+ * @cfg {Object} stripPrefix (required)
3374
+ *
3375
+ * The Object form of {@link Autolinker#cfg-stripPrefix}.
3284
3376
*/
3285
3377
3286
3378
/**
0 commit comments