Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,10 @@ test(
'calc(env(safe-area-inset-left))',
'calc(env(safe-area-inset-left))'
)

test(
'should handle subtractions with different units',
testFixture,
'calc(100% - calc(666px + 1em + 2em + 100px))',
'calc(100% - 766px - 3em)'
)
2 changes: 1 addition & 1 deletion src/lib/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function convertMathExpression(node, precision) {
return node
}

function flip(operator) {
export function flip(operator) {
return operator === '+' ? '-' : '+'
}

Expand Down
7 changes: 7 additions & 0 deletions src/lib/stringifier.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { flip } from "./reducer";

const order = {
"*": 0,
"/": 0,
Expand Down Expand Up @@ -28,6 +30,11 @@ function stringify(node, prec) {

if (right.type === 'MathExpression' && order[op] < order[right.operator])
str += "(" + stringify(right, prec) + ")"
else if (right.type === 'MathExpression' && op === "-" && ["+", "-"].includes(right.operator)) {
// fix #52 : a-(b+c) = a-b-c
right.operator = flip(right.operator);
str += stringify(right, prec)
}
else
str += stringify(right, prec)

Expand Down