Skip to content

Commit

Permalink
Media query nested selector fix (#317)
Browse files Browse the repository at this point in the history
* Start of media query selector fix

* Update tests

* Replace a return with break
  • Loading branch information
emmatown authored and Kye Hohenberger committed Sep 21, 2017
1 parent 39cd424 commit 30d8321
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,15 @@ exports[`css handles objects 1`] = `
`;

exports[`css nested 1`] = `
.glamor-0 .some-class {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.glamor-0 .some-class .some-other-class {
background-color: hotpink;
}
.glamor-0 .some-class {
background-color: pink;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.glamor-0 {
Expand Down
5 changes: 1 addition & 4 deletions packages/babel-plugin-emotion/test/macro/css.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('css', () => {
const tree = renderer.create(<div className={cls1} />).toJSON()
expect(tree).toMatchSnapshot()
})
test.skip('nested', () => {
test('nested', () => {
const cls1 = css`
color: yellow;
& .some-class {
Expand All @@ -166,9 +166,6 @@ describe('css', () => {
background-color: pink;
}
}
&.another-class {
display: flex;
}
`
const tree = renderer
.create(
Expand Down
13 changes: 10 additions & 3 deletions packages/emotion/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@ export function flush() {
sheet.inject()
}

let rule = ''

function insertionPlugin(context, content, selector, parent) {
switch (context) {
case -2:
if (rule !== '') sheet.insert(rule)
rule = ''
break
case 2: {
if (parent[0] === selector[0]) {
break
}
if (rule !== '') sheet.insert(rule)
rule = `${selector.join(',')}{${content}}`
break
}
// after an at rule block
case 3: // eslint-disable-line no-fallthrough
sheet.insert(`${selector.join(',')}{${content}}`)
rule = ''
}
}

Expand Down
60 changes: 55 additions & 5 deletions packages/emotion/test/__snapshots__/css.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ exports[`css css variables 1`] = `
/>
`;

exports[`css explicit & 1`] = `
<div
className="css-16ooeb3 another-class"
/>
`;

exports[`css explicit & 2`] = `
".css-16ooeb3.another-class {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}"
`;

exports[`css falsy property value in object 1`] = `
.glamor-0 {
display: -webkit-box;
Expand Down Expand Up @@ -223,22 +238,57 @@ exports[`css handles objects 1`] = `
/>
`;

exports[`css nested 1`] = `
exports[`css media query specificity 1`] = `
"@media (min-width:420px) {
.css-mfslnr {
width: 96px;
height: 96px;
}
}
.css-mfslnr {
width: 32px;
height: 32px;
border-radius: 50%;
}"
`;

exports[`css media query specificity 2`] = `
.glamor-0 {
color: yellow;
width: 32px;
height: 32px;
border-radius: 50%;
}
@media (min-width:420px) {
.glamor-0 {
width: 96px;
height: 96px;
}
}
<div
className="glamor-0"
/>
`;

exports[`css nested 1`] = `
.glamor-0 .some-class .some-other-class {
background-color: hotpink;
}
.glamor-0 .some-class {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.glamor-0 .some-class .some-other-class {
background-color: hotpink;
.glamor-0 {
color: yellow;
}
@media (max-width: 600px) {
@media (max-width:600px) {
.glamor-0 .some-class {
background-color: pink;
}
Expand Down
39 changes: 34 additions & 5 deletions packages/emotion/test/css.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import renderer from 'react-test-renderer'
import { css, flush } from 'emotion'
import { css, flush, sheet } from 'emotion'

describe('css', () => {
test('float property', () => {
Expand Down Expand Up @@ -166,7 +166,7 @@ describe('css', () => {
const tree = renderer.create(<div className={cls1} />).toJSON()
expect(tree).toMatchSnapshot()
})
test.skip('nested', () => {
test('nested', () => {
const cls1 = css`
color: yellow;
& .some-class {
Expand All @@ -178,9 +178,6 @@ describe('css', () => {
background-color: pink;
}
}
&.another-class {
display: flex;
}
`
const tree = renderer
.create(
Expand All @@ -193,6 +190,20 @@ describe('css', () => {
.toJSON()
expect(tree).toMatchSnapshot()
})
test('explicit &', () => {
flush()
const cls1 = css`
&.another-class {
display: flex;
}
`
const tree = renderer
.create(<div className={`${cls1} another-class`} />)
.toJSON()
expect(tree).toMatchSnapshot()
expect(sheet).toMatchSnapshot()
flush()
})
test('falsy property value in object', () => {
const cls = css({ display: 'flex', backgroundColor: undefined })
const tree = renderer.create(<div className={cls} />).toJSON()
Expand Down Expand Up @@ -252,4 +263,22 @@ describe('css', () => {
const tree2 = renderer.create(<div className={cls1} />).toJSON()
expect(tree2).toMatchSnapshot()
})
test.skip('media query specificity', () => {
flush()
const cls = css`
width: 32px;
height: 32px;
border-radius: 50%;
@media (min-width: 420px) {
width: 96px;
height: 96px;
}
`

const tree = renderer.create(<div className={cls} />).toJSON()
expect(sheet).toMatchSnapshot()
expect(tree).toMatchSnapshot()
flush()
})
})

0 comments on commit 30d8321

Please sign in to comment.