Skip to content

Commit 3b64505

Browse files
authored
Merge pull request #185 from gregjacobs/fix-truncate
Modify truncate to use … character and add the title property.
2 parents fab659e + 0a79897 commit 3b64505

File tree

5 files changed

+63
-23
lines changed

5 files changed

+63
-23
lines changed

src/AnchorTagBuilder.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ Autolinker.AnchorTagBuilder = Autolinker.Util.extend( Object, {
9797
attrs[ 'rel' ] = "noopener noreferrer";
9898
}
9999

100+
if( this.truncate ) {
101+
if( this.truncate.length && this.truncate.length < match.getAnchorText().length ) {
102+
attrs[ 'title' ] = match.getAnchorHref();
103+
}
104+
}
105+
100106
return attrs;
101107
},
102108

@@ -174,13 +180,13 @@ Autolinker.AnchorTagBuilder = Autolinker.Util.extend( Object, {
174180
truncateLocation = truncate.location;
175181

176182
if( truncateLocation === 'smart' ) {
177-
return Autolinker.truncate.TruncateSmart( anchorText, truncateLength, '..' );
183+
return Autolinker.truncate.TruncateSmart( anchorText, truncateLength );
178184

179185
} else if( truncateLocation === 'middle' ) {
180-
return Autolinker.truncate.TruncateMiddle( anchorText, truncateLength, '..' );
186+
return Autolinker.truncate.TruncateMiddle( anchorText, truncateLength );
181187

182188
} else {
183-
return Autolinker.truncate.TruncateEnd( anchorText, truncateLength, '..' );
189+
return Autolinker.truncate.TruncateEnd( anchorText, truncateLength );
184190
}
185191
}
186192

src/Util.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,21 @@ Autolinker.Util = {
102102
*
103103
* @param {String} str The string to truncate and add an ellipsis to.
104104
* @param {Number} truncateLen The length to truncate the string at.
105-
* @param {String} [ellipsisChars=..] The ellipsis character(s) to add to the end of `str`
106-
* when truncated. Defaults to '..'
105+
* @param {String} [ellipsisChars=...] The ellipsis character(s) to add to the end of `str`
106+
* when truncated. Defaults to '...'
107107
*/
108108
ellipsis : function( str, truncateLen, ellipsisChars ) {
109+
var ellipsisLength;
110+
109111
if( str.length > truncateLen ) {
110-
ellipsisChars = ( ellipsisChars == null ) ? '..' : ellipsisChars;
111-
str = str.substring( 0, truncateLen - ellipsisChars.length ) + ellipsisChars;
112+
if(ellipsisChars == null) {
113+
ellipsisChars = '&hellip;';
114+
ellipsisLength = 3;
115+
} else {
116+
ellipsisLength = ellipsisChars.length;
117+
}
118+
119+
str = str.substring( 0, truncateLen - ellipsisLength ) + ellipsisChars;
112120
}
113121
return str;
114122
},
@@ -211,4 +219,4 @@ Autolinker.Util = {
211219
return str.replace( this.trimRegex, '' );
212220
}
213221

214-
};
222+
};

src/truncate/TruncateMiddle.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,23 @@ Autolinker.truncate.TruncateMiddle = function(url, truncateLen, ellipsisChars){
1414
if (url.length <= truncateLen) {
1515
return url;
1616
}
17-
var availableLength = truncateLen - ellipsisChars.length;
17+
18+
var ellipsisLengthBeforeParsing;
19+
var ellipsisLength;
20+
21+
if(ellipsisChars == null) {
22+
ellipsisChars = '&hellip;';
23+
ellipsisLengthBeforeParsing = 8;
24+
ellipsisLength = 3;
25+
} else {
26+
ellipsisLengthBeforeParsing = ellipsisChars.length;
27+
ellipsisLength = ellipsisChars.length;
28+
}
29+
30+
var availableLength = truncateLen - ellipsisLength;
1831
var end = "";
1932
if (availableLength > 0) {
2033
end = url.substr((-1)*Math.floor(availableLength/2));
2134
}
22-
return (url.substr(0, Math.ceil(availableLength/2)) + ellipsisChars + end).substr(0, truncateLen);
35+
return (url.substr(0, Math.ceil(availableLength/2)) + ellipsisChars + end).substr(0, availableLength + ellipsisLengthBeforeParsing);
2336
};

