@@ -2,8 +2,9 @@ import CSSStyleDeclarationValueParser from './CSSStyleDeclarationValueParser.js'
2
2
import ICSSStyleDeclarationPropertyValue from './ICSSStyleDeclarationPropertyValue.js' ;
3
3
4
4
const RECT_REGEXP = / ^ r e c t \( ( .* ) \) $ / i;
5
- const SPLIT_COMMA_SEPARATED_REGEXP = / , (? = (?: (?: (? ! \) ) [ \s \S ] ) * \( ) | [ ^ \( \) ] * $ ) / ; // Split on commas that are outside of parentheses
6
- const SPLIT_SPACE_SEPARATED_REGEXP = / \s + (? = (?: (?: (? ! \) ) [ \s \S ] ) * \( ) | [ ^ \( \) ] * $ ) / ; // Split on spaces that are outside of parentheses
5
+ const SPLIT_COMMA_SEPARATED_WITH_PARANTHESES_REGEXP = / , (? = (?: (?: (? ! \) ) [ \s \S ] ) * \( ) | [ ^ \( \) ] * $ ) / ; // Split on commas that are outside of parentheses
6
+ const SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP = / \s + (? = (?: (?: (? ! \) ) [ \s \S ] ) * \( ) | [ ^ \( \) ] * $ ) / ; // Split on spaces that are outside of parentheses
7
+ const WHITE_SPACE_GLOBAL_REGEXP = / \s + / gm;
7
8
const BORDER_STYLE = [
8
9
'none' ,
9
10
'hidden' ,
@@ -497,7 +498,7 @@ export default class CSSStyleDeclarationPropertySetParser {
497
498
...this . getOutlineWidth ( 'initial' , important )
498
499
} ;
499
500
500
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
501
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
501
502
502
503
for ( const part of parts ) {
503
504
const width = this . getOutlineWidth ( part , important ) ;
@@ -649,7 +650,9 @@ export default class CSSStyleDeclarationPropertySetParser {
649
650
...this . getBorderImage ( 'initial' , important )
650
651
} ;
651
652
652
- const parts = value . replace ( / \s * , \s * / g, ',' ) . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
653
+ const parts = value
654
+ . replace ( / \s * , \s * / g, ',' )
655
+ . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
653
656
654
657
for ( const part of parts ) {
655
658
const width = this . getBorderWidth ( part , important ) ;
@@ -695,7 +698,7 @@ export default class CSSStyleDeclarationPropertySetParser {
695
698
} ;
696
699
}
697
700
698
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
701
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
699
702
const top = this . getBorderTopWidth ( parts [ 0 ] , important ) ;
700
703
const right = this . getBorderRightWidth ( parts [ 1 ] || parts [ 0 ] , important ) ;
701
704
const bottom = this . getBorderBottomWidth ( parts [ 2 ] || parts [ 0 ] , important ) ;
@@ -741,7 +744,7 @@ export default class CSSStyleDeclarationPropertySetParser {
741
744
} ;
742
745
}
743
746
744
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
747
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
745
748
const top = this . getBorderTopStyle ( parts [ 0 ] , important ) ;
746
749
const right = this . getBorderRightStyle ( parts [ 1 ] || parts [ 0 ] , important ) ;
747
750
const bottom = this . getBorderBottomStyle ( parts [ 2 ] || parts [ 0 ] , important ) ;
@@ -788,7 +791,7 @@ export default class CSSStyleDeclarationPropertySetParser {
788
791
} ;
789
792
}
790
793
791
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
794
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
792
795
const top = this . getBorderTopColor ( parts [ 0 ] , important ) ;
793
796
const right = this . getBorderRightColor ( parts [ 1 ] || parts [ 0 ] , important ) ;
794
797
const bottom = this . getBorderBottomColor ( parts [ 2 ] || parts [ 0 ] , important ) ;
@@ -843,7 +846,7 @@ export default class CSSStyleDeclarationPropertySetParser {
843
846
parsedValue = parsedValue . replace ( sourceMatch [ 0 ] , '' ) ;
844
847
}
845
848
846
- const parts = parsedValue . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
849
+ const parts = parsedValue . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
847
850
848
851
if ( sourceMatch ) {
849
852
parts . push ( sourceMatch [ 1 ] ) ;
@@ -1038,7 +1041,7 @@ export default class CSSStyleDeclarationPropertySetParser {
1038
1041
} ;
1039
1042
}
1040
1043
1041
- const parts = lowerValue . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
1044
+ const parts = lowerValue . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
1042
1045
1043
1046
if ( parts . length > 4 ) {
1044
1047
return null ;
@@ -1099,7 +1102,7 @@ export default class CSSStyleDeclarationPropertySetParser {
1099
1102
} ;
1100
1103
}
1101
1104
1102
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
1105
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
1103
1106
1104
1107
if ( parts . length > 4 ) {
1105
1108
return null ;
@@ -1154,7 +1157,7 @@ export default class CSSStyleDeclarationPropertySetParser {
1154
1157
} ;
1155
1158
}
1156
1159
1157
- const parts = lowerValue . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
1160
+ const parts = lowerValue . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
1158
1161
1159
1162
if ( parts . length > 2 ) {
1160
1163
return null ;
@@ -1545,7 +1548,7 @@ export default class CSSStyleDeclarationPropertySetParser {
1545
1548
} ;
1546
1549
}
1547
1550
1548
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
1551
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
1549
1552
const topLeft = this . getBorderTopLeftRadius ( parts [ 0 ] , important ) ;
1550
1553
const topRight = this . getBorderTopRightRadius ( parts [ 1 ] || parts [ 0 ] , important ) ;
1551
1554
const bottomRight = this . getBorderBottomRightRadius ( parts [ 2 ] || parts [ 0 ] , important ) ;
@@ -1683,7 +1686,7 @@ export default class CSSStyleDeclarationPropertySetParser {
1683
1686
...this . getBorderTopColor ( 'initial' , important )
1684
1687
} ;
1685
1688
1686
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
1689
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
1687
1690
1688
1691
for ( const part of parts ) {
1689
1692
const width = this . getBorderTopWidth ( part , important ) ;
@@ -1732,7 +1735,7 @@ export default class CSSStyleDeclarationPropertySetParser {
1732
1735
...this . getBorderRightColor ( 'initial' , important )
1733
1736
} ;
1734
1737
1735
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
1738
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
1736
1739
1737
1740
for ( const part of parts ) {
1738
1741
const width = this . getBorderRightWidth ( part , important ) ;
@@ -1781,7 +1784,7 @@ export default class CSSStyleDeclarationPropertySetParser {
1781
1784
...this . getBorderBottomColor ( 'initial' , important )
1782
1785
} ;
1783
1786
1784
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
1787
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
1785
1788
1786
1789
for ( const part of parts ) {
1787
1790
const width = this . getBorderBottomWidth ( part , important ) ;
@@ -1830,7 +1833,7 @@ export default class CSSStyleDeclarationPropertySetParser {
1830
1833
...this . getBorderLeftColor ( 'initial' , important )
1831
1834
} ;
1832
1835
1833
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
1836
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
1834
1837
1835
1838
for ( const part of parts ) {
1836
1839
const width = this . getBorderLeftWidth ( part , important ) ;
@@ -1873,7 +1876,7 @@ export default class CSSStyleDeclarationPropertySetParser {
1873
1876
} ;
1874
1877
}
1875
1878
1876
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
1879
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
1877
1880
const top = this . getPaddingTop ( parts [ 0 ] , important ) ;
1878
1881
const right = this . getPaddingRight ( parts [ 1 ] || parts [ 0 ] , important ) ;
1879
1882
const bottom = this . getPaddingBottom ( parts [ 2 ] || parts [ 0 ] , important ) ;
@@ -2006,7 +2009,7 @@ export default class CSSStyleDeclarationPropertySetParser {
2006
2009
} ;
2007
2010
}
2008
2011
2009
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
2012
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2010
2013
const top = this . getMarginTop ( parts [ 0 ] , important ) ;
2011
2014
const right = this . getMarginRight ( parts [ 1 ] || parts [ 0 ] , important ) ;
2012
2015
const bottom = this . getMarginBottom ( parts [ 2 ] || parts [ 0 ] , important ) ;
@@ -2164,7 +2167,7 @@ export default class CSSStyleDeclarationPropertySetParser {
2164
2167
} ;
2165
2168
}
2166
2169
2167
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
2170
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2168
2171
const flexGrow = this . getFlexGrow ( parts [ 0 ] , important ) ;
2169
2172
const flexShrink = this . getFlexShrink ( parts [ 1 ] || '1' , important ) ;
2170
2173
const flexBasis = this . getFlexBasis ( parts [ 2 ] || '0%' , important ) ;
@@ -2300,7 +2303,7 @@ export default class CSSStyleDeclarationPropertySetParser {
2300
2303
const parts = value
2301
2304
. replace ( / \s , \s / g, ',' )
2302
2305
. replace ( / \s \/ \s / g, '/' )
2303
- . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
2306
+ . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2304
2307
2305
2308
const backgroundPositions = [ ] ;
2306
2309
@@ -2397,7 +2400,7 @@ export default class CSSStyleDeclarationPropertySetParser {
2397
2400
return { 'background-size' : { value : lowerValue , important } } ;
2398
2401
}
2399
2402
2400
- const imageParts = lowerValue . split ( SPLIT_COMMA_SEPARATED_REGEXP ) ;
2403
+ const imageParts = lowerValue . split ( SPLIT_COMMA_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2401
2404
const parsed = [ ] ;
2402
2405
2403
2406
for ( const imagePart of imageParts ) {
@@ -2568,12 +2571,12 @@ export default class CSSStyleDeclarationPropertySetParser {
2568
2571
} ;
2569
2572
}
2570
2573
2571
- const imageParts = value . split ( SPLIT_COMMA_SEPARATED_REGEXP ) ;
2574
+ const imageParts = value . split ( SPLIT_COMMA_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2572
2575
let x = '' ;
2573
2576
let y = '' ;
2574
2577
2575
2578
for ( const imagePart of imageParts ) {
2576
- const parts = imagePart . trim ( ) . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
2579
+ const parts = imagePart . trim ( ) . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2577
2580
2578
2581
if ( x ) {
2579
2582
x += ',' ;
@@ -2681,11 +2684,11 @@ export default class CSSStyleDeclarationPropertySetParser {
2681
2684
return { 'background-position-x' : { value : lowerValue , important } } ;
2682
2685
}
2683
2686
2684
- const imageParts = lowerValue . split ( SPLIT_COMMA_SEPARATED_REGEXP ) ;
2687
+ const imageParts = lowerValue . split ( SPLIT_COMMA_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2685
2688
let parsedValue = '' ;
2686
2689
2687
2690
for ( const imagePart of imageParts ) {
2688
- const parts = imagePart . trim ( ) . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
2691
+ const parts = imagePart . trim ( ) . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2689
2692
2690
2693
if ( parsedValue ) {
2691
2694
parsedValue += ',' ;
@@ -2732,11 +2735,11 @@ export default class CSSStyleDeclarationPropertySetParser {
2732
2735
return { 'background-position-y' : { value : lowerValue , important } } ;
2733
2736
}
2734
2737
2735
- const imageParts = lowerValue . split ( SPLIT_COMMA_SEPARATED_REGEXP ) ;
2738
+ const imageParts = lowerValue . split ( SPLIT_COMMA_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2736
2739
let parsedValue = '' ;
2737
2740
2738
2741
for ( const imagePart of imageParts ) {
2739
- const parts = imagePart . trim ( ) . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
2742
+ const parts = imagePart . trim ( ) . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2740
2743
2741
2744
if ( parsedValue ) {
2742
2745
parsedValue += ',' ;
@@ -2808,7 +2811,7 @@ export default class CSSStyleDeclarationPropertySetParser {
2808
2811
return { 'background-image' : { value : lowerValue , important } } ;
2809
2812
}
2810
2813
2811
- const parts = value . split ( SPLIT_COMMA_SEPARATED_REGEXP ) ;
2814
+ const parts = value . split ( SPLIT_COMMA_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2812
2815
const parsed = [ ] ;
2813
2816
2814
2817
for ( const part of parts ) {
@@ -2917,7 +2920,9 @@ export default class CSSStyleDeclarationPropertySetParser {
2917
2920
...this . getLineHeight ( 'normal' , important )
2918
2921
} ;
2919
2922
2920
- const parts = value . replace ( / \s * \/ \s * / g, '/' ) . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
2923
+ const parts = value
2924
+ . replace ( / \s * \/ \s * / g, '/' )
2925
+ . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2921
2926
2922
2927
for ( let i = 0 , max = parts . length ; i < max ; i ++ ) {
2923
2928
const part = parts [ i ] ;
@@ -2985,7 +2990,7 @@ export default class CSSStyleDeclarationPropertySetParser {
2985
2990
if ( CSSStyleDeclarationValueParser . getGlobal ( lowerValue ) || FONT_STYLE . includes ( lowerValue ) ) {
2986
2991
return { 'font-style' : { value : lowerValue , important } } ;
2987
2992
}
2988
- const parts = value . split ( SPLIT_SPACE_SEPARATED_REGEXP ) ;
2993
+ const parts = value . split ( SPLIT_SPACE_SEPARATED_WITH_PARANTHESES_REGEXP ) ;
2989
2994
if ( parts . length === 2 && parts [ 0 ] === 'oblique' ) {
2990
2995
const degree = CSSStyleDeclarationValueParser . getDegree ( parts [ 1 ] ) ;
2991
2996
return degree ? { 'font-style' : { value : lowerValue , important } } : null ;
@@ -3256,4 +3261,62 @@ export default class CSSStyleDeclarationPropertySetParser {
3256
3261
}
3257
3262
return null ;
3258
3263
}
3264
+
3265
+ /**
3266
+ * Returns aspect ratio.
3267
+ *
3268
+ * @param value Value.
3269
+ * @param important Important.
3270
+ * @returns Property
3271
+ */
3272
+ public static getAspectRatio (
3273
+ value : string ,
3274
+ important : boolean
3275
+ ) : {
3276
+ [ key : string ] : ICSSStyleDeclarationPropertyValue ;
3277
+ } {
3278
+ const variable = CSSStyleDeclarationValueParser . getVariable ( value ) ;
3279
+ if ( variable ) {
3280
+ return { 'aspect-ratio' : { value : variable , important } } ;
3281
+ }
3282
+
3283
+ const lowerValue = value . toLowerCase ( ) ;
3284
+
3285
+ if ( CSSStyleDeclarationValueParser . getGlobal ( lowerValue ) ) {
3286
+ return { 'aspect-ratio' : { value : lowerValue , important } } ;
3287
+ }
3288
+
3289
+ let parsedValue = value ;
3290
+
3291
+ const hasAuto = parsedValue . includes ( 'auto' ) ;
3292
+
3293
+ if ( hasAuto ) {
3294
+ parsedValue = parsedValue . replace ( 'auto' , '' ) ;
3295
+ }
3296
+
3297
+ parsedValue = parsedValue . replace ( WHITE_SPACE_GLOBAL_REGEXP , '' ) ;
3298
+
3299
+ if ( ! parsedValue ) {
3300
+ return { 'aspect-ratio' : { value : 'auto' , important } } ;
3301
+ }
3302
+
3303
+ const aspectRatio = parsedValue . split ( '/' ) ;
3304
+
3305
+ if ( aspectRatio . length > 3 ) {
3306
+ return null ;
3307
+ }
3308
+
3309
+ const width = Number ( aspectRatio [ 0 ] ) ;
3310
+ const height = aspectRatio [ 1 ] ? Number ( aspectRatio [ 1 ] ) : 1 ;
3311
+
3312
+ if ( isNaN ( width ) || isNaN ( height ) ) {
3313
+ return null ;
3314
+ }
3315
+
3316
+ if ( hasAuto ) {
3317
+ return { 'aspect-ratio' : { value : `auto ${ width } / ${ height } ` , important } } ;
3318
+ }
3319
+
3320
+ return { 'aspect-ratio' : { value : `${ width } / ${ height } ` , important } } ;
3321
+ }
3259
3322
}
0 commit comments