Merge "Process links if leading whitespace are missed"

This commit is contained in:
Dmitrii Filippov 2020-02-05 18:08:28 +00:00 committed by Gerrit Code Review
commit c1b32fdda4
2 changed files with 65 additions and 7 deletions

View File

@ -278,16 +278,58 @@ limitations under the License.
let links = element.$.output.querySelectorAll('a');
assert.equal(links.length, 1);
assert.equal(links[0].getAttribute('href'), 'mailto:test@google.com');
assert.equal(links[0].innerHTML, 'mailto:test@google.com');
element.content = 'xx http://google.com yy';
links = element.$.output.querySelectorAll('a');
assert.equal(links.length, 1);
assert.equal(links[0].getAttribute('href'), 'http://google.com');
assert.equal(links[0].innerHTML, 'http://google.com');
element.content = 'xx https://google.com yy';
links = element.$.output.querySelectorAll('a');
assert.equal(links.length, 1);
assert.equal(links[0].getAttribute('href'), 'https://google.com');
assert.equal(links[0].innerHTML, 'https://google.com');
element.content = 'xx ssh://google.com yy';
links = element.$.output.querySelectorAll('a');
assert.equal(links.length, 0);
element.content = 'xx ftp://google.com yy';
links = element.$.output.querySelectorAll('a');
assert.equal(links.length, 0);
});
test('links without leading whitespace are linkified', () => {
element.content = 'xx abcmailto:test@google.com yy';
assert.equal(element.$.output.innerHTML.substr(0, 6), 'xx abc');
let links = element.$.output.querySelectorAll('a');
assert.equal(links.length, 1);
assert.equal(links[0].getAttribute('href'), 'mailto:test@google.com');
assert.equal(links[0].innerHTML, 'mailto:test@google.com');
element.content = 'xx defhttp://google.com yy';
assert.equal(element.$.output.innerHTML.substr(0, 6), 'xx def');
links = element.$.output.querySelectorAll('a');
assert.equal(links.length, 1);
assert.equal(links[0].getAttribute('href'), 'http://google.com');
assert.equal(links[0].innerHTML, 'http://google.com');
element.content = 'xx qwehttps://google.com yy';
assert.equal(element.$.output.innerHTML.substr(0, 6), 'xx qwe');
links = element.$.output.querySelectorAll('a');
assert.equal(links.length, 1);
assert.equal(links[0].getAttribute('href'), 'https://google.com');
assert.equal(links[0].innerHTML, 'https://google.com');
// Non-latin character
element.content = 'xx абвhttps://google.com yy';
assert.equal(element.$.output.innerHTML.substr(0, 6), 'xx абв');
links = element.$.output.querySelectorAll('a');
assert.equal(links.length, 1);
assert.equal(links[0].getAttribute('href'), 'https://google.com');
assert.equal(links[0].innerHTML, 'https://google.com');
element.content = 'xx ssh://google.com yy';
links = element.$.output.querySelectorAll('a');

View File

@ -22,7 +22,7 @@
*
* @type {RegExp}
*/
const URL_PROTOCOL_PATTERN = /^(https?:\/\/|mailto:)/;
const URL_PROTOCOL_PATTERN = /^(.*)(https?:\/\/|mailto:)/;
/**
* Construct a parser for linkifying text. Will linkify plain URLs that appear
@ -257,13 +257,29 @@
// the source text does not include a protocol, the protocol will be added
// by ba-linkify. Create the link if the href is provided and its protocol
// matches the expected pattern.
if (href && URL_PROTOCOL_PATTERN.test(href)) {
this.addText(text, href);
} else {
// For the sections of text that lie between the links found by
// ba-linkify, we search for the project-config-specified link patterns.
this.parseLinks(text, this.linkConfig);
if (href) {
const result = URL_PROTOCOL_PATTERN.exec(href);
if (result) {
const prefixText = result[1];
if (prefixText.length > 0) {
// Fix for simple cases from
// https://bugs.chromium.org/p/gerrit/issues/detail?id=11697
// When leading whitespace is missed before link,
// linkify add this text before link as a schema name to href.
// We suppose, that prefixText just a single word
// before link and add this word as is, without processing
// any patterns in it.
this.parseLinks(prefixText, []);
text = text.substring(prefixText.length);
href = href.substring(prefixText.length);
}
this.addText(text, href);
return;
}
}
// For the sections of text that lie between the links found by
// ba-linkify, we search for the project-config-specified link patterns.
this.parseLinks(text, this.linkConfig);
};
/**