src/truncate/TruncateSmart.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,23 @@
88
*
99
* @param {String} url A URL.
1010
* @param {Number} truncateLen The maximum length of the truncated output URL string.
11-
* @param {String} ellipsisChars The characters to place within the url, e.g. "..".
11+
* @param {String} ellipsisChars The characters to place within the url, e.g. "...".
1212
* @return {String} The truncated URL.
1313
*/
1414
Autolinker.truncate.TruncateSmart = function(url, truncateLen, ellipsisChars){
15+
16+
var ellipsisLengthBeforeParsing;
17+
var ellipsisLength;
18+
19+
if(ellipsisChars == null) {
20+
ellipsisChars = '&hellip;';
21+
ellipsisLength = 3;
22+
ellipsisLengthBeforeParsing = 8;
23+
} else {
24+
ellipsisLength = ellipsisChars.length;
25+
ellipsisLengthBeforeParsing = ellipsisChars.length;
26+
}
27+
1528
var parse_url = function(url){ // Functionality inspired by PHP function of same name
1629
var urlObj = {};
1730
var urlSub = url;
@@ -76,7 +89,7 @@ Autolinker.truncate.TruncateSmart = function(url, truncateLen, ellipsisChars){
7689
if (url.length <= truncateLen) {
7790
return url;
7891
}
79-
var availableLength = truncateLen - ellipsisChars.length;
92+
var availableLength = truncateLen - ellipsisLength;
8093
var urlObj = parse_url(url);
8194
// Clean up the URL
8295
if (urlObj.query) {
@@ -104,9 +117,9 @@ Autolinker.truncate.TruncateSmart = function(url, truncateLen, ellipsisChars){
104117
}
105118
if (str.length >= availableLength) {
106119
if (urlObj.host.length == truncateLen) {
107-
return (urlObj.host.substr(0, (truncateLen - ellipsisChars.length)) + ellipsisChars).substr(0, truncateLen);
120+
return (urlObj.host.substr(0, (truncateLen - ellipsisLength)) + ellipsisChars).substr(0, availableLength + ellipsisLengthBeforeParsing);
108121
}
109-
return buildSegment(str, availableLength).substr(0, truncateLen);
122+
return buildSegment(str, availableLength).substr(0, availableLength + ellipsisLengthBeforeParsing);
110123
}
111124
var pathAndQuery = "";
112125
if (urlObj.path) {
@@ -121,7 +134,7 @@ Autolinker.truncate.TruncateSmart = function(url, truncateLen, ellipsisChars){
121134
return (str + pathAndQuery).substr(0, truncateLen);
122135
}
123136
var remainingAvailableLength = availableLength - str.length;
124-
return (str + buildSegment(pathAndQuery, remainingAvailableLength)).substr(0, truncateLen);
137+
return (str + buildSegment(pathAndQuery, remainingAvailableLength)).substr(0, availableLength + ellipsisLengthBeforeParsing);
125138
} else {
126139
str += pathAndQuery;
127140
}
@@ -133,7 +146,7 @@ Autolinker.truncate.TruncateSmart = function(url, truncateLen, ellipsisChars){
133146
return (str + fragment).substr(0, truncateLen);
134147
}
135148
var remainingAvailableLength2 = availableLength - str.length;
136-
return (str + buildSegment(fragment, remainingAvailableLength2)).substr(0, truncateLen);
149+
return (str + buildSegment(fragment, remainingAvailableLength2)).substr(0, availableLength + ellipsisLengthBeforeParsing);
137150
} else {
138151
str += fragment;
139152
}
@@ -151,5 +164,5 @@ Autolinker.truncate.TruncateSmart = function(url, truncateLen, ellipsisChars){
151164
if (availableLength > 0) {
152165
end = str.substr((-1)*Math.floor(availableLength/2));
153166
}
154-
return (str.substr(0, Math.ceil(availableLength/2)) + ellipsisChars + end).substr(0, truncateLen);
167+
return (str.substr(0, Math.ceil(availableLength/2)) + ellipsisChars + end).substr(0, availableLength + ellipsisLengthBeforeParsing);
155168
};

tests/AutolinkerSpec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe( "Autolinker", function() {
77
var autolinker = new Autolinker( { newWindow: false, truncate: 25 } );
88

99
var result = autolinker.link( "Check out http://www.yahoo.com/some/long/path/to/a/file" );
10-
expect( result ).toBe( 'Check out <a href="http://www.yahoo.com/some/long/path/to/a/file">yahoo.com/some/long/pat..</a>' );
10+
expect( result ).toBe( 'Check out <a href="http://www.yahoo.com/some/long/path/to/a/file" title="http://www.yahoo.com/some/long/path/to/a/file">yahoo.com/some/long/pa&hellip;</a>' );
1111
} );
1212

1313
} );
@@ -1886,7 +1886,7 @@ describe( "Autolinker", function() {
18861886
it( "should truncate long a url/email/twitter to the given number of characters with the 'truncate' option specified", function() {
18871887
var result = Autolinker.link( "Test http://url.com/with/path", { truncate: 12, newWindow: false } );
18881888

1889-
expect( result ).toBe( 'Test <a href="http://url.com/with/path">url.com/wi..</a>' );
1889+
expect( result ).toBe( 'Test <a href="http://url.com/with/path" title="http://url.com/with/path">url.com/w&hellip;</a>' );
18901890
} );
18911891

18921892

@@ -1925,26 +1925,26 @@ describe( "Autolinker", function() {
19251925
it( 'should default the `location` to "end" if it is not provided', function() {
19261926
var result = Autolinker.link( "Test http://url.com/with/path", { truncate: { length: 12 }, newWindow: false } );
19271927

1928-
expect( result ).toBe( 'Test <a href="http://url.com/with/path">url.com/wi..</a>' );
1928+
expect( result ).toBe( 'Test <a href="http://url.com/with/path" title="http://url.com/with/path">url.com/w&hellip;</a>' );
19291929
} );
19301930

19311931

19321932
it( 'should truncate at the end when `location: "end"` is specified', function() {
19331933
var result = Autolinker.link( "Test http://url.com/with/path", { truncate: { length: 12, location: 'end' }, newWindow: false } );
19341934

1935-
expect( result ).toBe( 'Test <a href="http://url.com/with/path">url.com/wi..</a>' );
1935+
expect( result ).toBe( 'Test <a href="http://url.com/with/path" title="http://url.com/with/path">url.com/w&hellip;</a>' );
19361936
} );
19371937

19381938

19391939
it( 'should truncate in the middle when `location: "middle"` is specified', function() {
19401940
var result = Autolinker.link( "Test http://url.com/with/path", { truncate: { length: 12, location: 'middle' }, newWindow: false } );
1941-
expect( result ).toBe( 'Test <a href="http://url.com/with/path">url.c../path</a>' );
1941+
expect( result ).toBe( 'Test <a href="http://url.com/with/path" title="http://url.com/with/path">url.c&hellip;path</a>' );
19421942
} );
19431943

19441944

19451945
it( 'should truncate according to the "smart" truncation rules when `location: "smart"` is specified', function() {
19461946
var result = Autolinker.link( "Test http://url.com/with/path", { truncate: { length: 12, location: 'smart' }, newWindow: false } );
1947-
expect( result ).toBe( 'Test <a href="http://url.com/with/path">url.com/w..h</a>' );
1947+
expect( result ).toBe( 'Test <a href="http://url.com/with/path" title="http://url.com/with/path">url.com/&hellip;h</a>' );
19481948
} );
19491949

19501950
} );

0 commit comments

Comments
 (0)