import { describe, test } from "@odoo/hoot"; import { patchWithCleanup } from "@web/../tests/web_test_helpers"; import { testEditor } from "./_helpers/editor"; import { insertText } from "./_helpers/user_actions"; describe("inline code", () => { test("should convert text into inline code (start)", async () => { await testEditor({ contentBefore: "

`ab[]cd

", stepFunction: async (editor) => await insertText(editor, "`"), contentAfter: '

\u200Bab\u200B[]cd

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

[]ab`cd

", stepFunction: async (editor) => await insertText(editor, "`"), contentAfter: '

\u200B[]abcd

', }); }); test("should convert text into inline code (middle)", async () => { await testEditor({ contentBefore: "

ab`cd[]ef

", stepFunction: async (editor) => await insertText(editor, "`"), contentAfter: '

abcd\u200B[]ef

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

ab[]cd`ef

", stepFunction: async (editor) => await insertText(editor, "`"), contentAfter: '

ab[]cdef

', }); }); test("should convert text into inline code (end)", async () => { await testEditor({ contentBefore: "

ab`cd[]

", stepFunction: async (editor) => await insertText(editor, "`"), contentAfter: '

abcd\u200B[]

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

ab[]cd`

", stepFunction: async (editor) => await insertText(editor, "`"), contentAfter: '

ab[]cd

', }); }); test("should convert text into inline code, with parasite backticks", async () => { await testEditor({ contentBefore: "

a`b`cd[]e`f

", stepFunction: async (editor) => await insertText(editor, "`"), // The closest PREVIOUS backtick is prioritary contentAfter: '

a`bcd\u200B[]e`f

', }); await testEditor({ contentBefore: "

ab[]cd`e`f

", stepFunction: async (editor) => await insertText(editor, "`"), // If there is no previous backtick, use the closest NEXT backtick. contentAfter: '

ab[]cde`f

', }); }); test("should not convert text into inline code when traversing HTMLElements", async () => { await testEditor({ contentBefore: "

ab`cde[]fg

", stepFunction: async (editor) => await insertText(editor, "`"), contentAfter: "

ab`cde`[]fg

", }); }); test("should not convert text into inline code when interrupted by linebreak", async () => { await testEditor({ contentBefore: "

ab`c
d[]ef

", stepFunction: async (editor) => await insertText(editor, "`"), contentAfter: "

ab`c
d`[]ef

", }); }); test("should not convert text into inline code when inside inline code", async () => { await testEditor({ contentBefore: '

ab`cd[]ef

', stepFunction: async (editor) => await insertText(editor, "`"), contentAfter: '

ab`cd`[]ef

', }); }); test("should convert text into inline code even when text nodes are split", async () => { // BEFORE await testEditor({ contentBefore: "

b`c[]d

", stepFunction: async (editor) => { editor.document.getSelection().anchorNode.before(document.createTextNode("a")); /** @todo fix warnings */ patchWithCleanup(console, { warn: () => {} }); await insertText(editor, "`"); }, contentAfter: '

abc\u200B[]d

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

a`b[]c

", stepFunction: async (editor) => { editor.document.getSelection().anchorNode.after(document.createTextNode("d")); await insertText(editor, "`"); }, contentAfter: '

ab\u200B[]cd

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

b`c[]d

", stepFunction: async (editor) => { editor.document.getSelection().anchorNode.before(document.createTextNode("a")); editor.document.getSelection().anchorNode.after(document.createTextNode("e")); await insertText(editor, "`"); }, contentAfter: '

abc\u200B[]de

', }); }); test("should convert text into inline code even when the other backtick is in a separate text node", async () => { // BACKTICK IS PREVIOUS SIBLING await testEditor({ contentBefore: "

ab[]c

", stepFunction: async (editor) => { editor.document.getSelection().anchorNode.before(document.createTextNode("`")); /** @todo fix warnings */ patchWithCleanup(console, { warn: () => {} }); await insertText(editor, "`"); }, contentAfter: '

\u200Bab\u200B[]c

', }); // BACKTICK IS NEXT SIBLING await testEditor({ contentBefore: "

ab[]c

", stepFunction: async (editor) => { editor.document.getSelection().anchorNode.after(document.createTextNode("`")); await insertText(editor, "`"); }, contentAfter: '

ab[]c

', }); }); test("should not convert text into inline code when content is empty", async () => { await testEditor({ contentBefore: "

`[]

", stepFunction: async (editor) => insertText(editor, "`"), contentAfter: "

``[]

", }); await testEditor({ contentBefore: "

``[]

", stepFunction: async (editor) => insertText(editor, "`"), contentAfter: "

```[]

", }); await testEditor({ contentBefore: "

```[]

", stepFunction: async (editor) => insertText(editor, "`"), contentAfter: "

````[]

", }); await testEditor({ contentBefore: "

````[]

", stepFunction: async (editor) => insertText(editor, "`"), contentAfter: "

`````[]

", }); }); });