@@ -143,6 +143,44 @@ fn embed_image(
143143 // Really do nothing with the image!
144144 e. insert ( EmbeddedResources { id : maybe_id, kind : EmbeddedResourcesKind :: ListOnly } ) ;
145145 return ImageReference :: None ;
146+ } else if path. starts_with ( "data:" ) {
147+ // Handle data URLs by validating and decoding them at compile time
148+ if let Ok ( data_url) = dataurl:: DataUrl :: parse ( path) {
149+ let media_type = data_url. get_media_type ( ) ;
150+ if !media_type. starts_with ( "image/" ) {
151+ // Non-image data URLs should cause compilation errors
152+ diag. push_error ( format ! ( "Data URL with unsupported media type '{}'. Only image/* data URLs are supported" , media_type) , source_location) ;
153+ return ImageReference :: None ;
154+ }
155+
156+ let extension = media_type. split ( '/' ) . nth ( 1 ) . unwrap_or ( "" ) . to_string ( ) ;
157+ let decoded_data = if data_url. get_is_base64_encoded ( ) {
158+ data_url. get_data ( ) . to_vec ( )
159+ } else {
160+ data_url. get_text ( ) . as_bytes ( ) . to_vec ( )
161+ } ;
162+
163+ // Check for oversized data URLs (> 1 MiB)
164+ const MAX_DATA_URL_SIZE : usize = 1024 * 1024 ; // 1 MiB
165+ if decoded_data. len ( ) > MAX_DATA_URL_SIZE {
166+ diag. push_error (
167+ format ! (
168+ "Data URL is too large ({} bytes > {} bytes). Consider using a file reference instead." ,
169+ decoded_data. len( ) ,
170+ MAX_DATA_URL_SIZE
171+ ) ,
172+ source_location,
173+ ) ;
174+ return ImageReference :: None ;
175+ }
176+
177+ // For data URLs, store the decoded data with its extension
178+ let kind = EmbeddedResourcesKind :: DecodedData ( decoded_data, extension) ;
179+ e. insert ( EmbeddedResources { id : maybe_id, kind } )
180+ } else {
181+ diag. push_error ( format ! ( "Invalid data URL format: {}" , path) , source_location) ;
182+ return ImageReference :: None ;
183+ }
146184 } else if let Some ( _file) = crate :: fileaccess:: load_file ( std:: path:: Path :: new ( path) ) {
147185 #[ allow( unused_mut) ]
148186 let mut kind = EmbeddedResourcesKind :: RawData ;
@@ -173,11 +211,14 @@ fn embed_image(
173211 }
174212 } ;
175213
176- match e. kind {
214+ match & e. kind {
177215 #[ cfg( feature = "software-renderer" ) ]
178216 EmbeddedResourcesKind :: TextureData { .. } => {
179217 ImageReference :: EmbeddedTexture { resource_id : e. id }
180218 }
219+ EmbeddedResourcesKind :: DecodedData ( _, extension) => {
220+ ImageReference :: EmbeddedData { resource_id : e. id , extension : extension. clone ( ) }
221+ }
181222 _ => ImageReference :: EmbeddedData {
182223 resource_id : e. id ,
183224 extension : std:: path:: Path :: new ( path)
0 commit comments