anki/ts/domlib/surround/find-adjacent.test.ts
Henrik Giesel 97b28398ea
Fix some issues with new surround buttons (#1505)
* Add a store to indicate whether input trigger is active

Button state is then indicated by: caretIsInBold XOR boldTriggerActive

* Fix surrounding where normalization is tripped up by empty text nodes

* Add failing test for unsurrounding

* Fix failing test

* prohibitOverlapse does not need to be active, if aboveEnd is null

* Reinsert Italic and Underline button

* Refactor find-adjacent to use sum types

* Simplify return value of normalizeAdjacent
2021-11-24 10:33:14 +10:00

68 lines
2.0 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { findBefore, findAfter } from "./find-adjacent";
import { nodeToChildNodeRange } from "./child-node-range";
import { matchTagName } from "./matcher";
const parser = new DOMParser();
function p(html: string): Element {
const parsed = parser.parseFromString(html, "text/html");
return parsed.body;
}
describe("in a simple search", () => {
const html = p("<b>Before</b><u>This is a test</u><i>After</i>");
const range = nodeToChildNodeRange(html.children[1]);
describe("findBefore", () => {
test("finds an element", () => {
const matches = findBefore(range, matchTagName("b"));
expect(matches).toHaveLength(1);
});
test("does not find non-existing element", () => {
const matches = findBefore(range, matchTagName("i"));
expect(matches).toHaveLength(0);
});
});
describe("findAfter", () => {
test("finds an element", () => {
const matches = findAfter(range, matchTagName("i"));
expect(matches).toHaveLength(1);
});
test("does not find non-existing element", () => {
const matches = findAfter(range, matchTagName("b"));
expect(matches).toHaveLength(0);
});
});
});
describe("in a nested search", () => {
const htmlNested = p("<u><b>before</b></u>within<u><b>after</b></u>");
const rangeNested = nodeToChildNodeRange(htmlNested.childNodes[1]);
describe("findBefore", () => {
test("finds a nested element", () => {
const matches = findBefore(rangeNested, matchTagName("b"));
expect(matches).toHaveLength(1);
});
});
describe("findAfter", () => {
test("finds a nested element", () => {
const matches = findAfter(rangeNested, matchTagName("b"));
expect(matches).toHaveLength(1);
});
});
});