[FIX] web_editor: transform URL in multiple text nodes

Before this commit, if a URL was contained in two or more text nodes,
e.g. <p>"link" ".com"</p>, 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) <sge@odoo.com>
Signed-off-by: Rodolpho Cammarosano de Lima (rcdl) <rcdl@odoo.com>
This commit is contained in:
Rodolpho Lima
2024-01-09 06:28:01 +00:00
parent ac40c6eabb
commit f4ac261bbb
2 changed files with 12 additions and 0 deletions
@@ -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();
@@ -4297,6 +4297,16 @@ X[]
contentAfter: '<p>a http://test.com b <a href="http://test.com">http://test.com</a>[] c http://test.com d</p>',
//in reality: '<p>a http://test.com b <a href="http://test.com">http://test.com</a>&nbsp;[] c http://test.com d</p>'
});
await testEditor(BasicEditor, {
contentBefore: '<p>http://test.com[]</p>',
stepFunction: async (editor) => {
// Setup: simulate multiple text nodes in a p: <p>"http://test" ".com"</p>
editor.editable.firstChild.firstChild.splitText(11);
// Action: insert space
insertText(editor, ' ');
},
contentAfter: '<p><a href="http://test.com">http://test.com</a> []</p>',
});
});
it('should transform url after enter', async () => {
await testEditor(BasicEditor, {