postcss-calc 6.0.0 → 7.0.1
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 +27 -0
- package/README.md +34 -16
- package/dist/index.js +11 -12
- package/dist/lib/convert.js +46 -0
- package/dist/lib/reducer.js +255 -0
- package/dist/lib/stringifier.js +68 -0
- package/dist/lib/transform.js +37 -28
- package/dist/parser.js +3755 -0
- package/package.json +27 -22
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
+
# 7.0.1
|
|
2
|
+
|
|
3
|
+
- Updated: `postcss` to 7.0.2 (patch)
|
|
4
|
+
- Updated: `postcss-selector-parser` to 5.0.0-rc.4 (patch)
|
|
5
|
+
- Updated: `postcss-value-parser` to 3.3.1 (patch)
|
|
6
|
+
|
|
7
|
+
# 7.0.0
|
|
8
|
+
|
|
9
|
+
- Changed: Updated postcss-selector-parser to version 5.0.0-rc.3
|
|
10
|
+
- Changed: Dropped reduce-css-calc as a dependency
|
|
11
|
+
- Fixed: Support constant() and env() ([#42](https://github.com/postcss/postcss-calc/issues/42), [#48](https://github.com/postcss/postcss-calc/issues/48))
|
|
12
|
+
- Fixed: Support custom properties with "calc" in its name ([#50](https://github.com/postcss/postcss-calc/issues/50))
|
|
13
|
+
- Fixed: Remove unnecessary whitespace around `*` and `/` ([cssnano#625](https://github.com/cssnano/cssnano/issues/625))
|
|
14
|
+
- Fixed: Arithmetic bugs around subtraction ([#49](https://github.com/postcss/postcss-calc/issues/49))
|
|
15
|
+
- Fixed: Handling of nested calc statements ([reduce-css-calc#49](https://github.com/MoOx/reduce-css-calc/issues/49))
|
|
16
|
+
- Fixed: Bugs regarding complex calculations ([reduce-cs-calc#45](https://github.com/MoOx/reduce-css-calc/issues/45))
|
|
17
|
+
- Fixed: `100%` incorrectly being transformed to `1` ([reduce-css-calc#44](https://github.com/MoOx/reduce-css-calc/issues/44))
|
|
18
|
+
- Added: support for case-insensitive calc statements
|
|
19
|
+
|
|
20
|
+
# 6.0.2 - 2018-09-25
|
|
21
|
+
|
|
22
|
+
- Fixed: use PostCSS 7 (thanks to @douglasduteil)
|
|
23
|
+
|
|
24
|
+
# 6.0.1 - 2017-10-10
|
|
25
|
+
|
|
26
|
+
- Fixed: throwing error for attribute selectors without a value
|
|
27
|
+
|
|
1
28
|
# 6.0.0 - 2017-05-08
|
|
2
29
|
|
|
3
30
|
- Breaking: Updated PostCSS from v5.x to v6.x, and reduce-css-calc from v1.x
|
package/README.md
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# PostCSS Calc [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][PostCSS]
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[![NPM Version][npm-img]][npm-url]
|
|
4
|
+
[![Build Status][cli-img]][cli-url]
|
|
5
|
+
[![Support Chat][git-img]][git-url]
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
[PostCSS Calc] lets you reduce `calc()` references whenever it's possible. This
|
|
8
|
+
can be particularly useful with the [PostCSS Custom Properties] plugin.
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
When multiple units are mixed together in the same expression, the `calc()`
|
|
11
|
+
statement is left as is, to fallback to the [W3C calc() implementation].
|
|
9
12
|
|
|
10
13
|
## Installation
|
|
11
14
|
|
|
12
|
-
```
|
|
13
|
-
|
|
15
|
+
```bash
|
|
16
|
+
npm install postcss-calc
|
|
14
17
|
```
|
|
15
18
|
|
|
16
19
|
## Usage
|
|
@@ -31,7 +34,7 @@ var output = postcss()
|
|
|
31
34
|
.css
|
|
32
35
|
```
|
|
33
36
|
|
|
34
|
-
**Example** (with [
|
|
37
|
+
**Example** (with [PostCSS Custom Properties] enabled as well):
|
|
35
38
|
|
|
36
39
|
```js
|
|
37
40
|
// dependencies
|
|
@@ -86,7 +89,7 @@ h1 {
|
|
|
86
89
|
}
|
|
87
90
|
```
|
|
88
91
|
|
|
89
|
-
Checkout [tests]
|
|
92
|
+
Checkout [tests] for more examples.
|
|
90
93
|
|
|
91
94
|
### Options
|
|
92
95
|
|
|
@@ -103,7 +106,8 @@ var out = postcss()
|
|
|
103
106
|
|
|
104
107
|
#### `preserve` (default: `false`)
|
|
105
108
|
|
|
106
|
-
Allow you to preserve calc() usage in output so browsers will handle decimal
|
|
109
|
+
Allow you to preserve calc() usage in output so browsers will handle decimal
|
|
110
|
+
precision themselves.
|
|
107
111
|
|
|
108
112
|
```js
|
|
109
113
|
var out = postcss()
|
|
@@ -157,15 +161,29 @@ div[data-size="calc(3*3)"] {
|
|
|
157
161
|
|
|
158
162
|
## Contributing
|
|
159
163
|
|
|
160
|
-
Work on a branch, install dev-dependencies, respect coding style & run tests
|
|
164
|
+
Work on a branch, install dev-dependencies, respect coding style & run tests
|
|
165
|
+
before submitting a bug fix or a feature.
|
|
161
166
|
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
+
```bash
|
|
168
|
+
git clone git@github.com:postcss/postcss-calc.git
|
|
169
|
+
git checkout -b patch-1
|
|
170
|
+
npm install
|
|
171
|
+
npm test
|
|
167
172
|
```
|
|
168
173
|
|
|
169
174
|
## [Changelog](CHANGELOG.md)
|
|
170
175
|
|
|
171
176
|
## [License](LICENSE)
|
|
177
|
+
|
|
178
|
+
[cli-img]: https://img.shields.io/travis/postcss/postcss-calc/master.svg
|
|
179
|
+
[cli-url]: https://travis-ci.org/postcss/postcss-calc
|
|
180
|
+
[git-img]: https://img.shields.io/badge/support-chat-blue.svg
|
|
181
|
+
[git-url]: https://gitter.im/postcss/postcss
|
|
182
|
+
[npm-img]: https://img.shields.io/npm/v/postcss-calc.svg
|
|
183
|
+
[npm-url]: https://www.npmjs.com/package/postcss-calc
|
|
184
|
+
|
|
185
|
+
[PostCSS]: https://github.com/postcss
|
|
186
|
+
[PostCSS Calc]: https://github.com/postcss/postcss-calc
|
|
187
|
+
[PostCSS Custom Properties]: https://github.com/postcss/postcss-custom-properties
|
|
188
|
+
[tests]: src/__tests__/index.js
|
|
189
|
+
[W3C calc() implementation]: https://www.w3.org/TR/css3-values/#calc-notation
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.default = void 0;
|
|
6
7
|
|
|
7
|
-
var _postcss = require(
|
|
8
|
+
var _postcss = require("postcss");
|
|
8
9
|
|
|
9
|
-
var _transform = require(
|
|
10
|
-
|
|
11
|
-
var _transform2 = _interopRequireDefault(_transform);
|
|
10
|
+
var _transform = _interopRequireDefault(require("./lib/transform"));
|
|
12
11
|
|
|
13
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
var _default = (0, _postcss.plugin)('postcss-calc', function (opts) {
|
|
16
15
|
var options = Object.assign({
|
|
17
16
|
precision: 5,
|
|
18
17
|
preserve: false,
|
|
@@ -20,15 +19,15 @@ exports.default = (0, _postcss.plugin)('postcss-calc', function (opts) {
|
|
|
20
19
|
mediaQueries: false,
|
|
21
20
|
selectors: false
|
|
22
21
|
}, opts);
|
|
23
|
-
|
|
24
22
|
return function (css, result) {
|
|
25
23
|
css.walk(function (node) {
|
|
26
24
|
var type = node.type;
|
|
27
|
-
|
|
28
|
-
if (type === '
|
|
29
|
-
if (type === '
|
|
30
|
-
if (type === 'rule' && options.selectors) (0, _transform2.default)(node, "selector", options, result);
|
|
25
|
+
if (type === 'decl') (0, _transform.default)(node, "value", options, result);
|
|
26
|
+
if (type === 'atrule' && options.mediaQueries) (0, _transform.default)(node, "params", options, result);
|
|
27
|
+
if (type === 'rule' && options.selectors) (0, _transform.default)(node, "selector", options, result);
|
|
31
28
|
});
|
|
32
29
|
};
|
|
33
30
|
});
|
|
34
|
-
|
|
31
|
+
|
|
32
|
+
exports.default = _default;
|
|
33
|
+
module.exports = exports.default;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _cssUnitConverter = _interopRequireDefault(require("css-unit-converter"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
function convertNodes(left, right, precision) {
|
|
13
|
+
switch (left.type) {
|
|
14
|
+
case 'LengthValue':
|
|
15
|
+
case 'AngleValue':
|
|
16
|
+
case 'TimeValue':
|
|
17
|
+
case 'FrequencyValue':
|
|
18
|
+
case 'ResolutionValue':
|
|
19
|
+
return convertAbsoluteLength(left, right, precision);
|
|
20
|
+
|
|
21
|
+
default:
|
|
22
|
+
return {
|
|
23
|
+
left,
|
|
24
|
+
right
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function convertAbsoluteLength(left, right, precision) {
|
|
30
|
+
if (right.type === left.type) {
|
|
31
|
+
right = {
|
|
32
|
+
type: left.type,
|
|
33
|
+
value: (0, _cssUnitConverter.default)(right.value, right.unit, left.unit, precision),
|
|
34
|
+
unit: left.unit
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
left,
|
|
40
|
+
right
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
var _default = convertNodes;
|
|
45
|
+
exports.default = _default;
|
|
46
|
+
module.exports = exports.default;
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _convert = _interopRequireDefault(require("./convert"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
function reduce(node, precision) {
|
|
13
|
+
if (node.type === "MathExpression") return reduceMathExpression(node, precision);
|
|
14
|
+
return node;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function isEqual(left, right) {
|
|
18
|
+
return left.type === right.type && left.value === right.value;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isValueType(type) {
|
|
22
|
+
switch (type) {
|
|
23
|
+
case 'LengthValue':
|
|
24
|
+
case 'AngleValue':
|
|
25
|
+
case 'TimeValue':
|
|
26
|
+
case 'FrequencyValue':
|
|
27
|
+
case 'ResolutionValue':
|
|
28
|
+
case 'EmValue':
|
|
29
|
+
case 'ExValue':
|
|
30
|
+
case 'ChValue':
|
|
31
|
+
case 'RemValue':
|
|
32
|
+
case 'VhValue':
|
|
33
|
+
case 'VwValue':
|
|
34
|
+
case 'VminValue':
|
|
35
|
+
case 'VmaxValue':
|
|
36
|
+
case 'PercentageValue':
|
|
37
|
+
case 'Value':
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function convertMathExpression(node, precision) {
|
|
45
|
+
var nodes = (0, _convert.default)(node.left, node.right, precision);
|
|
46
|
+
var left = reduce(nodes.left, precision);
|
|
47
|
+
var right = reduce(nodes.right, precision);
|
|
48
|
+
|
|
49
|
+
if (left.type === "MathExpression" && right.type === "MathExpression") {
|
|
50
|
+
if (left.operator === '/' && right.operator === '*' || left.operator === '-' && right.operator === '+' || left.operator === '*' && right.operator === '/' || left.operator === '+' && right.operator === '-') {
|
|
51
|
+
if (isEqual(left.right, right.right)) nodes = (0, _convert.default)(left.left, right.left, precision);else if (isEqual(left.right, right.left)) nodes = (0, _convert.default)(left.left, right.right, precision);
|
|
52
|
+
left = reduce(nodes.left, precision);
|
|
53
|
+
right = reduce(nodes.right, precision);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
node.left = left;
|
|
58
|
+
node.right = right;
|
|
59
|
+
return node;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function flip(operator) {
|
|
63
|
+
return operator === '+' ? '-' : '+';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function flipValue(node) {
|
|
67
|
+
if (isValueType(node.type)) node.value = -node.value;else if (node.type == 'MathExpression') {
|
|
68
|
+
node.left = flipValue(node.left);
|
|
69
|
+
node.right = flipValue(node.right);
|
|
70
|
+
}
|
|
71
|
+
return node;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function reduceAddSubExpression(node, precision) {
|
|
75
|
+
var _node = node,
|
|
76
|
+
left = _node.left,
|
|
77
|
+
right = _node.right,
|
|
78
|
+
op = _node.operator;
|
|
79
|
+
if (left.type === 'Function' || right.type === 'Function') return node; // something + 0 => something
|
|
80
|
+
// something - 0 => something
|
|
81
|
+
|
|
82
|
+
if (right.value === 0) return left; // 0 + something => something
|
|
83
|
+
|
|
84
|
+
if (left.value === 0 && op === "+") return right; // 0 - something => -something
|
|
85
|
+
|
|
86
|
+
if (left.value === 0 && op === "-") return flipValue(right); // value + value
|
|
87
|
+
// value - value
|
|
88
|
+
|
|
89
|
+
if (left.type === right.type && isValueType(left.type)) {
|
|
90
|
+
node = Object.assign({}, left);
|
|
91
|
+
if (op === "+") node.value = left.value + right.value;else node.value = left.value - right.value;
|
|
92
|
+
} // value <op> (expr)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
if (isValueType(left.type) && (right.operator === '+' || right.operator === '-') && right.type === 'MathExpression') {
|
|
96
|
+
// value + (value + something) => (value + value) + something
|
|
97
|
+
// value + (value - something) => (value + value) - something
|
|
98
|
+
// value - (value + something) => (value - value) - something
|
|
99
|
+
// value - (value - something) => (value - value) + something
|
|
100
|
+
if (left.type === right.left.type) {
|
|
101
|
+
node = Object.assign({}, node);
|
|
102
|
+
node.left = reduce({
|
|
103
|
+
type: 'MathExpression',
|
|
104
|
+
operator: op,
|
|
105
|
+
left: left,
|
|
106
|
+
right: right.left
|
|
107
|
+
}, precision);
|
|
108
|
+
node.right = right.right;
|
|
109
|
+
node.operator = op === '-' ? flip(right.operator) : right.operator;
|
|
110
|
+
return reduce(node, precision);
|
|
111
|
+
} // value + (something + value) => (value + value) + something
|
|
112
|
+
// value + (something - value) => (value - value) + something
|
|
113
|
+
// value - (something + value) => (value - value) - something
|
|
114
|
+
// value - (something - value) => (value + value) - something
|
|
115
|
+
else if (left.type === right.right.type) {
|
|
116
|
+
node = Object.assign({}, node);
|
|
117
|
+
node.left = reduce({
|
|
118
|
+
type: 'MathExpression',
|
|
119
|
+
operator: op === '-' ? flip(right.operator) : right.operator,
|
|
120
|
+
left: left,
|
|
121
|
+
right: right.right
|
|
122
|
+
}, precision);
|
|
123
|
+
node.right = right.left;
|
|
124
|
+
return reduce(node, precision);
|
|
125
|
+
} // value - (something + something) => value - something - something
|
|
126
|
+
else if (op === '-' && right.operator === '+') {
|
|
127
|
+
node = Object.assign({}, node);
|
|
128
|
+
node.right.operator = '-';
|
|
129
|
+
return reduce(node, precision);
|
|
130
|
+
}
|
|
131
|
+
} // (expr) <op> value
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
if (left.type === 'MathExpression' && (left.operator === '+' || left.operator === '-') && isValueType(right.type)) {
|
|
135
|
+
// (value + something) + value => (value + value) + something
|
|
136
|
+
// (value - something) + value => (value + value) - something
|
|
137
|
+
// (value + something) - value => (value - value) + something
|
|
138
|
+
// (value - something) - value => (value - value) - something
|
|
139
|
+
if (right.type === left.left.type) {
|
|
140
|
+
node = Object.assign({}, left);
|
|
141
|
+
node.left = reduce({
|
|
142
|
+
type: 'MathExpression',
|
|
143
|
+
operator: op,
|
|
144
|
+
left: left.left,
|
|
145
|
+
right: right
|
|
146
|
+
}, precision);
|
|
147
|
+
return reduce(node, precision);
|
|
148
|
+
} // (something + value) + value => something + (value + value)
|
|
149
|
+
// (something - value1) + value2 => something - (value2 - value1)
|
|
150
|
+
// (something + value) - value => something + (value - value)
|
|
151
|
+
// (something - value) - value => something - (value + value)
|
|
152
|
+
else if (right.type === left.right.type) {
|
|
153
|
+
node = Object.assign({}, left);
|
|
154
|
+
|
|
155
|
+
if (left.operator === '-') {
|
|
156
|
+
node.right = reduce({
|
|
157
|
+
type: 'MathExpression',
|
|
158
|
+
operator: flip(op),
|
|
159
|
+
left: left.right,
|
|
160
|
+
right: right
|
|
161
|
+
}, precision);
|
|
162
|
+
|
|
163
|
+
if (node.right.value && node.right.value < 0) {
|
|
164
|
+
node.right.value = Math.abs(node.right.value);
|
|
165
|
+
node.operator = '+';
|
|
166
|
+
} else {
|
|
167
|
+
node.operator = left.operator;
|
|
168
|
+
}
|
|
169
|
+
} else {
|
|
170
|
+
node.right = reduce({
|
|
171
|
+
type: 'MathExpression',
|
|
172
|
+
operator: op,
|
|
173
|
+
left: left.right,
|
|
174
|
+
right: right
|
|
175
|
+
}, precision);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (node.right.value < 0) {
|
|
179
|
+
node.right.value *= -1;
|
|
180
|
+
node.operator = node.operator === '-' ? '+' : '-';
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return reduce(node, precision);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (left.type === 'MathExpression' && right.type === 'MathExpression' && op === '-' && right.operator === '-') {
|
|
188
|
+
node.right.operator = flip(node.right.operator);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return node;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function reduceDivisionExpression(node) {
|
|
195
|
+
if (!isValueType(node.right.type)) return node;
|
|
196
|
+
if (node.right.type !== 'Value') throw new Error(`Cannot divide by "${node.right.unit}", number expected`);
|
|
197
|
+
if (node.right.value === 0) throw new Error('Cannot divide by zero'); // something / value
|
|
198
|
+
|
|
199
|
+
if (isValueType(node.left.type)) {
|
|
200
|
+
node.left.value /= node.right.value;
|
|
201
|
+
return node.left;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return node;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function reduceMultiplicationExpression(node) {
|
|
208
|
+
// (expr) * value
|
|
209
|
+
if (node.left.type === 'MathExpression' && node.right.type === 'Value') {
|
|
210
|
+
if (isValueType(node.left.left.type) && isValueType(node.left.right.type)) {
|
|
211
|
+
node.left.left.value *= node.right.value;
|
|
212
|
+
node.left.right.value *= node.right.value;
|
|
213
|
+
return node.left;
|
|
214
|
+
}
|
|
215
|
+
} // something * value
|
|
216
|
+
else if (isValueType(node.left.type) && node.right.type === 'Value') {
|
|
217
|
+
node.left.value *= node.right.value;
|
|
218
|
+
return node.left;
|
|
219
|
+
} // value * (expr)
|
|
220
|
+
else if (node.left.type === 'Value' && node.right.type === 'MathExpression') {
|
|
221
|
+
if (isValueType(node.right.left.type) && isValueType(node.right.right.type)) {
|
|
222
|
+
node.right.left.value *= node.left.value;
|
|
223
|
+
node.right.right.value *= node.left.value;
|
|
224
|
+
return node.right;
|
|
225
|
+
}
|
|
226
|
+
} // value * something
|
|
227
|
+
else if (node.left.type === 'Value' && isValueType(node.right.type)) {
|
|
228
|
+
node.right.value *= node.left.value;
|
|
229
|
+
return node.right;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return node;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function reduceMathExpression(node, precision) {
|
|
236
|
+
node = convertMathExpression(node, precision);
|
|
237
|
+
|
|
238
|
+
switch (node.operator) {
|
|
239
|
+
case "+":
|
|
240
|
+
case "-":
|
|
241
|
+
return reduceAddSubExpression(node, precision);
|
|
242
|
+
|
|
243
|
+
case "/":
|
|
244
|
+
return reduceDivisionExpression(node, precision);
|
|
245
|
+
|
|
246
|
+
case "*":
|
|
247
|
+
return reduceMultiplicationExpression(node);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return node;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
var _default = reduce;
|
|
254
|
+
exports.default = _default;
|
|
255
|
+
module.exports = exports.default;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = _default;
|
|
7
|
+
var order = {
|
|
8
|
+
"*": 0,
|
|
9
|
+
"/": 0,
|
|
10
|
+
"+": 1,
|
|
11
|
+
"-": 1
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function round(value, prec) {
|
|
15
|
+
if (prec !== false) {
|
|
16
|
+
var precision = Math.pow(10, prec);
|
|
17
|
+
return Math.round(value * precision) / precision;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function stringify(node, prec) {
|
|
24
|
+
switch (node.type) {
|
|
25
|
+
case "MathExpression":
|
|
26
|
+
{
|
|
27
|
+
var left = node.left,
|
|
28
|
+
right = node.right,
|
|
29
|
+
op = node.operator;
|
|
30
|
+
var str = "";
|
|
31
|
+
if (left.type === 'MathExpression' && order[op] < order[left.operator]) str += `(${stringify(left, prec)})`;else str += stringify(left, prec);
|
|
32
|
+
str += order[op] ? ` ${node.operator} ` : node.operator;
|
|
33
|
+
if (right.type === 'MathExpression' && order[op] < order[right.operator]) str += `(${stringify(right, prec)})`;else str += stringify(right, prec);
|
|
34
|
+
return str;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
case "Value":
|
|
38
|
+
return round(node.value, prec);
|
|
39
|
+
|
|
40
|
+
case 'Function':
|
|
41
|
+
return node.value;
|
|
42
|
+
|
|
43
|
+
default:
|
|
44
|
+
return round(node.value, prec) + node.unit;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function _default(calc, node, originalValue, options, result, item) {
|
|
49
|
+
var str = stringify(node, options.precision);
|
|
50
|
+
|
|
51
|
+
if (node.type === "MathExpression") {
|
|
52
|
+
// if calc expression couldn't be resolved to a single value, re-wrap it as
|
|
53
|
+
// a calc()
|
|
54
|
+
str = `${calc}(${str})`; // if the warnWhenCannotResolve option is on, inform the user that the calc
|
|
55
|
+
// expression could not be resolved to a single value
|
|
56
|
+
|
|
57
|
+
if (options.warnWhenCannotResolve) {
|
|
58
|
+
result.warn("Could not reduce expression: " + originalValue, {
|
|
59
|
+
plugin: 'postcss-calc',
|
|
60
|
+
node: item
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return str;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
module.exports = exports.default;
|
package/dist/lib/transform.js
CHANGED
|
@@ -1,58 +1,66 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.default = void 0;
|
|
6
7
|
|
|
7
|
-
var _postcssSelectorParser = require(
|
|
8
|
+
var _postcssSelectorParser = _interopRequireDefault(require("postcss-selector-parser"));
|
|
8
9
|
|
|
9
|
-
var
|
|
10
|
+
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
|
10
11
|
|
|
11
|
-
var
|
|
12
|
+
var _parser = require("../parser");
|
|
12
13
|
|
|
13
|
-
var
|
|
14
|
+
var _reducer = _interopRequireDefault(require("./reducer"));
|
|
15
|
+
|
|
16
|
+
var _stringifier = _interopRequireDefault(require("./stringifier"));
|
|
14
17
|
|
|
15
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
19
|
|
|
17
|
-
|
|
20
|
+
// eslint-disable-next-line import/no-unresolved
|
|
21
|
+
var MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i;
|
|
18
22
|
|
|
19
23
|
function transformValue(value, options, result, item) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
return (0, _postcssValueParser.default)(value).walk(function (node) {
|
|
25
|
+
// skip anything which isn't a calc() function
|
|
26
|
+
if (node.type !== 'function' || !MATCH_CALC.test(node.value)) return node; // stringify calc expression and produce an AST
|
|
27
|
+
|
|
28
|
+
var contents = _postcssValueParser.default.stringify(node.nodes);
|
|
29
|
+
|
|
30
|
+
var ast = _parser.parser.parse(contents); // reduce AST to its simplest form, that is, either to a single value
|
|
31
|
+
// or a simplified calc expression
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
var reducedAst = (0, _reducer.default)(ast, options.precision, item); // stringify AST and write it back
|
|
35
|
+
|
|
36
|
+
node.type = 'word';
|
|
37
|
+
node.value = (0, _stringifier.default)(node.value, reducedAst, value, options, result, item);
|
|
38
|
+
}, true).toString();
|
|
28
39
|
}
|
|
29
40
|
|
|
30
41
|
function transformSelector(value, options, result, item) {
|
|
31
|
-
return (0,
|
|
42
|
+
return (0, _postcssSelectorParser.default)(function (selectors) {
|
|
32
43
|
selectors.walk(function (node) {
|
|
33
44
|
// attribute value
|
|
34
45
|
// e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
|
|
35
|
-
if (node.type === 'attribute') {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// tag value
|
|
46
|
+
if (node.type === 'attribute' && node.value) {
|
|
47
|
+
node.setValue(transformValue(node.value, options, result, item));
|
|
48
|
+
} // tag value
|
|
41
49
|
// e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
|
|
42
|
-
if (node.type === 'tag') node.value = transformValue(node.value, options, result, item);
|
|
43
50
|
|
|
51
|
+
|
|
52
|
+
if (node.type === 'tag') node.value = transformValue(node.value, options, result, item);
|
|
44
53
|
return;
|
|
45
54
|
});
|
|
46
|
-
}).
|
|
55
|
+
}).processSync(value);
|
|
47
56
|
}
|
|
48
57
|
|
|
49
|
-
|
|
50
|
-
var value = property === "selector" ? transformSelector(node[property], options, result, node) : transformValue(node[property], options, result, node);
|
|
51
|
-
|
|
52
|
-
// if the preserve option is enabled and the value has changed, write the
|
|
58
|
+
var _default = function _default(node, property, options, result) {
|
|
59
|
+
var 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
|
|
53
60
|
// transformed value into a cloned node which is inserted before the current
|
|
54
61
|
// node, preserving the original value. Otherwise, overwrite the original
|
|
55
62
|
// value.
|
|
63
|
+
|
|
56
64
|
if (options.preserve && node[property] !== value) {
|
|
57
65
|
var clone = node.clone();
|
|
58
66
|
clone[property] = value;
|
|
@@ -60,4 +68,5 @@ exports.default = function (node, property, options, result) {
|
|
|
60
68
|
} else node[property] = value;
|
|
61
69
|
};
|
|
62
70
|
|
|
63
|
-
|
|
71
|
+
exports.default = _default;
|
|
72
|
+
module.exports = exports.default;
|