@@ -560,11 +560,16 @@ interface ImageCandidate {
560
560
}
561
561
const escapedSpaceCharacters = / ( | \\ t | \\ n | \\ f | \\ r ) + / g
562
562
const imageSetUrlRE = / ^ (?: [ \w \- ] + \( .* ?\) | ' .* ?' | " .* ?" | \S * ) /
563
- export async function processSrcSet (
564
- srcs : string ,
565
- replacer : ( arg : ImageCandidate ) => Promise < string >
566
- ) : Promise < string > {
567
- const imageCandidates : ImageCandidate [ ] = splitSrcSet ( srcs )
563
+ function reduceSrcset ( ret : { url : string ; descriptor : string } [ ] ) {
564
+ return ret . reduce ( ( prev , { url, descriptor } , index ) => {
565
+ descriptor ??= ''
566
+ return ( prev +=
567
+ url + ` ${ descriptor } ${ index === ret . length - 1 ? '' : ', ' } ` )
568
+ } , '' )
569
+ }
570
+
571
+ function splitSrcSetDescriptor ( srcs : string ) : ImageCandidate [ ] {
572
+ return splitSrcSet ( srcs )
568
573
. map ( ( s ) => {
569
574
const src = s . replace ( escapedSpaceCharacters , ' ' ) . trim ( )
570
575
const [ url ] = imageSetUrlRE . exec ( src ) || [ ]
@@ -575,21 +580,30 @@ export async function processSrcSet(
575
580
}
576
581
} )
577
582
. filter ( ( { url } ) => ! ! url )
583
+ }
578
584
579
- const ret = await Promise . all (
580
- imageCandidates . map ( async ( { url, descriptor } ) => {
581
- return {
582
- url : await replacer ( { url, descriptor } ) ,
583
- descriptor
584
- }
585
- } )
586
- )
585
+ export function processSrcSet (
586
+ srcs : string ,
587
+ replacer : ( arg : ImageCandidate ) => Promise < string >
588
+ ) : Promise < string > {
589
+ return Promise . all (
590
+ splitSrcSetDescriptor ( srcs ) . map ( async ( { url, descriptor } ) => ( {
591
+ url : await replacer ( { url, descriptor } ) ,
592
+ descriptor
593
+ } ) )
594
+ ) . then ( ( ret ) => reduceSrcset ( ret ) )
595
+ }
587
596
588
- return ret . reduce ( ( prev , { url, descriptor } , index ) => {
589
- descriptor ??= ''
590
- return ( prev +=
591
- url + ` ${ descriptor } ${ index === ret . length - 1 ? '' : ', ' } ` )
592
- } , '' )
597
+ export function processSrcSetSync (
598
+ srcs : string ,
599
+ replacer : ( arg : ImageCandidate ) => string
600
+ ) : string {
601
+ return reduceSrcset (
602
+ splitSrcSetDescriptor ( srcs ) . map ( ( { url, descriptor } ) => ( {
603
+ url : replacer ( { url, descriptor } ) ,
604
+ descriptor
605
+ } ) )
606
+ )
593
607
}
594
608
595
609
function splitSrcSet ( srcs : string ) {
0 commit comments