From bb543d75a920720587deeb77eaa246e2558ebb6a Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 25 Jul 2022 10:24:03 +0000 Subject: [PATCH] [FIX] web_editor: breaking _onBeforeUnload when canceling edition After updating mass_mailing_snippets_menu_tabs to cancel the template's edition at the end of the tour, the editor starts blowing up during cleanup with: ``` Trying to set result to failed (UncaughtTypeError: Cannot read properties of null (reading 'anchorNode') at Sanitize._parse (/web/assets/363-d1667a2/web_editor.assets_wysiwyg.min.js:1453:105) at Sanitize.parse (/web/assets/363-d1667a2/web_editor.assets_wysiwyg.min.js:1450:6) at new Sanitize (/web/assets/363-d1667a2/web_editor.assets_wysiwyg.min.js:1448:6) at sanitize (/web/assets/363-d1667a2/web_editor.assets_wysiwyg.min.js:1460:53) at OdooEditor.cleanForSave (/web/assets/363-d1667a2/web_editor.assets_wysiwyg.min.js:1372:37) at Class.getValue (/web/assets/363-d1667a2/web_editor.assets_wysiwyg.min.js:2547:1121) at Class.isDirty (/web/assets/363-d1667a2/web_editor.assets_wysiwyg.min.js:2547:407) at _onBeforeUnload (/web/assets/363-d1667a2/web_editor.assets_wysiwyg.min.js:2509:560) ``` After consultation with the relevant team, there are edition contexts where it's perfectly valid to have no "live" selection for one reason or an other, so this should be fixed. Ideally the field should also properly be discarded such that the event listener is removed and the callback is never called at all, however the legacy client has no such hook at the field level (the controller seems to be the lowest). closes odoo/odoo#96517 Related: odoo/documentation#2550 Related: odoo/enterprise#29824 Signed-off-by: Xavier Morel (xmo) --- .../static/src/js/editor/odoo-editor/src/OdooEditor.js | 2 +- .../static/src/js/editor/odoo-editor/src/utils/sanitize.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js index 4ee545cf2e6..33a9ac919bb 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js @@ -1686,7 +1686,7 @@ export class OdooEditor extends EventTarget { */ _computeHistorySelection() { const sel = this.document.getSelection(); - if (!sel.anchorNode) { + if (!(sel && sel.anchorNode)) { return this._latestComputedSelection; } this._latestComputedSelection = { diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/sanitize.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/sanitize.js index 1904f8c3ff3..71550aecb97 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/sanitize.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/utils/sanitize.js @@ -123,7 +123,8 @@ class Sanitize { node = nodeP; } - const anchor = this.root.ownerDocument.getSelection().anchorNode; + const selection = this.root.ownerDocument.getSelection(); + const anchor = selection && selection.anchorNode; const anchorEl = anchor && closestElement(anchor); // Remove zero-width spaces added by `fillEmpty` when there is // content and the selection is not next to it.