@@ -17,11 +17,12 @@ import {
1717 type SrcSetValue ,
1818 type UnresolvedImageTransform ,
1919} from './types.js' ;
20- import { addCSSVarsToStyle , cssFitValues } from './utils/imageAttributes.js' ;
2120import { isESMImportedImage , isRemoteImage , resolveSrc } from './utils/imageKind.js' ;
2221import { inferRemoteSize } from './utils/remoteProbe.js' ;
2322import { createPlaceholderURL , stringifyPlaceholderURL } from './utils/url.js' ;
2423
24+ export const cssFitValues = [ 'fill' , 'contain' , 'cover' , 'scale-down' ] ;
25+
2526export async function getConfiguredImageService ( ) : Promise < ImageService > {
2627 if ( ! globalThis ?. astroAsset ?. imageService ) {
2728 const { default : service } : { default : ImageService } = await import (
@@ -149,14 +150,19 @@ export async function getImage(
149150 resolvedOptions . sizes ||= getSizesAttribute ( { width : resolvedOptions . width , layout } ) ;
150151 // The densities option is incompatible with the `layout` option
151152 delete resolvedOptions . densities ;
152- resolvedOptions . style = addCSSVarsToStyle (
153- {
154- fit : cssFitValues . includes ( resolvedOptions . fit ?? '' ) && resolvedOptions . fit ,
155- pos : resolvedOptions . position ,
156- } ,
157- resolvedOptions . style ,
158- ) ;
153+
154+ // Set data attribute for layout
159155 resolvedOptions [ 'data-astro-image' ] = layout ;
156+
157+ // Set data attributes for fit and position for CSP-compliant styling
158+ if ( resolvedOptions . fit && cssFitValues . includes ( resolvedOptions . fit ) ) {
159+ resolvedOptions [ 'data-astro-image-fit' ] = resolvedOptions . fit ;
160+ }
161+
162+ if ( resolvedOptions . position ) {
163+ // Normalize position value for data attribute (spaces to dashes)
164+ resolvedOptions [ 'data-astro-image-pos' ] = resolvedOptions . position . replace ( / \s + / g, '-' ) ;
165+ }
160166 }
161167
162168 const validatedOptions = service . validateOptions
0 commit comments