11/*!
22 * Autolinker.js
3- * 1.5 .0
3+ * 1.6 .0
44 *
55 * Copyright(c) 2017 Gregory Jacobs <[email protected] > 66 * MIT License
@@ -139,6 +139,7 @@ var Autolinker = function( cfg ) {
139139 this . newWindow = typeof cfg . newWindow === 'boolean' ? cfg . newWindow : true ;
140140 this . stripPrefix = this . normalizeStripPrefixCfg ( cfg . stripPrefix ) ;
141141 this . stripTrailingSlash = typeof cfg . stripTrailingSlash === 'boolean' ? cfg . stripTrailingSlash : true ;
142+ this . decodePercentEncoding = typeof cfg . decodePercentEncoding === 'boolean' ? cfg . decodePercentEncoding : true ;
142143
143144 // Validate the value of the `mention` cfg
144145 var mention = this . mention ;
@@ -240,7 +241,7 @@ Autolinker.parse = function( textOrHtml, options ) {
240241 *
241242 * Ex: 0.25.1
242243 */
243- Autolinker . version = '1.5 .0' ;
244+ Autolinker . version = '1.6 .0' ;
244245
245246
246247Autolinker . prototype = {
@@ -371,6 +372,16 @@ Autolinker.prototype = {
371372 * `http://google.com`.
372373 */
373374
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+
374385 /**
375386 * @cfg {Number/Object} [truncate=0]
376387 *
@@ -870,7 +881,7 @@ Autolinker.prototype = {
870881 new matchersNs . Email ( { tagBuilder : tagBuilder } ) ,
871882 new matchersNs . Phone ( { tagBuilder : tagBuilder } ) ,
872883 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 } )
874885 ] ;
875886
876887 return ( this . matchers = matchers ) ;
@@ -2935,6 +2946,10 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
29352946 * @inheritdoc Autolinker#cfg-stripTrailingSlash
29362947 */
29372948
2949+ /**
2950+ * @cfg {Boolean} decodePercentEncoding (required)
2951+ * @inheritdoc Autolinker#cfg-decodePercentEncoding
2952+ */
29382953
29392954 /**
29402955 * @constructor
@@ -2950,13 +2965,15 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
29502965 if ( cfg . protocolRelativeMatch == null ) throw new Error ( '`protocolRelativeMatch` cfg required' ) ;
29512966 if ( cfg . stripPrefix == null ) throw new Error ( '`stripPrefix` cfg required' ) ;
29522967 if ( cfg . stripTrailingSlash == null ) throw new Error ( '`stripTrailingSlash` cfg required' ) ;
2968+ if ( cfg . decodePercentEncoding == null ) throw new Error ( '`decodePercentEncoding` cfg required' ) ;
29532969
29542970 this . urlMatchType = cfg . urlMatchType ;
29552971 this . url = cfg . url ;
29562972 this . protocolUrlMatch = cfg . protocolUrlMatch ;
29572973 this . protocolRelativeMatch = cfg . protocolRelativeMatch ;
29582974 this . stripPrefix = cfg . stripPrefix ;
29592975 this . stripTrailingSlash = cfg . stripTrailingSlash ;
2976+ this . decodePercentEncoding = cfg . decodePercentEncoding ;
29602977 } ,
29612978
29622979
@@ -3075,6 +3092,9 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
30753092 if ( this . stripTrailingSlash ) {
30763093 anchorText = this . removeTrailingSlash ( anchorText ) ; // remove trailing slash, if there is one
30773094 }
3095+ if ( this . decodePercentEncoding ) {
3096+ anchorText = this . removePercentEncoding ( anchorText ) ;
3097+ }
30783098
30793099 return anchorText ;
30803100 } ,
@@ -3137,6 +3157,28 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
31373157 anchorText = anchorText . slice ( 0 , - 1 ) ;
31383158 }
31393159 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+ }
31403182 }
31413183
31423184} ) ;
@@ -3511,6 +3553,11 @@ Autolinker.matcher.Url = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
35113553 * @inheritdoc Autolinker#stripTrailingSlash
35123554 */
35133555
3556+ /**
3557+ * @cfg {Boolean} decodePercentEncoding (required)
3558+ * @inheritdoc Autolinker#decodePercentEncoding
3559+ */
3560+
35143561
35153562 /**
35163563 * @private
@@ -3644,6 +3691,7 @@ Autolinker.matcher.Url = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
36443691
36453692 this . stripPrefix = cfg . stripPrefix ;
36463693 this . stripTrailingSlash = cfg . stripTrailingSlash ;
3694+ this . decodePercentEncoding = cfg . decodePercentEncoding ;
36473695 } ,
36483696
36493697
@@ -3654,6 +3702,7 @@ Autolinker.matcher.Url = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
36543702 var matcherRegex = this . matcherRegex ,
36553703 stripPrefix = this . stripPrefix ,
36563704 stripTrailingSlash = this . stripTrailingSlash ,
3705+ decodePercentEncoding = this . decodePercentEncoding ,
36573706 tagBuilder = this . tagBuilder ,
36583707 matches = [ ] ,
36593708 match ;
@@ -3716,7 +3765,8 @@ Autolinker.matcher.Url = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
37163765 protocolUrlMatch : protocolUrlMatch ,
37173766 protocolRelativeMatch : ! ! protocolRelativeMatch ,
37183767 stripPrefix : stripPrefix ,
3719- stripTrailingSlash : stripTrailingSlash
3768+ stripTrailingSlash : stripTrailingSlash ,
3769+ decodePercentEncoding : decodePercentEncoding ,
37203770 } ) ) ;
37213771 }
37223772
0 commit comments