Skip to content

Commit 10267f3

Browse files
authored
Merge pull request #273 from adamdavidcole/match_mailto_prefix_for_email_urls
Match mailto prefix for email urls
2 parents acdf9a5 + 96f86a9 commit 10267f3

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

dist/Autolinker.js

Lines changed: 19 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/Autolinker.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/Autolinker.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/Autolinker.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/matcher/email-matcher.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ export class EmailMatcher extends Matcher {
2323
*/
2424
protected localPartCharRegex = new RegExp( `[${alphaNumericAndMarksCharsStr}!#$%&'*+/=?^_\`{|}~-]` );
2525

26+
/**
27+
* Valid URI scheme for email address URLs
28+
*/
29+
protected mailToScheme : string = 'mailto:';
30+
2631

2732
/**
2833
* @inheritdoc
2934
*/
3035
parseMatches( text: string ) {
3136
const tagBuilder = this.tagBuilder,
3237
localPartCharRegex = this.localPartCharRegex,
38+
mailToScheme = this.mailToScheme,
3339
matches: Match[] = [],
3440
len = text.length,
3541
noCurrentEmailAddress = new CurrentEmailAddress();
@@ -215,7 +221,8 @@ export class EmailMatcher extends Matcher {
215221
*/
216222
function captureMatchIfValidAndReset() {
217223
if( currentEmailAddress.hasDomainDot ) { // we need at least one dot in the domain to be considered a valid email address
218-
let emailAddress = text.slice( currentEmailAddress.idx, charIdx );
224+
let offset = currentEmailAddress.idx;
225+
let emailAddress = text.slice( offset, charIdx );
219226

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

235+
let matchedText = emailAddress;
236+
237+
// get the characters immediately preceding the email match
238+
const potentialMailToSchemeOffset = offset - mailToScheme.length
239+
const potentialMailToScheme = text.slice( potentialMailToSchemeOffset, offset );
240+
if ( potentialMailToScheme === mailToScheme ) {
241+
// if the email match is preceded by the 'mailTo:' scheme,
242+
// include those characters in the matched text
243+
offset = potentialMailToSchemeOffset;
244+
matchedText = text.slice( offset, charIdx );
245+
}
246+
228247
matches.push( new EmailMatch( {
229248
tagBuilder : tagBuilder,
230-
matchedText : emailAddress,
231-
offset : currentEmailAddress.idx,
249+
matchedText : matchedText,
250+
offset : offset,
232251
email : emailAddress
233252
} ) );
234253
}

tests/matcher/email-matcher.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ describe( "Autolinker.matcher.Email", () => {
242242
MatchChecker.expectEmailMatch( matches[ 0 ], 'asdf@asdf.com', 6 );
243243
} );
244244

245+
it( 'should match mailto: scheme prefix', () => {
246+
var matches = matcher.parseMatches( 'hello mailto:asdf@asdf.com there' );
247+
248+
expect( matches.length ).toBe( 1 );
249+
MatchChecker.expectEmailMatch( matches[ 0 ], 'asdf@asdf.com', 6 );
250+
} );
251+
245252
} );
246253

247254

0 commit comments

Comments
 (0)