Skip to content

Commit e0d7259

Browse files
ben-ebMoOx
authored andcommitted
Remove the calc identifier from unresolved nested expressions. (#19)
Fixes postcss/postcss-calc#32
1 parent f8b50e3 commit e0d7259

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ function reduceCSSCalc(value, decimalPrecision) {
106106
* @returns {String}
107107
*/
108108
function evaluateNestedExpression(expression, call) {
109+
// Remove the calc part from nested expressions to ensure
110+
// better browser compatibility
111+
expression = expression.replace(/((?:\-[a-z]+\-)?calc)/g, "")
109112
var evaluatedPart = ""
110113
var nonEvaluatedPart = expression
111114
var matches

test/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ test("ignore unrecognized values", function(t) {
103103
t.equal(reduceCSSCalc("calc((4px + 8px) + (--foo) + (10% * 20%))"), "calc(12px + (--foo) + 2%)", "ignore unrecognized part between parenthesis")
104104
t.equal(reduceCSSCalc("calc((4px + 8px) + var(--foo) + (10% * 20%))"), "calc(12px + var(--foo) + 2%)", "ignore unrecognized function")
105105

106-
t.equal(reduceCSSCalc("calc(calc(4px + 8px) + calc(var(--foo) + 10px) + calc(10% * 20%))"), "calc(12px + calc(var(--foo) + 10px) + 2%)", "ignore unrecognized nested call")
106+
t.equal(reduceCSSCalc("calc(calc(4px + 8px) + calc(var(--foo) + 10px) + calc(10% * 20%))"), "calc(12px + (var(--foo) + 10px) + 2%)", "ignore unrecognized nested call")
107107

108108
t.equal(reduceCSSCalc("calc(100% - var(--my-var))"), "calc(100% - var(--my-var))", "should not try to reduce 100% - var");
109109
t.end()
@@ -127,3 +127,10 @@ test("should handle calc() with values without leading 0", function(t) {
127127
t.equal(reduceCSSCalc("calc(.1 + .1 + 1.1 + 0.2)"), "1.5", "addition")
128128
t.end()
129129
})
130+
131+
test("should remove calc from unresolved nested expressions", function(t) {
132+
t.equal(reduceCSSCalc("-webkit-calc(21vh + -webkit-calc(21vh + 80px * 1/3 + 5px) + -webkit-calc(10vw + 80px * 2/3))"), "-webkit-calc(21vh + (21vh + 80px * 1/3 + 5px) + (10vw + 80px * 2/3))")
133+
t.equal(reduceCSSCalc("-moz-calc(21vh + -moz-calc(21vh + 80px * 1/3 + 5px) + -moz-calc(10vw + 80px * 2/3))"), "-moz-calc(21vh + (21vh + 80px * 1/3 + 5px) + (10vw + 80px * 2/3))")
134+
t.equal(reduceCSSCalc("calc(21vh + calc(21vh + 80px * 1/3 + 5px) + calc(10vw + 80px * 2/3))"), "calc(21vh + (21vh + 80px * 1/3 + 5px) + (10vw + 80px * 2/3))")
135+
t.end()
136+
})

0 commit comments

Comments
 (0)