55
66import { fetchImage } from '../utils/helpers.js' ;
77
8+
9+ function setImgPlaceholder ( img ) {
10+ const dsW = parseInt ( img . dataset ?. snapdomWidth || '' , 10 ) || 0 ;
11+ const dsH = parseInt ( img . dataset ?. snapdomHeight || '' , 10 ) || 0 ;
12+ const attrW = parseInt ( img . getAttribute ( 'width' ) || '' , 10 ) || 0 ;
13+ const attrH = parseInt ( img . getAttribute ( 'height' ) || '' , 10 ) || 0 ;
14+ const styleW = parseFloat ( img . style ?. width || '' ) || 0 ;
15+ const styleH = parseFloat ( img . style ?. height || '' ) || 0 ;
16+ const w = dsW || styleW || attrW || img . width || 100 ;
17+ const h = dsH || styleH || attrH || img . height || 100 ;
18+
19+ const fallback = document . createElement ( "div" ) ;
20+ fallback . style = `width: ${ w } px; height: ${ h } px; background: #ccc; display: inline-block; text-align: center; line-height: ${ h } px; color: #666; font-size: 12px;` ;
21+ fallback . innerText = "img" ;
22+ img . replaceWith ( fallback ) ;
23+ }
24+
825/**
926 * Converts all <img> elements in the clone to data URLs or replaces them with placeholders if loading fails.
1027 *
@@ -19,6 +36,7 @@ export async function inlineImages(clone, options = {}) {
1936 const eff = img . currentSrc || img . src || '' ;
2037 if ( eff ) img . setAttribute ( 'src' , eff ) ;
2138 }
39+
2240 img . removeAttribute ( 'srcset' ) ;
2341 img . removeAttribute ( 'sizes' ) ;
2442 const src = img . src ;
@@ -28,10 +46,36 @@ export async function inlineImages(clone, options = {}) {
2846 if ( ! img . width ) img . width = img . naturalWidth || 100 ;
2947 if ( ! img . height ) img . height = img . naturalHeight || 100 ;
3048 } catch {
31- const fallback = document . createElement ( "div" ) ;
32- fallback . style = `width: ${ img . width || 100 } px; height: ${ img . height || 100 } px; background: #ccc; display: inline-block; text-align: center; line-height: ${ img . height || 100 } px; color: #666; font-size: 12px;` ;
33- fallback . innerText = "img" ;
34- img . replaceWith ( fallback ) ;
49+ // Try defaultImageUrl (string or callback)
50+ const { defaultImageUrl } = options || { } ;
51+ if ( defaultImageUrl ) {
52+ try {
53+ const dsW = parseInt ( img . dataset ?. snapdomWidth || '' , 10 ) || 0 ;
54+ const dsH = parseInt ( img . dataset ?. snapdomHeight || '' , 10 ) || 0 ;
55+ const attrW = parseInt ( img . getAttribute ( 'width' ) || '' , 10 ) || 0 ;
56+ const attrH = parseInt ( img . getAttribute ( 'height' ) || '' , 10 ) || 0 ;
57+ const styleW = parseFloat ( img . style ?. width || '' ) || 0 ;
58+ const styleH = parseFloat ( img . style ?. height || '' ) || 0 ;
59+ const width = dsW || styleW || attrW || img . width || undefined ;
60+ const height = dsH || styleH || attrH || img . height || undefined ;
61+
62+ const fallbackUrl = typeof defaultImageUrl === 'function'
63+ ? await defaultImageUrl ( { width, height, src, element : img } )
64+ : defaultImageUrl ;
65+
66+ if ( fallbackUrl ) {
67+ const fallbackData = await fetchImage ( fallbackUrl , { useProxy : options . useProxy } ) ;
68+ img . src = fallbackData ;
69+ if ( ! img . width && width ) img . width = width ;
70+ if ( ! img . height && height ) img . height = height ;
71+ if ( ! img . width ) img . width = img . naturalWidth || 100 ;
72+ if ( ! img . height ) img . height = img . naturalHeight || 100 ;
73+ return ;
74+ }
75+ } catch { }
76+ }
77+
78+ setImgPlaceholder ( img ) ;
3579 }
3680 } ;
3781 for ( let i = 0 ; i < imgs . length ; i += 4 ) {
0 commit comments