import { DIRECTIONS, nodeSize } from "@html_editor/utils/position"; import { ensureFocus, getAdjacentCharacter, getCursorDirection, } from "@html_editor/utils/selection"; import { describe, expect, test } from "@odoo/hoot"; import { dispatch } from "@odoo/hoot-dom"; import { insertText, setupEditor, testEditor } from "../_helpers/editor"; import { unformat } from "../_helpers/format"; import { setSelection } from "../_helpers/selection"; describe("ensureFocus", () => { // TODO @phoenix: unskipped when ensureFocus is add in the code base test.todo( "should preserve the focus on the child of this.editable when executing a powerbox command even if it is enclosed in a contenteditable=false", async () => { await testEditor({ contentBefore: unformat(`
[]
[]
focusWasConserved
[]
[]
[]
a[bc]d
", stepFunction: (editor) => { const { anchorNode, anchorOffset, focusNode, focusOffset } = editor.document.getSelection(); expect(getCursorDirection(anchorNode, anchorOffset, focusNode, focusOffset)).toBe( DIRECTIONS.RIGHT ); }, }); }); test("should identify a backward selection", async () => { await testEditor({ contentBefore: "a]bc[d
", stepFunction: (editor) => { const { anchorNode, anchorOffset, focusNode, focusOffset } = editor.document.getSelection(); expect(getCursorDirection(anchorNode, anchorOffset, focusNode, focusOffset)).toBe( DIRECTIONS.LEFT ); }, }); }); test("should identify a collapsed selection", async () => { await testEditor({ contentBefore: "ab[]cd
", stepFunction: (editor) => { const { anchorNode, anchorOffset, focusNode, focusOffset } = editor.document.getSelection(); expect(getCursorDirection(anchorNode, anchorOffset, focusNode, focusOffset)).toBe( false ); }, }); }); }); describe("getAdjacentCharacter", () => { test("should return the ZWS character before the cursor", async () => { const { editor, el } = await setupEditor("abc\u200b
"); const p = el.firstChild; // Place the cursor at the end of the P (not in a leaf node) setSelection({ anchorNode: p, anchorOffset: nodeSize(p) }); const selection = editor.document.getSelection(); expect(getAdjacentCharacter(selection, "previous", el)).toBe("\u200b"); }); });