Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(picture): support sizes attribute #482

Merged
merged 4 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 22 additions & 4 deletions plugins/picture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,35 @@ export default function (): Plugin {
basePath: string,
) {
const src = img.getAttribute("src") as string;
const sizes = img.getAttribute("sizes");
const sourceFormats = saveTransform(basePath, src, imagick);

for (const sourceFormat of sourceFormats) {
const source = createSource(img.ownerDocument!, src, sourceFormat);
const source = createSource(
img.ownerDocument!,
src,
sourceFormat,
sizes,
);
picture.insertBefore(source, img);
}
}

function handleImg(imagick: string, img: Element, basePath: string) {
const src = img.getAttribute("src") as string;
const sizes = img.getAttribute("sizes");
const sourceFormats = saveTransform(basePath, src, imagick);
const picture = img.ownerDocument!.createElement("picture");

img.replaceWith(picture);

for (const sourceFormat of sourceFormats) {
const source = createSource(img.ownerDocument!, src, sourceFormat);
const source = createSource(
img.ownerDocument!,
src,
sourceFormat,
sizes,
);
picture.append(source);
}

Expand Down Expand Up @@ -194,19 +206,25 @@ function createSource(
document: Document,
src: string,
srcFormat: SourceFormat,
sizes?: string | null | undefined,
) {
const source = document.createElement("source");
const { scales, format } = srcFormat;
const { scales, format, width } = srcFormat;
const path = encodeURI(getPathAndExtension(src)[0]);
const srcset: string[] = [];

for (const [suffix, scale] of Object.entries(scales)) {
const scaleSuffix = scale === 1 ? "" : ` ${scale}x`;
const scaleSuffix = ` ${scale * width}w`;
cawa-93 marked this conversation as resolved.
Show resolved Hide resolved
srcset.push(`${path}${suffix}.${format}${scaleSuffix}`);
}

source.setAttribute("srcset", srcset.join(", "));
source.setAttribute("type", typeByExtension(format));

if (sizes) {
source.setAttribute("sizes", sizes);
}

return source;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/__snapshots__/picture.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ snapshot[`imagick plugin 3`] = `

<body>
<div>
<picture><source srcset="/kevin%20schmid%20unsplash-600w.png, /kevin%20schmid%20unsplash-600w@2.png 2x" type="image/png"><img src="/kevin schmid unsplash.jpg"></picture>
<picture><source srcset="/kevin%20schmid%20unsplash-600w.png 600w, /kevin%20schmid%20unsplash-600w@2.png 1200w" type="image/png"><img src="/kevin schmid unsplash.jpg"></picture>
</div>


<picture>
<source srcset="/kevin%20schmid%20unsplash-600w.avif, /kevin%20schmid%20unsplash-600w@2.avif 2x" type="image/avif"><source srcset="/kevin%20schmid%20unsplash-600w.webp, /kevin%20schmid%20unsplash-600w@2.webp 2x" type="image/webp"><source srcset="/kevin%20schmid%20unsplash-600w.jpg, /kevin%20schmid%20unsplash-600w@2.jpg 2x" type="image/jpeg"><img src="/kevin schmid unsplash.jpg">
<source srcset="/kevin%20schmid%20unsplash-600w.avif 600w, /kevin%20schmid%20unsplash-600w@2.avif 1200w" type="image/avif"><source srcset="/kevin%20schmid%20unsplash-600w.webp 600w, /kevin%20schmid%20unsplash-600w@2.webp 1200w" type="image/webp"><source srcset="/kevin%20schmid%20unsplash-600w.jpg 600w, /kevin%20schmid%20unsplash-600w@2.jpg 1200w" type="image/jpeg"><img src="/kevin schmid unsplash.jpg">
</picture>

<!-- This image will be converted to a picture -->
<picture><source srcset="/kevin%20schmid%20unsplash-300w.avif, /kevin%20schmid%20unsplash-300w@2.avif 2x" type="image/avif"><source srcset="/kevin%20schmid%20unsplash-300w.webp, /kevin%20schmid%20unsplash-300w@2.webp 2x" type="image/webp"><source srcset="/kevin%20schmid%20unsplash-300w.jpg, /kevin%20schmid%20unsplash-300w@2.jpg 2x" type="image/jpeg"><img src="/kevin schmid unsplash.jpg"></picture>
<picture><source srcset="/kevin%20schmid%20unsplash-300w.avif 300w, /kevin%20schmid%20unsplash-300w@2.avif 600w" type="image/avif"><source srcset="/kevin%20schmid%20unsplash-300w.webp 300w, /kevin%20schmid%20unsplash-300w@2.webp 600w" type="image/webp"><source srcset="/kevin%20schmid%20unsplash-300w.jpg 300w, /kevin%20schmid%20unsplash-300w@2.jpg 600w" type="image/jpeg"><img src="/kevin schmid unsplash.jpg"></picture>


</body></html>',
Expand Down