Keep the frames of pasted GIF, WebP, and AVIF images
A browser puts only the first frame of an image on the clipboard, with HTML that names the image address. - Documents and cards download the original of a GIF, WebP, or AVIF address. The host commands workspace_save_image_url and workspace_download_image read the format from the file contents and refuse other data. A failed download keeps the first frame and shows a message. - Copy image in the RuneStone browser keeps the bytes that the page loaded. The engine reads them through the DevTools Protocol, so the website gets no second request. The new copiedImage command gives them in parts below the line limit of the host. A paste uses them first. - An empty dropped image file shows a message. Firefox gives such a file when the website refuses its download. Copy image needs a new browser engine build. Card 0d5c18f7.
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { attachmentRel, hasFileDrag, imageFiles, imageInsertion, workspaceImageLink } from "../../../src/lib/workspace/media.ts";
|
||||
import { attachmentRel, dataUrlExtension, hasFileDrag, imageFiles, imageInsertion, mayAnimate, pastedImageSource, workspaceImageLink } from "../../../src/lib/workspace/media.ts";
|
||||
import { copiedImage, keepCopiedImage } from "../../../src/lib/workspace/copiedImage.ts";
|
||||
import { imageSource, editImageSource, imageSourceRanges } from "../../../src/lib/editor/imageSource.ts";
|
||||
import { imageDrops, trackImageDrop } from "../../../src/lib/editor/imageDrop.ts";
|
||||
|
||||
@@ -18,6 +19,51 @@ test("protected file drags and files without MIME metadata are recognized", () =
|
||||
assert.deepEqual(imageFiles({ files } as unknown as DataTransfer), [files[0]]);
|
||||
});
|
||||
|
||||
test("a copied web image names its original file in the clipboard HTML", () => {
|
||||
const copy = (src: string) => `<html><body>\r\n<!--StartFragment--><img src="${src}"/><!--EndFragment-->\r\n</body>\r\n</html>`;
|
||||
assert.equal(pastedImageSource(copy("https://media.example.com/cat.gif")), "https://media.example.com/cat.gif");
|
||||
assert.equal(pastedImageSource(copy("https://cdn.example.com/a/b.WEBP?width=200&ex=1")), "https://cdn.example.com/a/b.WEBP?width=200&ex=1");
|
||||
assert.equal(pastedImageSource("<img alt='x' data-src='a.png' src='http://example.com/x.avif'>"), "http://example.com/x.avif");
|
||||
assert.equal(pastedImageSource("<img src=https://example.com/x.gif>"), "https://example.com/x.gif");
|
||||
assert.equal(pastedImageSource(copy("https://example.com/photo.jpg")), "https://example.com/photo.jpg");
|
||||
// Firefox copies the element of the page with all of its attributes.
|
||||
assert.equal(pastedImageSource('<img id="img" src="https://h.example.net/om/1/x.gif" style="width:640px" onload="update()">'), "https://h.example.net/om/1/x.gif");
|
||||
// A missing address and other schemes keep the clipboard copy.
|
||||
assert.equal(pastedImageSource(copy("data:image/gif;base64,R0lGODlh")), undefined);
|
||||
assert.equal(pastedImageSource(copy("file:///C:/cat.gif")), undefined);
|
||||
assert.equal(pastedImageSource(copy("cat.gif")), undefined);
|
||||
assert.equal(pastedImageSource('<img srcset="https://example.com/x.gif 1x">'), undefined);
|
||||
assert.equal(pastedImageSource(""), undefined);
|
||||
// A copy of a page part with two images is not a copied image.
|
||||
assert.equal(pastedImageSource(copy("https://example.com/a.gif") + copy("https://example.com/b.gif")), undefined);
|
||||
});
|
||||
|
||||
test("only formats with frames get a download, and data URLs name their extension", () => {
|
||||
assert.equal(mayAnimate("https://media.example.com/cat.gif"), true);
|
||||
assert.equal(mayAnimate("https://cdn.example.com/a/b.WEBP?width=200"), true);
|
||||
assert.equal(mayAnimate("http://example.com/x.avif"), true);
|
||||
assert.equal(mayAnimate("https://example.com/photo.jpg"), false);
|
||||
assert.equal(mayAnimate("https://example.com/image?format=gif"), false);
|
||||
assert.equal(dataUrlExtension("data:image/gif;base64,R0lGODlh"), "gif");
|
||||
assert.equal(dataUrlExtension("data:image/jpeg;base64,/9j/"), "jpg");
|
||||
assert.equal(dataUrlExtension("data:image/webp;base64,UklG"), "webp");
|
||||
assert.equal(dataUrlExtension("data:image/svg+xml;base64,PHN2Zz4="), undefined);
|
||||
assert.equal(dataUrlExtension("data:text/html;base64,PGh0bWw+"), undefined);
|
||||
});
|
||||
|
||||
test("a paste uses only the original of the image that the RuneStone browser copied last", async () => {
|
||||
assert.equal(await copiedImage("https://example.com/a.gif"), undefined);
|
||||
let finish: (value: string) => void = () => {};
|
||||
keepCopiedImage("https://example.com/a.gif", new Promise(resolve => { finish = resolve; }));
|
||||
const waiting = copiedImage("https://example.com/a.gif");
|
||||
finish("data:image/gif;base64,R0lGODlh");
|
||||
assert.equal(await waiting, "data:image/gif;base64,R0lGODlh");
|
||||
assert.equal(await copiedImage("https://example.com/b.gif"), undefined);
|
||||
keepCopiedImage("https://example.com/b.gif", Promise.reject(new Error("engine stopped")));
|
||||
assert.equal(await copiedImage("https://example.com/b.gif"), undefined);
|
||||
assert.equal(await copiedImage("https://example.com/a.gif"), undefined, "a newer copy replaces the older one");
|
||||
});
|
||||
|
||||
test("image batches form independent Markdown blocks without extra blank lines", () => {
|
||||
assert.equal(imageInsertion("beforeafter", 6, 6, ["", ""]), "\n\n\n\n\n\n");
|
||||
assert.equal(imageInsertion("before\n\nafter", 8, 8, [""]), "\n\n");
|
||||
|
||||
Reference in New Issue
Block a user