postcss-calc 7.0.5 → 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 +4 -0
- package/dist/index.js +30 -21
- package/dist/lib/convertUnit.js +4 -4
- package/dist/lib/reducer.js +18 -24
- package/dist/lib/stringifier.js +10 -8
- package/dist/lib/transform.js +10 -10
- package/package.json +9 -6
package/CHANGELOG.md
CHANGED
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
|
@@ -45,21 +45,20 @@ function collectAddSubItems(preOperator, node, collected, precision) {
|
|
|
45
45
|
throw new Error(`invalid operator ${preOperator}`);
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
const type = node.type;
|
|
49
49
|
|
|
50
50
|
if (isValueType(type)) {
|
|
51
|
-
|
|
52
|
-
return x.node.type === type;
|
|
53
|
-
});
|
|
51
|
+
const itemIndex = collected.findIndex(x => x.node.type === type);
|
|
54
52
|
|
|
55
53
|
if (itemIndex >= 0) {
|
|
56
54
|
if (node.value === 0) {
|
|
57
55
|
return;
|
|
58
56
|
}
|
|
59
57
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
58
|
+
const {
|
|
59
|
+
left: reducedNode,
|
|
60
|
+
right: current
|
|
61
|
+
} = covertNodesUnits(collected[itemIndex].node, node, precision);
|
|
63
62
|
|
|
64
63
|
if (collected[itemIndex].preOperator === '-') {
|
|
65
64
|
collected[itemIndex].preOperator = '+';
|
|
@@ -103,18 +102,17 @@ function collectAddSubItems(preOperator, node, collected, precision) {
|
|
|
103
102
|
} else if (type === "MathExpression") {
|
|
104
103
|
if (isAddSubOperator(node.operator)) {
|
|
105
104
|
collectAddSubItems(preOperator, node.left, collected, precision);
|
|
106
|
-
|
|
105
|
+
const collectRightOperator = preOperator === '-' ? flip(node.operator) : node.operator;
|
|
107
106
|
collectAddSubItems(collectRightOperator, node.right, collected, precision);
|
|
108
107
|
} else {
|
|
109
108
|
// * or /
|
|
110
|
-
|
|
111
|
-
|
|
109
|
+
const reducedNode = reduce(node, precision); // prevent infinite recursive call
|
|
112
110
|
|
|
113
|
-
if (
|
|
114
|
-
collectAddSubItems(preOperator,
|
|
111
|
+
if (reducedNode.type !== "MathExpression" || isAddSubOperator(reducedNode.operator)) {
|
|
112
|
+
collectAddSubItems(preOperator, reducedNode, collected, precision);
|
|
115
113
|
} else {
|
|
116
114
|
collected.push({
|
|
117
|
-
node:
|
|
115
|
+
node: reducedNode,
|
|
118
116
|
preOperator
|
|
119
117
|
});
|
|
120
118
|
}
|
|
@@ -128,19 +126,15 @@ function collectAddSubItems(preOperator, node, collected, precision) {
|
|
|
128
126
|
}
|
|
129
127
|
|
|
130
128
|
function reduceAddSubExpression(node, precision) {
|
|
131
|
-
|
|
129
|
+
const collected = [];
|
|
132
130
|
collectAddSubItems('+', node, collected, precision);
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
});
|
|
136
|
-
var firstNonZeroItem = withoutZeroItem[0]; // could be undefined
|
|
131
|
+
const withoutZeroItem = collected.filter(item => !(isValueType(item.node.type) && item.node.value === 0));
|
|
132
|
+
const firstNonZeroItem = withoutZeroItem[0]; // could be undefined
|
|
137
133
|
// prevent producing "calc(-var(--a))" or "calc()"
|
|
138
134
|
// which is invalid css
|
|
139
135
|
|
|
140
136
|
if (!firstNonZeroItem || firstNonZeroItem.preOperator === '-' && !isValueType(firstNonZeroItem.node.type)) {
|
|
141
|
-
|
|
142
|
-
return isValueType(item.node.type) && item.node.value === 0;
|
|
143
|
-
});
|
|
137
|
+
const firstZeroItem = collected.find(item => isValueType(item.node.type) && item.node.value === 0);
|
|
144
138
|
withoutZeroItem.unshift(firstZeroItem);
|
|
145
139
|
} // make sure the preOperator of the first item is +
|
|
146
140
|
|
|
@@ -150,9 +144,9 @@ function reduceAddSubExpression(node, precision) {
|
|
|
150
144
|
withoutZeroItem[0].preOperator = '+';
|
|
151
145
|
}
|
|
152
146
|
|
|
153
|
-
|
|
147
|
+
let root = withoutZeroItem[0].node;
|
|
154
148
|
|
|
155
|
-
for (
|
|
149
|
+
for (let i = 1; i < withoutZeroItem.length; i++) {
|
|
156
150
|
root = {
|
|
157
151
|
type: 'MathExpression',
|
|
158
152
|
operator: withoutZeroItem[i].preOperator,
|
|
@@ -270,7 +264,7 @@ function covertNodesUnits(left, right, precision) {
|
|
|
270
264
|
case 'FrequencyValue':
|
|
271
265
|
case 'ResolutionValue':
|
|
272
266
|
if (right.type === left.type && right.unit && left.unit) {
|
|
273
|
-
|
|
267
|
+
const converted = (0, _convertUnit.default)(right.value, right.unit, left.unit, precision);
|
|
274
268
|
right = {
|
|
275
269
|
type: left.type,
|
|
276
270
|
value: converted,
|
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/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",
|