Skip to content

Commit 3233c74

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 3233c74

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-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

+28-1
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,33 @@ 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+
* @param {String} string
1601+
* @returns {String}
1602+
*/
1603+
export function buildLinkUrl(string) {
1604+
const match = URL_REGEX_WITH_INFOS.exec(string);
1605+
if (!match) {
1606+
return string;
1607+
}
1608+
const matchedString = match[0];
1609+
const schema = match[2];
1610+
const isEmail = !schema && matchedString.includes('@');
1611+
let prefix = '';
1612+
if (isEmail && !matchedString.startsWith('mailto:')) {
1613+
prefix = 'mailto:';
1614+
}
1615+
if (!isEmail && !schema) {
1616+
prefix = 'https://';
1617+
}
1618+
return prefix + matchedString;
1619+
}
1620+
15941621
/**
15951622
* Returns an array of url infos for url matched in the given string.
15961623
*
@@ -1602,7 +1629,7 @@ export function getUrlsInfosInString(string) {
16021629
match;
16031630
while ((match = URL_REGEX_WITH_INFOS.exec(string))) {
16041631
infos.push({
1605-
url: match[2] ? match[0] : 'https://' + match[0],
1632+
url: buildLinkUrl(string),
16061633
label: match[0],
16071634
index: match.index,
16081635
length: match[0].length,

0 commit comments

Comments
 (0)