Skip to content

Commit

Permalink
Merge pull request #550 from tienifr/fix/move-autoEmail-to-before-men…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
cead22 authored Jul 12, 2023
2 parents 0c685f8 + 3961539 commit d7e4249
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
60 changes: 60 additions & 0 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,3 +1063,63 @@ test('Skip rendering invalid markdown',() => {
testString = '> *This is multiline\nbold text*';
expect(parser.replace(testString)).toBe('<blockquote>*This is multiline</blockquote>bold text*');
});

test('Test for email with test+1@gmail.com@gmail.com', () => {
const testString = 'test+1@gmail.com@gmail.com';
const resultString = '<a href=\"mailto:test+1@gmail.com\">test+1@gmail.com</a>@gmail.com';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test for email with test@gmail.com@gmail.com', () => {
const testString = 'test@gmail.com@gmail.com';
const resultString = '<a href=\"mailto:test@gmail.com\">test@gmail.com</a>@gmail.com';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test for email with test@here@gmail.com', () => {
const testString = 'test@here@gmail.com';
const resultString = 'test@<a href=\"mailto:here@gmail.com\">here@gmail.com</a>';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test for email with test+1@here@gmail.com', () => {
const testString = 'test+1@here@gmail.com';
const resultString = 'test+1@<a href=\"mailto:here@gmail.com\">here@gmail.com</a>';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test user mention with @here@here.com', () => {
const testString = '@here@here.com';
const resultString = '<mention-user>@here@here.com</mention-user>';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test user mention with @abc_@here.com', () => {
const testString = '@abc_@here.com';
const resultString = '<mention-user>@abc_@here.com</mention-user>';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test user mention with @here_@here.com', () => {
const testString = '@here_@here.com';
const resultString = '<mention-user>@here_@here.com</mention-user>';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test here mention with @here_@here_.com', () => {
const testString = '@here_@here_.com';
const resultString = '<mention-here>@here</mention-here><em><mention-here>@here</mention-here></em>.com';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test here mention with @here@', () => {
const testString = '@here@';
const resultString = '<mention-here>@here</mention-here>@';
expect(parser.replace(testString)).toBe(resultString);
});

test('Test here mention with @here@here', () => {
const testString = '@here@here';
const resultString = '<mention-here>@here</mention-here><mention-here>@here</mention-here>';
expect(parser.replace(testString)).toBe(resultString);
});
1 change: 0 additions & 1 deletion __tests__/Str-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ describe('Str.isValidMention', () => {
expect(Str.isValidMention('*@username@expensify.com*')).toBeTruthy();
expect(Str.isValidMention(' @username@expensify.com')).toBeTruthy();
expect(Str.isValidMention('~@username@expensify.com~')).toBeTruthy();
expect(Str.isValidMention('#@username@expensify.com')).toBeTruthy();
expect(Str.isValidMention('_@username@expensify.com_')).toBeTruthy();
expect(Str.isValidMention('`@username@expensify.com`')).toBeFalsy();
expect(Str.isValidMention('\'@username@expensify.com\'')).toBeTruthy();
Expand Down
8 changes: 4 additions & 4 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ export default class ExpensiMark {
*/
{
name: 'hereMentions',
regex: /[`.a-zA-Z]?@here(?=_\b|\b)(?!((?:(?!<a).)+)?<\/a>|[^<]*(<\/pre>|<\/code>))/gm,
replacement: (match) => {
regex: /([a-zA-Z0-9.!$%&+/=?^`{|}_-]?)(@here)([a-zA-Z0-9.!$%&+/=?^`{|}_-]?)(?=\b)(?!(@[a-zA-Z0-9-]+?(\.[a-zA-Z]+)+)|((?:(?!<a).)+)?<\/a>|[^<]*(<\/pre>|<\/code>))/gm,
replacement: (match, g1, g2, g3) => {
if (!Str.isValidMention(match)) {
return match;
}
return `<mention-here>${match}</mention-here>`;
return `${g1}<mention-here>${g2}</mention-here>${g3}`;
},
},

Expand All @@ -110,7 +110,7 @@ export default class ExpensiMark {
*/
{
name: 'userMentions',
regex: new RegExp(`[\`.a-zA-Z]?@+${CONST.REG_EXP.EMAIL_PART}(?!((?:(?!<a).)+)?<\\/a>|[^<]*(<\\/pre>|<\\/code>))`, 'gm'),
regex: new RegExp(`[a-zA-Z0-9.!$%&+/=?^\`{|}-]?@+${CONST.REG_EXP.EMAIL_PART}(?!((?:(?!<a).)+)?<\\/a>|[^<]*(<\\/pre>|<\\/code>))`, 'gm'),
replacement: (match) => {
if (!Str.isValidMention(match)) {
return match;
Expand Down
12 changes: 10 additions & 2 deletions lib/str.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,16 @@ const Str = {
* @returns {bool}
*/
isValidMention(mention) {
// A valid mention starts with a space, *, _, #, ', ", or @ (with no preceding characters).
return /[\s*~_#'"@]/g.test(mention.charAt(0));
// Mentions can start @ proceeded by a space, eg "ping @user@domain.tld"
if (/[\s@]/g.test(mention.charAt(0))) {
return true;
}

// Mentions can also start and end with a *, _, ~, ', or " (with no other preceding characters)
// eg "ping *@user@domain.tld*"
const firstChar = mention.charAt(0);
const lastChar = mention.charAt(mention.length - 1);
return /[*~_'"]/g.test(firstChar) && /[*~_'"]/g.test(lastChar) && firstChar === lastChar;
},

/**
Expand Down

0 comments on commit d7e4249

Please sign in to comment.