File tree Expand file tree Collapse file tree 3 files changed +38
-9
lines changed Expand file tree Collapse file tree 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'
44import * as React from 'react'
55import { jsx , css , CacheProvider } from '@emotion/core'
66import { ThemeProvider } from 'emotion-theming'
7+ import { render } from '@testing-library/react'
78import renderer from 'react-test-renderer'
89import createCache from '@emotion/cache'
910
11+ // $FlowFixMe
12+ console . error = jest . fn ( )
13+ // $FlowFixMe
14+ console . warn = jest . fn ( )
15+
16+ afterEach ( ( ) => {
17+ jest . clearAllMocks ( )
18+ } )
19+
1020const SomeComponent = ( props : { lol : true } ) => ( props . lol ? 'yes' : 'no' )
1121
1222// 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'
256266
257267 expect ( tree . toJSON ( ) ) . toMatchSnapshot ( )
258268} )
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
1717let animationRegex = / _ E M O _ ( [ ^ _ ] + ?) _ ( [ ^ ] * ?) _ E M O _ / g
1818
1919const isCustomProperty = ( property : string ) => property . charCodeAt ( 1 ) === 45
20+ const isProcessableValue = value => value != null && typeof value !== 'boolean'
2021
2122const processStyleName = memoize (
2223 ( styleName : string ) =>
@@ -29,10 +30,6 @@ let processStyleValue = (
2930 key : string ,
3031 value : string | number
3132) : string | number => {
32- if ( value == null || typeof value === 'boolean' ) {
33- return ''
34- }
35-
3633 switch ( key ) {
3734 case 'animation' :
3835 case 'animationName' : {
@@ -272,7 +269,7 @@ function createStringFromObject(
272269 if ( typeof value !== 'object' ) {
273270 if ( registered != null && registered [ value ] !== undefined ) {
274271 string += `${ key } {${ registered [ value ] } }`
275- } else {
272+ } else if ( isProcessableValue ( value ) ) {
276273 string += `${ processStyleName ( key ) } :${ processStyleValue ( key , value ) } ;`
277274 }
278275 } else {
@@ -290,10 +287,12 @@ function createStringFromObject(
290287 ( registered == null || registered [ value [ 0 ] ] === undefined )
291288 ) {
292289 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+ }
297296 }
298297 } else {
299298 const interpolated = handleInterpolation (
You can’t perform that action at this time.
0 commit comments