Skip to content
Snippets Groups Projects
Commit cb84f993 authored by Rodolpho Lima's avatar Rodolpho Lima
Browse files

[FIX] web_editor: readonly links opening in the same tab


Before this commit, when viewing the content of an html field in
readonly mode, internal links (same origin or relative links) that did
not have the target attribute set to "_blank" would be opened in the
same browser tab. This is undesired, and particularly disturbing when
the content is inside an iframe.

This commit ensures that, in readonly mode, all links are opened
in a new tab.

task-3323602

closes odoo/odoo#125024

Signed-off-by: default avatarDavid Monjoie (dmo) <dmo@odoo.com>
parent a28b6a6f
Branches
Tags
No related merge requests found
......@@ -734,10 +734,10 @@ function stripHistoryIds(value) {
return value && value.replace(/\sdata-last-history-steps="[^"]*?"/, '') || value;
}
// Ensure all external links are opened in a new tab.
// Ensure all links are opened in a new tab.
const retargetLinks = (container) => {
for (const externalLink of container.querySelectorAll(`a:not([href^="${location.origin}"]):not([href^="/"])`)) {
externalLink.setAttribute('target', '_blank');
externalLink.setAttribute('rel', 'noreferrer');
for (const link of container.querySelectorAll('a')) {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noreferrer');
}
}
......@@ -256,4 +256,33 @@ QUnit.module("WebEditor.HtmlField", ({ beforeEach }) => {
assert.containsOnce(target, '.o_field_html[name="txt"] iframe[sandbox="allow-same-origin allow-popups allow-popups-to-escape-sandbox"]');
});
QUnit.module('Readonly mode');
QUnit.test("Links should open on a new tab", async (assert) => {
assert.expect(6);
serverData.models.partner.records = [{
id: 1,
txt: `
<body>
<a href="/contactus">Relative link</a>
<a href="${location.origin}/contactus">Internal link</a>
<a href="https://google.com">External link</a>
</body>`,
}];
await makeView({
type: "form",
resId: 1,
resModel: "partner",
serverData,
arch: `
<form>
<field name="txt" widget="html" readonly="1"/>
</form>`,
});
for (const link of target.querySelectorAll('a')) {
assert.strictEqual(link.getAttribute('target'), '_blank');
assert.strictEqual(link.getAttribute('rel'), 'noreferrer');
}
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment