Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions pkgs/id/web/src/editor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,63 @@ describe("ordered_list start parsing (#2005)", () => {
});
});

// ============================================================================
// #4100: task_list parseDOM start parsing -- the <ol> rule reads its start
// through parseOrderedListStart, like ordered_list (#2005): only plain
// non-negative safe integers survive; everything else falls back to 1
// instead of reaching the u64 markdown codec as a fraction/negative or a
// reinterpreted hex/scientific value.
// ============================================================================

describe("task_list start parsing (#4100)", () => {
const olGetAttrs = () => {
const rules = richSchema.nodes.task_list.spec.parseDOM ?? [];
const rule = rules.find((r) => r.tag === "ol");
if (!rule || !("getAttrs" in rule) || typeof rule.getAttrs !== "function") {
throw new Error("task_list parseDOM has no ol getAttrs rule");
}
return rule.getAttrs;
};

it.each([
["5", 5],
["0", 0],
["42", 42],
])("preserves valid start=%s as order %i", (raw, expected) => {
const el = document.createElement("ol");
el.classList.add("contains-task-list");
el.setAttribute("start", raw);
expect(olGetAttrs()(el)).toEqual({ order: expected });
});

it.each(["abc", "2.5", "0x10", "1e3", "", "-3", "9007199254740993"])(
"falls back to 1 for invalid start=%s",
(raw) => {
const el = document.createElement("ol");
el.classList.add("contains-task-list");
el.setAttribute("start", raw);
expect(olGetAttrs()(el)).toEqual({ order: 1 });
},
);

it("defaults order to 1 without a start attribute", () => {
const el = document.createElement("ol");
el.classList.add("contains-task-list");
expect(olGetAttrs()(el)).toEqual({ order: 1 });
});

it("validates the start of a checkbox-detected ordered task list too", () => {
const el = document.createElement("ol");
el.setAttribute("start", "2.5");
const li = document.createElement("li");
const checkbox = document.createElement("input");
checkbox.setAttribute("type", "checkbox");
li.appendChild(checkbox);
el.appendChild(li);
expect(olGetAttrs()(el)).toEqual({ order: 1 });
});
});

describe("rawSchema", () => {
it("has doc node type", () => {
expect(rawSchema.nodes.doc).toBeDefined();
Expand Down
9 changes: 6 additions & 3 deletions pkgs/id/web/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,14 @@ export const richSchema = new Schema({
tag: "ol",
getAttrs(dom) {
// Match <ol class=contains-task-list> (ordered task list);
// its start is the list's order.
// its start is the list's order, read through the same
// validated parse as ordered_list (#4100; the raw el.start
// reflection admits fractional/negative/hex starts the u64
// markdown codec cannot represent).
const el = dom as HTMLOListElement;
if (el.classList.contains("contains-task-list")) return { order: el.start };
if (el.classList.contains("contains-task-list")) return { order: parseOrderedListStart(el.getAttribute("start")) };
const firstLi = el.querySelector("li");
if (firstLi?.querySelector('input[type="checkbox"]')) return { order: el.start };
if (firstLi?.querySelector('input[type="checkbox"]')) return { order: parseOrderedListStart(el.getAttribute("start")) };
return false;
},
},
Expand Down
Loading