adds frontend
This commit is contained in:
83
test/asset-inliner.test.mjs
Normal file
83
test/asset-inliner.test.mjs
Normal file
@@ -0,0 +1,83 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { AssetInliner, splitSrcset } from "../src/asset-inliner.mjs";
|
||||
import { findExternalAssetRefs } from "../src/archiver.mjs";
|
||||
|
||||
test("inlines real srcset attributes without reading escaped src text from srcdoc", async () => {
|
||||
const fetched = [];
|
||||
const inliner = new AssetInliner();
|
||||
inliner.fetchAsset = async (rawUrl) => {
|
||||
fetched.push(rawUrl);
|
||||
return {
|
||||
bytes: Buffer.from("asset"),
|
||||
contentType: "image/png"
|
||||
};
|
||||
};
|
||||
|
||||
const html = `
|
||||
<img srcset="/small.png 1x, /large.png 2x">
|
||||
<iframe srcdoc="<script src="https://js.stripe.com/v3/foo.js"></script><img src="/nested.png">"></iframe>
|
||||
`;
|
||||
|
||||
const output = await inliner.inlineHtml(html, "https://example.com/article");
|
||||
|
||||
assert.deepEqual(fetched.sort(), [
|
||||
"https://example.com/large.png",
|
||||
"https://example.com/nested.png",
|
||||
"https://example.com/small.png",
|
||||
]);
|
||||
assert.doesNotMatch(output, /js\.stripe\.com/);
|
||||
assert.equal(inliner.warnings.length, 0);
|
||||
});
|
||||
|
||||
test("external asset reporting ignores escaped nested attributes inside srcdoc", () => {
|
||||
const refs = findExternalAssetRefs(`
|
||||
<iframe srcdoc="<img src="https://tracker.example/pixel.gif">"></iframe>
|
||||
<img src="https://cdn.example/picture.jpg">
|
||||
`);
|
||||
|
||||
assert.deepEqual(refs, ["https://cdn.example/picture.jpg"]);
|
||||
});
|
||||
|
||||
test("srcset parsing keeps image CDN transform commas inside URLs", async () => {
|
||||
assert.deepEqual(splitSrcset([
|
||||
"https://media.example/photos/id/master/w_120,c_limit/photo.jpg 120w",
|
||||
"https://media.example/photos/id/master/w_240,c_limit/photo.jpg 240w"
|
||||
].join(", ")), [
|
||||
"https://media.example/photos/id/master/w_120,c_limit/photo.jpg 120w",
|
||||
"https://media.example/photos/id/master/w_240,c_limit/photo.jpg 240w"
|
||||
]);
|
||||
|
||||
const fetched = [];
|
||||
const inliner = new AssetInliner();
|
||||
inliner.fetchAsset = async (rawUrl) => {
|
||||
fetched.push(rawUrl);
|
||||
return {
|
||||
bytes: Buffer.from("asset"),
|
||||
contentType: "image/jpeg"
|
||||
};
|
||||
};
|
||||
|
||||
await inliner.inlineSrcset(
|
||||
"https://media.example/photos/id/master/w_120,c_limit/photo.jpg 120w, https://media.example/photos/id/master/w_240,c_limit/photo.jpg 240w",
|
||||
"https://example.com/article"
|
||||
);
|
||||
|
||||
assert.deepEqual(fetched, [
|
||||
"https://media.example/photos/id/master/w_120,c_limit/photo.jpg",
|
||||
"https://media.example/photos/id/master/w_240,c_limit/photo.jpg"
|
||||
]);
|
||||
});
|
||||
|
||||
test("external asset reporting parses srcset-like attributes without splitting URL commas", () => {
|
||||
const refs = findExternalAssetRefs(`
|
||||
<img srcset="data:image/gif;base64,R0lGODlhAQABAAAAACw= 1x, https://cdn.example/image.jpg 2x">
|
||||
<link rel="preload" as="image" imagesrcset="https://media.example/photos/id/master/w_120,c_limit/photo.jpg 120w, https://media.example/photos/id/master/w_240,c_limit/photo.jpg 240w">
|
||||
`);
|
||||
|
||||
assert.deepEqual(refs, [
|
||||
"https://cdn.example/image.jpg",
|
||||
"https://media.example/photos/id/master/w_120,c_limit/photo.jpg",
|
||||
"https://media.example/photos/id/master/w_240,c_limit/photo.jpg"
|
||||
]);
|
||||
});
|
||||
Reference in New Issue
Block a user