@@ -354,17 +354,34 @@ private void RenderSectionWithBackground(IHtmlRenderer renderer, GlobalContext c
354354 }
355355 else if ( ! isBackgroundSizeAuto )
356356 {
357+ #if NET8_0_OR_GREATER
358+ // Optimize: Use stackalloc for small expected arrays (max 2 parts) on .NET 8+
359+ Span < Range > ranges = stackalloc Range [ 2 ] ;
360+ var count = BackgroundSize . AsSpan ( ) . Split ( ranges , ' ' ) ;
361+
362+ if ( count == 1 )
363+ {
364+ vmlSize = BackgroundSize [ ranges [ 0 ] ] ;
365+ vmlAspect = "atmost" ;
366+ }
367+ else if ( count >= 2 )
368+ {
369+ vmlSize = $ "{ BackgroundSize [ ranges [ 0 ] ] } ,{ BackgroundSize [ ranges [ 1 ] ] } ";
370+ }
371+ #else
372+ // For .NET 6/7, use standard Split
357373 var positions = BackgroundSize . Split ( ' ' ) ;
358374
359375 if ( positions . Length == 1 )
360376 {
361377 vmlSize = positions [ 0 ] ;
362378 vmlAspect = "atmost" ;
363379 }
364- else
380+ else if ( positions . Length >= 2 )
365381 {
366- vmlSize = string . Join ( ',' , positions ) ;
382+ vmlSize = $ " { positions [ 0 ] } , { positions [ 1 ] } " ;
367383 }
384+ #endif
368385 }
369386
370387 if ( isBackgroundSizeAuto )
@@ -491,30 +508,64 @@ protected virtual void RenderWrappedChildren(IHtmlRenderer renderer, GlobalConte
491508
492509 private ( string X , string Y ) ParseBackgroundPosition ( )
493510 {
494- var positions = BackgroundPosition . Split ( ' ' ) ;
511+ #if NET8_0_OR_GREATER
512+ // Optimize: Use stackalloc to avoid heap allocation (max 2 parts) on .NET 8+
513+ Span < Range > ranges = stackalloc Range [ 2 ] ;
514+ var count = BackgroundPosition . AsSpan ( ) . Split ( ranges , ' ' ) ;
495515
496- static bool IsTopOrBottom ( ref string axis )
516+ static bool IsTopOrBottom ( ReadOnlySpan < char > axis )
497517 {
498- if ( axis . Equals ( "top" , StringComparison . OrdinalIgnoreCase ) || axis . Equals ( "bottom" , StringComparison . OrdinalIgnoreCase ) )
499- {
500- return true ;
501- }
518+ return axis . Equals ( "top" , StringComparison . OrdinalIgnoreCase ) ||
519+ axis . Equals ( "bottom" , StringComparison . OrdinalIgnoreCase ) ;
520+ }
521+
522+ switch ( count )
523+ {
524+ case 1 :
525+ var pos0 = BackgroundPosition . AsSpan ( ranges [ 0 ] ) ;
526+ if ( IsTopOrBottom ( pos0 ) )
527+ {
528+ return ( "center" , BackgroundPosition [ ranges [ 0 ] ] ) ;
529+ }
530+
531+ return ( BackgroundPosition [ ranges [ 0 ] ] , "center" ) ;
532+
533+ case >= 2 :
534+ var pos0_2 = BackgroundPosition . AsSpan ( ranges [ 0 ] ) ;
535+ var pos1 = BackgroundPosition . AsSpan ( ranges [ 1 ] ) ;
536+
537+ if ( IsTopOrBottom ( pos0_2 ) || ( pos0_2 . Equals ( "center" , StringComparison . OrdinalIgnoreCase ) && IsTopOrBottom ( pos1 ) ) )
538+ {
539+ return ( BackgroundPosition [ ranges [ 1 ] ] , BackgroundPosition [ ranges [ 0 ] ] ) ;
540+ }
502541
503- return false ;
542+ return ( BackgroundPosition [ ranges [ 0 ] ] , BackgroundPosition [ ranges [ 1 ] ] ) ;
543+
544+ default :
545+ return ( "center" , "top" ) ;
546+ }
547+ #else
548+ // For .NET 6/7, use standard Split
549+ var positions = BackgroundPosition . Split ( ' ' ) ;
550+
551+ static bool IsTopOrBottom ( string axis )
552+ {
553+ return axis . Equals ( "top" , StringComparison . OrdinalIgnoreCase ) ||
554+ axis . Equals ( "bottom" , StringComparison . OrdinalIgnoreCase ) ;
504555 }
505556
506557 switch ( positions . Length )
507558 {
508559 case 1 :
509- if ( IsTopOrBottom ( ref positions [ 0 ] ) )
560+ if ( IsTopOrBottom ( positions [ 0 ] ) )
510561 {
511562 return ( "center" , positions [ 0 ] ) ;
512563 }
513564
514565 return ( positions [ 0 ] , "center" ) ;
515566
516- case 2 :
517- if ( IsTopOrBottom ( ref positions [ 0 ] ) || ( positions [ 0 ] . Equals ( "center" , StringComparison . OrdinalIgnoreCase ) && IsTopOrBottom ( ref positions [ 1 ] ) ) )
567+ case >= 2 :
568+ if ( IsTopOrBottom ( positions [ 0 ] ) || ( positions [ 0 ] . Equals ( "center" , StringComparison . OrdinalIgnoreCase ) && IsTopOrBottom ( positions [ 1 ] ) ) )
518569 {
519570 return ( positions [ 1 ] , positions [ 0 ] ) ;
520571 }
@@ -524,6 +575,7 @@ static bool IsTopOrBottom(ref string axis)
524575 default :
525576 return ( "center" , "top" ) ;
526577 }
578+ #endif
527579 }
528580
529581 private static ( string XPercent , string YPercent ) GetBackgroundPositionAsPercentage ( string backgroundPositionX , string backgroundPositionY )
0 commit comments