[FIX] web_editor: improve url regex

Before this commit:

There were certain issues with the URL regex, wherein it failed to validate
against domain list under certain circumstances involving URLs with two dots.
for eg. this.document.anything

After this commit:

The URL regex is modified to ensure that if a URL regex does not ends with
the domains in tldWhiteList, it must start with http://, https://, www. or
if it does not start with http://, https://, www., it must end with a
domain from the tldWhiteList.

task-3468646

Part-of: odoo/odoo#143250
This commit is contained in:
Deependra Solanki
2023-11-23 08:52:04 +00:00
parent d9bdcd281b
commit de34fed834
3 changed files with 4 additions and 4 deletions
@@ -50,7 +50,7 @@ const tldWhitelist = [
'ug', 'uk', 'um', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn',
'vu', 'wf', 'ws', 'ye', 'yt', 'yu', 'za', 'zm', 'zr', 'zw', 'co\\.uk'];
const urlRegexBase = `|(?:[-a-zA-Z0-9@:%._\\+~#=]{1,64}\\.))[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-zA-Z][a-zA-Z0-9]{1,62}|(?:[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.(?:${tldWhitelist.join('|')})\\b))(?:\\/[^\\s.,'")}\\]]*[?#.,)}\\]'"]?[^\\s.,'")}\\]]+|(?:[^!(){}.,\\[\\]'"\\s]+)|\\/[^\\s.,'")}\\]]*[^\\s?#.,'")}\\]]*)?`;
const urlRegexBase = `|(?:www.))[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-zA-Z][a-zA-Z0-9]{1,62}|(?:[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.(?:${tldWhitelist.join('|')})\\b))(?:\\/[^\\s.,'")}\\]]*[?#.,)}\\]'"]?[^\\s.,'")}\\]]+|(?:[^!(){}.,\\[\\]'"\\s]+)|\\/[^\\s.,'")}\\]]*[^\\s?#.,'")}\\]]*)?`;
const httpCapturedRegex= `(https?:\\/\\/)`;
export const URL_REGEX = new RegExp(`((?:(?:${httpCapturedRegex}${urlRegexBase})`, 'i');
@@ -87,7 +87,7 @@ describe('Link', () => {
testUrlRegex('https://www.google.com/');
testNotUrlRegex('google.shop/');
testUrlRegex('http://google.com/foo#test');
testUrlRegex('a.bcd.ef');
testNotUrlRegex('a.bcd.ef');
testUrlRegex('a.bc.de');
testNotUrlRegex('a.bc.d');
testNotUrlRegex('a.b.bc');
@@ -15,8 +15,8 @@ describe('urlRegex', () => {
const match = text.match(URL_REGEX);
chai.expect(match).to.be.equal(null);
});
it('should match abc.abc.abc', () => {
const url = 'abc.abc.abc';
it('should match www.abc.abc', () => {
const url = 'www.abc.abc';
const text = `abc ${url} abc`;
const match = text.match(URL_REGEX);
chai.expect(match[0]).to.be.equal(url);