File tree 3 files changed +38
-9
lines changed
3 files changed +38
-9
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @emotion/serialize ' : patch
3
+ ---
4
+
5
+ Don't cause invalid rule to be serialized when using object style with falsy value
Original file line number Diff line number Diff line change @@ -4,9 +4,19 @@ import 'test-utils/next-env'
4
4
import * as React from 'react'
5
5
import { jsx , css , CacheProvider } from '@emotion/core'
6
6
import { ThemeProvider } from 'emotion-theming'
7
+ import { render } from '@testing-library/react'
7
8
import renderer from 'react-test-renderer'
8
9
import createCache from '@emotion/cache'
9
10
11
+ // $FlowFixMe
12
+ console . error = jest . fn ( )
13
+ // $FlowFixMe
14
+ console . warn = jest . fn ( )
15
+
16
+ afterEach ( ( ) => {
17
+ jest . clearAllMocks ( )
18
+ } )
19
+
10
20
const SomeComponent = ( props : { lol : true } ) => ( props . lol ? 'yes' : 'no' )
11
21
12
22
// test to make sure flow prop errors work.
@@ -256,3 +266,18 @@ test('handles composition of styles without a final semi in a declaration block'
256
266
257
267
expect ( tree . toJSON ( ) ) . toMatchSnapshot ( )
258
268
} )
269
+
270
+ it ( "doesn't try to insert invalid rules caused by object style's value being falsy" , ( ) => {
271
+ render (
272
+ < CacheProvider value = { createCache ( { speedy : true } ) } >
273
+ < h1
274
+ css = { css ( { color : 'hotpink' , '@media (min-width 800px)' : undefined } ) }
275
+ >
276
+ { 'Emotion' }
277
+ </ h1 >
278
+ </ CacheProvider >
279
+ )
280
+
281
+ expect ( ( console . error : any ) . mock . calls ) . toMatchInlineSnapshot ( `Array []` )
282
+ expect ( ( console . warn : any ) . mock . calls ) . toMatchInlineSnapshot ( `Array []` )
283
+ } )
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ let hyphenateRegex = /[A-Z]|^ms/g
17
17
let animationRegex = / _ E M O _ ( [ ^ _ ] + ?) _ ( [ ^ ] * ?) _ E M O _ / g
18
18
19
19
const isCustomProperty = ( property : string ) => property . charCodeAt ( 1 ) === 45
20
+ const isProcessableValue = value => value != null && typeof value !== 'boolean'
20
21
21
22
const processStyleName = memoize (
22
23
( styleName : string ) =>
@@ -29,10 +30,6 @@ let processStyleValue = (
29
30
key : string ,
30
31
value : string | number
31
32
) : string | number => {
32
- if ( value == null || typeof value === 'boolean' ) {
33
- return ''
34
- }
35
-
36
33
switch ( key ) {
37
34
case 'animation' :
38
35
case 'animationName' : {
@@ -272,7 +269,7 @@ function createStringFromObject(
272
269
if ( typeof value !== 'object' ) {
273
270
if ( registered != null && registered [ value ] !== undefined ) {
274
271
string += `${ key } {${ registered [ value ] } }`
275
- } else {
272
+ } else if ( isProcessableValue ( value ) ) {
276
273
string += `${ processStyleName ( key ) } :${ processStyleValue ( key , value ) } ;`
277
274
}
278
275
} else {
@@ -290,10 +287,12 @@ function createStringFromObject(
290
287
( registered == null || registered [ value [ 0 ] ] === undefined )
291
288
) {
292
289
for ( let i = 0 ; i < value . length ; i ++ ) {
293
- string += `${ processStyleName ( key ) } :${ processStyleValue (
294
- key ,
295
- value [ i ]
296
- ) } ;`
290
+ if ( isProcessableValue ( value [ i ] ) ) {
291
+ string += `${ processStyleName ( key ) } :${ processStyleValue (
292
+ key ,
293
+ value [ i ]
294
+ ) } ;`
295
+ }
297
296
}
298
297
} else {
299
298
const interpolated = handleInterpolation (
You can’t perform that action at this time.
0 commit comments