Fix commentlinks with same prefix in pattern and link
If using the commentlinks.%NAME%.links option and having the same prefix
in the pattern and the url, the parsing did not work properly.
For example:
[commentlink "test"]
pattern = [Hh][Tt][Tt][Pp]example
link = http://example.com
and the line in the message:
httpexample 1234
would result in:
http<a href="://example.com" ...>httpexample 1234</a>
This bug was introduced in a change implementing the possibility to
have overlapping patterns, e.g. allowing a comma-separated list of
issue ids. (commit: fb48902ef303d008c6f0dd38d654106c00313530). This
would have however anyway only worked for the `html`-variant of
comment links, which allow to directly provide html-elements as a
substitute for the text matching the pattern.
Thus, this change restricts the logic introduced in the above mentioned
commit to the html-variant.
Bug: Issue 12197
Change-Id: I9efd9308f2b40f3fec8405c93849d13d4576562a
diff --git a/polygerrit-ui/app/elements/shared/gr-linked-text/gr-linked-text_test.html b/polygerrit-ui/app/elements/shared/gr-linked-text/gr-linked-text_test.html
index 6d413b7..19839a8 100644
--- a/polygerrit-ui/app/elements/shared/gr-linked-text/gr-linked-text_test.html
+++ b/polygerrit-ui/app/elements/shared/gr-linked-text/gr-linked-text_test.html
@@ -50,6 +50,10 @@
match: '([Bb]ug|[Ii]ssue)\\s*#?(\\d+)',
link: 'https://bugs.chromium.org/p/gerrit/issues/detail?id=$2',
},
+ prefixsameinlinkandpattern: {
+ match: '([Hh][Tt][Tt][Pp]example)\\s*#?(\\d+)',
+ link: 'https://bugs.chromium.org/p/gerrit/issues/detail?id=$2',
+ },
changeid: {
match: '(I[0-9a-f]{8,40})',
link: '#/q/$1',
@@ -116,6 +120,18 @@
assert.equal(linkEl.textContent, 'Bug 3650');
});
+ test('Pattern with same prefix as link was correctly parsed', () => {
+ // Pattern starts with the same prefix (`http`) as the url.
+ element.content = 'httpexample 3650';
+
+ assert.equal(element.$.output.childNodes.length, 1);
+ const linkEl = element.$.output.childNodes[0];
+ const url = 'https://bugs.chromium.org/p/gerrit/issues/detail?id=3650';
+ assert.equal(linkEl.target, '_blank');
+ assert.equal(linkEl.href, url);
+ assert.equal(linkEl.textContent, 'httpexample 3650');
+ });
+
test('Change-Id pattern was parsed and linked', () => {
// "Change-Id:" pattern.
const changeID = 'I11d6a37f5e9b5df0486f6c922d8836dfa780e03e';
diff --git a/polygerrit-ui/app/elements/shared/gr-linked-text/link-text-parser.js b/polygerrit-ui/app/elements/shared/gr-linked-text/link-text-parser.js
index 23a71f9..027c632 100644
--- a/polygerrit-ui/app/elements/shared/gr-linked-text/link-text-parser.js
+++ b/polygerrit-ui/app/elements/shared/gr-linked-text/link-text-parser.js
@@ -312,14 +312,15 @@
let result = match[0].replace(pattern,
patterns[p].html || patterns[p].link);
- let i;
- // Skip portion of replacement string that is equal to original.
- for (i = 0; i < result.length; i++) {
- if (result[i] !== match[0][i]) { break; }
- }
- result = result.slice(i);
-
if (patterns[p].html) {
+ let i;
+ // Skip portion of replacement string that is equal to original to
+ // allow overlapping patterns.
+ for (i = 0; i < result.length; i++) {
+ if (result[i] !== match[0][i]) { break; }
+ }
+ result = result.slice(i);
+
this.addHTML(
result,
susbtrIndex + match.index + i,
@@ -329,8 +330,8 @@
this.addLink(
match[0],
result,
- susbtrIndex + match.index + i,
- match[0].length - i,
+ susbtrIndex + match.index,
+ match[0].length,
outputArray);
} else {
throw Error('linkconfig entry ' + p +