Skip to content

Commit 3c47de6

Browse files
authored
Merge pull request #217 from viman/master
Change domain regex to not match domains with leading - or . characters
2 parents f2a3c00 + 57d8330 commit 3c47de6

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/RegexLib.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@ Autolinker.RegexLib = (function() {
4848
// See documentation below
4949
var alphaNumericCharsStr = alphaCharsStr + decimalNumbersStr;
5050

51+
// Simplified IP regular expression
52+
var ipRegex = new RegExp( '(?:[' + decimalNumbersStr + ']{1,3}\\.){3}[' + decimalNumbersStr + ']{1,3}' );
53+
54+
// Protected domain label which do not allow "-" character on the beginning and the end of a single label
55+
var domainLabelStr = '[' + alphaNumericCharsStr + '](?:[' + alphaNumericCharsStr + '\\-]*[' + alphaNumericCharsStr + '])?';
5156

5257
// See documentation below
53-
var domainNameRegex = new RegExp( '[' + alphaNumericCharsStr + '.\\-]*[' + alphaNumericCharsStr + '\\-]' );
58+
var domainNameRegex = new RegExp( '(?:(?:(?:' + domainLabelStr + '\\.)*(?:' + domainLabelStr + '))|(?:' + ipRegex.source + '))' );
5459

5560
return {
5661

src/matcher/Email.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ Autolinker.matcher.Email = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
1919
*/
2020
matcherRegex : (function() {
2121
var alphaNumericChars = Autolinker.RegexLib.alphaNumericCharsStr,
22-
emailRegex = new RegExp( '[' + alphaNumericChars + '\\-_\';:&=+$.,]+@' ), // something@ for email addresses (a.k.a. local-part)
22+
specialCharacters = '!#$%&\'*+\\-\\/=?^_`{|}~',
23+
restrictedSpecialCharacters = '\\s"(),:;<>@\\[\\]',
24+
validCharacters = alphaNumericChars + specialCharacters,
25+
validRestrictedCharacters = validCharacters + restrictedSpecialCharacters,
26+
emailRegex = new RegExp( '(?:(?:[' + validCharacters + '](?![^@]*\\.\\.)(?:[' + validCharacters + '.]*[' + validCharacters + '])?)|(?:\\"[' + validRestrictedCharacters + '.]+\\"))@'),
2327
domainNameRegex = Autolinker.RegexLib.domainNameRegex,
2428
tldRegex = Autolinker.tldRegex; // match our known top level domains (TLDs)
2529

tests/matcher/EmailSpec.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,45 @@ describe( "Autolinker.matcher.Email", function() {
7676
MatchChecker.expectEmailMatch( matches[ 0 ], 'o\'donnel@asdf.com', 0 );
7777
} );
7878

79+
it( 'should *not* match email with incorrect domain beginning with "-"', function() {
80+
var matches = matcher.parseMatches( 'asdf@-asdf.com' );
81+
82+
expect( matches.length ).toBe( 0 );
83+
} );
84+
85+
it( 'should *not* match email with incorrect domain ending with "-"', function() {
86+
var matches = matcher.parseMatches( 'asdf@asdf-.com' );
87+
88+
expect( matches.length ).toBe( 0 );
89+
} );
90+
91+
it( 'should *not* match email with incorrect domain beginning with "."', function() {
92+
var matches = matcher.parseMatches( 'asdf@.asdf.com' );
93+
94+
expect( matches.length ).toBe( 0 );
95+
} );
96+
97+
it( 'should *not* match email with incorrect local part beginning with "."', function() {
98+
var matches = matcher.parseMatches( '.asdf@asdf.com' );
99+
100+
expect( matches.length ).toBe( 1 );
101+
MatchChecker.expectEmailMatch( matches[ 0 ], 'asdf@asdf.com', 1 );
102+
} );
103+
104+
it( 'should *not* match email with incorrect local part ending with "."', function() {
105+
var matches = matcher.parseMatches( 'asdf.@asdf.com' );
106+
107+
expect( matches.length ).toBe( 0 );
108+
} );
109+
110+
it( 'should match email skipping incorrect local part tailing with ".."', function() {
111+
var matches = matcher.parseMatches( 'asdf..asdf@asdf.com' );
112+
113+
expect( matches.length ).toBe( 1 );
114+
MatchChecker.expectEmailMatch( matches[ 0 ], 'asdf@asdf.com', 6 );
115+
} );
116+
79117
} );
80118

81119

82-
} );
120+
} );

0 commit comments

Comments
 (0)