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
61 changes: 61 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,
parseImageDimension,
parseOrderedListStart,
rawSchema,
richSchema,
Expand Down Expand Up @@ -658,3 +659,63 @@ describe("getSendableSteps", () => {
expect(result!.steps.length).toBeGreaterThan(0);
});
});

// ============================================================================
// #2010: image parseDOM width/height parsing — only plain non-negative
// safe integers survive; everything else is dropped to null at parse time
// (no NaN, fractions, negatives, or reinterpreted hex/scientific values)
// ============================================================================

describe("image dimension parsing (#2010)", () => {
it.each([
["200", 200],
["0", 0],
["42", 42],
])("preserves valid dimension=%s as %i", (raw, expected) => {
expect(parseImageDimension(raw)).toBe(expected);
});

it.each(["200px", "50%", "2.5", "-5", "abc", "0x10", "1e3", "", "9007199254740993"])(
"drops invalid dimension=%s to null",
(raw) => {
expect(parseImageDimension(raw)).toBeNull();
},
);

it("drops an absent dimension to null", () => {
expect(parseImageDimension(null)).toBeNull();
});

const imgGetAttrs = () => {
const rule = richSchema.nodes.image.spec.parseDOM?.[0];
if (!rule || !("getAttrs" in rule) || typeof rule.getAttrs !== "function") {
throw new Error("image parseDOM rule has no getAttrs");
}
return rule.getAttrs;
};

it.each([
["200", 200],
["200px", null],
["50%", null],
["2.5", null],
["-5", null],
["0x10", null],
])("parseDOM getAttrs maps width=%s to %s", (raw, expected) => {
const el = document.createElement("img");
el.setAttribute("src", "x.png");
el.setAttribute("width", raw);
el.setAttribute("height", raw);
const attrs = imgGetAttrs()(el) as { width: unknown; height: unknown };
expect(attrs.width).toBe(expected);
expect(attrs.height).toBe(expected);
});

it("parseDOM getAttrs defaults dimensions to null when absent", () => {
const el = document.createElement("img");
el.setAttribute("src", "x.png");
const attrs = imgGetAttrs()(el) as { width: unknown; height: unknown };
expect(attrs.width).toBeNull();
expect(attrs.height).toBeNull();
});
});
24 changes: 22 additions & 2 deletions pkgs/id/web/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ export function parseOrderedListStart(raw: string | null): number {
return Number.isSafeInteger(n) ? n : 1;
}

/**
* Parse an <img> `width`/`height` attribute into the image node's
* width/height attr (#2010). Only a plain non-negative decimal integer
* that fits in a safe integer is preserved (0 included); anything else
* — malformed text, fractions, negatives, units/percentages, hex or
* scientific notation, empty, an absent attribute, or an
* unrepresentable magnitude — is dropped to null at parse time. The
* bare Number() this replaces let NaN through and reinterpreted values
* like "0x10" as 16, while the markdown codec reads dimensions as u64,
* so invalid dimensions were silently dropped (or invented) on save
* instead of being rejected where the paste happened.
*/
export function parseImageDimension(raw: string | null): number | null {
if (raw === null) return null;
const trimmed = raw.trim();
if (!/^\d+$/.test(trimmed)) return null;
const n = Number(trimmed);
return Number.isSafeInteger(n) ? n : null;
}

/**
* 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 @@ -160,8 +180,8 @@ export const richSchema = new Schema({
src: el.getAttribute("src"),
alt: el.getAttribute("alt"),
title: el.getAttribute("title"),
width: el.getAttribute("width") ? Number(el.getAttribute("width")) : null,
height: el.getAttribute("height") ? Number(el.getAttribute("height")) : null,
width: parseImageDimension(el.getAttribute("width")),
height: parseImageDimension(el.getAttribute("height")),
linkHref: anchor?.getAttribute("href") ?? null,
linkTitle: anchor?.getAttribute("title") ?? null,
};
Expand Down
Loading