Merge "Allow commitLink replacements overlap"

This commit is contained in:
Andrew Bonventre
2016-12-02 15:34:53 +00:00
committed by Gerrit Code Review
2 changed files with 41 additions and 3 deletions

View File

@@ -175,5 +175,28 @@ limitations under the License.
assert.equal(element.$.output.innerHTML, 'foo:baz');
});
test('overlapping links', function() {
element.config = {
b1: {
match: '(B:\\s*)(\\d+)',
html: '$1<a href="ftp://foo/$2">$2</a>',
},
b2: {
match: '(B:\\s*\\d+\\s*,\\s*)(\\d+)',
html: '$1<a href="ftp://foo/$2">$2</a>',
},
};
element.content = '- B: 123, 45';
var links = Polymer.dom(element.root).querySelectorAll('a');
assert.equal(links.length, 2);
assert.equal(element.$$('span').textContent, '- B: 123, 45');
assert.equal(links[0].href, 'ftp://foo/123');
assert.equal(links[0].textContent, '123');
assert.equal(links[1].href, 'ftp://foo/45');
assert.equal(links[1].textContent, '45');
});
});
</script>

View File

@@ -165,12 +165,27 @@ GrLinkTextParser.prototype.parseLinks = function(text, patterns) {
var result = match[0].replace(pattern,
patterns[p].html || patterns[p].link);
// Skip portion of replacement string that is equal to original.
for (var i = 0; i < result.length; i++) {
if (result[i] !== match[0][i]) {
break;
}
}
result = result.slice(i);
if (patterns[p].html) {
this.addHTML(
result, susbtrIndex + match.index, match[0].length, outputArray);
result,
susbtrIndex + match.index + i,
match[0].length - i,
outputArray);
} else if (patterns[p].link) {
this.addLink(match[0], result,
susbtrIndex + match.index, match[0].length, outputArray);
this.addLink(
match[0],
result,
susbtrIndex + match.index + i,
match[0].length - i,
outputArray);
} else {
throw Error('linkconfig entry ' + p +
' doesnt contain a link or html attribute.');