@@ -12,8 +12,6 @@ use crate::jsontypes::RawSourceMap;
1212use crate :: types:: { DecodedMap , RawToken , SourceMap , SourceMapIndex , SourceMapSection } ;
1313use crate :: vlq:: parse_vlq_segment_into;
1414
15- const DATA_PREAMBLE : & str = "data:application/json;base64," ;
16-
1715#[ derive( PartialEq , Eq ) ]
1816enum HeaderState {
1917 Undecided ,
@@ -339,12 +337,22 @@ pub fn decode_slice(slice: &[u8]) -> Result<DecodedMap> {
339337 decode_common ( rsm)
340338}
341339
342- /// Loads a sourcemap from a data URL
340+ /// Loads a sourcemap from a data URL.
341+ ///
342+ /// The URL should match the regex
343+ /// `data:application/json;(charset=utf-?8;)?base64,(?<base64sourcemap>.+)`.
343344pub fn decode_data_url ( url : & str ) -> Result < DecodedMap > {
344- if !url. starts_with ( DATA_PREAMBLE ) {
345- return Err ( Error :: InvalidDataUrl ) ;
346- }
347- let data_b64 = & url[ DATA_PREAMBLE . len ( ) ..] ;
345+ let rest = url
346+ . strip_prefix ( "data:application/json" )
347+ . ok_or ( Error :: InvalidDataUrl ) ?;
348+ let rest = match rest. strip_prefix ( ";charset=" ) {
349+ Some ( rest) => rest
350+ . strip_prefix ( "utf-8" )
351+ . or_else ( || rest. strip_prefix ( "utf8" ) )
352+ . ok_or ( Error :: InvalidDataUrl ) ?,
353+ None => rest,
354+ } ;
355+ let data_b64 = rest. strip_prefix ( ";base64," ) . ok_or ( Error :: InvalidDataUrl ) ?;
348356 let data = data_encoding:: BASE64
349357 . decode ( data_b64. as_bytes ( ) )
350358 . map_err ( |_| Error :: InvalidDataUrl ) ?;
0 commit comments