From 6dd38640ce28c151e441e4f156e2d44e5389a685 Mon Sep 17 00:00:00 2001 From: David Monjoie Date: Thu, 4 May 2023 12:39:21 +0000 Subject: [PATCH] [FIX] web_editor: fix select all in empty paragraph The previous code with `parentsUntil` would not return any result in an empty paragraph because the only valid element would be the anchorNode itself but `parentsUntil` would only return parents and never the base node itself. It makes sense, but is not suitable for the case at hand. Before this commit: CTRL+A does nothing from inside an empty paragraph. After this commit: CTRL+A correctly triggers a select all from inside an empty paragraph. Task-3150956 closes odoo/odoo#121727 X-original-commit: 4bcba1d785afeaa8287adc48076f208d04fc72c3 Signed-off-by: David Monjoie (dmo) Co-authored-by: dhba-odoo Co-authored-by: David Monjoie --- .../static/src/js/wysiwyg/wysiwyg.js | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js index 6f984a1cea0..292900212c9 100644 --- a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js +++ b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js @@ -1840,21 +1840,16 @@ const Wysiwyg = Widget.extend({ if (e && e.key === 'a' && (e.ctrlKey || e.metaKey)) { e.preventDefault(); const selection = this.odooEditor.document.getSelection(); - const containerSelector = '#wrap>*, [contenteditable], .oe_structure>*'; - let $deepestParent = - selection ? - $(selection.anchorNode).parentsUntil(containerSelector).last() : - $(); - - if ($deepestParent.is('html')) { - // In case we didn't find a suitable container - // we need to restrict the selection inside to the editable area. - $deepestParent = this.$editable.find(containerSelector); - } - - if ($deepestParent.length) { + const containerSelector = '#wrap>*, .oe_structure>*, [contenteditable]'; + const container = + (selection && + closestElement(selection.anchorNode, containerSelector)) || + // In case a suitable container could not be found then the + // selection is restricted inside the editable area. + this.$editable.find(containerSelector); + if (container) { const range = document.createRange(); - range.selectNodeContents($deepestParent.parent()[0]); + range.selectNodeContents(container); selection.removeAllRanges(); selection.addRange(range); }