From f261efa1d78aebb16651081972ecd2c70d6401c0 Mon Sep 17 00:00:00 2001 From: Antoine Guenet Date: Thu, 12 Jan 2023 16:39:19 +0000 Subject: [PATCH] [FIX] web_editor: properly convert backticks in split text nodes Wrong syntax made it so that having a text node after the text node in which we're inserting a closing backtick character, converted all the text after the backtick to the word "NaN". This fixes that syntax error and ensures such cases are properly handled. X-original-commit: 2833b42c09582dbcfed334d9fe6628a8eaf476b8 Part-of: odoo/odoo#110784 --- .../js/editor/odoo-editor/src/OdooEditor.js | 5 +-- .../odoo-editor/test/spec/editor.test.js | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 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 8055344966c..5b5270399bd 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 @@ -3200,9 +3200,8 @@ export class OdooEditor extends EventTarget { } sibling = textNode.nextSibling; while (sibling && sibling.nodeType === Node.TEXT_NODE) { - sibling.textContent =+ textNode.textContent; - textNode.remove(); - textNode = sibling; + textNode.textContent += sibling.textContent; + sibling.remove(); sibling = textNode.nextSibling; } setSelection(textNode, offset); diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/editor.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/editor.test.js index 37fa81d9b61..dc0c171485b 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/editor.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/editor.test.js @@ -5727,6 +5727,46 @@ X[] contentAfter: '

ab`cd`[]ef

', }); }); + it('should convert text into inline code even when text nodes are split', async () => { + // BEFORE + await testEditor(BasicEditor, { + contentBefore: '

b`c[]d

', + stepFunction: async editor => { + editor.document.getSelection().anchorNode.before(document.createTextNode('a')); + insertText(editor, '`'); + }, + contentAfter: '

abc\u200B[]d

', + }); + // AFTER + await testEditor(BasicEditor, { + contentBefore: '

a`b[]c

', + stepFunction: async editor => { + editor.document.getSelection().anchorNode.after(document.createTextNode('d')); + insertText(editor, '`'); + }, + contentAfter: '

ab\u200B[]cd

', + }); + // BOTH + await testEditor(BasicEditor, { + contentBefore: '

b`c[]d

', + stepFunction: async editor => { + editor.document.getSelection().anchorNode.before(document.createTextNode('a')); + editor.document.getSelection().anchorNode.after(document.createTextNode('e')); + insertText(editor, '`'); + }, + contentAfter: '

abc\u200B[]de

', + }); + }); + it('should convert text into inline code even when the other backtick is in a separate text node', async () => { + await testEditor(BasicEditor, { + contentBefore: '

ab[]c

', + stepFunction: async editor => { + editor.document.getSelection().anchorNode.before(document.createTextNode('`')); + insertText(editor, '`'); + }, + contentAfter: '

\u200Bab\u200B[]c

', + }); + }); }); });