Skip to content

Commit

Permalink
fix: avoid duplicating width/height attributes (#12673)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Sep 13, 2024
1 parent be8e39d commit 899eede
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-cups-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/enhanced-img': patch
---

fix: avoid duplicating width/height attributes
4 changes: 2 additions & 2 deletions packages/enhanced-img/src/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ function serialize_img_attributes(content, attributes, details) {
/** @type {number | undefined} */
let user_height;
for (const attribute of attributes) {
if (attribute.name === 'width') user_width = parseInt(attribute.value[0]);
if (attribute.name === 'height') user_height = parseInt(attribute.value[0]);
if (attribute.name === 'width') user_width = parseInt(attribute.value[0].raw);
if (attribute.name === 'height') user_height = parseInt(attribute.value[0].raw);
}
if (!user_width && !user_height) {
attribute_strings.push(`width=${details.width}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/enhanced-img/test/Output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<picture><source srcset={__DECLARED_ASSET_0__} type="image/avif" /><source srcset={__DECLARED_ASSET_1__} type="image/webp" /><source srcset={__DECLARED_ASSET_2__} type="image/png" /><img src={__DECLARED_ASSET_3__} alt="production test" width=1440 height=1440 /></picture>

<picture><source srcset="/1 1440w, /2 960w" type="image/avif" /><source srcset="/3 1440w, /4 960w" type="image/webp" /><source srcset="5 1440w, /6 960w" type="image/png" /><img src="/7" width="5" height="10" alt="dimensions test" width=1440 height=1440 /></picture>
<picture><source srcset="/1 1440w, /2 960w" type="image/avif" /><source srcset="/3 1440w, /4 960w" type="image/webp" /><source srcset="5 1440w, /6 960w" type="image/png" /><img src="/7" width="5" height="10" alt="dimensions test" /></picture>

<picture><source srcset="/1 1440w, /2 960w" type="image/avif" /><source srcset="/3 1440w, /4 960w" type="image/webp" /><source srcset="5 1440w, /6 960w" type="image/png" /><img src="/7" alt="directive test" width=1440 height=1440 /></picture>

Expand Down

0 comments on commit 899eede

Please sign in to comment.