Check for overlapping pattern matches

Previously, there was an issue where pattern matches could overlap.
This adds a a check so a match cannot be inserted if a previous match
overlaps with it.

Bug: Issue 4885
Change-Id: I33bd36360382f28792ce9e4597a378eafecdbd56
This commit is contained in:
Becky Siegel
2016-11-04 10:24:43 -07:00
parent 828882892d
commit f949c63222
2 changed files with 25 additions and 3 deletions

View File

@@ -47,6 +47,10 @@ limitations under the License.
match: '(I[0-9a-f]{8,40})',
link: '#/q/$1'
},
changeid2: {
match: 'Change-Id: +(I[0-9a-f]{8,40})',
link: '#/q/$1'
},
googlesearch: {
match: 'google:(.+)',
link: 'https://bing.com/search?q=$1', // html should supercede link.
@@ -151,7 +155,6 @@ limitations under the License.
assert.equal(bugLinkEl.textContent, 'Issue 3650');
});
test('html field in link config', function() {
element.content = 'google:do a barrel roll';
var linkEl = element.$.output.childNodes[0];

View File

@@ -93,12 +93,31 @@ GrLinkTextParser.prototype.addLink =
if (!text) {
return;
}
this.addItem(text, href, null, position, length, outputArray);
if (!this.hasOverlap(position, length, outputArray)) {
this.addItem(text, href, null, position, length, outputArray);
}
};
GrLinkTextParser.prototype.addHTML =
function(html, position, length, outputArray) {
this.addItem(null, null, html, position, length, outputArray);
if (!this.hasOverlap(position, length, outputArray)) {
this.addItem(null, null, html, position, length, outputArray);
}
};
GrLinkTextParser.prototype.hasOverlap =
function(position, length, outputArray) {
var endPosition = position + length;
for (var i = 0; i < outputArray.length; i++) {
var arrayItemStart = outputArray[i].position;
var arrayItemEnd = outputArray[i].position + outputArray[i].length;
if ((position >= arrayItemStart && position < arrayItemEnd) ||
(endPosition > arrayItemStart && endPosition <= arrayItemEnd) ||
(position === arrayItemStart && position === arrayItemEnd)) {
return true;
}
}
return false;
};
GrLinkTextParser.prototype.parse = function(text) {