From bc25b371ef560385c7e37e3875879be1a5a8e894 Mon Sep 17 00:00:00 2001 From: Nicolas Bayet Date: Wed, 14 Jul 2021 11:12:27 +0000 Subject: [PATCH] [FIX] web_editor: properly capture any form of url on paste The regex that automatically detect link when pasting text or when pressing space when editing was not good enough and for instance did not capture fully an url like: https://1234-abc.runbot007.odoo.com/web#id=3&menu_id=221 Task ID: 2602725 closes odoo/odoo#73972 X-original-commit: ebe74cb8017ebadbdb6b651e43c59a1d506ba8de Signed-off-by: Antoine Guenet --- .../static/lib/odoo-editor/src/utils/utils.js | 5 ++- .../lib/odoo-editor/test/editor-test.js | 1 + .../odoo-editor/test/spec/urlRegex.test.js | 43 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 addons/web_editor/static/lib/odoo-editor/test/spec/urlRegex.test.js 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); + }); +});