diff --git a/addons/web_editor/static/lib/odoo-editor/src/utils/utils.js b/addons/web_editor/static/lib/odoo-editor/src/utils/utils.js index c80cf38a34e..decba9fa0b1 100644 --- a/addons/web_editor/static/lib/odoo-editor/src/utils/utils.js +++ b/addons/web_editor/static/lib/odoo-editor/src/utils/utils.js @@ -25,9 +25,10 @@ export const CTGROUPS = { BR: CTYPES.BR, }; -export const URL_REGEX = /((?:https?:\/\/)?(?:[a-z0-9-]{1,63}\.){1,2}[a-z]{2,15}(?:\/[^\s]*)?)/gi; +export const URL_REGEX = + /((?:(?:https?:\/\/)|(?:[-a-zA-Z0-9@:%._\+~#=]{1,64}\.))[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,63}\b(?:[^\s]*))/gi; export const URL_REGEX_WITH_INFOS = - /((https?:\/\/)?([a-z0-9-]{1,63}\.){1,2}[a-z]{2,15}(\/[^\s]*)?(\s|$))/gi; + /((?:(https?:\/\/)|(?:[-a-zA-Z0-9@:%._\+~#=]{1,64}\.))[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,63}\b(?:[^\s]*))/gi; export const YOUTUBE_URL_GET_VIDEO_ID = /^(?:(?:https?:)?\/\/)?(?:(?:www|m)\.)?(?:youtube\.com|youtu\.be)(?:\/(?:[\w-]+\?v=|embed\/|v\/)?)([^\s?&#]+)(?:\S+)?$/i; diff --git a/addons/web_editor/static/lib/odoo-editor/test/editor-test.js b/addons/web_editor/static/lib/odoo-editor/test/editor-test.js index 3c5be85dcf7..7b2b15237e3 100644 --- a/addons/web_editor/static/lib/odoo-editor/test/editor-test.js +++ b/addons/web_editor/static/lib/odoo-editor/test/editor-test.js @@ -7,6 +7,7 @@ import './spec/fontSize.test.js'; import './spec/insertHTML.test.js'; import './spec/fontAwesome.test.js'; import './spec/autostep.test.js'; +import './spec/urlRegex.test.js'; mocha.run(failures => { if (failures) { diff --git a/addons/web_editor/static/lib/odoo-editor/test/spec/urlRegex.test.js b/addons/web_editor/static/lib/odoo-editor/test/spec/urlRegex.test.js new file mode 100644 index 00000000000..5b84700f78a --- /dev/null +++ b/addons/web_editor/static/lib/odoo-editor/test/spec/urlRegex.test.js @@ -0,0 +1,43 @@ +import { URL_REGEX } from '../../src/OdooEditor.js'; + +describe('urlRegex', () => { + it('should not match foo.com', () => { + const url = 'foo.com'; + const text = `abc ${url} abc`; + const match = text.match(URL_REGEX); + 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); + 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); + 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); + 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); + 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); + chai.expect(match[0]).to.be.equal(url); + }); +});