[FIX] web_editor: make sure all external links open in new tab

This collects all external links on clean the editor so as to make them
open in a new tab.

task-2740021

closes odoo/odoo#86289

X-original-commit: 8d2c8b4bad7a7aaa313aad361b48d77823841dbe
Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
This commit is contained in:
Antoine Guenet
2022-03-14 09:42:35 +00:00
parent 32c901f7ea
commit 8f7d64e2de
2 changed files with 15 additions and 3 deletions
@@ -307,6 +307,7 @@ var FieldHtml = basic_fields.DebouncedField.extend(TranslatableFieldMixin, {
var def = new Promise(function (resolve) {
resolver = resolve;
});
const externalLinkSelector = `a:not([href^="${location.origin}"]):not([href^="/"])`;
if (this.nodeOptions.cssReadonly) {
this.$iframe = $('<iframe class="o_readonly d-none"/>');
this.$iframe.appendTo(this.$el);
@@ -372,6 +373,12 @@ var FieldHtml = basic_fields.DebouncedField.extend(TranslatableFieldMixin, {
self._onClick(ev);
}
});
// Ensure all external links are opened in a new tab.
for (const externalLink of cwindow.document.body.querySelectorAll(externalLinkSelector)) {
externalLink.setAttribute('target', '_blank');
externalLink.setAttribute('rel', 'noreferrer');
}
});
});
} else {
@@ -379,6 +386,11 @@ var FieldHtml = basic_fields.DebouncedField.extend(TranslatableFieldMixin, {
this.$content.appendTo(this.$el);
this._qwebPlugin = new QWebPlugin();
this._qwebPlugin.sanitizeElement(this.$content[0]);
// Ensure all external links are opened in a new tab.
for (const externalLink of this.$content.find(externalLinkSelector)) {
externalLink.setAttribute('target', '_blank');
externalLink.setAttribute('rel', 'noreferrer');
}
resolver();
}
@@ -448,7 +448,7 @@ QUnit.module('web_editor', {}, function () {
});
let $field = form.$('.oe_form_field[name="body"]');
assert.strictEqual($field.children('.o_readonly').html(),
'<p><a href="https://www.external.com" target="_blank">External website</a></p>',
'<p><a href="https://www.external.com" target="_blank" rel="noreferrer">External website</a></p>',
"should have rendered a div with correct content in readonly");
const promise = new Promise((resolve) => _formResolveTestPromise = resolve);
@@ -476,7 +476,7 @@ QUnit.module('web_editor', {}, function () {
$field = form.$('.oe_form_field[name="body"]');
assert.strictEqual($field.children('.o_readonly').html(),
'<p><a href="https://www.external.com" target="_blank">External website</a></p>',
'<p><a href="https://www.external.com" target="_blank" rel="noreferrer">External website</a></p>',
"the link shouldn't change");
testUtils.mock.unpatch(LinkDialog);
@@ -575,7 +575,7 @@ QUnit.module('web_editor', {}, function () {
$field = form.$('.oe_form_field[name="body"]');
assert.strictEqual($field.children('.o_readonly').html(),
'<p><a href="http://www.test.com" target="_blank">New external link</a></p>',
'<p><a href="http://www.test.com" target="_blank" rel="noreferrer">New external link</a></p>',
"the link should be created with the right format");
testUtils.mock.unpatch(LinkDialog);