@@ -67,25 +67,42 @@ const descriptor = {
6767 const backgrounds = [ ] ;
6868 for ( const bgValue of bgValues ) {
6969 const bg = [ ] ;
70- for ( const [ longhand , value ] of Object . entries ( bgValue ) ) {
70+ let hasPosition = false ;
71+ const originValue = bgValue [ backgroundOrigin . property ] ;
72+ const clipValue = bgValue [ backgroundClip . property ] ;
73+ const isDefaultBox =
74+ originValue === initialValues . get ( backgroundOrigin . property ) &&
75+ clipValue === initialValues . get ( backgroundClip . property ) ;
76+ for ( const [ longhand ] of shorthandFor ) {
77+ const value = bgValue [ longhand ] ;
7178 if ( value ) {
7279 const arr = bgMap . get ( longhand ) ;
7380 arr . push ( value ) ;
74- bgMap . set ( longhand , arr ) ;
75- if ( value !== initialValues . get ( longhand ) ) {
76- if ( longhand === backgroundSize . property ) {
77- bg . push ( `/ ${ value } ` ) ;
78- } else {
79- bg . push ( value ) ;
81+ if ( longhand === backgroundOrigin . property ) {
82+ if ( ! isDefaultBox ) {
83+ bg . push ( originValue ) ;
8084 }
81- } else if ( longhand === backgroundImage . property ) {
82- if ( v === "none" ) {
83- bg . push ( value ) ;
85+ } else if ( longhand === backgroundClip . property ) {
86+ if ( ! isDefaultBox && originValue !== clipValue ) {
87+ bg . push ( clipValue ) ;
8488 }
85- } else if ( longhand === backgroundColor . property ) {
86- if ( v === "transparent" ) {
89+ } else if ( value !== initialValues . get ( longhand ) ) {
90+ if ( longhand === backgroundPosition . property ) {
91+ hasPosition = true ;
92+ bg . push ( value ) ;
93+ } else if ( longhand === backgroundSize . property ) {
94+ if ( hasPosition ) {
95+ bg . push ( `/ ${ value } ` ) ;
96+ } else {
97+ bg . push ( initialValues . get ( backgroundPosition . property ) , `/ ${ value } ` ) ;
98+ }
99+ } else {
87100 bg . push ( value ) ;
88101 }
102+ } else if ( longhand === backgroundImage . property && v === "none" ) {
103+ bg . push ( value ) ;
104+ } else if ( longhand === backgroundColor . property && v === "transparent" ) {
105+ bg . push ( value ) ;
89106 }
90107 }
91108 }
@@ -115,6 +132,9 @@ const descriptor = {
115132 let l = 0 ;
116133 for ( const [ longhand ] of shorthandFor ) {
117134 const val = this . getPropertyValue ( longhand ) ;
135+ if ( ! val || parsers . hasVarFunc ( val ) ) {
136+ return "" ;
137+ }
118138 if ( longhand === backgroundImage . property ) {
119139 if ( val === "none" && v === "none" && this . getPropertyValue ( backgroundColor . property ) === "transparent" ) {
120140 return val ;
@@ -123,20 +143,19 @@ const descriptor = {
123143 const imgValues = parsers . splitValue ( val , {
124144 delimiter : ","
125145 } ) ;
126- l = imgValues . length ;
146+ l = Math . max ( l , imgValues . length ) ;
127147 bgMap . set ( longhand , imgValues ) ;
128148 }
129149 } else if ( longhand === backgroundColor . property ) {
130150 if ( val !== initialValues . get ( longhand ) || v . includes ( val ) ) {
131151 bgMap . set ( longhand , [ val ] ) ;
132152 }
133153 } else if ( val !== initialValues . get ( longhand ) ) {
134- bgMap . set (
135- longhand ,
136- parsers . splitValue ( val , {
137- delimiter : ","
138- } )
139- ) ;
154+ const values = parsers . splitValue ( val , {
155+ delimiter : ","
156+ } ) ;
157+ l = Math . max ( l , values . length ) ;
158+ bgMap . set ( longhand , values ) ;
140159 }
141160 }
142161 if ( l === 0 ) {
@@ -149,53 +168,73 @@ const descriptor = {
149168 }
150169 const bgValues = [ ] ;
151170 for ( let i = 0 ; i < l ; i ++ ) {
152- bgValues [ i ] = [ ] ;
153- }
154- for ( const [ longhand , values ] of bgMap ) {
155- for ( let i = 0 ; i < l ; i ++ ) {
156- switch ( longhand ) {
157- case backgroundColor . property : {
158- if ( i === l - 1 ) {
159- const value = values [ 0 ] ;
160- if ( parsers . hasVarFunc ( value ) ) {
161- return "" ;
162- }
163- if ( value && value !== initialValues . get ( longhand ) ) {
164- const bgValue = bgValues [ i ] ;
165- bgValue . push ( value ) ;
166- }
167- }
168- break ;
171+ const bg = [ ] ;
172+ let hasPosition = false ;
173+ let originValue , clipValue ;
174+ const originValues = bgMap . get ( backgroundOrigin . property ) ;
175+ const clipValues = bgMap . get ( backgroundClip . property ) ;
176+ if ( originValues ) {
177+ if ( originValues [ i ] !== undefined ) {
178+ originValue = originValues [ i ] ;
179+ } else {
180+ originValue = initialValues . get ( backgroundOrigin . property ) ;
181+ }
182+ } else {
183+ originValue = initialValues . get ( backgroundOrigin . property ) ;
184+ }
185+ if ( clipValues ) {
186+ if ( clipValues [ i ] !== undefined ) {
187+ clipValue = clipValues [ i ] ;
188+ } else {
189+ clipValue = initialValues . get ( backgroundClip . property ) ;
190+ }
191+ } else {
192+ clipValue = initialValues . get ( backgroundClip . property ) ;
193+ }
194+ const isDefaultBox =
195+ originValue === initialValues . get ( backgroundOrigin . property ) &&
196+ clipValue === initialValues . get ( backgroundClip . property ) ;
197+ for ( const [ longhand ] of shorthandFor ) {
198+ let value ;
199+ if ( bgMap . has ( longhand ) ) {
200+ const values = bgMap . get ( longhand ) ;
201+ value = values [ i ] !== undefined ? values [ i ] : initialValues . get ( longhand ) ;
202+ } else {
203+ value = initialValues . get ( longhand ) ;
204+ }
205+ if ( parsers . hasVarFunc ( value ) ) {
206+ return "" ;
207+ }
208+ if ( longhand === backgroundOrigin . property ) {
209+ if ( ! isDefaultBox ) {
210+ bg . push ( originValue ) ;
169211 }
170- case backgroundSize . property : {
171- const value = values [ i ] ;
172- if ( parsers . hasVarFunc ( value ) ) {
173- return "" ;
174- }
175- if ( value && value !== initialValues . get ( longhand ) ) {
176- const bgValue = bgValues [ i ] ;
177- bgValue . push ( `/ ${ value } ` ) ;
178- }
179- break ;
212+ } else if ( longhand === backgroundClip . property ) {
213+ if ( ! isDefaultBox && originValue !== clipValue ) {
214+ bg . push ( clipValue ) ;
180215 }
181- default : {
182- const value = values [ i ] ;
183- if ( parsers . hasVarFunc ( value ) ) {
184- return "" ;
185- }
186- if ( value && value !== initialValues . get ( longhand ) ) {
187- const bgValue = bgValues [ i ] ;
188- bgValue . push ( value ) ;
216+ } else if ( longhand === backgroundColor . property ) {
217+ if ( i === l - 1 && ( value !== initialValues . get ( longhand ) || bgMap . has ( longhand ) ) ) {
218+ bg . push ( value ) ;
219+ }
220+ } else if ( value !== initialValues . get ( longhand ) ) {
221+ if ( longhand === backgroundPosition . property ) {
222+ hasPosition = true ;
223+ bg . push ( value ) ;
224+ } else if ( longhand === backgroundSize . property ) {
225+ if ( hasPosition ) {
226+ bg . push ( `/ ${ value } ` ) ;
227+ } else {
228+ bg . push ( initialValues . get ( backgroundPosition . property ) , `/ ${ value } ` ) ;
189229 }
230+ } else {
231+ bg . push ( value ) ;
190232 }
191233 }
192234 }
235+ bgValues . push ( bg . join ( " " ) ) ;
193236 }
194- const backgrounds = [ ] ;
195- for ( const bgValue of bgValues ) {
196- backgrounds . push ( bgValue . join ( " " ) ) ;
197- }
198- return backgrounds . join ( ", " ) ;
237+ return bgValues . join ( ", " ) ;
199238 } ,
200239 enumerable : true ,
201240 configurable : true
0 commit comments