source: frontend/node_modules/postcss-calc/src/lib/transform.js@ cdcff72

Last change on this file since cdcff72 was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.2 KB
Line 
1'use strict';
2const selectorParser = require('postcss-selector-parser');
3const valueParser = require('postcss-value-parser');
4
5const { parser } = require('../parser.js');
6
7const reducer = require('./reducer.js');
8const stringifier = require('./stringifier.js');
9
10const MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i;
11
12/**
13 * @param {string} value
14 * @param {{precision: number, warnWhenCannotResolve: boolean}} options
15 * @param {import("postcss").Result} result
16 * @param {import("postcss").ChildNode} item
17 */
18function transformValue(value, options, result, item) {
19 return valueParser(value)
20 .walk((node) => {
21 // skip anything which isn't a calc() function
22 if (node.type !== 'function' || !MATCH_CALC.test(node.value)) {
23 return;
24 }
25
26 // stringify calc expression and produce an AST
27 const contents = valueParser.stringify(node.nodes);
28 const ast = parser.parse(contents);
29
30 // reduce AST to its simplest form, that is, either to a single value
31 // or a simplified calc expression
32 const reducedAst = reducer(ast, options.precision);
33
34 // stringify AST and write it back
35 /** @type {valueParser.Node} */ (node).type = 'word';
36 node.value = stringifier(
37 node.value,
38 reducedAst,
39 value,
40 options,
41 result,
42 item
43 );
44
45 return false;
46 })
47 .toString();
48}
49/**
50 * @param {import("postcss-selector-parser").Selectors} value
51 * @param {{precision: number, warnWhenCannotResolve: boolean}} options
52 * @param {import("postcss").Result} result
53 * @param {import("postcss").ChildNode} item
54 */
55function transformSelector(value, options, result, item) {
56 return selectorParser((selectors) => {
57 selectors.walk((node) => {
58 // attribute value
59 // e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
60 if (node.type === 'attribute' && node.value) {
61 node.setValue(transformValue(node.value, options, result, item));
62 }
63
64 // tag value
65 // e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
66 if (node.type === 'tag') {
67 node.value = transformValue(node.value, options, result, item);
68 }
69
70 return;
71 });
72 }).processSync(value);
73}
74
75/**
76 * @param {any} node
77 * @param {{precision: number, preserve: boolean, warnWhenCannotResolve: boolean}} options
78 * @param {'value'|'params'|'selector'} property
79 * @param {import("postcss").Result} result
80 */
81module.exports = (node, property, options, result) => {
82 let value = node[property];
83
84 try {
85 value =
86 property === 'selector'
87 ? transformSelector(node[property], options, result, node)
88 : transformValue(node[property], options, result, node);
89 } catch (error) {
90 if (error instanceof Error) {
91 result.warn(error.message, { node });
92 } else {
93 result.warn('Error', { node });
94 }
95 return;
96 }
97
98 // if the preserve option is enabled and the value has changed, write the
99 // transformed value into a cloned node which is inserted before the current
100 // node, preserving the original value. Otherwise, overwrite the original
101 // value.
102 if (options.preserve && node[property] !== value) {
103 const clone = node.clone();
104 clone[property] = value;
105 node.parent.insertBefore(node, clone);
106 } else {
107 node[property] = value;
108 }
109};
Note: See TracBrowser for help on using the repository browser.