1
1
/*!
2
2
* Autolinker.js
3
- * 1.5 .0
3
+ * 1.6 .0
4
4
*
5
5
* Copyright(c) 2017 Gregory Jacobs <greg@greg-jacobs.com>
6
6
* MIT License
@@ -139,6 +139,7 @@ var Autolinker = function( cfg ) {
139
139
this . newWindow = typeof cfg . newWindow === 'boolean' ? cfg . newWindow : true ;
140
140
this . stripPrefix = this . normalizeStripPrefixCfg ( cfg . stripPrefix ) ;
141
141
this . stripTrailingSlash = typeof cfg . stripTrailingSlash === 'boolean' ? cfg . stripTrailingSlash : true ;
142
+ this . decodePercentEncoding = typeof cfg . decodePercentEncoding === 'boolean' ? cfg . decodePercentEncoding : true ;
142
143
143
144
// Validate the value of the `mention` cfg
144
145
var mention = this . mention ;
@@ -240,7 +241,7 @@ Autolinker.parse = function( textOrHtml, options ) {
240
241
*
241
242
* Ex: 0.25.1
242
243
*/
243
- Autolinker . version = '1.5 .0' ;
244
+ Autolinker . version = '1.6 .0' ;
244
245
245
246
246
247
Autolinker . prototype = {
@@ -371,6 +372,16 @@ Autolinker.prototype = {
371
372
* `http://google.com`.
372
373
*/
373
374
375
+ /**
376
+ * @cfg {Boolean} [decodePercentEncoding=true]
377
+ *
378
+ * `true` to decode percent-encoded characters in URL matches, `false` to keep
379
+ * the percent-encoded characters.
380
+ *
381
+ * Example when `true`: `https://en.wikipedia.org/wiki/San_Jos%C3%A9` will
382
+ * be displayed as `https://en.wikipedia.org/wiki/San_José`.
383
+ */
384
+
374
385
/**
375
386
* @cfg {Number/Object} [truncate=0]
376
387
*
@@ -870,7 +881,7 @@ Autolinker.prototype = {
870
881
new matchersNs . Email ( { tagBuilder : tagBuilder } ) ,
871
882
new matchersNs . Phone ( { tagBuilder : tagBuilder } ) ,
872
883
new matchersNs . Mention ( { tagBuilder : tagBuilder , serviceName : this . mention } ) ,
873
- new matchersNs . Url ( { tagBuilder : tagBuilder , stripPrefix : this . stripPrefix , stripTrailingSlash : this . stripTrailingSlash } )
884
+ new matchersNs . Url ( { tagBuilder : tagBuilder , stripPrefix : this . stripPrefix , stripTrailingSlash : this . stripTrailingSlash , decodePercentEncoding : this . decodePercentEncoding } )
874
885
] ;
875
886
876
887
return ( this . matchers = matchers ) ;
@@ -2935,6 +2946,10 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
2935
2946
* @inheritdoc Autolinker#cfg-stripTrailingSlash
2936
2947
*/
2937
2948
2949
+ /**
2950
+ * @cfg {Boolean} decodePercentEncoding (required)
2951
+ * @inheritdoc Autolinker#cfg-decodePercentEncoding
2952
+ */
2938
2953
2939
2954
/**
2940
2955
* @constructor
@@ -2950,13 +2965,15 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
2950
2965
if ( cfg . protocolRelativeMatch == null ) throw new Error ( '`protocolRelativeMatch` cfg required' ) ;
2951
2966
if ( cfg . stripPrefix == null ) throw new Error ( '`stripPrefix` cfg required' ) ;
2952
2967
if ( cfg . stripTrailingSlash == null ) throw new Error ( '`stripTrailingSlash` cfg required' ) ;
2968
+ if ( cfg . decodePercentEncoding == null ) throw new Error ( '`decodePercentEncoding` cfg required' ) ;
2953
2969
2954
2970
this . urlMatchType = cfg . urlMatchType ;
2955
2971
this . url = cfg . url ;
2956
2972
this . protocolUrlMatch = cfg . protocolUrlMatch ;
2957
2973
this . protocolRelativeMatch = cfg . protocolRelativeMatch ;
2958
2974
this . stripPrefix = cfg . stripPrefix ;
2959
2975
this . stripTrailingSlash = cfg . stripTrailingSlash ;
2976
+ this . decodePercentEncoding = cfg . decodePercentEncoding ;
2960
2977
} ,
2961
2978
2962
2979
@@ -3075,6 +3092,9 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
3075
3092
if ( this . stripTrailingSlash ) {
3076
3093
anchorText = this . removeTrailingSlash ( anchorText ) ; // remove trailing slash, if there is one
3077
3094
}
3095
+ if ( this . decodePercentEncoding ) {
3096
+ anchorText = this . removePercentEncoding ( anchorText ) ;
3097
+ }
3078
3098
3079
3099
return anchorText ;
3080
3100
} ,
@@ -3137,6 +3157,28 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
3137
3157
anchorText = anchorText . slice ( 0 , - 1 ) ;
3138
3158
}
3139
3159
return anchorText ;
3160
+ } ,
3161
+
3162
+ /**
3163
+ * Decodes percent-encoded characters from the given `anchorText`, in preparation for the text to be displayed.
3164
+ *
3165
+ * @private
3166
+ * @param {String } anchorText The text of the anchor that is being generated, for which to decode any percent-encoded characters.
3167
+ * @return {String } The `anchorText`, with the percent-encoded characters decoded.
3168
+ */
3169
+ removePercentEncoding : function ( anchorText ) {
3170
+ try {
3171
+ return decodeURIComponent ( anchorText
3172
+ . replace ( / % 2 2 / gi, '"' )
3173
+ . replace ( / % 2 6 / gi, '&' )
3174
+ . replace ( / % 2 7 / gi, ''' )
3175
+ . replace ( / % 3 C / gi, '<' )
3176
+ . replace ( / % 3 E / gi, '>' )
3177
+ ) ;
3178
+ } catch ( e ) {
3179
+ // Invalid escape sequence.
3180
+ return anchorText ;
3181
+ }
3140
3182
}
3141
3183
3142
3184
} ) ;
@@ -3511,6 +3553,11 @@ Autolinker.matcher.Url = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
3511
3553
* @inheritdoc Autolinker#stripTrailingSlash
3512
3554
*/
3513
3555
3556
+ /**
3557
+ * @cfg {Boolean} decodePercentEncoding (required)
3558
+ * @inheritdoc Autolinker#decodePercentEncoding
3559
+ */
3560
+
3514
3561
3515
3562
/**
3516
3563
* @private
@@ -3644,6 +3691,7 @@ Autolinker.matcher.Url = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
3644
3691
3645
3692
this . stripPrefix = cfg . stripPrefix ;
3646
3693
this . stripTrailingSlash = cfg . stripTrailingSlash ;
3694
+ this . decodePercentEncoding = cfg . decodePercentEncoding ;
3647
3695
} ,
3648
3696
3649
3697
@@ -3654,6 +3702,7 @@ Autolinker.matcher.Url = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
3654
3702
var matcherRegex = this . matcherRegex ,
3655
3703
stripPrefix = this . stripPrefix ,
3656
3704
stripTrailingSlash = this . stripTrailingSlash ,
3705
+ decodePercentEncoding = this . decodePercentEncoding ,
3657
3706
tagBuilder = this . tagBuilder ,
3658
3707
matches = [ ] ,
3659
3708
match ;
@@ -3716,7 +3765,8 @@ Autolinker.matcher.Url = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
3716
3765
protocolUrlMatch : protocolUrlMatch ,
3717
3766
protocolRelativeMatch : ! ! protocolRelativeMatch ,
3718
3767
stripPrefix : stripPrefix ,
3719
- stripTrailingSlash : stripTrailingSlash
3768
+ stripTrailingSlash : stripTrailingSlash ,
3769
+ decodePercentEncoding : decodePercentEncoding ,
3720
3770
} ) ) ;
3721
3771
}
3722
3772
0 commit comments