Skip to content

Commit 6436c71

Browse files
authored
Merge pull request #375 from talkjs/master
Added an underscore to allowed domain label symbols
2 parents 37efcf8 + ddfe622 commit 6436c71

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/regex-lib.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ export const alphaCharsStr = /A-Za-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u0
6969
* The string form of a regular expression that would match all emoji characters
7070
* Based on the emoji regex defined in this article: https://thekevinscott.com/emojis-in-javascript/
7171
*/
72-
export const emojiStr = /\u2700-\u27bf\udde6-\uddff\ud800-\udbff\udc00-\udfff\ufe0e\ufe0f\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0\ud83c\udffb-\udfff\u200d\u3299\u3297\u303d\u3030\u24c2\ud83c\udd70-\udd71\udd7e-\udd7f\udd8e\udd91-\udd9a\udde6-\uddff\ude01-\ude02\ude1a\ude2f\ude32-\ude3a\ude50-\ude51\u203c\u2049\u25aa-\u25ab\u25b6\u25c0\u25fb-\u25fe\u00a9\u00ae\u2122\u2139\udc04\u2600-\u26FF\u2b05\u2b06\u2b07\u2b1b\u2b1c\u2b50\u2b55\u231a\u231b\u2328\u23cf\u23e9-\u23f3\u23f8-\u23fa\udccf\u2935\u2934\u2190-\u21ff/
73-
.source;
72+
export const emojiStr =
73+
/\u2700-\u27bf\udde6-\uddff\ud800-\udbff\udc00-\udfff\ufe0e\ufe0f\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0\ud83c\udffb-\udfff\u200d\u3299\u3297\u303d\u3030\u24c2\ud83c\udd70-\udd71\udd7e-\udd7f\udd8e\udd91-\udd9a\udde6-\uddff\ude01-\ude02\ude1a\ude2f\ude32-\ude3a\ude50-\ude51\u203c\u2049\u25aa-\u25ab\u25b6\u25c0\u25fb-\u25fe\u00a9\u00ae\u2122\u2139\udc04\u2600-\u26FF\u2b05\u2b06\u2b07\u2b1b\u2b1c\u2b50\u2b55\u231a\u231b\u2328\u23cf\u23e9-\u23f3\u23f8-\u23fa\udccf\u2935\u2934\u2190-\u21ff/
74+
.source;
7475

7576
/**
7677
* The string form of a regular expression that would match all of the
@@ -161,9 +162,9 @@ export const alphaNumericAndMarksCharsStr = alphaCharsAndMarksStr + decimalNumbe
161162
// Simplified IP regular expression
162163
const ipStr = '(?:[' + decimalNumbersStr + ']{1,3}\\.){3}[' + decimalNumbersStr + ']{1,3}';
163164

164-
// Protected domain label which do not allow "-" character on the beginning and the end of a single label
165+
// Protected domain label which do not allow "-" or "_" character on the beginning and the end of a single label
165166
// prettier-ignore
166-
const domainLabelStr = '[' + alphaNumericAndMarksCharsStr + '](?:[' + alphaNumericAndMarksCharsStr + '\\-]{0,61}[' + alphaNumericAndMarksCharsStr + '])?';
167+
const domainLabelStr = '[' + alphaNumericAndMarksCharsStr + '](?:[' + alphaNumericAndMarksCharsStr + '\\-_]{0,61}[' + alphaNumericAndMarksCharsStr + '])?';
167168

168169
const getDomainLabelStr = (group: number) => {
169170
return '(?=(' + domainLabelStr + '))\\' + group;

tests/autolinker-url.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,13 @@ describe('Autolinker Url Matching -', () => {
307307
);
308308
});
309309

310+
it('should match a url with underscores in domain label', function () {
311+
let result = autolinker.link('https://gcs_test_env.storage.googleapis.com/file.pdf');
312+
expect(result).toBe(
313+
'<a href="https://gcs_test_env.storage.googleapis.com/file.pdf">gcs_test_env.storage.googleapis.com/file.pdf</a>'
314+
);
315+
});
316+
310317
it('should not match local urls with numbers when NOT prefixed with http://', function () {
311318
let result1 = autolinker.link('localhost.local001/test');
312319
expect(result1).toBe('localhost.local001/test');
@@ -653,6 +660,13 @@ describe('Autolinker Url Matching -', () => {
653660
);
654661
});
655662

663+
it('should match a url with underscores in domain label', function () {
664+
let result = autolinker.link('gcs_test_env.storage.googleapis.com/file.pdf');
665+
expect(result).toBe(
666+
'<a href="http://gcs_test_env.storage.googleapis.com/file.pdf">gcs_test_env.storage.googleapis.com/file.pdf</a>'
667+
);
668+
});
669+
656670
it('should automatically link a URL with accented characters', function () {
657671
let result = autolinker.link('Joe went to mañana.com today.');
658672
expect(result).toBe('Joe went to <a href="http://mañana.com">mañana.com</a> today.');
@@ -680,6 +694,13 @@ describe('Autolinker Url Matching -', () => {
680694
expect(result).toBe('Joe went to <a href="//YAHOO.COM">YAHOO.COM</a>');
681695
});
682696

697+
it('should match a url with underscores in domain label', function () {
698+
let result = autolinker.link('//gcs_test_env.storage.googleapis.com/file.pdf');
699+
expect(result).toBe(
700+
'<a href="//gcs_test_env.storage.googleapis.com/file.pdf">gcs_test_env.storage.googleapis.com/file.pdf</a>'
701+
);
702+
});
703+
683704
it('should NOT automatically link supposed protocol-relative URLs in the form of abc//yahoo.com, which is most likely not supposed to be interpreted as a URL', function () {
684705
let result1 = autolinker.link('Joe went to abc//yahoo.com');
685706
expect(result1).toBe('Joe went to abc//yahoo.com');

0 commit comments

Comments
 (0)