[FIX] web_editor: conserve changes on edit link label

Issue:
======
When you update the link button label from the sidebar it loses its
style.

Steps to reproduce the issue:
=============================
- Got to website editor
- Insert a text block
- Added a button in the text block and any label
- Click on the button to edit it from the sidebar
- Change font size or font color or any style you want
- Update the label
- The style is lost

Origin of the issue:
====================
When updating the label, we search for the first child that has that
text, but when we have `ZWS start`  it will be considered as the first
child and then we update the inner text of the `a` element so we loose
the span of the text which has the custom styles.

Solution:
=========
We search for the first child which is not `ZWS`

task-3721686

closes odoo/odoo#157758

X-original-commit: 999d5787e3d8d0518692f987341dd6b298d7ffc4
Signed-off-by: Antoine Guenet (age) <age@odoo.com>
This commit is contained in:
Mahdi Cheikh Rouhou (macr)
2024-03-18 08:45:39 +00:00
parent 3ad350922f
commit 63fa6dd45d
@@ -475,13 +475,17 @@ export class Link extends Component {
$link.html(this.state.originalHTML);
} else if (linkInfos.content && linkInfos.content.length) {
let contentWrapperEl = $link[0];
// Update the first child element that has the same inner text
const text = $link[0].innerText.replaceAll("\u200B", "").trim();
// Update the first not ZWS child element that has the same inner text
// as the link with the new content while preserving child
// elements within the link. (e.g. the link is bold and italic)
while (contentWrapperEl.firstElementChild
&& (contentWrapperEl.firstElementChild.innerText === $link[0].innerText)) {
contentWrapperEl = contentWrapperEl.firstElementChild;
}
let child;
do {
contentWrapperEl = child || contentWrapperEl;
child = [...contentWrapperEl.children].find(
(element) => !element.hasAttribute("data-o-link-zws")
);
} while (child?.innerText.replaceAll('\u200B', '').trim() === text);
contentWrapperEl.innerText = linkInfos.content;
} else {
$link.text(linkInfos.url);