diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js index d24377a81ff..0ad8f61cb5c 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js @@ -47,7 +47,6 @@ import { fillEmpty, isEmptyBlock, URL_REGEX, - URL_REGEX_WITH_INFOS, isSelectionFormat, YOUTUBE_URL_GET_VIDEO_ID, unwrapContents, @@ -4594,7 +4593,7 @@ export class OdooEditor extends EventTarget { const textSliced = selection.anchorNode.textContent.slice(0, selection.anchorOffset); const textNodeSplitted = textSliced.split(/\s/); const potentialUrl = textNodeSplitted.pop(); - const match = potentialUrl.match(URL_REGEX_WITH_INFOS); + const match = potentialUrl.match(URL_REGEX); if (match && match[0] === potentialUrl && !EMAIL_REGEX.test(potentialUrl)) { const url = match[2] ? match[0] : 'http://' + match[0]; @@ -4703,6 +4702,9 @@ export class OdooEditor extends EventTarget { // Avoid transforming dynamic placeholder pattern to url. if(!text.match(/\${.*}/gi)) { splitAroundUrl = text.split(URL_REGEX); + // Remove 'http(s)://' capturing group from the result (indexes + // 2, 5, 8, ...). + splitAroundUrl = splitAroundUrl.filter((_, index) => ((index + 1) % 3)); } if (splitAroundUrl.length === 3 && !splitAroundUrl[0] && !splitAroundUrl[2]) { // Pasted content is a single URL. diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/sanitize.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/sanitize.js index a455374e73b..6b930465d42 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/sanitize.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/sanitize.js @@ -19,8 +19,8 @@ import { isArtificialVoidElement, ancestors, EMAIL_REGEX, - URL_REGEX_WITH_INFOS, PHONE_REGEX, + URL_REGEX, } from './utils.js'; const NOT_A_NUMBER = /[^\d]/g; @@ -94,7 +94,7 @@ export function deduceURLfromText(text, link) { return match[1] ? match[0] : 'mailto:' + match[0]; } // Check for http link. - match = label.match(URL_REGEX_WITH_INFOS); + match = label.match(URL_REGEX); if (match && match[0] === label) { const currentHttpProtocol = (link?.href.match(/^http(s)?:\/\//gi) || [])[0]; if (match[2]) { 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 8ce3f40f358..098b3a5a817 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 @@ -48,11 +48,9 @@ const tldWhitelist = [ '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 httpRegex = `(?:https?:\\/\\/)`; const httpCapturedRegex= `(https?:\\/\\/)`; -export const URL_REGEX = new RegExp(`((?:(?:${httpRegex}${urlRegexBase})`, 'i'); -export const URL_REGEX_WITH_INFOS = new RegExp(`((?:(?:${httpCapturedRegex}${urlRegexBase})`, 'i'); +export const URL_REGEX = new RegExp(`((?:(?:${httpCapturedRegex}${urlRegexBase})`, 'i'); export const YOUTUBE_URL_GET_VIDEO_ID = /^(?:(?:https?:)?\/\/)?(?:(?:www|m)\.)?(?:youtube\.com|youtu\.be)(?:\/(?:[\w-]+\?v=|embed\/|v\/)?)([^\s?&#]+)(?:\S+)?$/i; export const EMAIL_REGEX = /^(mailto:)?[\w-.]+@(?:[\w-]+\.)+[\w-]{2,4}$/i; diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/link.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/link.test.js index 627a5b173fc..5d9bd7b9de5 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/link.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/link.test.js @@ -1,6 +1,6 @@ /** @odoo-module */ -import { URL_REGEX, URL_REGEX_WITH_INFOS } from '../../src/OdooEditor.js'; +import { URL_REGEX } from '../../src/OdooEditor.js'; import { BasicEditor, click, @@ -27,13 +27,11 @@ const testUrlRegex = (content, {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) => { it(`should NOT be a link: ${url}`, () => { window.chai.assert.notExists(url.match(URL_REGEX)); - window.chai.assert.notExists(url.match(URL_REGEX_WITH_INFOS)); }); } diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/urlRegex.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/urlRegex.test.js index 2d0dc6e80f7..265f901e83f 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/urlRegex.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/urlRegex.test.js @@ -1,6 +1,6 @@ /** @odoo-module */ -import { URL_REGEX, URL_REGEX_WITH_INFOS } from '../../src/OdooEditor.js'; +import { URL_REGEX } from '../../src/OdooEditor.js'; describe('urlRegex', () => { it('should match foo.com', () => { @@ -46,48 +46,3 @@ describe('urlRegex', () => { chai.expect(match[0]).to.be.equal(url); }); }); - -describe('urlRegex with infos', () => { - it('should match foo.com', () => { - const url = 'foo.com'; - const text = `abc ${url} abc`; - const match = text.match(URL_REGEX_WITH_INFOS); - chai.expect(match[0]).to.be.equal(url); - }); - it('should not match foo.else', () => { - const url = 'foo.else'; - const text = `abc ${url} abc`; - const match = text.match(URL_REGEX_WITH_INFOS); - chai.expect(match).to.be.equal(null); - }); - it('should match abc.abc.abc', () => { - const url = 'abc.abc.abc'; - const text = `abc ${url} abc`; - const match = text.match(URL_REGEX_WITH_INFOS); - chai.expect(match[0]).to.be.equal(url); - }); - it('should match http://abc.abc.abc', () => { - const url = 'http://abc.abc.abc'; - const text = `abc ${url} abc`; - const match = text.match(URL_REGEX_WITH_INFOS); - chai.expect(match[0]).to.be.equal(url); - }); - it('should match https://abc.abc.abc', () => { - const url = 'https://abc.abc.abc'; - const text = `abc ${url} abc`; - const match = text.match(URL_REGEX_WITH_INFOS); - chai.expect(match[0]).to.be.equal(url); - }); - it('should match 1234-abc.runbot007.odoo.com/web#id=3&menu_id=221', () => { - const url = '1234-abc.runbot007.odoo.com/web#id=3&menu_id=221'; - const text = `abc ${url} abc`; - const match = text.match(URL_REGEX_WITH_INFOS); - chai.expect(match[0]).to.be.equal(url); - }); - it('should match https://1234-abc.runbot007.odoo.com/web#id=3&menu_id=221', () => { - const url = 'https://1234-abc.runbot007.odoo.com/web#id=3&menu_id=221'; - const text = `abc ${url} abc`; - const match = text.match(URL_REGEX_WITH_INFOS); - chai.expect(match[0]).to.be.equal(url); - }); -});