1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | var _postcssSelectorParser = _interopRequireDefault(require("postcss-selector-parser"));
|
---|
9 |
|
---|
10 | var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
---|
11 |
|
---|
12 | var _parser = require("../parser");
|
---|
13 |
|
---|
14 | var _reducer = _interopRequireDefault(require("./reducer"));
|
---|
15 |
|
---|
16 | var _stringifier = _interopRequireDefault(require("./stringifier"));
|
---|
17 |
|
---|
18 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
19 |
|
---|
20 | // eslint-disable-next-line import/no-unresolved
|
---|
21 | const MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i;
|
---|
22 |
|
---|
23 | function transformValue(value, options, result, item) {
|
---|
24 | return (0, _postcssValueParser.default)(value).walk(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
|
---|
29 |
|
---|
30 |
|
---|
31 | const contents = _postcssValueParser.default.stringify(node.nodes);
|
---|
32 |
|
---|
33 | const 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 | const 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();
|
---|
43 | }
|
---|
44 |
|
---|
45 | function transformSelector(value, options, result, item) {
|
---|
46 | return (0, _postcssSelectorParser.default)(selectors => {
|
---|
47 | selectors.walk(node => {
|
---|
48 | // attribute value
|
---|
49 | // e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
|
---|
50 | if (node.type === 'attribute' && node.value) {
|
---|
51 | node.setValue(transformValue(node.value, options, result, item));
|
---|
52 | } // tag value
|
---|
53 | // e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
|
---|
54 |
|
---|
55 |
|
---|
56 | if (node.type === 'tag') {
|
---|
57 | node.value = transformValue(node.value, options, result, item);
|
---|
58 | }
|
---|
59 |
|
---|
60 | return;
|
---|
61 | });
|
---|
62 | }).processSync(value);
|
---|
63 | }
|
---|
64 |
|
---|
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 | // transformed value into a cloned node which is inserted before the current
|
---|
68 | // node, preserving the original value. Otherwise, overwrite the original
|
---|
69 | // value.
|
---|
70 |
|
---|
71 | if (options.preserve && node[property] !== value) {
|
---|
72 | const clone = node.clone();
|
---|
73 | clone[property] = value;
|
---|
74 | node.parent.insertBefore(node, clone);
|
---|
75 | } else {
|
---|
76 | node[property] = value;
|
---|
77 | }
|
---|
78 | };
|
---|
79 |
|
---|
80 | exports.default = _default;
|
---|
81 | module.exports = exports.default; |
---|