Skip to content

Commit 39c081f

Browse files
committed
Merge pull request #22 from thejameskyle/tjk/media
Add support for media queries
2 parents 4f03255 + 259b1b3 commit 39c081f

10 files changed

+76
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
- Added: "mediaQueries" option for `@media` support
2+
13
# 5.1.0 - 2016-01-07
24

35
- Added: "warnWhenCannotResolve" option to warn when calc() are not reduced to a single value

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ var out = postcss()
118118
.css
119119
```
120120

121+
#### `mediaQueries` (default: `false`)
122+
123+
Allows calc() usage as part of media query declarations.
124+
125+
```js
126+
var out = postcss()
127+
.use(calc({mediaQueries: true}))
128+
.process(css)
129+
.css
130+
```
131+
121132
---
122133

123134
## Contributing

index.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,44 @@ module.exports = postcss.plugin("postcss-calc", function(options) {
1515
var precision = options.precision
1616
var preserve = options.preserve
1717
var warnWhenCannotResolve = options.warnWhenCannotResolve
18+
var mediaQueries = options.mediaQueries
1819

1920
return function(style, result) {
21+
function transformValue(node, property) {
22+
var value = node[property]
2023

21-
style.walkDecls(function transformDecl(decl) {
22-
if (!decl.value || decl.value.indexOf("calc(") === -1) {
24+
if (!value || !CONTAINS_CALC.test(value)) {
2325
return
2426
}
2527

2628
helpers.try(function transformCSSCalc() {
27-
var value = reduceCSSCalc(decl.value, precision)
29+
var reducedValue = reduceCSSCalc(value, precision)
2830

29-
if (warnWhenCannotResolve && CONTAINS_CALC.test(value)) {
30-
result.warn("Could not reduce expression: " + decl.value,
31-
{plugin: "postcss-calc", node: decl})
31+
if (warnWhenCannotResolve && CONTAINS_CALC.test(reducedValue)) {
32+
result.warn("Could not reduce expression: " + value,
33+
{plugin: "postcss-calc", node: node})
3234
}
3335

3436
if (!preserve) {
35-
decl.value = value
37+
node[property] = reducedValue
3638
return
3739
}
3840

39-
if (value != decl.value) {
40-
var clone = decl.clone()
41-
clone.value = value
42-
decl.parent.insertBefore(decl, clone)
41+
if (reducedValue != value) {
42+
var clone = node.clone()
43+
clone[property] = reducedValue
44+
node.parent.insertBefore(node, clone)
4345
}
44-
}, decl.source)
46+
}, node.source)
47+
}
48+
49+
style.walk(function(rule) {
50+
if (mediaQueries && rule.type === "atrule") {
51+
return transformValue(rule, "params")
52+
}
53+
else if (rule.type === "decl") {
54+
return transformValue(rule, "value")
55+
}
4556
})
4657
}
4758
})

test/fixtures/calc.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ stuff {
1818
prefixed-unresolved: -webkit-calc(var(--gutter-y) - 1px);
1919
z-index: calc(var(--zIndexThing) + 1);
2020
}
21+
22+
@media (min-width: calc(10px + 10px)) {
23+
.mediaQueriesShouldBeDisabledByDefault {}
24+
}

test/fixtures/calc.expected.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ stuff {
66
prefixed-unresolved: -webkit-calc(1.125rem - 1px);
77
z-index: 111;
88
}
9+
10+
@media (min-width: calc(10px + 10px)) {
11+
.mediaQueriesShouldBeDisabledByDefault {}
12+
}

test/fixtures/media.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@media (min-width: calc(10px + 10px)) {}

test/fixtures/media.expected.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@media (min-width: 20px) {}

test/fixtures/preserve-media.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@media (min-width: calc(10px + 10px)) {
2+
.test {
3+
display: block;
4+
}
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@media (min-width: 20px) {
2+
.test {
3+
display: block;
4+
}
5+
}
6+
@media (min-width: calc(10px + 10px)) {
7+
.test {
8+
display: block;
9+
}
10+
}

test/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ test("calc", function(t) {
4242
"should resolve what is possible in complex calc"
4343
)
4444

45+
compareFixtures(
46+
t,
47+
"media",
48+
{mediaQueries: true},
49+
"should resolve media queries"
50+
)
51+
4552
compareFixtures(
4653
t,
4754
"precision",
@@ -57,6 +64,14 @@ test("calc", function(t) {
5764
"should have a preserve option that allow to keep original calc() usage"
5865
)
5966

67+
compareFixtures(
68+
t,
69+
"preserve-media",
70+
{preserve: true, mediaQueries: true},
71+
"should have a preserve option that allow to keep original calc() usage" +
72+
"with media"
73+
)
74+
6075
var result = compareFixtures(
6176
t,
6277
"warnWhenCannotResolve",

0 commit comments

Comments
 (0)