// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { surround } from "./surround";
import { easyBold, easyUnderline, p } from "./test-utils";
describe("surround text", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("111222");
});
test("all text", () => {
const range = new Range();
range.selectNode(body.firstChild!);
const surroundedRange = surround(range, body, easyBold);
expect(body).toHaveProperty("innerHTML", "111222");
expect(surroundedRange.toString()).toEqual("111222");
});
test("first half", () => {
const range = new Range();
range.setStart(body.firstChild!, 0);
range.setEnd(body.firstChild!, 3);
const surroundedRange = surround(range, body, easyBold);
expect(body).toHaveProperty("innerHTML", "111222");
expect(surroundedRange.toString()).toEqual("111");
});
test("second half", () => {
const range = new Range();
range.setStart(body.firstChild!, 3);
range.setEnd(body.firstChild!, 6);
const surroundedRange = surround(range, body, easyBold);
expect(body).toHaveProperty("innerHTML", "111222");
expect(surroundedRange.toString()).toEqual("222");
});
});
describe("surround text next to nested", () => {
describe("before", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("beforeafter");
});
test("enlarges bottom tag of nested", () => {
const range = new Range();
range.selectNode(body.firstChild!);
surround(range, body, easyUnderline);
expect(body).toHaveProperty("innerHTML", "beforeafter");
// expect(surroundedRange.toString()).toEqual("before");
});
test("moves nested down", () => {
const range = new Range();
range.selectNode(body.firstChild!);
surround(range, body, easyBold);
expect(body).toHaveProperty("innerHTML", "beforeafter");
// expect(surroundedRange.toString()).toEqual("before");
});
});
describe("after", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("beforeafter");
});
test("enlarges bottom tag of nested", () => {
const range = new Range();
range.selectNode(body.childNodes[1]);
surround(range, body, easyUnderline);
expect(body).toHaveProperty("innerHTML", "beforeafter");
// expect(surroundedRange.toString()).toEqual("after");
});
test("moves nested down", () => {
const range = new Range();
range.selectNode(body.childNodes[1]);
surround(range, body, easyBold);
expect(body).toHaveProperty("innerHTML", "beforeafter");
// expect(surroundedRange.toString()).toEqual("after");
});
});
describe("two nested", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("aaabbbccc");
});
test("extends to both", () => {
const range = new Range();
range.selectNode(body.firstChild!);
surround(range, body, easyBold);
expect(body).toHaveProperty("innerHTML", "aaabbbccc");
// expect(surroundedRange.toString()).toEqual("aaa");
});
});
});
describe("surround across block element", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("Before
");
});
test("does not insert empty elements", () => {
const range = new Range();
range.setStartBefore(body.firstChild!);
range.setEndAfter(body.lastChild!);
const surroundedRange = surround(range, body, easyBold);
expect(body).toHaveProperty(
"innerHTML",
"Before
",
);
expect(surroundedRange.toString()).toEqual("BeforeFirstSecond");
});
});
describe("next to nested", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("111222333444555");
});
test("surround after", () => {
const range = new Range();
range.selectNode(body.lastChild!);
surround(range, body, easyBold);
expect(body).toHaveProperty("innerHTML", "111222333444555");
// expect(surroundedRange.toString()).toEqual("555");
});
});
describe("next to element with nested non-matching", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("111222333444555");
});
test("surround after", () => {
const range = new Range();
range.selectNode(body.lastChild!);
surround(range, body, easyBold);
expect(body).toHaveProperty(
"innerHTML",
"111222333444555",
);
// expect(surroundedRange.toString()).toEqual("555");
});
});
describe("next to element with text element text", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("111222333444555");
});
test("surround after", () => {
const range = new Range();
range.selectNode(body.lastChild!);
surround(range, body, easyBold);
expect(body).toHaveProperty("innerHTML", "111222333444555");
// expect(surroundedRange.toString()).toEqual("555");
});
});
describe("surround elements that already have nested block", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("12
");
});
test("normalizes nodes", () => {
const range = new Range();
range.selectNode(body.children[0]);
surround(range, body, easyBold);
expect(body).toHaveProperty("innerHTML", "12
");
// expect(surroundedRange.toString()).toEqual("12");
});
});
describe("surround complicated nested structure", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("12345");
});
test("normalize nodes", () => {
const range = new Range();
range.setStartBefore(body.firstElementChild!.firstChild!);
range.setEndAfter(body.lastElementChild!.firstChild!);
const surroundedRange = surround(range, body, easyBold);
expect(body).toHaveProperty(
"innerHTML",
"12345",
);
expect(surroundedRange.toString()).toEqual("12345");
});
});
describe("skips over empty elements", () => {
describe("joins two newly created", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("before
after");
});
test("normalize nodes", () => {
const range = new Range();
range.setStartBefore(body.firstChild!);
range.setEndAfter(body.childNodes[2]!);
const surroundedRange = surround(range, body, easyBold);
expect(body).toHaveProperty("innerHTML", "before
after");
expect(surroundedRange.toString()).toEqual("beforeafter");
});
});
describe("joins with already existing", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("before
after");
});
test("normalize nodes", () => {
const range = new Range();
range.selectNode(body.firstChild!);
surround(range, body, easyBold);
expect(body).toHaveProperty("innerHTML", "before
after");
// expect(surroundedRange.toString()).toEqual("before");
});
test("normalize node contents", () => {
const range = new Range();
range.selectNodeContents(body.firstChild!);
const surroundedRange = surround(range, body, easyBold);
expect(body).toHaveProperty("innerHTML", "before
after");
expect(surroundedRange.toString()).toEqual("before");
});
});
});
// TODO
// describe("special cases when surrounding within range.commonAncestor", () => {
// // these are not vital but rather define how the algorithm works in edge cases
// test("does not normalize beyond level of contained text nodes", () => {
// const body = p("beforenestedafter");
// const range = new Range();
// range.selectNode(body.firstChild!.childNodes[1].firstChild!);
// const { addedNodes, removedNodes, surroundedRange } = surround(
// range,
// body,
// easyBold,
// );
// expect(addedNodes).toHaveLength(1);
// expect(removedNodes).toHaveLength(0);
// expect(body).toHaveProperty(
// "innerHTML",
// "beforenestedafter",
// );
// expect(surroundedRange.toString()).toEqual("nested");
// });
// test("does not normalize beyond level of contained text nodes 2", () => {
// const body = p("aaabbbccc");
// const range = new Range();
// range.setStartBefore(body.firstChild!.firstChild!);
// range.setEndAfter(body.firstChild!.childNodes[1].firstChild!);
// const { addedNodes, removedNodes } = surround(range, body, easyBold);
// expect(body).toHaveProperty("innerHTML", "aaabbbccc");
// expect(addedNodes).toHaveLength(1);
// expect(removedNodes).toHaveLength(2);
// // expect(surroundedRange.toString()).toEqual("aaabbb"); // is aaabbbccc instead
// });
// test("does normalize beyond level of contained text nodes", () => {
// const body = p("aaabbbccc");
// const range = new Range();
// range.setStartBefore(body.firstChild!.childNodes[1].firstChild!.firstChild!);
// range.setEndAfter(body.firstChild!.childNodes[1].childNodes[1].firstChild!);
// const { addedNodes, removedNodes } = surround(range, body, easyBold);
// expect(body).toHaveProperty("innerHTML", "aaabbbccc");
// expect(addedNodes).toHaveLength(1);
// expect(removedNodes).toHaveLength(4);
// // expect(surroundedRange.toString()).toEqual("aaabbb"); // is aaabbbccc instead
// });
// test("does remove even if there is already equivalent surrounding in place", () => {
// const body = p("beforenestedafter");
// const range = new Range();
// range.selectNode(body.firstChild!.childNodes[1].firstChild!.firstChild!);
// const { addedNodes, removedNodes, surroundedRange } = surround(
// range,
// body,
// easyBold,
// );
// expect(addedNodes).toHaveLength(1);
// expect(removedNodes).toHaveLength(1);
// expect(body).toHaveProperty(
// "innerHTML",
// "beforenestedafter",
// );
// expect(surroundedRange.toString()).toEqual("nested");
// });
// });