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

fix: support shorthand attribute syntax on enhanced-img #11884

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading