Skip to content

Commit 0ae767e

Browse files
committed
[FIX] html_editor: prevent removal of oe_unremovable with link tools
Steps to reproduce: - Open website builder - Click on "Contact Us" button in the header - Click on "Remove Link" - Save - Bug: Website is dead Website relies on some elements to not be removed from the view in order to function correctly. These elements are marked with the class `oe_unremovable`. And the editor/builder is supposed to not remove them. With the website builder refactor, the link tools from html_editor is now used. But it lacked proper handling of `oe_unremovable`, which could lead to removing a critical element of a website view Website refactor: 9fe45e2 task-4367641 closes odoo#218453 X-original-commit: 881dcc0 Signed-off-by: Géry Debongnie <[email protected]> Signed-off-by: Sébastien Blondiau (blse) <[email protected]>
1 parent 13e0073 commit 0ae767e

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

addons/html_editor/static/src/main/link/link_plugin.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export class LinkPlugin extends Plugin {
164164
return (
165165
!!linkEl &&
166166
!this.isLinkImmutable(linkEl) &&
167+
!this.isUnremovable(linkEl) &&
167168
isHtmlContentSupported(selection)
168169
);
169170
},
@@ -589,6 +590,7 @@ export class LinkPlugin extends Plugin {
589590
recordInfo: this.config.getRecordInfo?.() || {},
590591
canEdit:
591592
!this.linkInDocument || !this.linkInDocument.classList.contains("o_link_readonly"),
593+
canRemove: this.linkInDocument && !this.isUnremovable(this.linkInDocument),
592594
canUpload: this.config.allowFile,
593595
onUpload: this.config.onAttachmentChange,
594596
type: this.type || "",
@@ -799,10 +801,17 @@ export class LinkPlugin extends Plugin {
799801
this.dependencies.selection.setCursorEnd(linkElement);
800802
}
801803

804+
isUnremovable(linkEl) {
805+
return this.getResource("unremovable_node_predicates").some((p) => p(linkEl));
806+
}
807+
802808
/**
803809
* Remove the link from the collapsed selection
804810
*/
805811
removeLinkInDocument(link = this.linkInDocument) {
812+
if (this.isUnremovable(link)) {
813+
return;
814+
}
806815
const cursors = this.dependencies.selection.preserveSelection();
807816
if (link && link.isContentEditable) {
808817
cursors.update(callbacksForCursorUpdate.unwrap(link));

addons/html_editor/static/src/main/link/link_popover.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ export class LinkPopover extends Component {
3030
LinkPopoverState: Object,
3131
recordInfo: Object,
3232
canEdit: { type: Boolean, optional: true },
33+
canRemove: { type: Boolean, optional: true },
3334
canUpload: { type: Boolean, optional: true },
3435
onUpload: { type: Function, optional: true },
3536
allowCustomStyle: { type: Boolean, optional: true },
3637
allowTargetBlank: { type: Boolean, optional: true },
3738
};
3839
static defaultProps = {
3940
canEdit: true,
41+
canRemove: true,
4042
};
4143
static components = { CheckBox };
4244
colorsData = [

addons/html_editor/static/src/main/link/link_popover.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
<a href="#" class="mx-1 o_we_edit_link text-dark" t-on-click="onClickEdit" title="Edit Link">
140140
<i class="fa fa-edit"></i>
141141
</a>
142-
<a href="#" class="ms-1 o_we_remove_link text-dark" t-on-click="onClickRemove" title="Remove Link">
142+
<a href="#" t-if="props.canRemove" class="ms-1 o_we_remove_link text-dark" t-on-click="onClickRemove" title="Remove Link">
143143
<i class="fa fa-chain-broken"></i>
144144
</a>
145145
</t>

addons/html_editor/static/tests/link/popover.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ describe("should open a popover", () => {
8080
expect(".o_we_edit_link").toHaveCount(1);
8181
expect(".o_we_remove_link").toHaveCount(1);
8282
});
83+
test("link popover should not have the remove button when link is unremovable", async () => {
84+
await setupEditor('<p>a<a class="oe_unremovable" href="http://test.test/">bcd[]</a>e</p>');
85+
await expectElementCount(".o-we-linkpopover", 1);
86+
expect(".o_we_copy_link").toHaveCount(1);
87+
expect(".o_we_edit_link").toHaveCount(1);
88+
expect(".o_we_remove_link").toHaveCount(0);
89+
});
8390
test("link popover should not repositioned when clicking in the input field", async () => {
8491
await setupEditor("<p>this is a <a>li[]nk</a></p>");
8592
await waitFor(".o_we_href_input_link");

addons/html_editor/static/tests/toolbar.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ test("toolbar link buttons react to selection change", async () => {
220220
expect(".btn[name='unlink']").toHaveCount(1);
221221
});
222222

223+
test("toolbar unlink button should not be present when link is unremovable", async () => {
224+
await setupEditor('<p>a<a class="oe_unremovable" href="http://test.test/">bc[d]</a>e</p>');
225+
await waitFor(".o-we-toolbar");
226+
expect(".btn[name='link']").toHaveCount(1);
227+
expect(".btn[name='link']").toHaveClass("active");
228+
expect(".btn[name='unlink']").toHaveCount(0);
229+
});
230+
223231
test("toolbar format buttons should react to format change", async () => {
224232
await setupEditor(
225233
`<div class="o-paragraph">[\ufeff<a href="http://test.com">\ufefftest.com\ufeff</a>\ufeff&nbsp;]</div>`

0 commit comments

Comments
 (0)