Fix issue with gr-linked-text not properly linking >1 config matches

+ Turns out the format passed back in the config corresponds to
  the String.prototype.replace method's, so some code could be
  removed.
+ Display the initial content before linkifying it, to prevent
  a jump in page content due to waiting for the project config
  to load.

Change-Id: I953cff81bba499b3fdb92846b72ac532d39f549b
This commit is contained in:
Andrew Bonventre 2016-01-05 14:36:00 -05:00 committed by Dave Borowitz
parent 08343f7dab
commit b0d9bad665
3 changed files with 41 additions and 23 deletions

View File

@ -34,15 +34,26 @@ limitations under the License.
is: 'gr-linked-text',
properties: {
content: String,
content: {
type: String,
observer: '_contentChanged',
},
config: Object,
},
observers: [
'_contentChanged(content, config)',
'_contentOrConfigChanged(content, config)',
],
_contentChanged: function(content, config) {
_contentChanged: function(content) {
// In the case where the config may not be set (perhaps due to the
// request for it still being in flight), set the content anyway to
// prevent waiting on the config to display the text.
if (this.config != null) { return; }
this.$.output.textContent = content;
},
_contentOrConfigChanged: function(content, config) {
var output = this.$.output;
output.textContent = '';
var parser = new GrLinkTextParser(config, function(text, href) {

View File

@ -20,8 +20,6 @@ function GrLinkTextParser(linkConfig, callback) {
Object.preventExtensions(this);
}
GrLinkTextParser.SUB_REGEX = /\$(\d+)/;
GrLinkTextParser.prototype.addText = function(text, href) {
if (!text) {
return;
@ -45,22 +43,22 @@ GrLinkTextParser.prototype.parseChunk = function(text, href) {
GrLinkTextParser.prototype.parseLinks = function(text, patterns) {
for (var p in patterns) {
var pattern = new RegExp(patterns[p].match);
var match = text.match(pattern);
if (!match) { continue; }
var pattern = new RegExp(patterns[p].match, 'g');
var link = patterns[p].link;
var subMatch = link.match(GrLinkTextParser.SUB_REGEX);
link = link.replace(GrLinkTextParser.SUB_REGEX, match[subMatch[1]]);
var match;
while (match = pattern.exec(text)) {
var link = match[0].replace(pattern, patterns[p].link);
// PolyGerrit doesn't use hash-based navigation like GWT. Account for this.
if (link[0] == '#') {
link = link.substr(1);
// PolyGerrit doesn't use hash-based navigation like GWT.
// Account for this.
if (link[0] == '#') {
link = link.substr(1);
}
var before = text.substr(0, match.index);
this.addText(before);
text = text.substr(match.index + match[0].length);
this.addText(match[0], link);
}
var before = text.substr(0, match.index);
this.addText(before);
text = text.substr(match.index + match[0].length);
this.addText(match[0], link);
}
this.addText(text);
};

View File

@ -65,29 +65,38 @@ limitations under the License.
{
'text': 'Change-Id: I11d6a37f5e9b5df0486f6c922d8836dfa780e03e',
'linkedText': 'Change-Id: <a href="/q/I11d6a37f5e9b5df0486f6c922d8836dfa780e03e" target="_blank">I11d6a37f5e9b5df0486f6c922d8836dfa780e03e</a>'
}
},
{
'text': 'Issue 3650\nIssue 3450',
'linkedText': '<a href="https://code.google.com/p/gerrit/issues/detail?id=3650" target="_blank">Issue 3650</a>\n<a href="https://code.google.com/p/gerrit/issues/detail?id=3450" target="_blank">Issue 3450</a>',
},
];
});
test('URL pattern was parsed and linked.', function() {
// Reguar inline link.
element._contentChanged(testStrings[0].text, element.config);
element.content = testStrings[0].text;
assert.equal(element.$.output.innerHTML, testStrings[0].linkedText);
});
test('Bug pattern was parsed and linked', function() {
// "Issue/Bug" pattern.
element._contentChanged(testStrings[1].text, element.config);
element.content = testStrings[1].text;
assert.equal(element.$.output.innerHTML, testStrings[1].linkedText);
element._contentChanged(testStrings[2].text, element.config);
element.content = testStrings[2].text;
assert.equal(element.$.output.innerHTML, testStrings[2].linkedText);
});
test('Change-Id pattern was parsed and linked', function() {
// "Change-Id:" pattern.
element._contentChanged(testStrings[3].text, element.config);
element.content = testStrings[3].text;
assert.equal(element.$.output.innerHTML, testStrings[3].linkedText);
});
test('Multiple matches', function() {
element.content = testStrings[4].text;
assert.equal(element.$.output.innerHTML, testStrings[4].linkedText);
});
});
</script>