mirror of
https://github.com/outline/outline.git
synced 2026-11-21 21:44:30 +03:00
* wip * fix: Position grips in merged cells from the rendered layout * fix: Only merge row and column selections that cover whole rows or columns * perf: Find merged cell spans without scanning the table map * refactor: Extract helpers for cells starting in a row or column
245 lines
7.1 KiB
TypeScript
245 lines
7.1 KiB
TypeScript
import { Schema } from "prosemirror-model";
|
|
import { TableMap } from "prosemirror-tables";
|
|
import { doc, table, td, tr } from "@shared/test/editor";
|
|
import {
|
|
getCellAttrs,
|
|
getCellSpan,
|
|
getCellsStartingInColumn,
|
|
getCellsStartingInRow,
|
|
isValidCellAlignment,
|
|
isValidCellMarks,
|
|
} from "./table";
|
|
|
|
const schema = new Schema({
|
|
nodes: {
|
|
doc: { content: "text*" },
|
|
text: {},
|
|
},
|
|
marks: {
|
|
background: {
|
|
attrs: { color: {} },
|
|
},
|
|
strong: {},
|
|
em: {},
|
|
},
|
|
});
|
|
|
|
describe("isValidCellAlignment", () => {
|
|
it("accepts null", () => {
|
|
expect(isValidCellAlignment(null)).toBe(true);
|
|
});
|
|
|
|
it("accepts the three allowed alignment strings", () => {
|
|
expect(isValidCellAlignment("left")).toBe(true);
|
|
expect(isValidCellAlignment("center")).toBe(true);
|
|
expect(isValidCellAlignment("right")).toBe(true);
|
|
});
|
|
|
|
it("rejects arbitrary strings", () => {
|
|
expect(isValidCellAlignment("justify")).toBe(false);
|
|
expect(isValidCellAlignment("LEFT")).toBe(false);
|
|
expect(isValidCellAlignment("")).toBe(false);
|
|
});
|
|
|
|
it("rejects CSS injection payloads", () => {
|
|
expect(
|
|
isValidCellAlignment(
|
|
"left; background-image: url(https://evil.example/exfil)"
|
|
)
|
|
).toBe(false);
|
|
expect(isValidCellAlignment("left; background: red")).toBe(false);
|
|
});
|
|
|
|
it("rejects non-string non-null values", () => {
|
|
expect(isValidCellAlignment(undefined)).toBe(false);
|
|
expect(isValidCellAlignment(0)).toBe(false);
|
|
expect(isValidCellAlignment({})).toBe(false);
|
|
expect(isValidCellAlignment([])).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("isValidCellMarks", () => {
|
|
it("accepts undefined and null", () => {
|
|
expect(isValidCellMarks(undefined)).toBe(true);
|
|
expect(isValidCellMarks(null)).toBe(true);
|
|
});
|
|
|
|
it("accepts an empty array", () => {
|
|
expect(isValidCellMarks([])).toBe(true);
|
|
});
|
|
|
|
it("accepts a background mark with a valid 6-digit hex color", () => {
|
|
expect(
|
|
isValidCellMarks(
|
|
[{ type: "background", attrs: { color: "#FDEA9B" } }],
|
|
schema
|
|
)
|
|
).toBe(true);
|
|
});
|
|
|
|
it("accepts a background mark with a valid 8-digit hex color", () => {
|
|
expect(
|
|
isValidCellMarks(
|
|
[{ type: "background", attrs: { color: "#FDEA9BB3" } }],
|
|
schema
|
|
)
|
|
).toBe(true);
|
|
});
|
|
|
|
it("accepts a background mark with a valid 3-digit hex color", () => {
|
|
expect(
|
|
isValidCellMarks(
|
|
[{ type: "background", attrs: { color: "#fff" } }],
|
|
schema
|
|
)
|
|
).toBe(true);
|
|
});
|
|
|
|
it("accepts a background mark with a valid 4-digit hex color", () => {
|
|
expect(
|
|
isValidCellMarks(
|
|
[{ type: "background", attrs: { color: "#fff8" } }],
|
|
schema
|
|
)
|
|
).toBe(true);
|
|
});
|
|
|
|
it("rejects a background mark with a malformed 4-digit hex color", () => {
|
|
expect(
|
|
isValidCellMarks(
|
|
[{ type: "background", attrs: { color: "#fffz" } }],
|
|
schema
|
|
)
|
|
).toBe(false);
|
|
});
|
|
|
|
it("rejects a background mark with a non-hex color", () => {
|
|
expect(
|
|
isValidCellMarks(
|
|
[{ type: "background", attrs: { color: "red" } }],
|
|
schema
|
|
)
|
|
).toBe(false);
|
|
});
|
|
|
|
it("rejects a background mark carrying a CSS injection payload", () => {
|
|
expect(
|
|
isValidCellMarks(
|
|
[
|
|
{
|
|
type: "background",
|
|
attrs: {
|
|
color: "#fff; background-image: url(https://evil.example)",
|
|
},
|
|
},
|
|
],
|
|
schema
|
|
)
|
|
).toBe(false);
|
|
});
|
|
|
|
it("rejects a background mark with a missing color attr", () => {
|
|
expect(isValidCellMarks([{ type: "background", attrs: {} }], schema)).toBe(
|
|
false
|
|
);
|
|
expect(isValidCellMarks([{ type: "background" }], schema)).toBe(false);
|
|
});
|
|
|
|
it("rejects mark types that are not registered in the schema", () => {
|
|
expect(isValidCellMarks([{ type: "not_a_mark" }], schema)).toBe(false);
|
|
});
|
|
|
|
it("does not check schema registration when no schema is provided", () => {
|
|
expect(isValidCellMarks([{ type: "anything_goes" }])).toBe(true);
|
|
});
|
|
|
|
it("rejects non-array values", () => {
|
|
expect(isValidCellMarks("marks")).toBe(false);
|
|
expect(isValidCellMarks({})).toBe(false);
|
|
expect(isValidCellMarks(42)).toBe(false);
|
|
});
|
|
|
|
it("rejects arrays containing malformed entries", () => {
|
|
expect(isValidCellMarks([null], schema)).toBe(false);
|
|
expect(isValidCellMarks(["strong"], schema)).toBe(false);
|
|
expect(isValidCellMarks([{}], schema)).toBe(false);
|
|
expect(isValidCellMarks([{ type: 123 }], schema)).toBe(false);
|
|
expect(isValidCellMarks([{ type: "strong", attrs: "oops" }], schema)).toBe(
|
|
false
|
|
);
|
|
});
|
|
|
|
it("accepts registered marks without attrs", () => {
|
|
expect(isValidCellMarks([{ type: "strong" }], schema)).toBe(true);
|
|
expect(isValidCellMarks([{ type: "em" }], schema)).toBe(true);
|
|
});
|
|
});
|
|
|
|
// Shared tests run in both node and jsdom; reading cell attributes requires a DOM.
|
|
describe.runIf(typeof document !== "undefined")("getCellAttrs", () => {
|
|
const cell = (attributes: Record<string, string>) => {
|
|
const element = document.createElement("td");
|
|
for (const [name, value] of Object.entries(attributes)) {
|
|
element.setAttribute(name, value);
|
|
}
|
|
return getCellAttrs(element);
|
|
};
|
|
|
|
it("reads the background written by the editor", () => {
|
|
expect(cell({ "data-bgcolor": "#FDEA9B" }).marks).toEqual([
|
|
{ type: "background", attrs: { color: "#FDEA9B" } },
|
|
]);
|
|
});
|
|
|
|
it("reads a background styled by an external application", () => {
|
|
expect(cell({ style: "background-color: #93c47d" }).marks).toEqual([
|
|
{ type: "background", attrs: { color: "#93c47d" } },
|
|
]);
|
|
expect(cell({ style: "background: yellow" }).marks).toEqual([
|
|
{ type: "background", attrs: { color: "#ffff00" } },
|
|
]);
|
|
});
|
|
|
|
it("ignores near-white and transparent cell backgrounds", () => {
|
|
expect(cell({ style: "background-color: #ffffff" }).marks).toBe(undefined);
|
|
expect(cell({ style: "background-color: transparent" }).marks).toBe(
|
|
undefined
|
|
);
|
|
expect(cell({}).marks).toBe(undefined);
|
|
});
|
|
});
|
|
|
|
describe("getCellSpan", () => {
|
|
const map = TableMap.get(
|
|
doc([
|
|
table([
|
|
tr([td("A", { rowspan: 2 }), td("B", { colspan: 2 })]),
|
|
tr([td("C"), td("D")]),
|
|
tr([td("E"), td("F"), td("G")]),
|
|
]),
|
|
]).child(0)
|
|
);
|
|
|
|
it("returns the rows spanned by a cell", () => {
|
|
expect(getCellSpan(map, 0, 0, "row")).toEqual({ start: 0, end: 2 });
|
|
expect(getCellSpan(map, 1, 0, "row")).toEqual({ start: 0, end: 2 });
|
|
expect(getCellSpan(map, 2, 0, "row")).toEqual({ start: 2, end: 3 });
|
|
});
|
|
|
|
it("returns the columns spanned by a cell", () => {
|
|
expect(getCellSpan(map, 0, 2, "column")).toEqual({ start: 1, end: 3 });
|
|
expect(getCellSpan(map, 1, 2, "column")).toEqual({ start: 2, end: 3 });
|
|
});
|
|
|
|
it("returns the cells that start in a row or column", () => {
|
|
const cell = (row: number, col: number) => map.map[row * map.width + col];
|
|
expect(getCellsStartingInRow(map, 1)).toEqual([cell(1, 1), cell(1, 2)]);
|
|
expect(getCellsStartingInColumn(map, 1)).toEqual([
|
|
cell(0, 1),
|
|
cell(1, 1),
|
|
cell(2, 1),
|
|
]);
|
|
expect(getCellsStartingInColumn(map, 2)).toEqual([cell(1, 2), cell(2, 2)]);
|
|
});
|
|
});
|