[FIX] web_editor: update link url regex

In order to fix wrong url recognition, improve the url REGEX by:
- Recognize an url with a dot at the end in the string
  `www.google.be.`. It was not recognized before this commit.
- Do not allow a special character after an url that is not
  separated by "/", "?", or "#". Before this commit, the content
  `www.google.be!` would have been recognized the url as
  `www.google.be!` instead of `www.google.be`.
- Do not include in the url some character if they appear at the end.
  The chars are:
  - .
  - ,
  - }
  - )
  - ]
  - '
  - "

task-3373367

closes odoo/odoo#134660

X-original-commit: 0327ed28e855c89f9c13266c4aba5c99d80263ac
Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
This commit is contained in:
Nicolas Bayet
2023-09-07 14:45:28 +00:00
parent 8bd1d87880
commit 661705da13
2 changed files with 51 additions and 7 deletions
@@ -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]*(?<![.,})\\]'"]))?`;
const httpRegex = `(?:https?:\\/\\/)`;
const httpCapturedRegex= `(https?:\\/\\/)`;
export const URL_REGEX = new RegExp(`((?:(?:${httpRegex}${urlRegexBase}))`, 'gi');
export const URL_REGEX_WITH_INFOS = new RegExp(`((?:(?:${httpCapturedRegex}${urlRegexBase}))`, 'gi');
export const URL_REGEX = new RegExp(`((?:(?:${httpRegex}${urlRegexBase})`, 'gi');
export const URL_REGEX_WITH_INFOS = new RegExp(`((?:(?:${httpCapturedRegex}${urlRegexBase})`, 'gi');
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;
@@ -17,10 +17,17 @@ const convertToLink = createLink;
const unlink = async function (editor) {
editor.execCommand('unlink');
};
const testUrlRegex = (url) => {
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/');