From 82bf73fe3049978d5fa0b6984e129c5aa2870d95 Mon Sep 17 00:00:00 2001 From: Gorash Date: Mon, 17 Jun 2019 13:52:21 +0000 Subject: [PATCH] [FIX] web_editor: avoid dialog error on email edition Under certain unknown conditions, the range cannot be applied and triggered an error. In order to allow the user to keep editing, we need to prevent the dialog from showing, hence the use of `console.error`. If this error appears, the carret is moved to the beginning of the focused node. If this error appears, then bullet can generate an error (I managed to have the bug once or simulate it by manually breaking the dom and range) closes odoo/odoo#34284 Signed-off-by: Nicolas Martinelli (nim) --- .../static/src/js/wysiwyg/plugin/bullet.js | 2 +- .../static/src/js/wysiwyg/plugin/helper.js | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/addons/web_editor/static/src/js/wysiwyg/plugin/bullet.js b/addons/web_editor/static/src/js/wysiwyg/plugin/bullet.js index 6a516dd2ad5..1d5c2d47a33 100644 --- a/addons/web_editor/static/src/js/wysiwyg/plugin/bullet.js +++ b/addons/web_editor/static/src/js/wysiwyg/plugin/bullet.js @@ -47,7 +47,7 @@ var BulletPlugin = AbstractPlugin.extend({ res = this._convertList(false, [], start, end, type); } else { var ul = this._createList(type); - res = [].slice.call(ul.children); + res = ul ? [].slice.call(ul.children) : []; } var startLeaf = this.context.invoke('HelperPlugin.firstLeaf', start.node); diff --git a/addons/web_editor/static/src/js/wysiwyg/plugin/helper.js b/addons/web_editor/static/src/js/wysiwyg/plugin/helper.js index 521afe6137a..a025d6a0082 100644 --- a/addons/web_editor/static/src/js/wysiwyg/plugin/helper.js +++ b/addons/web_editor/static/src/js/wysiwyg/plugin/helper.js @@ -1047,6 +1047,7 @@ var HelperPlugin = AbstractPlugin.extend({ var reAllNBSP = /\u00A0/g; var reMultipleSpace = /(\s){2,}/; + var textContent = range.sc.textContent; var startBlanks = range.sc.textContent.match(reStartBlanks)[0] || ''; var endBlanks = range.sc.textContent.match(reEndBlanks)[0] || ''; var trimmed = range.sc.textContent.replace(reStartBlanks, '').replace(reEndBlanks, ''); @@ -1060,7 +1061,25 @@ var HelperPlugin = AbstractPlugin.extend({ this._removeInvisibleChar(range); - range.select(); + try { + range.select(); + } catch (e) { + /* Under certain unknown conditions, the range cannot be + applied and triggered an error. In order to allow the user to + keep editing, we need to prevent the dialog from showing, hence + the use of `console.error`. + If this error appears, the carret is moved to the beginning + of the focused node.*/ + console.error(e); + console.error('Informations for debugging', [ + ['textContent', textContent], + ['trimmed', trimmed], + ['cleanContents', cleanContents], + ['start', range.sc.outerHTML || range.sc.textContent, range.so], + ['end', range.ec.outerHTML || range.ec.textContent, range.eo], + ['parent', range.sc.parentNode && range.sc.parentNode.outerHTML], + ]); + } this.context.invoke('editor.saveRange'); }, /**