diff --git a/pkgs/id/web/src/editor.test.ts b/pkgs/id/web/src/editor.test.ts index a809488111..52a080a4f6 100644 --- a/pkgs/id/web/src/editor.test.ts +++ b/pkgs/id/web/src/editor.test.ts @@ -13,6 +13,7 @@ import { hasToolbar, initEditor, isEditable, + parseOrderedListStart, rawSchema, richSchema, } from "./editor"; @@ -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(); diff --git a/pkgs/id/web/src/editor.ts b/pkgs/id/web/src/editor.ts index 364ac864e1..2489354dce 100644 --- a/pkgs/id/web/src/editor.ts +++ b/pkgs/id/web/src/editor.ts @@ -38,6 +38,24 @@ import { createWrapPlugins } from "./wrap"; */ export type ContentMode = "rich" | "markdown" | "plain" | "raw" | "media" | "binary"; +/** + * Parse an
    `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. @@ -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")) }; }, }, ],