postcss-calc 6.0.2 → 7.0.3
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 +38 -0
- package/README.md +34 -16
- package/dist/index.js +19 -11
- package/dist/lib/convertUnit.js +157 -0
- package/dist/lib/reducer.js +340 -0
- package/dist/lib/stringifier.js +81 -0
- package/dist/lib/transform.js +45 -31
- package/dist/parser.js +3804 -0
- package/package.json +32 -25
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
|
|
32
|
+
if (left.type === 'MathExpression' && order[op] < order[left.operator]) {
|
|
33
|
+
str += `(${stringify(left, prec)})`;
|
|
34
|
+
} else {
|
|
35
|
+
str += stringify(left, prec);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
str += order[op] ? ` ${node.operator} ` : node.operator;
|
|
39
|
+
|
|
40
|
+
if (right.type === 'MathExpression' && order[op] < order[right.operator]) {
|
|
41
|
+
str += `(${stringify(right, prec)})`;
|
|
42
|
+
} else {
|
|
43
|
+
str += stringify(right, prec);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return str;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
case 'Number':
|
|
50
|
+
return round(node.value, prec);
|
|
51
|
+
|
|
52
|
+
case 'Function':
|
|
53
|
+
return node.value;
|
|
54
|
+
|
|
55
|
+
default:
|
|
56
|
+
return round(node.value, prec) + node.unit;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function _default(calc, node, originalValue, options, result, item) {
|
|
61
|
+
var str = stringify(node, options.precision);
|
|
62
|
+
var shouldPrintCalc = node.type === "MathExpression" || node.type === "Function";
|
|
63
|
+
|
|
64
|
+
if (shouldPrintCalc) {
|
|
65
|
+
// if calc expression couldn't be resolved to a single value, re-wrap it as
|
|
66
|
+
// a calc()
|
|
67
|
+
str = `${calc}(${str})`; // if the warnWhenCannotResolve option is on, inform the user that the calc
|
|
68
|
+
// expression could not be resolved to a single value
|
|
69
|
+
|
|
70
|
+
if (options.warnWhenCannotResolve) {
|
|
71
|
+
result.warn("Could not reduce expression: " + originalValue, {
|
|
72
|
+
plugin: 'postcss-calc',
|
|
73
|
+
node: item
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return str;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
module.exports = exports.default;
|
package/dist/lib/transform.js
CHANGED
|
@@ -1,67 +1,81 @@
|
|
|
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
|
-
|
|
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)) {
|
|
27
|
+
return node;
|
|
28
|
+
} // stringify calc expression and produce an AST
|
|
23
29
|
|
|
24
|
-
var reduced = (0, _reduceCssCalc2.default)(value, options.precision);
|
|
25
|
-
// if the warnWhenCannotResolve option is on, inform the user that the calc
|
|
26
|
-
// expression could not be resolved to a single value
|
|
27
|
-
if (options.warnWhenCannotResolve && MATCH_CALC.test(reduced)) {
|
|
28
|
-
result.warn("Could not reduce expression: " + value, { plugin: 'postcss-calc', node: item });
|
|
29
|
-
}
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
var contents = _postcssValueParser.default.stringify(node.nodes);
|
|
32
|
+
|
|
33
|
+
var ast = _parser.parser.parse(contents); // reduce AST to its simplest form, that is, either to a single value
|
|
34
|
+
// or a simplified calc expression
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
var reducedAst = (0, _reducer.default)(ast, options.precision); // stringify AST and write it back
|
|
38
|
+
|
|
39
|
+
node.type = 'word';
|
|
40
|
+
node.value = (0, _stringifier.default)(node.value, reducedAst, value, options, result, item);
|
|
41
|
+
return false;
|
|
42
|
+
}).toString();
|
|
32
43
|
}
|
|
33
44
|
|
|
34
45
|
function transformSelector(value, options, result, item) {
|
|
35
|
-
return (0,
|
|
46
|
+
return (0, _postcssSelectorParser.default)(function (selectors) {
|
|
36
47
|
selectors.walk(function (node) {
|
|
37
48
|
// attribute value
|
|
38
49
|
// e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
|
|
39
|
-
if (node.type === 'attribute') {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// tag value
|
|
50
|
+
if (node.type === 'attribute' && node.value) {
|
|
51
|
+
node.setValue(transformValue(node.value, options, result, item));
|
|
52
|
+
} // tag value
|
|
45
53
|
// e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
|
|
46
|
-
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
if (node.type === 'tag') {
|
|
57
|
+
node.value = transformValue(node.value, options, result, item);
|
|
58
|
+
}
|
|
47
59
|
|
|
48
60
|
return;
|
|
49
61
|
});
|
|
50
|
-
}).
|
|
62
|
+
}).processSync(value);
|
|
51
63
|
}
|
|
52
64
|
|
|
53
|
-
|
|
54
|
-
var value = property === "selector" ? transformSelector(node[property], options, result, node) : transformValue(node[property], options, result, node);
|
|
55
|
-
|
|
56
|
-
// if the preserve option is enabled and the value has changed, write the
|
|
65
|
+
var _default = function _default(node, property, options, result) {
|
|
66
|
+
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
|
|
57
67
|
// transformed value into a cloned node which is inserted before the current
|
|
58
68
|
// node, preserving the original value. Otherwise, overwrite the original
|
|
59
69
|
// value.
|
|
70
|
+
|
|
60
71
|
if (options.preserve && node[property] !== value) {
|
|
61
72
|
var clone = node.clone();
|
|
62
73
|
clone[property] = value;
|
|
63
74
|
node.parent.insertBefore(node, clone);
|
|
64
|
-
} else
|
|
75
|
+
} else {
|
|
76
|
+
node[property] = value;
|
|
77
|
+
}
|
|
65
78
|
};
|
|
66
79
|
|
|
67
|
-
|
|
80
|
+
exports.default = _default;
|
|
81
|
+
module.exports = exports.default;
|