Skip to content

Commit

Permalink
Add support for media queries. Resolves #21
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebuilds committed Jan 7, 2016
1 parent 4f03255 commit 3c477ab
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 12 deletions.
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
33 changes: 21 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,40 @@ 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)) {
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.walkAtRules(function transformAtRule(atRule) {
transformValue(atRule, 'params')
})

style.walkDecls(function transformDecl(decl) {
transformValue(decl, 'value')
})

This comment has been minimized.

Copy link
@ben-eb

ben-eb Jan 7, 2016

Member

These can be consolidated:

style.walk(function (rule) {
  if (rule.type === 'atrule') {
    return transformValue(rule, 'params')
  } else if (rule.type === 'decl') {
    return transformValue(rule, 'value')
  }
});

This comment has been minimized.

Copy link
@jamiebuilds

jamiebuilds Jan 7, 2016

Author Contributor

done

}
})
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)) {
.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

0 comments on commit 3c477ab

Please sign in to comment.