Skip to content

Commit 2f0e438

Browse files
committed
Keep the original URL text when the stripPrefix option is false
Previously, `http://` was prepended
1 parent 2679fd8 commit 2f0e438

File tree

2 files changed

+60
-45
lines changed

2 files changed

+60
-45
lines changed

src/match/Url.js

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,116 +2,116 @@
22
/**
33
* @class Autolinker.match.Url
44
* @extends Autolinker.match.Match
5-
*
5+
*
66
* Represents a Url match found in an input string which should be Autolinked.
7-
*
7+
*
88
* See this class's superclass ({@link Autolinker.match.Match}) for more details.
99
*/
1010
Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
11-
11+
1212
/**
1313
* @cfg {String} url (required)
14-
*
14+
*
1515
* The url that was matched.
1616
*/
17-
17+
1818
/**
1919
* @cfg {Boolean} protocolUrlMatch (required)
20-
*
20+
*
2121
* `true` if the URL is a match which already has a protocol (i.e. 'http://'), `false` if the match was from a 'www' or
2222
* known TLD match.
2323
*/
24-
24+
2525
/**
2626
* @cfg {Boolean} protocolRelativeMatch (required)
27-
*
27+
*
2828
* `true` if the URL is a protocol-relative match. A protocol-relative match is a URL that starts with '//',
2929
* and will be either http:// or https:// based on the protocol that the site is loaded under.
3030
*/
31-
31+
3232
/**
3333
* @cfg {Boolean} stripPrefix (required)
3434
* @inheritdoc Autolinker#stripPrefix
3535
*/
36-
36+
3737

3838
/**
3939
* @private
4040
* @property {RegExp} urlPrefixRegex
41-
*
41+
*
4242
* A regular expression used to remove the 'http://' or 'https://' and/or the 'www.' from URLs.
4343
*/
4444
urlPrefixRegex: /^(https?:\/\/)?(www\.)?/i,
45-
45+
4646
/**
4747
* @private
4848
* @property {RegExp} protocolRelativeRegex
49-
*
49+
*
5050
* The regular expression used to remove the protocol-relative '//' from the {@link #url} string, for purposes
5151
* of {@link #getAnchorText}. A protocol-relative URL is, for example, "//yahoo.com"
5252
*/
5353
protocolRelativeRegex : /^\/\//,
54-
54+
5555
/**
5656
* @private
5757
* @property {Boolean} protocolPrepended
58-
*
58+
*
5959
* Will be set to `true` if the 'http://' protocol has been prepended to the {@link #url} (because the
6060
* {@link #url} did not have a protocol)
6161
*/
6262
protocolPrepended : false,
63-
63+
6464

6565
/**
6666
* Returns a string name for the type of match that this class represents.
67-
*
67+
*
6868
* @return {String}
6969
*/
7070
getType : function() {
7171
return 'url';
7272
},
73-
74-
73+
74+
7575
/**
7676
* Returns the url that was matched, assuming the protocol to be 'http://' if the original
7777
* match was missing a protocol.
78-
*
78+
*
7979
* @return {String}
8080
*/
8181
getUrl : function() {
8282
var url = this.url;
83-
83+
8484
// if the url string doesn't begin with a protocol, assume 'http://'
8585
if( !this.protocolRelativeMatch && !this.protocolUrlMatch && !this.protocolPrepended ) {
8686
url = this.url = 'http://' + url;
87-
87+
8888
this.protocolPrepended = true;
8989
}
90-
90+
9191
return url;
9292
},
93-
93+
9494

