11export interface CssAtRuleAST {
2- rules : Rule [ ] ;
32 declarations : StyleDeclaration [ ] ;
3+ rules : Rule [ ] ;
44}
55
66interface Rule {
7- selectors : string [ ] ;
87 declarations : StyleDeclaration [ ] ;
8+ selectors : string [ ] ;
99}
1010
1111interface StyleDeclaration extends Record < string , string > {
1212 property : string ;
1313 value : string ;
1414}
1515
16- export const normalizeStylesObject = (
17- css : Object ,
18- window : Window
19- ) : { props : string [ ] ; expectedStyle : StyleDeclaration } => {
16+ export const normalizeStyles = ( css : Partial < CSSStyleDeclaration > ) :
17+ { expectedStyle : StyleDeclaration ; props : string [ ] ; } => {
2018 const normalizer = document . createElement ( "div" ) ;
2119 document . body . appendChild ( normalizer ) ;
2220
2321 const { props, expectedStyle } = Object . entries ( css ) . reduce (
2422 ( acc , [ property , value ] ) => {
23+
24+ if ( typeof value !== "string" ) {
25+ return acc ;
26+ }
27+
2528 normalizer . style . setProperty ( property , value ) ;
2629
2730 const normalizedValue = window
@@ -30,71 +33,24 @@ export const normalizeStylesObject = (
3033 . trim ( ) ;
3134
3235 return {
33- props : [ ...acc . props , property ] ,
3436 expectedStyle : {
3537 ...acc . expectedStyle ,
3638 [ property ] : normalizedValue ,
3739 } ,
40+ props : [ ...acc . props , property ] ,
3841 } ;
3942 } ,
40- { props : [ ] as string [ ] , expectedStyle : { } as StyleDeclaration }
43+ { expectedStyle : { } as StyleDeclaration , props : [ ] as string [ ] } ,
4144 ) ;
4245
4346 document . body . removeChild ( normalizer ) ;
4447
45- return { props, expectedStyle } ;
46- } ;
47-
48- export const normalizeStylesString = ( expectedRule : CssAtRuleAST , window : Window ) => {
49- const normalizer = document . createElement ( "div" ) ;
50- document . body . appendChild ( normalizer ) ;
51-
52- const rules = expectedRule ?. rules [ 0 ] || { declarations : [ ] } ;
53- const { props, expectedStyle } = rules ?. declarations . reduce (
54- ( acc , { property, value } ) => {
55- normalizer . style . setProperty ( property , value ) ;
56-
57- const normalizedValue = window
58- . getComputedStyle ( normalizer )
59- . getPropertyValue ( property )
60- . trim ( ) ;
61-
62- return {
63- props : [ ...acc . props , property ] ,
64- expectedStyle : {
65- ...acc . expectedStyle ,
66- [ property ] : normalizedValue ,
67- } ,
68- } ;
69- } ,
70- { props : [ ] as string [ ] , expectedStyle : { } as StyleDeclaration }
71- ) ;
72-
73- document . body . removeChild ( normalizer ) ;
74-
75- return { props, expectedStyle } ;
48+ return { expectedStyle, props } ;
7649} ;
7750
78- export const getProps = ( props : string [ ] , received : CSSStyleDeclaration ) => {
51+ export const getReceivedStyle = ( props : string [ ] , received : CSSStyleDeclaration ) : StyleDeclaration => {
7952 return props . reduce ( ( acc , prop ) => {
8053 acc [ prop ] = received ?. getPropertyValue ( prop ) . trim ( ) ;
8154 return acc ;
8255 } , { } as StyleDeclaration ) ;
83-
8456} ;
85-
86- export const isSameStyle = ( expectedStyle : StyleDeclaration , receivedStyle : StyleDeclaration ) : boolean => {
87- return ! ! Object . keys ( expectedStyle ) . length &&
88- Object . entries ( expectedStyle ) . every ( ( [ expectedProp , expectedValue ] ) => {
89- const isCustomProperty = expectedProp . startsWith ( "--" ) ;
90- const spellingVariants = [ expectedProp ] ;
91- expectedProp !== null ;
92-
93- if ( ! isCustomProperty )
94- spellingVariants . push ( expectedProp . toLowerCase ( ) ) ;
95- return spellingVariants . some (
96- ( searchProp ) => receivedStyle [ searchProp ] === expectedValue
97- ) ;
98- } ) ;
99- }
100-
0 commit comments