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
53 changes: 53 additions & 0 deletions pkgs/id/web/src/editor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
hasToolbar,
initEditor,
isEditable,
parseOrderedListStart,
rawSchema,
richSchema,
} from "./editor";
Expand Down Expand Up @@ -248,6 +249,58 @@ describe("richSchema list tight attrs (#1469)", () => {
});
});

// ============================================================================
// #2005: ordered_list parseDOM start parsing — only non-negative safe
// integers survive; everything else falls back to 1 (no reinterpretation)
// ============================================================================

describe("ordered_list start parsing (#2005)", () => {
it.each([
["5", 5],
["0", 0],
["42", 42],
])("preserves valid start=%s as order %i", (raw, expected) => {
expect(parseOrderedListStart(raw)).toBe(expected);
});

it.each(["abc", "2.5", "0x10", "1e3", "", "-3", "9007199254740993"])(
"falls back to 1 for invalid start=%s",
(raw) => {
expect(parseOrderedListStart(raw)).toBe(1);
},
);

it("falls back to 1 when the start attribute is absent", () => {
expect(parseOrderedListStart(null)).toBe(1);
});

it.each([
["5", 5],
["0", 0],
["abc", 1],
["2.5", 1],
["0x10", 1],
["", 1],
["-3", 1],
])("parseDOM getAttrs maps start=%s to order %i", (raw, expected) => {
const rule = richSchema.nodes.ordered_list.spec.parseDOM?.[0];
if (!rule || !("getAttrs" in rule) || typeof rule.getAttrs !== "function") {
throw new Error("ordered_list parseDOM rule has no getAttrs");
}
const el = document.createElement("ol");
el.setAttribute("start", raw);
expect(rule.getAttrs(el)).toEqual({ order: expected });
});

it("parseDOM getAttrs defaults order to 1 without a start attribute", () => {
const rule = richSchema.nodes.ordered_list.spec.parseDOM?.[0];
if (!rule || !("getAttrs" in rule) || typeof rule.getAttrs !== "function") {
throw new Error("ordered_list parseDOM rule has no getAttrs");
}
expect(rule.getAttrs(document.createElement("ol"))).toEqual({ order: 1 });
});
});

describe("rawSchema", () => {
it("has doc node type", () => {
expect(rawSchema.nodes.doc).toBeDefined();
Expand Down
20 changes: 19 additions & 1 deletion pkgs/id/web/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ import { createWrapPlugins } from "./wrap";
*/
export type ContentMode = "rich" | "markdown" | "plain" | "raw" | "media" | "binary";

/**
* Parse an <ol> `start` attribute into the ordered_list `order` attr
* (#2005). Only a plain non-negative decimal integer that fits in a
* safe integer is preserved (0 included); anything else — malformed
* text, fractions, negatives, hex/scientific notation, empty, or an
* unrepresentable magnitude — falls back to 1. The bare Number() this
* replaces let NaN/fractional/reinterpreted starts through, and the
* markdown codec (which reads order as u64) then silently renumbered
* pasted lists from 1 on save.
*/
export function parseOrderedListStart(raw: string | null): number {
if (raw === null) return 1;
const trimmed = raw.trim();
if (!/^\d+$/.test(trimmed)) return 1;
const n = Number(trimmed);
return Number.isSafeInteger(n) ? n : 1;
}

/**
* Full schema with list support and GFM extensions for rich/markdown/plain modes.
* Extends prosemirror-schema-basic with strikethrough mark, task list nodes, and table nodes.
Expand Down Expand Up @@ -97,7 +115,7 @@ export const richSchema = new Schema({
tag: "ol",
getAttrs(dom) {
const el = dom as HTMLElement;
return { order: el.hasAttribute("start") ? Number(el.getAttribute("start")) : 1 };
return { order: parseOrderedListStart(el.getAttribute("start")) };
},
},
],
Expand Down
Loading