From 29dcb119f558edd66b46aa1e7372a82ae1a7bf38 Mon Sep 17 00:00:00 2001 From: Younn Olivier Date: Fri, 5 Aug 2022 16:29:18 +0000 Subject: [PATCH] [FIX] website: copy frontend link when using the anchor option Before this commit, clicking on the "anchor" icon from a snippet editor would copy the client action's url (introduced in [1]). This commit changes the snippet's path computation to use the target's ownerDocument location instead of the global one. [1]: https://github.com/odoo/odoo/commit/31cc10b91dc7762e23b4bde9b945be0c4ce3fe3b task-2687506 Part-of: odoo/odoo#96401 --- .../static/src/js/editor/snippets.options.js | 2 +- .../tours/snippet_editor_panel_options.js | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/addons/website/static/src/js/editor/snippets.options.js b/addons/website/static/src/js/editor/snippets.options.js index def4c2f75a3..6ff5c31bf9e 100644 --- a/addons/website/static/src/js/editor/snippets.options.js +++ b/addons/website/static/src/js/editor/snippets.options.js @@ -2458,7 +2458,7 @@ options.registry.anchor = options.Class.extend({ } this._setAnchorName(anchorName + n); } - return `${window.location.pathname}#${this.$target[0].id}`; + return `${this.ownerDocument.location.pathname}#${this.$target[0].id}`; }, /** * Creates a safe id/anchor from text. diff --git a/addons/website/static/tests/tours/snippet_editor_panel_options.js b/addons/website/static/tests/tours/snippet_editor_panel_options.js index f2bb5bc2c8e..0045f7e02b5 100644 --- a/addons/website/static/tests/tours/snippet_editor_panel_options.js +++ b/addons/website/static/tests/tours/snippet_editor_panel_options.js @@ -42,4 +42,35 @@ wTourUtils.dragNDrop({ console.error("The paragraph text selection was lost."); } }, +}, { + content: "Click on the anchor option", + trigger: '#oe_snippets .snippet-option-anchor we-button', + run() { + // The clipboard cannot be accessed from a script. + // https://w3c.github.io/editing/docs/execCommand/#dfn-the-copy-command + // The execCommand is patched for that step so that ClipboardJS still + // sends the 'success' event. + const oldExecCommand = document.execCommand; + document.execCommand = () => true; + this.$anchor[0].click(); + document.execCommand = oldExecCommand; + } +}, { + content: "Check the copied url from the notification toast", + trigger: '.o_notification_manager .o_notification_content', + run() { + const { textContent } = this.$anchor[0]; + const url = textContent.substring(textContent.indexOf('/')); + + // The url should not target the client action + if (url.startsWith('/@')) { + console.error('The anchor option should target the frontend'); + } + + const iframeDocument = document.querySelector('.o_iframe').contentDocument; + const snippetId = iframeDocument.querySelector('.s_text_image').id; + if (!url || url.indexOf(snippetId) < 0) { + console.error('The anchor option does not target the correct snippet.'); + } + }, }]);