Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiple nested selectors in media queries #324

Merged
merged 4 commits into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions packages/emotion/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,42 @@ export function flush() {
sheet.inject()
}

let rule = ''
let isRootSelector = false
let queue = []

const insertRule = sheet.insert.bind(sheet)

function insertionPlugin(context, content, selectors, parent) {
function insertionPlugin(
context,
content,
selectors,
parent,
line,
column,
length,
id
) {
switch (context) {
case -2: {
if (rule !== '') {
if (isRootSelector === true) {
queue.push(rule)
} else {
queue.unshift(rule)
}
rule = ''
}

queue.forEach(insertRule)
queue = []
break
}

case 2: {
if (rule !== '') {
if (isRootSelector === true) {
if (id === 0) {
const joinedSelectors = selectors.join(',')
const rule = `${joinedSelectors}{${content}}`
if (parent.join(',') === joinedSelectors || parent[0] === '') {
queue.push(rule)
} else {
queue.unshift(rule)
}
}

const joinedSelectors = selectors.join(',')

isRootSelector = parent.join(',') === joinedSelectors || parent[0] === ''
rule = `${joinedSelectors}{${content}}`
break
}
// after an at rule block
case 3: // eslint-disable-line no-fallthrough
case 3:
queue.push(`${selectors.join(',')}{${content}}`)
rule = ''
}
}

Expand Down
14 changes: 7 additions & 7 deletions packages/emotion/test/__snapshots__/css.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -239,17 +239,17 @@ exports[`css handles objects 1`] = `
`;

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

@media (min-width:420px) {
.css-mfslnr {
width: 96px;
height: 96px;
}
}

.css-mfslnr {
width: 32px;
height: 32px;
border-radius: 50%;
}"
`;

Expand Down
124 changes: 44 additions & 80 deletions packages/emotion/test/__snapshots__/selectivity.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,161 +1,125 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`css complex nested media queries 1`] = `
@media (max-width:600px) {
.glamor-0 h1 {
"@media (max-width:600px) {
.css-acogag h1 {
font-size: 1.4rem;
}
}

@media (max-width:400px),(max-height:420px) {
.glamor-0 h1 {
.css-acogag h1 {
font-size: 1.1rem;
}
}

<div
className="glamor-0"
/>
}"
`;

exports[`css complex nested styles 1`] = `
.glamor-0 {
".css-s7aswl {
color: blue;
}

.glamor-0:hover {
.css-s7aswl:hover {
color: green;
}

.glamor-0:hover .name {
.css-s7aswl:hover .name {
color: amethyst;
}

.glamor-0:hover .name:focus {
.css-s7aswl:hover .name:focus {
color: burlywood;
}

@media (min-width:420px) {
.glamor-0:hover .name:focus {
.css-s7aswl:hover .name:focus {
color: rebeccapurple;
}
}

<div
className="glamor-0"
/>
}"
`;

exports[`css handles media query merges 1`] = `
.glamor-0 {
".css-yno01n {
color: darkslateblue;
color: red;
color: purple;
}

@media (min-width:420px) {
.glamor-0 {
.css-yno01n {
color: amethyst;
}
}

@media (min-width:640px) {
.glamor-0 {
.css-yno01n {
color: rebeccapurple;
}
}

@media (min-width:960px) {
.glamor-0 {
.css-yno01n {
color: burlywood;
}
}

@media (min-width:640px) {
.glamor-0 {
.css-yno01n {
color: blue;
}
}

@media (min-width:640px) {
.glamor-0 {
.css-yno01n {
color: aquamarine;
}
}

<div
className="glamor-0"
/>
}"
`;

exports[`css selectivity sheet 1`] = `
".css-s7aswl {
exports[`css media queries with multiple nested selectors 1`] = `
".css-17riori {
color: blue;
}

.css-s7aswl:hover {
color: green;
}

.css-s7aswl:hover .name {
color: amethyst;
}

.css-s7aswl:hover .name:focus {
color: burlywood;
}

@media (min-width:420px) {
.css-s7aswl:hover .name:focus {
color: rebeccapurple;
@media (max-width:400px) {
.css-17riori {
color: green;
}
}

@media (max-width:600px) {
.css-acogag h1 {
font-size: 1.4rem;
.css-17riori h1 {
color: red;
}
}

@media (max-width:400px),(max-height:420px) {
.css-acogag h1 {
font-size: 1.1rem;
.css-17riori span {
color: red;
}
}

.css-yno01n {
color: darkslateblue;
color: red;
color: purple;
}
}"
`;

@media (min-width:420px) {
.css-yno01n {
color: amethyst;
}
exports[`css media query with nested selector with nested selector on root 1`] = `
".css-1ssiu3j span {
color: blue;
}

@media (min-width:640px) {
.css-yno01n {
color: rebeccapurple;
@media (max-width:400px) {
.css-1ssiu3j {
color: green;
}
}

@media (min-width:960px) {
.css-yno01n {
color: burlywood;
.css-1ssiu3j span {
color: red;
}
}
}"
`;

@media (min-width:640px) {
.css-yno01n {
color: blue;
exports[`css media query with nested selector without declarations on root 1`] = `
"@media (max-width:400px) {
.css-18muius {
color: green;
}
}

@media (min-width:640px) {
.css-yno01n {
color: aquamarine;
.css-18muius span {
color: red;
}
}"
`;
2 changes: 1 addition & 1 deletion packages/emotion/test/css.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ describe('css', () => {
const tree2 = renderer.create(<div className={cls1} />).toJSON()
expect(tree2).toMatchSnapshot()
})
test.skip('media query specificity', () => {
test('media query specificity', () => {
flush()
const cls = css`
width: 32px;
Expand Down
Loading