Skip to content

Commit

Permalink
fix: support shorthand attribute syntax on enhanced-img (#11884)
Browse files Browse the repository at this point in the history
Co-authored-by: Willow (GHOST) <ghostdevbusiness@gmail.com>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 14, 2024
1 parent 877f4bb commit bd2eae7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-parrots-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/enhanced-img": patch
---

fix: support shorthand attribute syntax
4 changes: 4 additions & 0 deletions packages/enhanced-img/src/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export function image(opts) {
.trim();
s.update(node.start, node.end, dynamic_img_to_picture(content, node, src_var_name));
return;
} else if (src_attribute.type === 'AttributeShorthand') {
const src_var_name = content.substring(src_attribute.start, src_attribute.end).trim();
s.update(node.start, node.end, dynamic_img_to_picture(content, node, src_var_name));
return;
}

const original_url = src_attribute.raw.trim();
Expand Down
3 changes: 3 additions & 0 deletions packages/enhanced-img/test/Input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import manual_image1 from './no.png';
import manual_image2 from './no.svg';
const src = manual_image1;
const images = [manual_image1, manual_image2];
let foo: string = 'bar';
Expand Down Expand Up @@ -31,6 +32,8 @@

<enhanced:img src="/src/foo.png" alt="absolute path test" />

<enhanced:img {src} alt="attribute shorthand test" />

<enhanced:img src="./foo.svg" alt="svg test" />

{#each images as image}
Expand Down
12 changes: 12 additions & 0 deletions packages/enhanced-img/test/Output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import manual_image2 from './no.svg';
const src = manual_image1;
const images = [manual_image1, manual_image2];
let foo: string = 'bar';
Expand All @@ -30,6 +31,17 @@

<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="absolute path test" width=1440 height=1440 /></picture>

{#if typeof src === 'string'}
<img src={src.img.src} alt="attribute shorthand test" width={src.img.w} height={src.img.h} />
{:else}
<picture>
{#each Object.entries(src.sources) as [format, srcset]}
<source {srcset} type={'image/' + format} />
{/each}
<img src={src.img.src} alt="attribute shorthand test" width={src.img.w} height={src.img.h} />
</picture>
{/if}

<img src={___ASSET___0} alt="svg test" />

{#each images as image}
Expand Down

0 comments on commit bd2eae7

Please sign in to comment.