Skip to content

Commit

Permalink
Don't cause invalid rule to be serialized when using object style wit…
Browse files Browse the repository at this point in the history
…h falsy value (#1581)
  • Loading branch information
Andarist authored and emmatown committed Oct 29, 2019
1 parent 6cdb569 commit a55f3d4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-oranges-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emotion/serialize': patch
---

Don't cause invalid rule to be serialized when using object style with falsy value
25 changes: 25 additions & 0 deletions packages/core/__tests__/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ import 'test-utils/next-env'
import * as React from 'react'
import { jsx, css, CacheProvider } from '@emotion/core'
import { ThemeProvider } from 'emotion-theming'
import { render } from '@testing-library/react'
import renderer from 'react-test-renderer'
import createCache from '@emotion/cache'

// $FlowFixMe
console.error = jest.fn()
// $FlowFixMe
console.warn = jest.fn()

afterEach(() => {
jest.clearAllMocks()
})

const SomeComponent = (props: { lol: true }) => (props.lol ? 'yes' : 'no')

// test to make sure flow prop errors work.
Expand Down Expand Up @@ -256,3 +266,18 @@ test('handles composition of styles without a final semi in a declaration block'

expect(tree.toJSON()).toMatchSnapshot()
})

it("doesn't try to insert invalid rules caused by object style's value being falsy", () => {
render(
<CacheProvider value={createCache({ speedy: true })}>
<h1
css={css({ color: 'hotpink', '@media (min-width 800px)': undefined })}
>
{'Emotion'}
</h1>
</CacheProvider>
)

expect((console.error: any).mock.calls).toMatchInlineSnapshot(`Array []`)
expect((console.warn: any).mock.calls).toMatchInlineSnapshot(`Array []`)
})
17 changes: 8 additions & 9 deletions packages/serialize/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let hyphenateRegex = /[A-Z]|^ms/g
let animationRegex = /_EMO_([^_]+?)_([^]*?)_EMO_/g

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

const processStyleName = memoize(
(styleName: string) =>
Expand All @@ -29,10 +30,6 @@ let processStyleValue = (
key: string,
value: string | number
): string | number => {
if (value == null || typeof value === 'boolean') {
return ''
}

switch (key) {
case 'animation':
case 'animationName': {
Expand Down Expand Up @@ -272,7 +269,7 @@ function createStringFromObject(
if (typeof value !== 'object') {
if (registered != null && registered[value] !== undefined) {
string += `${key}{${registered[value]}}`
} else {
} else if (isProcessableValue(value)) {
string += `${processStyleName(key)}:${processStyleValue(key, value)};`
}
} else {
Expand All @@ -290,10 +287,12 @@ function createStringFromObject(
(registered == null || registered[value[0]] === undefined)
) {
for (let i = 0; i < value.length; i++) {
string += `${processStyleName(key)}:${processStyleValue(
key,
value[i]
)};`
if (isProcessableValue(value[i])) {
string += `${processStyleName(key)}:${processStyleValue(
key,
value[i]
)};`
}
}
} else {
const interpolated = handleInterpolation(
Expand Down

0 comments on commit a55f3d4

Please sign in to comment.