postcss-calc 7.0.2 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -1
- package/dist/index.js +30 -21
- package/dist/lib/convertUnit.js +4 -4
- package/dist/lib/reducer.js +175 -188
- package/dist/lib/stringifier.js +10 -8
- package/dist/lib/transform.js +10 -10
- package/dist/parser.js +0 -0
- package/package.json +9 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
# 8.0.0
|
|
2
|
+
|
|
3
|
+
- Breaking: Updated PostCSS from v7.x to v8.x ([#125](https://github.com/postcss/postcss-calc/pull/125))
|
|
4
|
+
|
|
5
|
+
# 7.0.5
|
|
6
|
+
|
|
7
|
+
- Fixed: reduction
|
|
8
|
+
|
|
9
|
+
# 7.0.4
|
|
10
|
+
|
|
11
|
+
- Fixed: strips away important factors from multiplications in calc() ([#107](https://github.com/postcss/postcss-calc/issues/107))
|
|
12
|
+
|
|
13
|
+
# 7.0.3
|
|
14
|
+
|
|
15
|
+
- Fixed: substracted css-variable from zero ([#111](https://github.com/postcss/postcss-calc/issues/111))
|
|
16
|
+
|
|
1
17
|
# 7.0.2
|
|
2
18
|
|
|
3
19
|
- Fixed: incorrect reduction of subtraction from zero ([#88](https://github.com/postcss/postcss-calc/issues/88))
|
|
@@ -57,7 +73,7 @@
|
|
|
57
73
|
# 5.2.1 - 2016-04-10
|
|
58
74
|
|
|
59
75
|
- Fixed: support for multiline value
|
|
60
|
-
([#27](https://github.com/postcss/postcss-calc/pull/27))
|
|
76
|
+
([#27](https://github.com/postcss/postcss-calc/pull/27))
|
|
61
77
|
|
|
62
78
|
# 5.2.0 - 2016-01-08
|
|
63
79
|
|
package/dist/index.js
CHANGED
|
@@ -5,38 +5,47 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
|
|
8
|
-
var _postcss = require("postcss");
|
|
9
|
-
|
|
10
8
|
var _transform = _interopRequireDefault(require("./lib/transform"));
|
|
11
9
|
|
|
12
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
function pluginCreator(opts) {
|
|
13
|
+
const options = Object.assign({
|
|
16
14
|
precision: 5,
|
|
17
15
|
preserve: false,
|
|
18
16
|
warnWhenCannotResolve: false,
|
|
19
17
|
mediaQueries: false,
|
|
20
18
|
selectors: false
|
|
21
19
|
}, opts);
|
|
22
|
-
return
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
20
|
+
return {
|
|
21
|
+
postcssPlugin: 'postcss-calc',
|
|
22
|
+
|
|
23
|
+
OnceExit(css, {
|
|
24
|
+
result
|
|
25
|
+
}) {
|
|
26
|
+
css.walk(node => {
|
|
27
|
+
const {
|
|
28
|
+
type
|
|
29
|
+
} = node;
|
|
30
|
+
|
|
31
|
+
if (type === 'decl') {
|
|
32
|
+
(0, _transform.default)(node, "value", options, result);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (type === 'atrule' && options.mediaQueries) {
|
|
36
|
+
(0, _transform.default)(node, "params", options, result);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (type === 'rule' && options.selectors) {
|
|
40
|
+
(0, _transform.default)(node, "selector", options, result);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
38
45
|
};
|
|
39
|
-
}
|
|
46
|
+
}
|
|
40
47
|
|
|
48
|
+
pluginCreator.postcss = true;
|
|
49
|
+
var _default = pluginCreator;
|
|
41
50
|
exports.default = _default;
|
|
42
51
|
module.exports = exports.default;
|
package/dist/lib/convertUnit.js
CHANGED
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
7
|
+
const conversions = {
|
|
8
8
|
// Absolute length units
|
|
9
9
|
'px': {
|
|
10
10
|
'px': 1,
|
|
@@ -131,8 +131,8 @@ var conversions = {
|
|
|
131
131
|
};
|
|
132
132
|
|
|
133
133
|
function convertUnit(value, sourceUnit, targetUnit, precision) {
|
|
134
|
-
|
|
135
|
-
|
|
134
|
+
const sourceUnitNormalized = sourceUnit.toLowerCase();
|
|
135
|
+
const targetUnitNormalized = targetUnit.toLowerCase();
|
|
136
136
|
|
|
137
137
|
if (!conversions[targetUnitNormalized]) {
|
|
138
138
|
throw new Error("Cannot convert to " + targetUnit);
|
|
@@ -142,7 +142,7 @@ function convertUnit(value, sourceUnit, targetUnit, precision) {
|
|
|
142
142
|
throw new Error("Cannot convert from " + sourceUnit + " to " + targetUnit);
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
|
|
145
|
+
const converted = conversions[targetUnitNormalized][sourceUnitNormalized] * value;
|
|
146
146
|
|
|
147
147
|
if (precision !== false) {
|
|
148
148
|
precision = Math.pow(10, parseInt(precision) || 5);
|
package/dist/lib/reducer.js
CHANGED
|
@@ -9,10 +9,6 @@ var _convertUnit = _interopRequireDefault(require("./convertUnit"));
|
|
|
9
9
|
|
|
10
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
11
|
|
|
12
|
-
function isEqual(left, right) {
|
|
13
|
-
return left.type === right.type && left.value === right.value;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
12
|
function isValueType(type) {
|
|
17
13
|
switch (type) {
|
|
18
14
|
case 'LengthValue':
|
|
@@ -40,178 +36,126 @@ function flip(operator) {
|
|
|
40
36
|
return operator === '+' ? '-' : '+';
|
|
41
37
|
}
|
|
42
38
|
|
|
43
|
-
function
|
|
44
|
-
|
|
45
|
-
node.value = -node.value;
|
|
46
|
-
} else if (node.type === 'MathExpression') {
|
|
47
|
-
if (node.operator === '*' || node.operator === '/') {
|
|
48
|
-
node.left = flipValue(node.left);
|
|
49
|
-
} else {
|
|
50
|
-
node.left = flipValue(node.left);
|
|
51
|
-
node.right = flipValue(node.right);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return node;
|
|
39
|
+
function isAddSubOperator(operator) {
|
|
40
|
+
return operator === '+' || operator === '-';
|
|
56
41
|
}
|
|
57
42
|
|
|
58
|
-
function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return node.left;
|
|
63
|
-
} // 0 + something => something
|
|
43
|
+
function collectAddSubItems(preOperator, node, collected, precision) {
|
|
44
|
+
if (!isAddSubOperator(preOperator)) {
|
|
45
|
+
throw new Error(`invalid operator ${preOperator}`);
|
|
46
|
+
}
|
|
64
47
|
|
|
48
|
+
const type = node.type;
|
|
65
49
|
|
|
66
|
-
if (isValueType(
|
|
67
|
-
|
|
68
|
-
} // 0 - something => -something
|
|
50
|
+
if (isValueType(type)) {
|
|
51
|
+
const itemIndex = collected.findIndex(x => x.node.type === type);
|
|
69
52
|
|
|
53
|
+
if (itemIndex >= 0) {
|
|
54
|
+
if (node.value === 0) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
70
57
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
58
|
+
const {
|
|
59
|
+
left: reducedNode,
|
|
60
|
+
right: current
|
|
61
|
+
} = covertNodesUnits(collected[itemIndex].node, node, precision);
|
|
75
62
|
|
|
63
|
+
if (collected[itemIndex].preOperator === '-') {
|
|
64
|
+
collected[itemIndex].preOperator = '+';
|
|
65
|
+
reducedNode.value *= -1;
|
|
66
|
+
}
|
|
76
67
|
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
if (preOperator === "+") {
|
|
69
|
+
reducedNode.value += current.value;
|
|
70
|
+
} else {
|
|
71
|
+
reducedNode.value -= current.value;
|
|
72
|
+
} // make sure reducedNode.value >= 0
|
|
79
73
|
|
|
80
|
-
var _covertNodesUnits = covertNodesUnits(node.left, node.right, precision),
|
|
81
|
-
left = _covertNodesUnits.left,
|
|
82
|
-
right = _covertNodesUnits.right;
|
|
83
74
|
|
|
84
|
-
|
|
85
|
-
|
|
75
|
+
if (reducedNode.value >= 0) {
|
|
76
|
+
collected[itemIndex] = {
|
|
77
|
+
node: reducedNode,
|
|
78
|
+
preOperator: '+'
|
|
79
|
+
};
|
|
80
|
+
} else {
|
|
81
|
+
reducedNode.value *= -1;
|
|
82
|
+
collected[itemIndex] = {
|
|
83
|
+
node: reducedNode,
|
|
84
|
+
preOperator: '-'
|
|
85
|
+
};
|
|
86
|
+
}
|
|
86
87
|
} else {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (node.right.type === 'MathExpression' && (node.right.operator === '+' || node.right.operator === '-')) {
|
|
95
|
-
// something - (something + something) => something - something - something
|
|
96
|
-
// something - (something - something) => something - something + something
|
|
97
|
-
if ((node.right.operator === '+' || node.right.operator === '-') && node.operator === '-') {
|
|
98
|
-
node.right.operator = flip(node.right.operator);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (isValueType(node.left.type)) {
|
|
102
|
-
// value + (value + something) => value + something
|
|
103
|
-
// value + (value - something) => value - something
|
|
104
|
-
// value - (value + something) => value - something
|
|
105
|
-
// value - (value - something) => value + something
|
|
106
|
-
if (node.left.type === node.right.left.type) {
|
|
107
|
-
var _left = node.left,
|
|
108
|
-
_operator = node.operator,
|
|
109
|
-
_right = node.right;
|
|
110
|
-
node.left = reduce({
|
|
111
|
-
type: 'MathExpression',
|
|
112
|
-
operator: _operator,
|
|
113
|
-
left: _left,
|
|
114
|
-
right: _right.left
|
|
88
|
+
// make sure node.value >= 0
|
|
89
|
+
if (node.value >= 0) {
|
|
90
|
+
collected.push({
|
|
91
|
+
node,
|
|
92
|
+
preOperator
|
|
115
93
|
});
|
|
116
|
-
|
|
117
|
-
node.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
// something - (something + value) => dimension - something
|
|
122
|
-
// something - (something - value) => dimension - something
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (node.left.type === node.right.right.type) {
|
|
126
|
-
var _left2 = node.left,
|
|
127
|
-
_right2 = node.right;
|
|
128
|
-
node.left = reduce({
|
|
129
|
-
type: 'MathExpression',
|
|
130
|
-
operator: _right2.operator,
|
|
131
|
-
left: _left2,
|
|
132
|
-
right: _right2.right
|
|
94
|
+
} else {
|
|
95
|
+
node.value *= -1;
|
|
96
|
+
collected.push({
|
|
97
|
+
node,
|
|
98
|
+
preOperator: flip(preOperator)
|
|
133
99
|
});
|
|
134
|
-
node.right = _right2.left;
|
|
135
|
-
return reduce(node, precision);
|
|
136
100
|
}
|
|
137
101
|
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (node.right.type === node.left.left.type) {
|
|
147
|
-
var _left3 = node.left,
|
|
148
|
-
_operator2 = node.operator,
|
|
149
|
-
_right3 = node.right;
|
|
150
|
-
_left3.left = reduce({
|
|
151
|
-
type: 'MathExpression',
|
|
152
|
-
operator: _operator2,
|
|
153
|
-
left: _left3.left,
|
|
154
|
-
right: _right3
|
|
155
|
-
}, precision);
|
|
156
|
-
return reduce(_left3, precision);
|
|
157
|
-
} // (something + dimension) + dimension => something + dimension
|
|
158
|
-
// (something - dimension) + dimension => something - dimension
|
|
159
|
-
// (something + dimension) - dimension => something + dimension
|
|
160
|
-
// (something - dimension) - dimension => something - dimension
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (node.right.type === node.left.right.type) {
|
|
164
|
-
var _left4 = node.left,
|
|
165
|
-
_operator3 = node.operator,
|
|
166
|
-
_right4 = node.right;
|
|
167
|
-
|
|
168
|
-
if (_left4.operator === '-') {
|
|
169
|
-
_left4.operator = _operator3 === '-' ? '-' : '+';
|
|
170
|
-
_left4.right = reduce({
|
|
171
|
-
type: 'MathExpression',
|
|
172
|
-
operator: _operator3 === '-' ? '+' : '-',
|
|
173
|
-
left: _right4,
|
|
174
|
-
right: _left4.right
|
|
175
|
-
}, precision);
|
|
176
|
-
} else {
|
|
177
|
-
_left4.right = reduce({
|
|
178
|
-
type: 'MathExpression',
|
|
179
|
-
operator: _operator3,
|
|
180
|
-
left: _left4.right,
|
|
181
|
-
right: _right4
|
|
182
|
-
}, precision);
|
|
183
|
-
}
|
|
102
|
+
} else if (type === "MathExpression") {
|
|
103
|
+
if (isAddSubOperator(node.operator)) {
|
|
104
|
+
collectAddSubItems(preOperator, node.left, collected, precision);
|
|
105
|
+
const collectRightOperator = preOperator === '-' ? flip(node.operator) : node.operator;
|
|
106
|
+
collectAddSubItems(collectRightOperator, node.right, collected, precision);
|
|
107
|
+
} else {
|
|
108
|
+
// * or /
|
|
109
|
+
const reducedNode = reduce(node, precision); // prevent infinite recursive call
|
|
184
110
|
|
|
185
|
-
if (
|
|
186
|
-
|
|
187
|
-
|
|
111
|
+
if (reducedNode.type !== "MathExpression" || isAddSubOperator(reducedNode.operator)) {
|
|
112
|
+
collectAddSubItems(preOperator, reducedNode, collected, precision);
|
|
113
|
+
} else {
|
|
114
|
+
collected.push({
|
|
115
|
+
node: reducedNode,
|
|
116
|
+
preOperator
|
|
117
|
+
});
|
|
188
118
|
}
|
|
189
|
-
|
|
190
|
-
_left4.parenthesized = node.parenthesized;
|
|
191
|
-
return reduce(_left4, precision);
|
|
192
119
|
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
|
|
120
|
+
} else {
|
|
121
|
+
collected.push({
|
|
122
|
+
node,
|
|
123
|
+
preOperator
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
196
127
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
128
|
+
function reduceAddSubExpression(node, precision) {
|
|
129
|
+
const collected = [];
|
|
130
|
+
collectAddSubItems('+', node, collected, precision);
|
|
131
|
+
const withoutZeroItem = collected.filter(item => !(isValueType(item.node.type) && item.node.value === 0));
|
|
132
|
+
const firstNonZeroItem = withoutZeroItem[0]; // could be undefined
|
|
133
|
+
// prevent producing "calc(-var(--a))" or "calc()"
|
|
134
|
+
// which is invalid css
|
|
135
|
+
|
|
136
|
+
if (!firstNonZeroItem || firstNonZeroItem.preOperator === '-' && !isValueType(firstNonZeroItem.node.type)) {
|
|
137
|
+
const firstZeroItem = collected.find(item => isValueType(item.node.type) && item.node.value === 0);
|
|
138
|
+
withoutZeroItem.unshift(firstZeroItem);
|
|
139
|
+
} // make sure the preOperator of the first item is +
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
if (withoutZeroItem[0].preOperator === '-' && isValueType(withoutZeroItem[0].node.type)) {
|
|
143
|
+
withoutZeroItem[0].node.value *= -1;
|
|
144
|
+
withoutZeroItem[0].preOperator = '+';
|
|
145
|
+
}
|
|
204
146
|
|
|
205
|
-
|
|
206
|
-
var _newNodes = covertNodesUnits(node.left.left, node.right.right, precision);
|
|
147
|
+
let root = withoutZeroItem[0].node;
|
|
207
148
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
149
|
+
for (let i = 1; i < withoutZeroItem.length; i++) {
|
|
150
|
+
root = {
|
|
151
|
+
type: 'MathExpression',
|
|
152
|
+
operator: withoutZeroItem[i].preOperator,
|
|
153
|
+
left: root,
|
|
154
|
+
right: withoutZeroItem[i].node
|
|
155
|
+
};
|
|
212
156
|
}
|
|
213
157
|
|
|
214
|
-
return
|
|
158
|
+
return root;
|
|
215
159
|
}
|
|
216
160
|
|
|
217
161
|
function reduceDivisionExpression(node) {
|
|
@@ -223,51 +167,93 @@ function reduceDivisionExpression(node) {
|
|
|
223
167
|
throw new Error(`Cannot divide by "${node.right.unit}", number expected`);
|
|
224
168
|
}
|
|
225
169
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
} // something / value
|
|
170
|
+
return applyNumberDivision(node.left, node.right.value);
|
|
171
|
+
} // apply (expr) / number
|
|
229
172
|
|
|
230
173
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
174
|
+
function applyNumberDivision(node, divisor) {
|
|
175
|
+
if (divisor === 0) {
|
|
176
|
+
throw new Error('Cannot divide by zero');
|
|
234
177
|
}
|
|
235
178
|
|
|
236
|
-
|
|
179
|
+
if (isValueType(node.type)) {
|
|
180
|
+
node.value /= divisor;
|
|
181
|
+
return node;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (node.type === "MathExpression" && isAddSubOperator(node.operator)) {
|
|
185
|
+
// turn (a + b) / num into a/num + b/num
|
|
186
|
+
// is good for further reduction
|
|
187
|
+
// checkout the test case
|
|
188
|
+
// "should reduce division before reducing additions"
|
|
189
|
+
return {
|
|
190
|
+
type: "MathExpression",
|
|
191
|
+
operator: node.operator,
|
|
192
|
+
left: applyNumberDivision(node.left, divisor),
|
|
193
|
+
right: applyNumberDivision(node.right, divisor)
|
|
194
|
+
};
|
|
195
|
+
} // it is impossible to reduce it into a single value
|
|
196
|
+
// .e.g the node contains css variable
|
|
197
|
+
// so we just preserve the division and let browser do it
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
type: "MathExpression",
|
|
202
|
+
operator: '/',
|
|
203
|
+
left: node,
|
|
204
|
+
right: {
|
|
205
|
+
type: "Number",
|
|
206
|
+
value: divisor
|
|
207
|
+
}
|
|
208
|
+
};
|
|
237
209
|
}
|
|
238
210
|
|
|
239
211
|
function reduceMultiplicationExpression(node) {
|
|
240
212
|
// (expr) * number
|
|
241
|
-
if (node.
|
|
242
|
-
|
|
243
|
-
node.left.left.value *= node.right.value;
|
|
244
|
-
node.left.right.value *= node.right.value;
|
|
245
|
-
return node.left;
|
|
246
|
-
}
|
|
247
|
-
} // something * number
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (isValueType(node.left.type) && node.right.type === 'Number') {
|
|
251
|
-
node.left.value *= node.right.value;
|
|
252
|
-
return node.left;
|
|
213
|
+
if (node.right.type === 'Number') {
|
|
214
|
+
return applyNumberMultiplication(node.left, node.right.value);
|
|
253
215
|
} // number * (expr)
|
|
254
216
|
|
|
255
217
|
|
|
256
|
-
if (node.left.type === 'Number'
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
} // number * something
|
|
218
|
+
if (node.left.type === 'Number') {
|
|
219
|
+
return applyNumberMultiplication(node.right, node.left.value);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return node;
|
|
223
|
+
} // apply (expr) / number
|
|
263
224
|
|
|
264
225
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
226
|
+
function applyNumberMultiplication(node, multiplier) {
|
|
227
|
+
if (isValueType(node.type)) {
|
|
228
|
+
node.value *= multiplier;
|
|
229
|
+
return node;
|
|
268
230
|
}
|
|
269
231
|
|
|
270
|
-
|
|
232
|
+
if (node.type === "MathExpression" && isAddSubOperator(node.operator)) {
|
|
233
|
+
// turn (a + b) * num into a*num + b*num
|
|
234
|
+
// is good for further reduction
|
|
235
|
+
// checkout the test case
|
|
236
|
+
// "should reduce multiplication before reducing additions"
|
|
237
|
+
return {
|
|
238
|
+
type: "MathExpression",
|
|
239
|
+
operator: node.operator,
|
|
240
|
+
left: applyNumberMultiplication(node.left, multiplier),
|
|
241
|
+
right: applyNumberMultiplication(node.right, multiplier)
|
|
242
|
+
};
|
|
243
|
+
} // it is impossible to reduce it into a single value
|
|
244
|
+
// .e.g the node contains css variable
|
|
245
|
+
// so we just preserve the division and let browser do it
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
type: "MathExpression",
|
|
250
|
+
operator: '*',
|
|
251
|
+
left: node,
|
|
252
|
+
right: {
|
|
253
|
+
type: "Number",
|
|
254
|
+
value: multiplier
|
|
255
|
+
}
|
|
256
|
+
};
|
|
271
257
|
}
|
|
272
258
|
|
|
273
259
|
function covertNodesUnits(left, right, precision) {
|
|
@@ -278,7 +264,7 @@ function covertNodesUnits(left, right, precision) {
|
|
|
278
264
|
case 'FrequencyValue':
|
|
279
265
|
case 'ResolutionValue':
|
|
280
266
|
if (right.type === left.type && right.unit && left.unit) {
|
|
281
|
-
|
|
267
|
+
const converted = (0, _convertUnit.default)(right.value, right.unit, left.unit, precision);
|
|
282
268
|
right = {
|
|
283
269
|
type: left.type,
|
|
284
270
|
value: converted,
|
|
@@ -301,14 +287,15 @@ function covertNodesUnits(left, right, precision) {
|
|
|
301
287
|
|
|
302
288
|
function reduce(node, precision) {
|
|
303
289
|
if (node.type === "MathExpression") {
|
|
290
|
+
if (isAddSubOperator(node.operator)) {
|
|
291
|
+
// reduceAddSubExpression will call reduce recursively
|
|
292
|
+
return reduceAddSubExpression(node, precision);
|
|
293
|
+
}
|
|
294
|
+
|
|
304
295
|
node.left = reduce(node.left, precision);
|
|
305
296
|
node.right = reduce(node.right, precision);
|
|
306
297
|
|
|
307
298
|
switch (node.operator) {
|
|
308
|
-
case "+":
|
|
309
|
-
case "-":
|
|
310
|
-
return reduceAddSubExpression(node, precision);
|
|
311
|
-
|
|
312
299
|
case "/":
|
|
313
300
|
return reduceDivisionExpression(node, precision);
|
|
314
301
|
|
package/dist/lib/stringifier.js
CHANGED
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = _default;
|
|
7
|
-
|
|
7
|
+
const order = {
|
|
8
8
|
"*": 0,
|
|
9
9
|
"/": 0,
|
|
10
10
|
"+": 1,
|
|
@@ -13,7 +13,7 @@ var order = {
|
|
|
13
13
|
|
|
14
14
|
function round(value, prec) {
|
|
15
15
|
if (prec !== false) {
|
|
16
|
-
|
|
16
|
+
const precision = Math.pow(10, prec);
|
|
17
17
|
return Math.round(value * precision) / precision;
|
|
18
18
|
}
|
|
19
19
|
|
|
@@ -24,10 +24,12 @@ function stringify(node, prec) {
|
|
|
24
24
|
switch (node.type) {
|
|
25
25
|
case "MathExpression":
|
|
26
26
|
{
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
const {
|
|
28
|
+
left,
|
|
29
|
+
right,
|
|
30
|
+
operator: op
|
|
31
|
+
} = node;
|
|
32
|
+
let str = "";
|
|
31
33
|
|
|
32
34
|
if (left.type === 'MathExpression' && order[op] < order[left.operator]) {
|
|
33
35
|
str += `(${stringify(left, prec)})`;
|
|
@@ -58,8 +60,8 @@ function stringify(node, prec) {
|
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
function _default(calc, node, originalValue, options, result, item) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
let str = stringify(node, options.precision);
|
|
64
|
+
const shouldPrintCalc = node.type === "MathExpression" || node.type === "Function";
|
|
63
65
|
|
|
64
66
|
if (shouldPrintCalc) {
|
|
65
67
|
// if calc expression couldn't be resolved to a single value, re-wrap it as
|
package/dist/lib/transform.js
CHANGED
|
@@ -18,23 +18,23 @@ var _stringifier = _interopRequireDefault(require("./stringifier"));
|
|
|
18
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
19
|
|
|
20
20
|
// eslint-disable-next-line import/no-unresolved
|
|
21
|
-
|
|
21
|
+
const MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i;
|
|
22
22
|
|
|
23
23
|
function transformValue(value, options, result, item) {
|
|
24
|
-
return (0, _postcssValueParser.default)(value).walk(
|
|
24
|
+
return (0, _postcssValueParser.default)(value).walk(node => {
|
|
25
25
|
// skip anything which isn't a calc() function
|
|
26
26
|
if (node.type !== 'function' || !MATCH_CALC.test(node.value)) {
|
|
27
27
|
return node;
|
|
28
28
|
} // stringify calc expression and produce an AST
|
|
29
29
|
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
const contents = _postcssValueParser.default.stringify(node.nodes);
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
const ast = _parser.parser.parse(contents); // reduce AST to its simplest form, that is, either to a single value
|
|
34
34
|
// or a simplified calc expression
|
|
35
35
|
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
const reducedAst = (0, _reducer.default)(ast, options.precision); // stringify AST and write it back
|
|
38
38
|
|
|
39
39
|
node.type = 'word';
|
|
40
40
|
node.value = (0, _stringifier.default)(node.value, reducedAst, value, options, result, item);
|
|
@@ -43,8 +43,8 @@ function transformValue(value, options, result, item) {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
function transformSelector(value, options, result, item) {
|
|
46
|
-
return (0, _postcssSelectorParser.default)(
|
|
47
|
-
selectors.walk(
|
|
46
|
+
return (0, _postcssSelectorParser.default)(selectors => {
|
|
47
|
+
selectors.walk(node => {
|
|
48
48
|
// attribute value
|
|
49
49
|
// e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
|
|
50
50
|
if (node.type === 'attribute' && node.value) {
|
|
@@ -62,14 +62,14 @@ function transformSelector(value, options, result, item) {
|
|
|
62
62
|
}).processSync(value);
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
var _default =
|
|
66
|
-
|
|
65
|
+
var _default = (node, property, options, result) => {
|
|
66
|
+
const value = property === "selector" ? transformSelector(node[property], options, result, node) : transformValue(node[property], options, result, node); // if the preserve option is enabled and the value has changed, write the
|
|
67
67
|
// transformed value into a cloned node which is inserted before the current
|
|
68
68
|
// node, preserving the original value. Otherwise, overwrite the original
|
|
69
69
|
// value.
|
|
70
70
|
|
|
71
71
|
if (options.preserve && node[property] !== value) {
|
|
72
|
-
|
|
72
|
+
const clone = node.clone();
|
|
73
73
|
clone[property] = value;
|
|
74
74
|
node.parent.insertBefore(node, clone);
|
|
75
75
|
} else {
|
package/dist/parser.js
CHANGED
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-calc",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"description": "PostCSS plugin to reduce calc()",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"css",
|
|
@@ -36,22 +36,25 @@
|
|
|
36
36
|
"@babel/polyfill": "^7.0.0",
|
|
37
37
|
"@babel/preset-env": "^7.1.0",
|
|
38
38
|
"@babel/register": "^7.0.0",
|
|
39
|
-
"ava": "^
|
|
39
|
+
"ava": "^3.15.0",
|
|
40
40
|
"babel-eslint": "^10.0.1",
|
|
41
41
|
"babel-plugin-add-module-exports": "^1.0.0",
|
|
42
|
-
"cross-env": "^
|
|
43
|
-
"del-cli": "^
|
|
42
|
+
"cross-env": "^7.0.0",
|
|
43
|
+
"del-cli": "^3.0.0",
|
|
44
44
|
"eslint": "^5.7.0",
|
|
45
45
|
"eslint-config-i-am-meticulous": "^11.0.0",
|
|
46
46
|
"eslint-plugin-babel": "^5.2.1",
|
|
47
47
|
"eslint-plugin-import": "^2.14.0",
|
|
48
|
-
"jison-gho": "^0.6.1-
|
|
48
|
+
"jison-gho": "^0.6.1-216",
|
|
49
|
+
"postcss": "^8.2.2"
|
|
49
50
|
},
|
|
50
51
|
"dependencies": {
|
|
51
|
-
"postcss": "^7.0.27",
|
|
52
52
|
"postcss-selector-parser": "^6.0.2",
|
|
53
53
|
"postcss-value-parser": "^4.0.2"
|
|
54
54
|
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"postcss": "^8.2.2"
|
|
57
|
+
},
|
|
55
58
|
"ava": {
|
|
56
59
|
"require": [
|
|
57
60
|
"@babel/register",
|