Skip to content

Add support for media queries #22

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

Merged
merged 3 commits into from
Jan 8, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Added: support for `@media` queries

# 5.1.0 - 2016-01-07

- Added: "warnWhenCannotResolve" option to warn when calc() are not reduced to a single value
Expand Down
34 changes: 22 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,41 @@ module.exports = postcss.plugin("postcss-calc", function(options) {
var warnWhenCannotResolve = options.warnWhenCannotResolve

return function(style, result) {
function transformValue(node, property) {
var value = node[property]

style.walkDecls(function transformDecl(decl) {
if (!decl.value || decl.value.indexOf("calc(") === -1) {
if (!value || !CONTAINS_CALC.test(value)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should stick to a simple indexOf for everything instead of a regexp? (for perf)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's really not going to make any noticeable impact to perf, but I'll do it if you want me to.

return
}

helpers.try(function transformCSSCalc() {
var value = reduceCSSCalc(decl.value, precision)
var reducedValue = reduceCSSCalc(value, precision)

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

if (!preserve) {
decl.value = value
node[property] = reducedValue
return
}

if (value != decl.value) {
var clone = decl.clone()
clone.value = value
decl.parent.insertBefore(decl, clone)
if (reducedValue != value) {
var clone = node.clone()
clone[property] = reducedValue
node.parent.insertBefore(node, clone)
}
}, decl.source)
}, node.source)
}

style.walk(function(rule) {
if (rule.type === "atrule") {
return transformValue(rule, "params")
}
else if (rule.type === "decl") {
return transformValue(rule, "value")
}
})
}
})
1 change: 1 addition & 0 deletions test/fixtures/media.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@media (min-width: calc(10px + 10px)) {}
1 change: 1 addition & 0 deletions test/fixtures/media.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@media (min-width: 20px) {}
5 changes: 5 additions & 0 deletions test/fixtures/preserve-media.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@media (min-width: calc(10px + 10px)) {
.test {
display: block;
}
}
10 changes: 10 additions & 0 deletions test/fixtures/preserve-media.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@media (min-width: 20px) {
.test {
display: block;
}
}
@media (min-width: calc(10px + 10px)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you have a real world example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.test {
display: block;
}
}
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ test("calc", function(t) {
"should resolve what is possible in complex calc"
)

compareFixtures(
t,
"media",
{},
"should resolve media queries"
)

compareFixtures(
t,
"precision",
Expand All @@ -57,6 +64,14 @@ test("calc", function(t) {
"should have a preserve option that allow to keep original calc() usage"
)

compareFixtures(
t,
"preserve-media",
{preserve: true},
"should have a preserve option that allow to keep original calc() usage" +
"with media"
)

var result = compareFixtures(
t,
"warnWhenCannotResolve",
Expand Down