diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js index 04af5fb3666..61cab5821e2 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/utils.js @@ -47,12 +47,12 @@ 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]*`; +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]*(? { - it(`should be a link: ${url}`, () => { - window.chai.assert.exists(url.match(URL_REGEX)); - window.chai.assert.exists(url.match(URL_REGEX_WITH_INFOS)); +const testUrlRegex = (content, {expectedUrl} = {}) => { + const message = expectedUrl ? + `should have the text be "${content}" with one link ${expectedUrl}` : + `should be a link: ${content}`; + it(message, () => { + const match = content.match(URL_REGEX); + if (expectedUrl) { + window.chai.assert.equal(expectedUrl, match && match[0]); + } + window.chai.assert.exists(content.match(URL_REGEX)); + window.chai.assert.exists(content.match(URL_REGEX_WITH_INFOS)); }); } const testNotUrlRegex = (url) => { @@ -33,10 +40,47 @@ const testNotUrlRegex = (url) => { describe('Link', () => { describe('regex', () => { testUrlRegex('google.com'); + testUrlRegex('a google.com b', {expectedUrl: 'google.com'}); + + // Url separator + testUrlRegex('google.com/', {expectedUrl: 'google.com/'}); + testUrlRegex('google.com?', {expectedUrl: 'google.com?'}); + testUrlRegex('google.com#', {expectedUrl: 'google.com#'}); + + testUrlRegex('google.com!', {expectedUrl: 'google.com'}); + testUrlRegex('google.com)', {expectedUrl: 'google.com'}); + testUrlRegex('google.com(', {expectedUrl: 'google.com'}); + testUrlRegex('google.com. a', {expectedUrl: 'google.com'}); + testUrlRegex('google.com, a', {expectedUrl: 'google.com'}); + + // Some special characters should not be included if at the end. + testUrlRegex('google.com/.', {expectedUrl: 'google.com/'}); + testUrlRegex('google.com/,', {expectedUrl: 'google.com/'}); + testUrlRegex('google.com/)', {expectedUrl: 'google.com/'}); + testUrlRegex('google.com/]', {expectedUrl: 'google.com/'}); + testUrlRegex('google.com/}', {expectedUrl: 'google.com/'}); + testUrlRegex("google.com/'", {expectedUrl: 'google.com/'}); + testUrlRegex('google.com/"', {expectedUrl: 'google.com/'}); + + // The previous special character should be included when they are nt at the end. + testUrlRegex('google.com/.a', {expectedUrl: 'google.com/.a'}); + testUrlRegex('google.com/,a', {expectedUrl: 'google.com/,a'}); + testUrlRegex('google.com/)a', {expectedUrl: 'google.com/)a'}); + testUrlRegex('google.com/]a', {expectedUrl: 'google.com/]a'}); + testUrlRegex('google.com/}a', {expectedUrl: 'google.com/}a'}); + testUrlRegex("google.com/'a", {expectedUrl: "google.com/'a"}); + testUrlRegex('google.com/"a', {expectedUrl: 'google.com/"a'}); + + // Other special character can be included at the end. + testUrlRegex('google.com/(', {expectedUrl: 'google.com/('}); + testUrlRegex('google.com/[', {expectedUrl: 'google.com/['}); + testUrlRegex('google.com/{', {expectedUrl: 'google.com/{'}); + testUrlRegex('google.co.uk'); testUrlRegex('http://google.com'); testUrlRegex('https://google.com'); testUrlRegex('https://www.google.com'); + testUrlRegex('https://google.shop'); testNotUrlRegex('google.shop'); testUrlRegex('google.com/'); testUrlRegex('http://google.com/');