Skip to content

Commit e44b9d9

Browse files
committed
Fix placeholder dimensions when image loading fails
1 parent 20dccf1 commit e44b9d9

1 file changed

Lines changed: 26 additions & 22 deletions

File tree

src/modules/images.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,25 @@
55

66
import { snapFetch } from './snapFetch.js'
77

8+
/**
9+
* Extract dimensions from an image element in priority order
10+
* @param {HTMLImageElement} img
11+
* @returns {{ width: number, height: number }}
12+
*/
13+
function extractImageDimensions(img) {
14+
const dsW = parseInt(img.dataset?.snapdomWidth || '', 10) || 0
15+
const dsH = parseInt(img.dataset?.snapdomHeight || '', 10) || 0
16+
const attrW = parseInt(img.getAttribute('width') || '', 10) || 0
17+
const attrH = parseInt(img.getAttribute('height') || '', 10) || 0
18+
const styleW = parseFloat(img.style?.width || '') || 0
19+
const styleH = parseFloat(img.style?.height || '') || 0
20+
21+
const w = dsW || styleW || attrW || img.width || img.naturalWidth || 100
22+
const h = dsH || styleH || attrH || img.height || img.naturalHeight || 100
23+
24+
return { width: w, height: h }
25+
}
26+
827
/**
928
* Converts all <img> elements in the clone to data URLs or replaces them with
1029
* placeholders if loading fails. Compatible with the new non-throwing snapFetch.
@@ -41,19 +60,10 @@ export async function inlineImages(clone, options = {}) {
4160
return
4261
}
4362
// Try fallbackURL (string or callback)
63+
const { width: fbW, height: fbH } = extractImageDimensions(img)
4464
const { fallbackURL } = options || {}
4565
if (fallbackURL) {
4666
try {
47-
const dsW = parseInt(img.dataset?.snapdomWidth || '', 10) || 0
48-
const dsH = parseInt(img.dataset?.snapdomHeight || '', 10) || 0
49-
const attrW = parseInt(img.getAttribute('width') || '', 10) || 0
50-
const attrH = parseInt(img.getAttribute('height') || '', 10) || 0
51-
const styleW = parseFloat(img.style?.width || '') || 0
52-
const styleH = parseFloat(img.style?.height || '') || 0
53-
54-
const fbW = dsW || styleW || attrW || img.width || undefined
55-
const fbH = dsH || styleH || attrH || img.height || undefined
56-
5767
const fallbackUrl =
5868
typeof fallbackURL === 'function'
5969
? await fallbackURL({ width: fbW, height: fbH, src, element: img })
@@ -64,30 +74,24 @@ export async function inlineImages(clone, options = {}) {
6474
img.src = fallbackData.data
6575

6676
// Mantener tu comportamiento actual:
67-
if (!img.width && fbW) img.width = fbW
68-
if (!img.height && fbH) img.height = fbH
69-
if (!img.width) img.width = img.naturalWidth || 100
70-
if (!img.height) img.height = img.naturalHeight || 100
77+
if (!img.width) img.width = fbW
78+
if (!img.height) img.height = fbH
7179
return
7280
}
7381
} catch {
7482
// noop → cae al placeholder
7583
}
7684
}
7785

78-
// Failure path: sized, neutral fallback
79-
const w = img.width || img.naturalWidth || 100
80-
const h = img.height || img.naturalHeight || 100
81-
8286
if (options.placeholders !== false) {
8387
const fallback = document.createElement('div')
8488
fallback.style.cssText = [
85-
`width:${w}px`,
86-
`height:${h}px`,
89+
`width:${fbW}px`,
90+
`height:${fbH}px`,
8791
'background:#ccc',
8892
'display:inline-block',
8993
'text-align:center',
90-
`line-height:${h}px`,
94+
`line-height:${fbH}px`,
9195
'color:#666',
9296
'font-size:12px',
9397
'overflow:hidden'
@@ -96,7 +100,7 @@ export async function inlineImages(clone, options = {}) {
96100
img.replaceWith(fallback)
97101
} else {
98102
const spacer = document.createElement('div')
99-
spacer.style.cssText = `display:inline-block;width:${w}px;height:${h}px;visibility:hidden;`
103+
spacer.style.cssText = `display:inline-block;width:${fbW}px;height:${fbH}px;visibility:hidden;`
100104
img.replaceWith(spacer)
101105
}
102106
}

0 commit comments

Comments
 (0)