Skip to content

fix 52: incorrect calculation when subtracting #53

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 1 commit into from
Jan 11, 2019
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