9595
/**
9696
* Returns the anchor href that should be generated for the match.
97-
*
97+
*
9898
* @return {String}
9999
*/
100100
getAnchorHref : function() {
101101
var url = this.getUrl();
102-
103-
return url.replace( /&/g, '&' ); // any &'s in the URL should be converted back to '&' if they were displayed as & in the source html
102+
103+
return url.replace( /&/g, '&' ); // any &'s in the URL should be converted back to '&' if they were displayed as & in the source html
104104
},
105-
106-
105+
106+
107107
/**
108108
* Returns the anchor text that should be generated for the match.
109-
*
109+
*
110110
* @return {String}
111111
*/
112112
getAnchorText : function() {
113-
var anchorText = this.getUrl();
114-
113+
var anchorText = this.getMatchedText();
114+
115115
if( this.protocolRelativeMatch ) {
116116
// Strip off any protocol-relative '//' from the anchor text
117117
anchorText = this.stripProtocolRelativePrefix( anchorText );
@@ -120,18 +120,18 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
120120
anchorText = this.stripUrlPrefix( anchorText );
121121
}
122122
anchorText = this.removeTrailingSlash( anchorText ); // remove trailing slash, if there is one
123-
123+
124124
return anchorText;
125125
},
126-
127-
126+
127+
128128
// ---------------------------------------
129-
129+
130130
// Utility Functionality
131-
131+
132132
/**
133133
* Strips the URL prefix (such as "http://" or "https://") from the given text.
134-
*
134+
*
135135
* @private
136136
* @param {String} text The text of the anchor that is being generated, for which to strip off the
137137
* url prefix (such as stripping off "http://")
@@ -140,11 +140,11 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
140140
stripUrlPrefix : function( text ) {
141141
return text.replace( this.urlPrefixRegex, '' );
142142
},
143-
144-
143+
144+
145145
/**
146146
* Strips any protocol-relative '//' from the anchor text.
147-
*
147+
*
148148
* @private
149149
* @param {String} text The text of the anchor that is being generated, for which to strip off the
150150
* protocol-relative prefix (such as stripping off "//")
@@ -153,11 +153,11 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
153153
stripProtocolRelativePrefix : function( text ) {
154154
return text.replace( this.protocolRelativeRegex, '' );
155155
},
156-
157-
156+
157+
158158
/**
159159
* Removes any trailing slash from the given `anchorText`, in preparation for the text to be displayed.
160-
*
160+
*
161161
* @private
162162
* @param {String} anchorText The text of the anchor that is being generated, for which to remove any trailing
163163
* slash ('/') that may exist.
@@ -169,5 +169,5 @@ Autolinker.match.Url = Autolinker.Util.extend( Autolinker.match.Match, {
169169
}
170170
return anchorText;
171171
}
172-
172+
173173
} );

tests/AutolinkerSpec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,21 @@ describe( "Autolinker", function() {
13221322
} );
13231323

13241324

1325+
it( 'should leave the original text as-is when the `stripPrefix` option is `false`', function() {
1326+
var result1 = Autolinker.link( 'My url.com', { stripPrefix: false, newWindow: false } );
1327+
expect( result1 ).toBe( 'My <a href="http://url.com">url.com</a>' );
1328+
1329+
var result2 = Autolinker.link( 'My www.url.com', { stripPrefix: false, newWindow: false } );
1330+
expect( result2 ).toBe( 'My <a href="http://www.url.com">www.url.com</a>' );
1331+
1332+
var result3 = Autolinker.link( 'My http://url.com', { stripPrefix: false, newWindow: false } );
1333+
expect( result3 ).toBe( 'My <a href="http://url.com">http://url.com</a>' );
1334+
1335+
var result4 = Autolinker.link( 'My http://www.url.com', { stripPrefix: false, newWindow: false } );
1336+
expect( result4 ).toBe( 'My <a href="http://www.url.com">http://www.url.com</a>' );
1337+
} );
1338+
1339+
13251340
it( "should remove the prefix by default", function() {
13261341
var result = Autolinker.link( "Test http://www.url.com", { newWindow: false } );
13271342
expect( result ).toBe( 'Test <a href="http://www.url.com">url.com</a>' );

0 commit comments

Comments
 (0)