Skip to content

Commit

Permalink
fix for URLs preceded with text are not linked correctly gregjacobs#159
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Cole committed Jun 7, 2019
1 parent 10267f3 commit 8acb9fe
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 10 deletions.
33 changes: 26 additions & 7 deletions dist/Autolinker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/Autolinker.js.map

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion dist/Autolinker.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/Autolinker.min.js.map

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/matcher/url-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,25 @@ export class UrlMatcher extends Matcher {
}
}

// The autolinker accepts many characters in a url's scheme (like `fake://test.com`).
// However, in cases where a URL is missing whitespace before an obvious link,
// (for example: `nowhitespacehttp://www.test.com`), we only want the match to start
// at the http:// part. We will check if the match contains a common scheme and then
// shift the match to start from there.
const foundCommonScheme = [ 'http://', 'https://' ].find(
(commonScheme) => !!schemeUrlMatch && schemeUrlMatch.indexOf( commonScheme ) !== -1
);
if ( foundCommonScheme ) {
// If we found an overmatched URL, we want to find the index
// of where the match should start and shift the match to
// start from the beginning of the common scheme
const indexOfSchemeStart = matchStr.indexOf( foundCommonScheme );

matchStr = matchStr.substr( indexOfSchemeStart );
schemeUrlMatch = schemeUrlMatch.substr( indexOfSchemeStart );
offset = offset + indexOfSchemeStart;
}

let urlMatchType: UrlMatchTypeOptions = schemeUrlMatch ? 'scheme' : ( wwwUrlMatch ? 'www' : 'tld' ),
protocolUrlMatch = !!schemeUrlMatch;

Expand Down
7 changes: 7 additions & 0 deletions tests/autolinker-url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ describe( "Autolinker Url Matching -", () => {
expect( result5 ).toBe( 'do not link first char: .<a href="aa://example">aa://example</a>' );
} );

it( "should autolink protocol starting at http:// or http:// if URL is preceded with text", function() {
let result1 = autolinker.link( 'link this: xxxhttp://example.com' );
expect( result1 ).toBe( 'link this: xxx<a href="http://example.com">example.com</a>' );

let result2 = autolinker.link( 'link this: abchttps://www.example.com' );
expect( result2 ).toBe( 'link this: abc<a href="https://www.example.com">example.com</a>' );
} );

it( "should NOT autolink possible URLs with the 'javascript:' URI scheme", function() {
let result = autolinker.link( "do not link javascript:window.alert('hi') please" );
Expand Down
12 changes: 12 additions & 0 deletions tests/matcher/url-matcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ describe( "Autolinker.matcher.Url", function() {
expect( Date.now() - start ).toBeLessThan( 100 );
} );

it( 'if scheme contains a common protocol, should only begin match from start of common protocol', function() {
let matches1 = matcher.parseMatches( 'text with a typohttp://www.example.com mid-sentence' );

expect( matches1.length ).toBe( 1 );
MatchChecker.expectUrlMatch( matches1[ 0 ], 'http://www.example.com', 16 );

let matches2 = matcher.parseMatches( 'text with a typohttps://www.example.com mid-sentence' );

expect( matches2.length ).toBe( 1 );
MatchChecker.expectUrlMatch( matches2[ 0 ], 'https://www.example.com', 16 );
} );

} );

} );
Expand Down

0 comments on commit 8acb9fe

Please sign in to comment.