Files
RuneStone/tests/unit/editor/imageSizes.spec.ts
T
Knotty Wyvern 2b28707ba0 Keep the view in place while scrolling through tables and images
The view jumped to other text in large documents. A short line with
another height, such as a code fence, became the CodeMirror reference
for the line height. CodeMirror then estimated all heights again, and
its layout loop stopped before it restored the scroll position. Each new
table or image height started this check.

- A mark span keeps lines with another height out of that reading.
- A resize observer records the preview heights. A measure request from
  `toDOM` restarted the layout loop.
- A hidden layer measures each table before its first draw.
- Images keep their space from their natural size, in Write and Read.

The scrollStability smoke test and the imageSizes unit test check this.
Real wheel input on large documents gave no jumps in Write and Read.
Card be7df1de.
2026-11-04 21:41:20 -07:00

33 lines
1.6 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { estimatedImageHeight, imageSize, rememberImageSize } from "../../../src/lib/workspace/imageSizes.ts";
import { renderMarkdownHtml } from "../../../src/lib/workspace/noteHtml.ts";
test("a rendered image has size attributes only when its size is known", () => {
const unknown = "data:image/png;base64,AAAA";
const known = "data:image/png;base64,BBBB";
rememberImageSize(known, 1400, 700);
const urls = new Map([["a.png", unknown], ["b.png", known]]);
assert.doesNotMatch(renderMarkdownHtml("![a](a.png)", urls), /width=/);
assert.match(renderMarkdownHtml("![b](b.png)", urls), /width="1400" height="700"/);
});
test("a large data URL keeps its size without the image data as the key", () => {
const url = `data:image/png;base64,${"A".repeat(5000)}${"B".repeat(100)}${"C".repeat(5000)}`;
const other = `data:image/png;base64,${"A".repeat(5000)}${"D".repeat(100)}${"C".repeat(5000)}`;
rememberImageSize(url, 10, 20);
assert.deepEqual(imageSize(url), { width: 10, height: 20 });
assert.equal(imageSize(other), undefined);
});
test("an image without a real size stays unknown", () => {
rememberImageSize("data:image/svg+xml;base64,AAAA", 0, 0);
assert.equal(imageSize("data:image/svg+xml;base64,AAAA"), undefined);
});
test("the estimate scales a wide image to the text column and keeps a small image", () => {
assert.equal(estimatedImageHeight('<img src="x" alt="">'), -1);
assert.equal(estimatedImageHeight('<img width="1480" height="740">'), 370 + 45);
assert.equal(estimatedImageHeight('<img width="100" height="50">'), 50 + 45);
});