// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { surroundNoSplitting as surround } from "./no-splitting";
const parser = new DOMParser();
function p(html: string): HTMLBodyElement {
const parsed = parser.parseFromString(html, "text/html");
return parsed.body as HTMLBodyElement;
}
describe("surround text", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("111222");
});
test("all text", () => {
const range = new Range();
range.selectNode(body.firstChild!);
const { addedNodes, removedNodes, surroundedRange } = surround(
range,
document.createElement("b"),
body,
);
expect(addedNodes).toHaveLength(1);
expect(removedNodes).toHaveLength(0);
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 { addedNodes, removedNodes, surroundedRange } = surround(
range,
document.createElement("b"),
body,
);
expect(addedNodes).toHaveLength(1);
expect(removedNodes).toHaveLength(0);
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 { addedNodes, removedNodes, surroundedRange } = surround(
range,
document.createElement("b"),
body,
);
expect(addedNodes).toHaveLength(1);
expect(removedNodes).toHaveLength(0);
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!);
const { addedNodes, removedNodes, surroundedRange } = surround(
range,
document.createElement("u"),
body,
);
expect(addedNodes).toHaveLength(1);
expect(removedNodes).toHaveLength(1);
expect(body).toHaveProperty("innerHTML", "beforeafter");
expect(surroundedRange.toString()).toEqual("before");
});
test("moves nested down", () => {
const range = new Range();
range.selectNode(body.firstChild!);
const { addedNodes, removedNodes, surroundedRange } = surround(
range,
document.createElement("b"),
body,
);
expect(addedNodes).toHaveLength(1);
expect(removedNodes).toHaveLength(1);
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]);
const { addedNodes, removedNodes, surroundedRange } = surround(
range,
document.createElement("u"),
body,
);
expect(addedNodes).toHaveLength(1);
expect(removedNodes).toHaveLength(1);
expect(body).toHaveProperty("innerHTML", "beforeafter");
expect(surroundedRange.toString()).toEqual("after");
});
test("moves nested down", () => {
const range = new Range();
range.selectNode(body.childNodes[1]);
const { addedNodes, removedNodes, surroundedRange } = surround(
range,
document.createElement("b"),
body,
);
expect(addedNodes).toHaveLength(1);
expect(removedNodes).toHaveLength(1);
expect(body).toHaveProperty("innerHTML", "beforeafter");
expect(surroundedRange.toString()).toEqual("after");
});
});
});
describe("surround across block element", () => {
let body: HTMLBodyElement;
beforeEach(() => {
body = p("Before