Skip to content

Commit

Permalink
fix: fix nested images in enhanced-img (#12945)
Browse files Browse the repository at this point in the history
  • Loading branch information
ottomated authored Nov 4, 2024
1 parent c4b76d0 commit c484ced
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-moons-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/enhanced-img': patch
---

fix: correctly handle `<enhanced:img />` elements nested in other DOM elements
7 changes: 5 additions & 2 deletions packages/enhanced-img/src/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ export function image(opts) {
* @type {Array<ReturnType<typeof update_element>>}
*/
const pending_ast_updates = [];
// TODO: switch to zimmerframe with Svelte 5

walk(
/** @type {import('svelte/compiler').AST.Root} */ (ast),
Expand All @@ -142,15 +141,19 @@ export function image(opts) {
},
/** @param {import('svelte/compiler').AST.RegularElement} node */
// @ts-ignore
RegularElement(node) {
RegularElement(node, { next }) {
if ('name' in node && node.name === 'enhanced:img') {
// Compare node tag match
const src = get_attr_value(node, 'src');

if (!src || typeof src === 'boolean') return;

pending_ast_updates.push(update_element(node, src));

return;
}

next();
}
}
);
Expand Down
4 changes: 4 additions & 0 deletions packages/enhanced-img/test/Input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

<enhanced:img src="./dev.png" alt="dev test" />

<div>
<enhanced:img src="./dev.png" alt="nested test" />
</div>

<enhanced:img src="./prod.png" alt="production test" />

<enhanced:img src="./dev.png" width="5" height="10" alt="dimensions test" />
Expand Down
4 changes: 4 additions & 0 deletions packages/enhanced-img/test/Output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

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

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

<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" /></picture>
Expand Down

0 comments on commit c484ced

Please sign in to comment.