From f4ac261bbb831d19445425f0ef7c8f731669234e Mon Sep 17 00:00:00 2001 From: Rodolpho Lima Date: Wed, 20 Dec 2023 15:19:28 +0000 Subject: [PATCH] [FIX] web_editor: transform URL in multiple text nodes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, if a URL was contained in two or more text nodes, e.g.

"link" ".com"

, pressing the space key after the URL failed to convert it into a link. Such text node split can happen as a result of backspace or delete in the middle of a text node. This commit makes sure adjacent text nodes are merged before searching for a URL in its content. task-3468763 closes odoo/odoo#148362 X-original-commit: 04f9a90127eac2a403ffe77d1e90dbe659a8fa42 Signed-off-by: Geelen Sébastien (sge) Signed-off-by: Rodolpho Cammarosano de Lima (rcdl) --- .../static/src/js/editor/odoo-editor/src/OdooEditor.js | 2 ++ .../src/js/editor/odoo-editor/test/spec/editor.test.js | 10 ++++++++++ 2 files changed, 12 insertions(+) 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 04bb6cc17d7..8b3f8cd16cc 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 @@ -4682,6 +4682,8 @@ export class OdooEditor extends EventTarget { !closestElement(selection.anchorNode).closest('a') && selection.anchorNode.nodeType === Node.TEXT_NODE ) { + // Merge adjacent text nodes. + selection.anchorNode.parentNode.normalize(); const textSliced = selection.anchorNode.textContent.slice(0, selection.anchorOffset); const textNodeSplitted = textSliced.split(/\s/); const potentialUrl = textNodeSplitted.pop(); 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 ca5cae85585..08b77d11f3d 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 @@ -4297,6 +4297,16 @@ X[] contentAfter: '

a http://test.com b http://test.com[] c http://test.com d

', //in reality: '

a http://test.com b http://test.com [] c http://test.com d

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

http://test.com[]

', + stepFunction: async (editor) => { + // Setup: simulate multiple text nodes in a p:

"http://test" ".com"

+ editor.editable.firstChild.firstChild.splitText(11); + // Action: insert space + insertText(editor, ' '); + }, + contentAfter: '

http://test.com []

', + }); }); it('should transform url after enter', async () => { await testEditor(BasicEditor, {