Skip to content

Commit

Permalink
[IMP] html_editor: delete first element that is contenteditable=false
Browse files Browse the repository at this point in the history
New spec:
========
`ctrl+a` should select all the contents in the editable zone,
regardless if the first element is contenteditable=false .

The old behavior introduced int this [commit]

[commit]: odoo@440606c

closes odoo#173528

Signed-off-by: Rodolpho Cammarosano de Lima (rcdl) <[email protected]>
  • Loading branch information
Mtaylorr committed Jul 19, 2024
1 parent f4316e4 commit 40bf4d4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
9 changes: 1 addition & 8 deletions addons/html_editor/static/src/core/selection_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,7 @@ export class SelectionPlugin extends Plugin {
const containerSelector = "#wrap>*, .oe_structure>*, [contenteditable]";
const container =
selection && closestElement(selection.anchorNode, containerSelector);
let [anchorNode, anchorOffset] = startPos(container);
if (
anchorNode.firstChild &&
anchorNode.firstChild.isContentEditable === false
) {
anchorNode = anchorNode.firstChild.nextSibling;
anchorOffset = 0;
}
const [anchorNode, anchorOffset] = startPos(container);
const [focusNode, focusOffset] = endPos(container);
this.setSelection({ anchorNode, anchorOffset, focusNode, focusOffset });
}
Expand Down
30 changes: 20 additions & 10 deletions addons/html_editor/static/tests/banner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ test("remove all content should preserves the first paragraph tag inside the ban
);
});

test("first element of an editable that is contenteditable='false' must not be selected with ctrl+a", async () => {
test("Everything gets selected with ctrl+a, including a contenteditable=false as first element", async () => {
const { el, editor } = await setupEditor("<p>[]</p>");
insertText(editor, "/bannerinfo");
press("enter");
Expand All @@ -93,22 +93,32 @@ test("first element of an editable that is contenteditable='false' must not be s
insertText(editor, "Test2");
press(["ctrl", "a"]);
expect(getContent(el)).toBe(
`<div class="o_editor_banner o_not_editable lh-1 d-flex align-items-center alert alert-info pb-0 pt-3" role="status" contenteditable="false">
`[<div class="o_editor_banner o_not_editable lh-1 d-flex align-items-center alert alert-info pb-0 pt-3" role="status" contenteditable="false">
<i class="o_editor_banner_icon mb-3 fst-normal" aria-label="Banner Info">💡</i>
<div class="w-100 ms-3" contenteditable="true">
<p><br></p>
</div>
</div><p>[Test1</p><p>Test2<br></p>]`,
{ message: "should not select the banner if it s the first element in the editable" }
</div><p>Test1</p><p>Test2<br></p>]`,
{ message: "should select everything" }
);

press("Backspace");
expect(getContent(el)).toBe(
`<div class="o_editor_banner o_not_editable lh-1 d-flex align-items-center alert alert-info pb-0 pt-3" role="status" contenteditable="false">
<i class="o_editor_banner_icon mb-3 fst-normal" aria-label="Banner Info">💡</i>
<div class="w-100 ms-3" contenteditable="true">
<p><br></p>
</div>
</div><p placeholder="Type "/" for commands" class="o-we-hint">[]<br></p>`
`<p placeholder="Type "/" for commands" class="o-we-hint">[]<br></p>`
);
});

test("Everything gets selected with ctrl+a, including a contenteditable=false as first two elements", async () => {
const { el } = await setupEditor(
'<div contenteditable="false">a</div><div contenteditable="false">b</div><p>cd[]</p>'
);
press(["ctrl", "a"]);
expect(getContent(el)).toBe(
'[<div contenteditable="false">a</div><div contenteditable="false">b</div><p>cd</p>]'
);

press("Backspace");
expect(getContent(el)).toBe(
`<p placeholder="Type "/" for commands" class="o-we-hint">[]<br></p>`
);
});

0 comments on commit 40bf4d4

Please sign in to comment.