Skip to content

Commit 65931f2

Browse files
committed
fix: ensure src attribute is properly formed
1 parent feb0154 commit 65931f2

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

.changeset/sharp-hats-shake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/enhanced-img': patch
3+
---
4+
5+
fix: ensure src attribute is properly formed

packages/enhanced-img/src/preprocessor.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ function get_attr_value(node, attr) {
220220
* height: string | number
221221
* }} details
222222
*/
223-
function img_attributes_to_markdown(content, attributes, details) {
223+
function serialize_img_attributes(content, attributes, details) {
224224
const attribute_strings = attributes.map((attribute) => {
225225
if (attribute.name === 'src') {
226226
return `src=${details.src}`;
@@ -279,22 +279,18 @@ function img_to_picture(content, node, image) {
279279
}
280280

281281
let res = '<picture>';
282+
282283
for (const [format, srcset] of Object.entries(image.sources)) {
283284
res += `<source srcset={"${srcset}"}${sizes_string} type="image/${format}" />`;
284285
}
285-
// Need to handle src differently when using either Vite's renderBuiltUrl or relative base path in Vite.
286-
// See https://github.com/vitejs/vite/blob/b93dfe3e08f56cafe2e549efd80285a12a3dc2f0/packages/vite/src/node/plugins/asset.ts#L132
287-
const src =
288-
image.img.src.startsWith('"+') && image.img.src.endsWith('+"')
289-
? `{"${image.img.src.substring(2, image.img.src.length - 2)}"}`
290-
: `"${image.img.src}"`;
291-
res += `<img ${img_attributes_to_markdown(content, attributes, {
292-
src,
286+
287+
res += `<img ${serialize_img_attributes(content, attributes, {
288+
src: image.img.src.startsWith('__VITE_ASSET__') ? `{"${image.img.src}"}` : `"${image.img.src}"`,
293289
width: image.img.w,
294290
height: image.img.h
295291
})} />`;
296-
res += '</picture>';
297-
return res;
292+
293+
return (res += '</picture>');
298294
}
299295

300296
/**
@@ -320,13 +316,13 @@ function dynamic_img_to_picture(content, node, src_var_name) {
320316
};
321317

322318
return `{#if typeof ${src_var_name} === 'string'}
323-
<img ${img_attributes_to_markdown(content, node.attributes, details)} />
319+
<img ${serialize_img_attributes(content, node.attributes, details)} />
324320
{:else}
325321
<picture>
326322
{#each Object.entries(${src_var_name}.sources) as [format, srcset]}
327323
<source {srcset}${sizes_string} type={'image/' + format} />
328324
{/each}
329-
<img ${img_attributes_to_markdown(content, attributes, details)} />
325+
<img ${serialize_img_attributes(content, attributes, details)} />
330326
</picture>
331327
{/if}`;
332328
}

0 commit comments

Comments
 (0)