From d01a1bd382eb9dc6087f8ffd9a14457260116e8d Mon Sep 17 00:00:00 2001 From: Rodolpho Lima Date: Wed, 7 Jun 2023 15:55:15 +0000 Subject: [PATCH] [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#125382 X-original-commit: cb84f993bbf38130482c1f50e550bfdf94a60845 Signed-off-by: David Monjoie (dmo) --- .../static/src/js/backend/html_field.js | 8 ++--- .../static/tests/html_field_tests.js | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/addons/web_editor/static/src/js/backend/html_field.js b/addons/web_editor/static/src/js/backend/html_field.js index 5ba5e514834..319013c3194 100644 --- a/addons/web_editor/static/src/js/backend/html_field.js +++ b/addons/web_editor/static/src/js/backend/html_field.js @@ -805,10 +805,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'); } } diff --git a/addons/web_editor/static/tests/html_field_tests.js b/addons/web_editor/static/tests/html_field_tests.js index ee6ebb26c59..a77472e760b 100644 --- a/addons/web_editor/static/tests/html_field_tests.js +++ b/addons/web_editor/static/tests/html_field_tests.js @@ -252,4 +252,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: ` + + Relative link + Internal link + External link + `, + }]; + await makeView({ + type: "form", + resId: 1, + resModel: "partner", + serverData, + arch: ` +
+ + `, + }); + + for (const link of target.querySelectorAll('a')) { + assert.strictEqual(link.getAttribute('target'), '_blank'); + assert.strictEqual(link.getAttribute('rel'), 'noreferrer'); + } + }); });