Skip to content

Commit 2f63f08

Browse files
authored
Merge pull request MoOx#53 from sylvainpolletvillard/master
fix 52: incorrect calculation when subtracting
2 parents 4b129b1 + d038914 commit 2f63f08

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/__tests__/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,10 @@ test(
352352
'calc(env(safe-area-inset-left))',
353353
'calc(env(safe-area-inset-left))'
354354
)
355+
356+
test(
357+
'should handle subtractions with different units',
358+
testFixture,
359+
'calc(100% - calc(666px + 1em + 2em + 100px))',
360+
'calc(100% - 766px - 3em)'
361+
)

src/lib/reducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function convertMathExpression(node, precision) {
6262
return node
6363
}
6464

65-
function flip(operator) {
65+
export function flip(operator) {
6666
return operator === '+' ? '-' : '+'
6767
}
6868

src/lib/stringifier.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { flip } from "./reducer";
2+
13
const order = {
24
"*": 0,
35
"/": 0,
@@ -28,6 +30,11 @@ function stringify(node, prec) {
2830

2931
if (right.type === 'MathExpression' && order[op] < order[right.operator])
3032
str += "(" + stringify(right, prec) + ")"
33+
else if (right.type === 'MathExpression' && op === "-" && ["+", "-"].includes(right.operator)) {
34+
// fix #52 : a-(b+c) = a-b-c
35+
right.operator = flip(right.operator);
36+
str += stringify(right, prec)
37+
}
3138
else
3239
str += stringify(right, prec)
3340

0 commit comments

Comments
 (0)