From bcec057e909cd93c9b5dbc2313036630916d5be6 Mon Sep 17 00:00:00 2001 From: Rodolpho Lima Date: Thu, 23 Feb 2023 12:11:19 +0000 Subject: [PATCH] [FIX] web_editor: hide toolbar for fields with no HTML support Before this commit, only the immediate parent of a selected node was checked for HTML content support in order to determine the toolbar's visibility. This led to incorrectly displaying the toolbar for fields like the 'list-price' on an e-commerce product page, whose text content is wrapped in an extra 'span' element inside the field's span element. This commit fixes it by checking all of the node's ancestors up to the editable root for the attributes that determine if a field supports HTML content. This commit also prevents a server error if the user somehow styles the product's price (with ctrl+B for example. It's worth noting that such style would not be kept in the saved version of the page). lxml.HtmlElement's `text` property has `None` value if the span element does not contain any text before its first child element (see https://lxml.de/apidoc/lxml.html.html). Therefore an element like `42.00` would result in `None` when reading its `text` property. The `text_content` method is more suitable for such task as it returns the text content of an element and its children. task-3188550 opw-3171669 closes odoo/odoo#114867 X-original-commit: 524ba7f89286aa03ed6a4da30177c9ce7018c59a Signed-off-by: David Monjoie (dmo) --- addons/web_editor/models/ir_qweb_fields.py | 2 +- addons/web_editor/static/src/js/editor/snippets.editor.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/addons/web_editor/models/ir_qweb_fields.py b/addons/web_editor/models/ir_qweb_fields.py index e5262d966ac..bf6a75c7909 100644 --- a/addons/web_editor/models/ir_qweb_fields.py +++ b/addons/web_editor/models/ir_qweb_fields.py @@ -518,7 +518,7 @@ class Monetary(models.AbstractModel): def from_html(self, model, field, element): lang = self.user_lang() - value = element.find('span').text.strip() + value = element.find('span').text_content().strip() return float(value.replace(lang.thousands_sep, '') .replace(lang.decimal_point, '.')) diff --git a/addons/web_editor/static/src/js/editor/snippets.editor.js b/addons/web_editor/static/src/js/editor/snippets.editor.js index 4f99eb43114..d6392b5eb37 100644 --- a/addons/web_editor/static/src/js/editor/snippets.editor.js +++ b/addons/web_editor/static/src/js/editor/snippets.editor.js @@ -13,6 +13,7 @@ const SmoothScrollOnDrag = require('web/static/src/js/core/smooth_scroll_on_drag const {getCSSVariableValue} = require('web_editor.utils'); const gridUtils = require('@web_editor/js/common/grid_layout_utils'); const QWeb = core.qweb; +const {closestElement} = require('@web_editor/js/editor/odoo-editor/src/utils/utils'); var _t = core._t; @@ -4189,8 +4190,8 @@ var SnippetsMenu = Widget.extend({ } if (!range || !$currentSelectionTarget.parents('#wrapwrap, .iframe-editor-wrapper .o_editable').length || - $(selection.anchorNode).parent('[data-oe-model]:not([data-oe-type="html"]):not([data-oe-field="arch"]):not([data-oe-translation-initial-sha])').length || - $(selection.focusNode).parent('[data-oe-model]:not([data-oe-type="html"]):not([data-oe-field="arch"]):not([data-oe-translation-initial-sha])').length || + closestElement(selection.anchorNode, '[data-oe-model]:not([data-oe-type="html"]):not([data-oe-field="arch"]):not([data-oe-translation-initial-sha])') || + closestElement(selection.focusNode, '[data-oe-model]:not([data-oe-type="html"]):not([data-oe-field="arch"]):not([data-oe-translation-initial-sha])') || (e && $(e.target).closest('.fa, img').length || this.options.wysiwyg.lastMediaClicked && $(this.options.wysiwyg.lastMediaClicked).is('.fa, img')) || (this.options.wysiwyg.lastElement && !this.options.wysiwyg.lastElement.isContentEditable)