Skip to content

Commit

Permalink
Add title to annchor when a truncate is done
Browse files Browse the repository at this point in the history
  • Loading branch information
olafleur committed Nov 22, 2016
1 parent c269a8b commit 0a79897
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/AnchorTagBuilder.js
Expand Up @@ -97,6 +97,12 @@ Autolinker.AnchorTagBuilder = Autolinker.Util.extend( Object, {
attrs[ 'rel' ] = "noopener noreferrer";
}

if( this.truncate ) {
if( this.truncate.length && this.truncate.length < match.getAnchorText().length ) {
attrs[ 'title' ] = match.getAnchorHref();
}
}

return attrs;
},

Expand Down
1 change: 1 addition & 0 deletions src/truncate/TruncateMiddle.js
Expand Up @@ -16,6 +16,7 @@ Autolinker.truncate.TruncateMiddle = function(url, truncateLen, ellipsisChars){
}

var ellipsisLengthBeforeParsing;
var ellipsisLength;

if(ellipsisChars == null) {
ellipsisChars = '&hellip;';
Expand Down
1 change: 1 addition & 0 deletions src/truncate/TruncateSmart.js
Expand Up @@ -14,6 +14,7 @@
Autolinker.truncate.TruncateSmart = function(url, truncateLen, ellipsisChars){

var ellipsisLengthBeforeParsing;
var ellipsisLength;

if(ellipsisChars == null) {
ellipsisChars = '&hellip;';
Expand Down
12 changes: 6 additions & 6 deletions tests/AutolinkerSpec.js
Expand Up @@ -7,7 +7,7 @@ describe( "Autolinker", function() {
var autolinker = new Autolinker( { newWindow: false, truncate: 25 } );

var result = autolinker.link( "Check out http://www.yahoo.com/some/long/path/to/a/file" );
expect( result ).toBe( 'Check out <a href="http://www.yahoo.com/some/long/path/to/a/file">yahoo.com/some/long/pa&hellip;</a>' );
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>' );
} );

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

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


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

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


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

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


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


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

} );
Expand Down

0 comments on commit 0a79897

Please sign in to comment.