import { describe, expect, test } from "@odoo/hoot"; import { testEditor, setupEditor } from "../_helpers/editor"; import { unlinkFromPopover, unlinkByCommand, unlinkFromToolbar } from "../_helpers/user_actions"; import { getContent, setSelection } from "../_helpers/selection"; describe("range collapsed, remove by popover unlink button", () => { test("should remove the link if collapsed range at the end of a link", async () => { await testEditor({ contentBefore: '

abcd[]e

', stepFunction: unlinkFromPopover, contentAfter: "

abcd[]e

", }); // With fontawesome at the start of the link. await testEditor({ contentBefore: '

a\u200Bbcd[]e

', stepFunction: unlinkFromPopover, contentAfter: '

abcd[]e

', }); // With fontawesome at the middle of the link. await testEditor({ contentBefore: '

abc\u200Bd[]e

', stepFunction: unlinkFromPopover, contentAfter: '

abcd[]e

', }); // With fontawesome at the end of the link. await testEditor({ contentBefore: '

abcd[]\u200Be

', stepFunction: unlinkFromPopover, contentAfter: '

abcd[]e

', }); }); test("should remove the link if collapsed range in the middle a link", async () => { await testEditor({ contentBefore: '

ab[]cde

', stepFunction: unlinkFromPopover, contentAfter: "

ab[]cde

", }); // With fontawesome at the start of the link. await testEditor({ contentBefore: '

a\u200Bb[]cde

', stepFunction: unlinkFromPopover, contentAfter: '

ab[]cde

', }); // With fontawesome at the middle of the link. await testEditor({ contentBefore: '

ab[]c\u200Bde

', stepFunction: unlinkFromPopover, contentAfter: '

ab[]cde

', }); // With fontawesome at the end of the link. await testEditor({ contentBefore: '

ab[]cd\u200Be

', stepFunction: unlinkFromPopover, contentAfter: '

ab[]cde

', }); }); test("should remove the link if collapsed range at the start of a link", async () => { await testEditor({ contentBefore: '

a[]bcde

', stepFunction: unlinkFromPopover, contentAfter: "

a[]bcde

", }); // With fontawesome at the start of the link. await testEditor({ contentBefore: '

a\u200B[]bcde

', stepFunction: unlinkFromPopover, contentAfter: '

a[]bcde

', }); // With fontawesome at the middle of the link. await testEditor({ contentBefore: '

a[]bc\u200Bde

', stepFunction: unlinkFromPopover, contentAfter: '

a[]bcde

', }); // With fontawesome at the end of the link. await testEditor({ contentBefore: '

a[]bcd\u200Be

', stepFunction: unlinkFromPopover, contentAfter: '

a[]bcde

', }); }); test("should remove only the current link if collapsed range in the middle of a link", async () => { await testEditor({ contentBefore: '

abc[]def

', stepFunction: unlinkFromPopover, contentAfter: '

abc[]def

', }); // With fontawesome at the start of the link. await testEditor({ contentBefore: '

ab\u200Bc[]def

', stepFunction: unlinkFromPopover, contentAfter: '

abc[]def

', }); // With fontawesome at the middle of the link. await testEditor({ contentBefore: '

abc\u200Bd[]efg

', stepFunction: unlinkFromPopover, contentAfter: '

abcd[]efg

', }); // With fontawesome at the end of the link. await testEditor({ contentBefore: '

abc[]d\u200Bef

', stepFunction: unlinkFromPopover, contentAfter: '

abc[]def

', }); }); }); describe("range not collapsed", () => { describe("remove by toolbar unlink button", () => { test("should remove the link in the selected range at the end of a link", async () => { // FORWARD await testEditor({ contentBefore: '

abc[d]e

', stepFunction: unlinkFromToolbar, contentAfter: '

abc[d]e

', }); }); test("should remove fully selected link by toolbar unlink button", async () => { await testEditor({ contentBefore: '

a[bcd]e

', stepFunction: unlinkFromToolbar, contentAfterEdit: "

a[bcd]e

", contentAfter: "

a[bcd]e

", }); }); test("should remove fully selected link along with text by toolbar unlink button", async () => { await testEditor({ contentBefore: '

a[bcdef]g

', stepFunction: unlinkFromToolbar, contentAfterEdit: "

a[bcdef]g

", contentAfter: "

a[bcdef]g

", }); }); test("should remove fully selected link along with text by toolbar unlink button (2)", async () => { await testEditor({ contentBefore: '

a[bcdef]g

', stepFunction: unlinkFromToolbar, contentAfterEdit: "

a[bcdef]g

", contentAfter: "

a[bcdef]g

", }); }); test("should remove fully selected formatted link by toolbar unlink button", async () => { await testEditor({ contentBefore: '

a[bcd]e

', stepFunction: unlinkFromToolbar, contentAfterEdit: "

a[bcd]e

", contentAfter: "

a[bcd]e

", }); }); }); describe("remove by command", () => { test("should remove the link in the selected range at the end of a link", async () => { // FORWARD await testEditor({ contentBefore: '

abc[d]e

', stepFunction: async (editor) => { await unlinkByCommand(editor); }, contentAfterEdit: '

a\ufeff\ufeffbc\ufeff\ufeff[d]e

', contentAfter: '

abc[d]e

', }); // BACKWARD await testEditor({ contentBefore: '

abc]d[e

', stepFunction: async (editor) => { await unlinkByCommand(editor); }, contentAfterEdit: '

a\ufeff\ufeffbc\ufeff\ufeff]d[e

', contentAfter: '

abc]d[e

', }); }); test("should remove the link in the selected range in the middle of a link", async () => { // FORWARD await testEditor({ contentBefore: '

ab[c]de

', stepFunction: async (editor) => { await unlinkByCommand(editor); }, contentAfter: '

ab[c]de

', }); // BACKWARD await testEditor({ contentBefore: '

ab]c[de

', stepFunction: async (editor) => { await unlinkByCommand(editor); }, contentAfter: '

ab]c[de

', }); }); test("should remove the link in the selected range at the start of a link", async () => { // FORWARD await testEditor({ contentBefore: '

a[b]cde

', stepFunction: async (editor) => { await unlinkByCommand(editor); }, contentAfterEdit: '

a[b]\ufeff\ufeffcd\ufeff\ufeffe

', contentAfter: '

a[b]cde

', }); // BACKWARD await testEditor({ contentBefore: '

a]b[cde

', stepFunction: async (editor) => { await unlinkByCommand(editor); }, contentAfterEdit: '

a]b[\ufeff\ufeffcd\ufeff\ufeffe

', contentAfter: '

a]b[cde

', }); }); test("should remove the link in the selected range overlapping the end of a link", async () => { // FORWARD await testEditor({ contentBefore: '

abc[de]f

', stepFunction: async (editor) => { await unlinkByCommand(editor); }, contentAfter: '

abc[de]f

', }); // BACKWARD await testEditor({ contentBefore: '

abc]de[f

', stepFunction: async (editor) => { await unlinkByCommand(editor); }, contentAfter: '

abc]de[f

', }); }); test("should remove the link in the selected range overlapping the start of a link", async () => { // FORWARD await testEditor({ contentBefore: '

a[bc]def

', stepFunction: async (editor) => { await unlinkByCommand(editor); }, contentAfter: '

a[bc]def

', }); // BACKWARD await testEditor({ contentBefore: '

a]bc[def

', stepFunction: async (editor) => { await unlinkByCommand(editor); }, contentAfter: '

a]bc[def

', }); }); test("should not unlink selected non-editable links", async () => { await testEditor({ contentBefore: '

[abcdef]

', stepFunction: async (editor) => { await unlinkByCommand(editor); }, contentAfter: '

[abcdef]

', }); }); }); test("should be able to remove link if selection has FEFF character", async () => { const { el } = await setupEditor( '

[test]

' ); const link = el.querySelector("a"); const firstFeffChar = link.firstChild; const secondFeffChar = link.lastChild; setSelection({ anchorNode: firstFeffChar, anchorOffset: 0, focusNode: secondFeffChar, focusOffset: 1, }); await unlinkFromToolbar(); expect(getContent(el)).toBe("

[test]

"); }); test("should be able to remove link if selection has FEFF character (2)", async () => { const { el } = await setupEditor( '

[]test

' ); const link = el.querySelector("a"); const firstFeffChar = link.firstChild; const textNode = firstFeffChar.nextSibling; const secondFeffChar = link.lastChild; setSelection({ anchorNode: secondFeffChar, anchorOffset: 1, focusNode: textNode, focusOffset: 0, }); await unlinkFromToolbar(); expect(getContent(el)).toBe("

]test[

"); }); }); describe("empty link", () => { test("should not remove empty link in uneditable zone", async () => { await testEditor({ contentBefore: '

', contentAfter: '

', }); }); test("should not remove empty link in uneditable zone (2)", async () => { await testEditor({ contentBefore: '

[]

', contentAfter: '

[]

', }); }); });