@@ -248,26 +248,43 @@ export function hypenatePropsObject(object: {[key: string]: any}): {[key: string
248
248
* for that.
249
249
*/
250
250
export function computeStyle ( element : HTMLElement , prop : string ) : string {
251
- const gcs = window . getComputedStyle ( element ) ;
251
+ const styles = window . getComputedStyle ( element ) ;
252
252
253
253
// this is casted to any because the `CSSStyleDeclaration` type is a fixed
254
254
// set of properties and `prop` is a dynamic reference to a property within
255
255
// the `CSSStyleDeclaration` list.
256
- let value = gcs [ prop as any ] ;
256
+ let value = getComputedValue ( styles , prop as keyof CSSStyleDeclaration ) ;
257
257
258
258
// Firefox returns empty string values for `margin` and `padding` properties
259
259
// when extracted using getComputedStyle (see similar issue here:
260
260
// https://github.com/jquery/jquery/issues/3383). In this situation
261
261
// we want to emulate the value that is returned by creating the top,
262
262
// right, bottom and left properties as individual style lookups.
263
263
if ( value . length === 0 && ( prop === 'margin' || prop === 'padding' ) ) {
264
+ const t = getComputedValue ( styles , ( prop + 'Top' ) as 'marginTop' | 'paddingTop' ) ;
265
+ const r = getComputedValue ( styles , ( prop + 'Right' ) as 'marginRight' | 'paddingRight' ) ;
266
+ const b = getComputedValue ( styles , ( prop + 'Bottom' ) as 'marginBottom' | 'paddingBottom' ) ;
267
+ const l = getComputedValue ( styles , ( prop + 'Left' ) as 'marginLeft' | 'paddingLeft' ) ;
268
+
264
269
// reconstruct the padding/margin value as `top right bottom left`
265
- const propTop = ( prop + 'Top' ) as 'marginTop' | 'paddingTop' ;
266
- const propRight = ( prop + 'Right' ) as 'marginRight' | 'paddingRight' ;
267
- const propBottom = ( prop + 'Bottom' ) as 'marginBottom' | 'paddingBottom' ;
268
- const propLeft = ( prop + 'Left' ) as 'marginLeft' | 'paddingLeft' ;
269
- value = `${ gcs [ propTop ] } ${ gcs [ propRight ] } ${ gcs [ propBottom ] } ${ gcs [ propLeft ] } ` ;
270
+ // we `trim()` the value because if all of the values above are
271
+ // empty string values then we would like the return value to
272
+ // also be an empty string.
273
+ value = `${ t } ${ r } ${ b } ${ l } ` . trim ( ) ;
270
274
}
271
275
272
276
return value ;
273
277
}
278
+
279
+ /**
280
+ * Reads and returns the provided property style from the provided styles collection.
281
+ *
282
+ * This function is useful because it will return an empty string in the
283
+ * event that the value obtained from the styles collection is a non-string
284
+ * value (which is usually the case if the `styles` object is mocked out).
285
+ */
286
+ function getComputedValue < K extends keyof CSSStyleDeclaration > (
287
+ styles : CSSStyleDeclaration , prop : K ) : string {
288
+ const value = styles [ prop ] ;
289
+ return typeof value === 'string' ? value : '' ;
290
+ }
0 commit comments