Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

End the url at the first unmatched closing parenthesis #578

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions __tests__/ExpensiMark-HTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,10 +631,25 @@ test('Test a url ending with a question mark autolinks correctly', () => {
expect(parser.replace(testString)).toBe(resultString);
});

test('Test a url ending with a closing parentheses autolinks correctly', () => {
const testString = 'https://github.com/Expensify/ReactNativeChat/pull/645)';
const resultString = '<a href="https://github.com/Expensify/ReactNativeChat/pull/645" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/645</a>)';
expect(parser.replace(testString)).toBe(resultString);
test('Test urls with unmatched closing parentheses autolinks correctly', () => {
ShogunFire marked this conversation as resolved.
Show resolved Hide resolved
const testCases = [
{
testString: 'https://github.com/Expensify/ReactNativeChat/pull/645)',
resultString: '<a href="https://github.com/Expensify/ReactNativeChat/pull/645" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/645</a>)',
},
{
testString: 'https://github.com/Expensify/ReactNativeChat/pull/test(645))',
resultString: '<a href="https://github.com/Expensify/ReactNativeChat/pull/test(645)" target="_blank" rel="noreferrer noopener">https://github.com/Expensify/ReactNativeChat/pull/test(645)</a>)',
},
{
testString: 'google.com/(toto))titi)',
resultString: '<a href="https://google.com/(toto)" target="_blank" rel="noreferrer noopener">google.com/(toto)</a>)titi)',
},

];
testCases.forEach(testCase => {
expect(parser.replace(testCase.testString)).toBe(testCase.resultString);
});
});

test('Test urls autolinks correctly', () => {
Expand Down
38 changes: 17 additions & 21 deletions lib/ExpensiMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,26 +446,22 @@ export default class ExpensiMark {
let startIndex = 0;

while (match !== null) {
// we want to avoid matching ending ) unless it is a closing parenthesis for the URL
if (textToCheck[(match.index + match[2].length) - 1] === ')' && !match[2].includes('(')) {
match[0] = match[0].substr(0, match[0].length - 1);
match[2] = match[2].substr(0, match[2].length - 1);
}

// remove extra ) parentheses
let brace = 0;
if (match[2][match[2].length - 1] === ')') {
for (let i = 0; i < match[2].length; i++) {
if (match[2][i] === '(') {
brace++;
// We end the link at the last closing parenthesis that matches an opening parenthesis because unmatched closing parentheses are unlikely to be in the url
// and can be part of markdown for example
let unmatchedOpenParentheses = 0;
let url = match[2];
for (let i = 0; i < url.length; i++) {
if (url[i] === '(') {
unmatchedOpenParentheses++;
} else if (url[i] === ')') {
// Unmatched closing parenthesis
if (unmatchedOpenParentheses <= 0) {
const numberOfCharsToRemove = url.length - i;
match[0] = match[0].substr(0, match[0].length - numberOfCharsToRemove);
url = url.substr(0, url.length - numberOfCharsToRemove);
break;
}
if (match[2][i] === ')') {
brace--;
}
}
if (brace) {
match[0] = match[0].substr(0, match[0].length + brace);
match[2] = match[2].substr(0, match[2].length + brace);
unmatchedOpenParentheses--;
}
}
replacedText = replacedText.concat(textToCheck.substr(startIndex, (match.index - startIndex)));
Expand All @@ -479,7 +475,7 @@ export default class ExpensiMark {
// If we find a URL with a leading @ sign, we need look for other domains in the rest of the string
if ((match.index !== 0) && (textToCheck[match.index - 1] === '@')) {
const domainRegex = new RegExp('^(([a-z-0-9]+\\.)+[a-z]{2,})(\\S*)', 'i');
const domainMatch = domainRegex.exec(match[2]);
const domainMatch = domainRegex.exec(url);

// If we find another domain in the remainder of the string, we apply the auto link rule again and set a flag to avoid re-doing below.
if ((domainMatch !== null) && (domainMatch[3] !== '')) {
Expand All @@ -506,7 +502,7 @@ export default class ExpensiMark {
filterRules: ['bold', 'strikethrough', 'italic'],
shouldEscapeText: false,
});
replacedText = replacedText.concat(replacement(match[0], linkText, match[2]));
replacedText = replacedText.concat(replacement(match[0], linkText, url));
}
startIndex = match.index + (match[0].length);

Expand Down
Loading