From 59f45f7c47340327043576350ca6a535cd91b4bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Geelen=20=28sge=29?= Date: Fri, 24 Jun 2022 07:17:04 +0000 Subject: [PATCH] [FIX] web_editor: correct link behaviors * Broaden the valid URL pool by including some URL without subdomain if they have a white listed TLD. * Change a link 'href' when we change the link label, if the new label is a valid URL. * Ensure the domain extension cannot be only digit. task-2858621 task-2901795 closes odoo/odoo#97297 X-original-commit: 500bc6dc9958fe8a9ca396f87544e8866efa744b Signed-off-by: David Monjoie (dmo) --- .../editor/odoo-editor/src/utils/sanitize.js | 16 +++++++-- .../js/editor/odoo-editor/src/utils/utils.js | 33 ++++++++++++++++--- 2 files changed, 43 insertions(+), 6 deletions(-) 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 5d3939836ee..1904f8c3ff3 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 @@ -13,6 +13,9 @@ import { isMediaElement, getDeepRange, isUnbreakable, + closestElement, + getUrlsInfosInString, + URL_REGEX, } from './utils.js'; const NOT_A_NUMBER = /[^\d]/g; @@ -120,9 +123,10 @@ class Sanitize { node = nodeP; } + const anchor = this.root.ownerDocument.getSelection().anchorNode; + const anchorEl = anchor && closestElement(anchor); // Remove zero-width spaces added by `fillEmpty` when there is // content and the selection is not next to it. - const anchor = this.root.ownerDocument.getSelection().anchorNode; if ( node.nodeType === Node.TEXT_NODE && node.textContent.includes('\u200B') && @@ -187,7 +191,15 @@ class Sanitize { ) { node.setAttribute('contenteditable', 'false'); } - + // update link URL if label is a new valid link + if (node.nodeName === 'A' && anchorEl === node) { + const linkLabel = node.textContent; + const match = linkLabel.match(URL_REGEX); + if (match && match[0] === node.textContent) { + const urlInfo = getUrlsInfosInString(linkLabel)[0]; + node.setAttribute('href', urlInfo.url); + } + } if (node.firstChild) { this._parse(node.firstChild); } 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 1a1a2207d2a..e97c4167e6b 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 @@ -23,11 +23,36 @@ export const CTGROUPS = { BLOCK: CTYPES.BLOCK_OUTSIDE | CTYPES.BLOCK_INSIDE, BR: CTYPES.BR, }; +const tldWhitelist = [ + 'com', 'net', 'org', 'ac', 'ad', 'ae', 'af', 'ag', 'ai', 'al', 'am', 'an', + 'ao', 'aq', 'ar', 'as', 'at', 'au', 'aw', 'ax', 'az', 'ba', 'bb', 'bd', + 'be', 'bf', 'bg', 'bh', 'bi', 'bj', 'bl', 'bm', 'bn', 'bo', 'br', 'bq', + 'bs', 'bt', 'bv', 'bw', 'by', 'bz', 'ca', 'cc', 'cd', 'cf', 'cg', 'ch', + 'ci', 'ck', 'cl', 'cm', 'cn', 'co', 'cr', 'cs', 'cu', 'cv', 'cw', 'cx', + 'cy', 'cz', 'dd', 'de', 'dj', 'dk', 'dm', 'do', 'dz', 'ec', 'ee', 'eg', + 'eh', 'er', 'es', 'et', 'eu', 'fi', 'fj', 'fk', 'fm', 'fo', 'fr', 'ga', + 'gb', 'gd', 'ge', 'gf', 'gg', 'gh', 'gi', 'gl', 'gm', 'gn', 'gp', 'gq', + 'gr', 'gs', 'gt', 'gu', 'gw', 'gy', 'hk', 'hm', 'hn', 'hr', 'ht', 'hu', + 'id', 'ie', 'il', 'im', 'in', 'io', 'iq', 'ir', 'is', 'it', 'je', 'jm', + 'jo', 'jp', 'ke', 'kg', 'kh', 'ki', 'km', 'kn', 'kp', 'kr', 'kw', 'ky', + 'kz', 'la', 'lb', 'lc', 'li', 'lk', 'lr', 'ls', 'lt', 'lu', 'lv', 'ly', + 'ma', 'mc', 'md', 'me', 'mf', 'mg', 'mh', 'mk', 'ml', 'mm', 'mn', 'mo', + 'mp', 'mq', 'mr', 'ms', 'mt', 'mu', 'mv', 'mw', 'mx', 'my', 'mz', 'na', + 'nc', 'ne', 'nf', 'ng', 'ni', 'nl', 'no', 'np', 'nr', 'nu', 'nz', 'om', + 'pa', 'pe', 'pf', 'pg', 'ph', 'pk', 'pl', 'pm', 'pn', 'pr', 'ps', 'pt', + 'pw', 'py', 'qa', 're', 'ro', 'rs', 'ru', 'rw', 'sa', 'sb', 'sc', 'sd', + 'se', 'sg', 'sh', 'si', 'sj', 'sk', 'sl', 'sm', 'sn', 'so', 'sr', 'ss', + 'st', 'su', 'sv', 'sx', 'sy', 'sz', 'tc', 'td', 'tf', 'tg', 'th', 'tj', + 'tk', 'tl', 'tm', 'tn', 'to', 'tp', 'tr', 'tt', 'tv', 'tw', 'tz', 'ua', + 'ug', 'uk', 'um', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn', + 'vu', 'wf', 'ws', 'ye', 'yt', 'yu', 'za', 'zm', 'zr', 'zw', 'co\\.uk']; -export const URL_REGEX = - /((?:(?:https?:\/\/)|(?:[-a-zA-Z0-9@:%._\+~#=]{1,64}\.))[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-zA-Z0-9()]{2,63}\b(?:(?!\.)[^\s]*))/gi; -export const URL_REGEX_WITH_INFOS = - /((?:(https?:\/\/)|(?:[-a-zA-Z0-9@:%._\+~#=]{1,64}\.))[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-zA-Z0-9()]{2,63}\b(?:(?!\.)[^\s]*))/gi; +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 YOUTUBE_URL_GET_VIDEO_ID = /^(?:(?:https?:)?\/\/)?(?:(?:www|m)\.)?(?:youtube\.com|youtu\.be)(?:\/(?:[\w-]+\?v=|embed\/|v\/)?)([^\s?&#]+)(?:\S+)?$/i;