Skip to content

Commit a55f3d4

Browse files
Andaristemmatown
authored andcommittedOct 29, 2019
Don't cause invalid rule to be serialized when using object style with falsy value (#1581)
1 parent 6cdb569 commit a55f3d4

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed
 

‎.changeset/big-oranges-carry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@emotion/serialize': patch
3+
---
4+
5+
Don't cause invalid rule to be serialized when using object style with falsy value

‎packages/core/__tests__/css.js

+25
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@ import 'test-utils/next-env'
44
import * as React from 'react'
55
import { jsx, css, CacheProvider } from '@emotion/core'
66
import { ThemeProvider } from 'emotion-theming'
7+
import { render } from '@testing-library/react'
78
import renderer from 'react-test-renderer'
89
import 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+
1020
const 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+
})

‎packages/serialize/src/index.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ let hyphenateRegex = /[A-Z]|^ms/g
1717
let animationRegex = /_EMO_([^_]+?)_([^]*?)_EMO_/g
1818

1919
const isCustomProperty = (property: string) => property.charCodeAt(1) === 45
20+
const isProcessableValue = value => value != null && typeof value !== 'boolean'
2021

2122
const 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(

0 commit comments

Comments
 (0)
Please sign in to comment.