Skip to content

Commit 50128cc

Browse files
committed
CPT-490 Fix links in Odoo Editor
Note that the file(s) have been moved from addons/web_editor/static/lib/odoo-editor/src to addons/web_editor/static/src/js/editor/odoo-editor/src from Odoo 16 onwards.
1 parent 1a47426 commit 50128cc

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import {
6868
setCursorStart,
6969
paragraphRelatedElements,
7070
isUnbreakable,
71+
buildLinkUrl,
7172
} from './utils/utils.js';
7273
import { editorCommands } from './commands/commands.js';
7374
import { Powerbox } from './powerbox/Powerbox.js';
@@ -3310,7 +3311,7 @@ export class OdooEditor extends EventTarget {
33103311
const selectionIsInsideALink = !!closestElement(sel.anchorNode, 'a');
33113312
if (splitAroundUrl.length === 3 && !splitAroundUrl[0] && !splitAroundUrl[2]) {
33123313
// Pasted content is a single URL.
3313-
const url = /^https?:\/\//i.test(text) ? text : 'https://' + text;
3314+
const url = buildLinkUrl(text);
33143315
const youtubeUrl = this.options.allowCommandVideo &&YOUTUBE_URL_GET_VIDEO_ID.exec(url);
33153316
const urlFileExtention = url.split('.').pop();
33163317
const isImageUrl = ['jpg', 'jpeg', 'png', 'gif', 'svg'].includes(urlFileExtention.toLowerCase());
@@ -3399,9 +3400,7 @@ export class OdooEditor extends EventTarget {
33993400
} else {
34003401
this.historyPauseSteps();
34013402
for (let i = 0; i < splitAroundUrl.length; i++) {
3402-
const url = /^https?:\/\//gi.test(splitAroundUrl[i])
3403-
? splitAroundUrl[i]
3404-
: 'https://' + splitAroundUrl[i];
3403+
const url = buildLinkUrl(splitAroundUrl[i]);
34053404
// Even indexes will always be plain text, and odd indexes will always be URL.
34063405
// only allow images emebed inside an existing link. No other url or video embed.
34073406
if (i % 2 && !selectionIsInsideALink) {

addons/web_editor/static/lib/odoo-editor/src/utils/utils.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,37 @@ export function makeContentsInline(node) {
15911591
}
15921592
}
15931593

1594+
/**
1595+
* Prepends mailto: or https:// if the string matches
1596+
* the URL regex. Returns the original string if the
1597+
* the regex does not match or the original string
1598+
* already contains mailto: or https://.
1599+
*
1600+
* If there is already a regex match for the string,
1601+
* it should be passed here to avoid infinite looping.
1602+
*
1603+
* @param {String} string
1604+
* @param {String} match (optional)
1605+
* @returns {String}
1606+
*/
1607+
export function buildLinkUrl(string, match) {
1608+
match = match || URL_REGEX_WITH_INFOS.exec(string);
1609+
if (!match) {
1610+
return string;
1611+
}
1612+
const matchedString = match[0];
1613+
const schema = match[2];
1614+
const isEmail = !schema && matchedString.includes('@');
1615+
let prefix = '';
1616+
if (isEmail && !matchedString.startsWith('mailto:')) {
1617+
prefix = 'mailto:';
1618+
}
1619+
if (!isEmail && !schema) {
1620+
prefix = 'https://';
1621+
}
1622+
return prefix + matchedString;
1623+
}
1624+
15941625
/**
15951626
* Returns an array of url infos for url matched in the given string.
15961627
*
@@ -1602,7 +1633,7 @@ export function getUrlsInfosInString(string) {
16021633
match;
16031634
while ((match = URL_REGEX_WITH_INFOS.exec(string))) {
16041635
infos.push({
1605-
url: match[2] ? match[0] : 'https://' + match[0],
1636+
url: buildLinkUrl(string, match),
16061637
label: match[0],
16071638
index: match.index,
16081639
length: match[0].length,

0 commit comments

Comments
 (0)