Skip to content

Commit

Permalink
Merge pull request gregjacobs#273 from adamdavidcole/match_mailto_pre…
Browse files Browse the repository at this point in the history
…fix_for_email_urls

Match mailto prefix for email urls
  • Loading branch information
gregjacobs authored Jun 5, 2019
2 parents acdf9a5 + 96f86a9 commit 10267f3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
23 changes: 19 additions & 4 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.

2 changes: 1 addition & 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.

25 changes: 22 additions & 3 deletions src/matcher/email-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ export class EmailMatcher extends Matcher {
*/
protected localPartCharRegex = new RegExp( `[${alphaNumericAndMarksCharsStr}!#$%&'*+/=?^_\`{|}~-]` );

/**
* Valid URI scheme for email address URLs
*/
protected mailToScheme : string = 'mailto:';


/**
* @inheritdoc
*/
parseMatches( text: string ) {
const tagBuilder = this.tagBuilder,
localPartCharRegex = this.localPartCharRegex,
mailToScheme = this.mailToScheme,
matches: Match[] = [],
len = text.length,
noCurrentEmailAddress = new CurrentEmailAddress();
Expand Down Expand Up @@ -215,7 +221,8 @@ export class EmailMatcher extends Matcher {
*/
function captureMatchIfValidAndReset() {
if( currentEmailAddress.hasDomainDot ) { // we need at least one dot in the domain to be considered a valid email address
let emailAddress = text.slice( currentEmailAddress.idx, charIdx );
let offset = currentEmailAddress.idx;
let emailAddress = text.slice( offset, charIdx );

// If we read a '.' or '-' char that ended the email address
// (valid domain name characters, but only valid email address
Expand All @@ -225,10 +232,22 @@ export class EmailMatcher extends Matcher {
emailAddress = emailAddress.slice( 0, -1 );
}

let matchedText = emailAddress;

// get the characters immediately preceding the email match
const potentialMailToSchemeOffset = offset - mailToScheme.length
const potentialMailToScheme = text.slice( potentialMailToSchemeOffset, offset );
if ( potentialMailToScheme === mailToScheme ) {
// if the email match is preceded by the 'mailTo:' scheme,
// include those characters in the matched text
offset = potentialMailToSchemeOffset;
matchedText = text.slice( offset, charIdx );
}

matches.push( new EmailMatch( {
tagBuilder : tagBuilder,
matchedText : emailAddress,
offset : currentEmailAddress.idx,
matchedText : matchedText,
offset : offset,
email : emailAddress
} ) );
}
Expand Down
7 changes: 7 additions & 0 deletions tests/matcher/email-matcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ describe( "Autolinker.matcher.Email", () => {
MatchChecker.expectEmailMatch( matches[ 0 ], 'asdf@asdf.com', 6 );
} );

it( 'should match mailto: scheme prefix', () => {
var matches = matcher.parseMatches( 'hello mailto:asdf@asdf.com there' );

expect( matches.length ).toBe( 1 );
MatchChecker.expectEmailMatch( matches[ 0 ], 'asdf@asdf.com', 6 );
} );

} );


Expand Down

0 comments on commit 10267f3

Please sign in to comment.