Files
outline/shared/editor/lib/sliceWithoutFirstNode.test.ts
Tom Moor df8f8848ef fix: Crash when pasting a single block into the document title (#13451)
Removing the first node from the pasted slice kept the original open
depths, which are no longer valid for the remaining content. When the
clipboard held a single top-level node the content became empty while
openEnd stayed above zero, and ProseMirror walked into a null child.

Fixes OUTLINE-CLOUD-51V
2026-08-15 20:23:16 -04:00

71 lines
2.1 KiB
TypeScript

import { Fragment, Schema, Slice } from "prosemirror-model";
import { EditorState, Selection } from "prosemirror-state";
import { sliceWithoutFirstNode } from "./sliceWithoutFirstNode";
const schema = new Schema({
nodes: {
doc: { content: "block+" },
paragraph: { content: "inline*", group: "block" },
horizontal_rule: { group: "block" },
text: { group: "inline" },
},
});
const paragraph = (text: string) =>
schema.nodes.paragraph.create(null, text ? schema.text(text) : null);
describe("sliceWithoutFirstNode", () => {
it("returns undefined for an empty slice", () => {
expect(sliceWithoutFirstNode(Slice.empty)).toBeUndefined();
});
it("returns undefined when the slice holds a single node", () => {
const slice = new Slice(Fragment.from(paragraph("one")), 1, 1);
expect(sliceWithoutFirstNode(slice)).toBeUndefined();
});
it("removes the first node and keeps the open depths", () => {
const slice = new Slice(
Fragment.fromArray([paragraph("one"), paragraph("two")]),
1,
1
);
const result = sliceWithoutFirstNode(slice);
expect(result?.content.childCount).toBe(1);
expect(result?.content.firstChild?.textContent).toBe("two");
expect(result?.openStart).toBe(1);
expect(result?.openEnd).toBe(1);
});
it("clamps the open depths to leaf nodes", () => {
const slice = new Slice(
Fragment.fromArray([
paragraph("one"),
schema.nodes.horizontal_rule.create(),
]),
1,
1
);
const result = sliceWithoutFirstNode(slice);
expect(result?.content.childCount).toBe(1);
expect(result?.openStart).toBe(0);
expect(result?.openEnd).toBe(0);
});
it("returns a slice that can replace a selection", () => {
const slice = new Slice(
Fragment.fromArray([paragraph("one"), paragraph("two")]),
1,
1
);
const state = EditorState.create({ schema });
const tr = state.tr
.setSelection(Selection.atStart(state.doc))
.replaceSelection(sliceWithoutFirstNode(slice) ?? Slice.empty);
expect(tr.doc.textContent).toBe("two");
});
});