postcss-calc 10.1.0 → 11.0.0-rc.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.
Files changed (67) hide show
  1. package/README.md +56 -1
  2. package/package.json +19 -13
  3. package/src/index.js +122 -27
  4. package/src/lib/convertUnits.js +105 -0
  5. package/src/lib/node.js +189 -0
  6. package/src/lib/parser.js +314 -0
  7. package/src/lib/serialize.js +226 -0
  8. package/src/lib/simplify/abs.js +21 -0
  9. package/src/lib/simplify/atan2.js +25 -0
  10. package/src/lib/simplify/bucket.js +48 -0
  11. package/src/lib/simplify/call.js +59 -0
  12. package/src/lib/simplify/cancel.js +39 -0
  13. package/src/lib/simplify/clamp.js +26 -0
  14. package/src/lib/simplify/exp.js +18 -0
  15. package/src/lib/simplify/fold.js +63 -0
  16. package/src/lib/simplify/hypot.js +22 -0
  17. package/src/lib/simplify/inverse-trig.js +29 -0
  18. package/src/lib/simplify/log.js +25 -0
  19. package/src/lib/simplify/min-max.js +23 -0
  20. package/src/lib/simplify/mod-rem.js +47 -0
  21. package/src/lib/simplify/pow.js +20 -0
  22. package/src/lib/simplify/product.js +133 -0
  23. package/src/lib/simplify/round.js +113 -0
  24. package/src/lib/simplify/sign.js +22 -0
  25. package/src/lib/simplify/sqrt.js +18 -0
  26. package/src/lib/simplify/sum.js +83 -0
  27. package/src/lib/simplify/trig.js +40 -0
  28. package/src/lib/simplify.js +40 -0
  29. package/src/lib/tokenizer.js +115 -0
  30. package/types/index.d.ts +15 -17
  31. package/types/lib/convertUnits.d.ts +16 -0
  32. package/types/lib/node.d.ts +90 -0
  33. package/types/lib/parser.d.ts +39 -0
  34. package/types/lib/serialize.d.ts +20 -0
  35. package/types/lib/simplify/abs.d.ts +7 -0
  36. package/types/lib/simplify/atan2.d.ts +7 -0
  37. package/types/lib/simplify/bucket.d.ts +19 -0
  38. package/types/lib/simplify/call.d.ts +12 -0
  39. package/types/lib/simplify/cancel.d.ts +19 -0
  40. package/types/lib/simplify/clamp.d.ts +7 -0
  41. package/types/lib/simplify/exp.d.ts +7 -0
  42. package/types/lib/simplify/fold.d.ts +12 -0
  43. package/types/lib/simplify/hypot.d.ts +7 -0
  44. package/types/lib/simplify/inverse-trig.d.ts +7 -0
  45. package/types/lib/simplify/log.d.ts +7 -0
  46. package/types/lib/simplify/min-max.d.ts +8 -0
  47. package/types/lib/simplify/mod-rem.d.ts +8 -0
  48. package/types/lib/simplify/pow.d.ts +7 -0
  49. package/types/lib/simplify/product.d.ts +16 -0
  50. package/types/lib/simplify/round.d.ts +8 -0
  51. package/types/lib/simplify/sign.d.ts +7 -0
  52. package/types/lib/simplify/sqrt.d.ts +7 -0
  53. package/types/lib/simplify/sum.d.ts +18 -0
  54. package/types/lib/simplify/trig.d.ts +7 -0
  55. package/types/lib/simplify.d.ts +18 -0
  56. package/types/lib/tokenizer.d.ts +19 -0
  57. package/types/lib/type.d.ts +16 -0
  58. package/src/lib/convertUnit.js +0 -160
  59. package/src/lib/reducer.js +0 -390
  60. package/src/lib/stringifier.js +0 -98
  61. package/src/lib/transform.js +0 -109
  62. package/src/parser.d.ts +0 -79
  63. package/src/parser.js +0 -4200
  64. package/types/lib/convertUnit.d.ts +0 -8
  65. package/types/lib/reducer.d.ts +0 -14
  66. package/types/lib/stringifier.d.ts +0 -5
  67. package/types/lib/transform.d.ts +0 -6
@@ -1,109 +0,0 @@
1
- 'use strict';
2
- const selectorParser = require('postcss-selector-parser');
3
- const valueParser = require('postcss-value-parser');
4
-
5
- const { parser } = require('../parser.js');
6
-
7
- const reducer = require('./reducer.js');
8
- const stringifier = require('./stringifier.js');
9
-
10
- const 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
- */
18
- function 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
- */
55
- function 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
- */
81
- module.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
- };
package/src/parser.d.ts DELETED
@@ -1,79 +0,0 @@
1
- export interface MathExpression {
2
- type: 'MathExpression';
3
- right: CalcNode;
4
- left: CalcNode;
5
- operator: '*' | '+' | '-' | '/';
6
- }
7
-
8
- export interface ParenthesizedExpression {
9
- type: 'ParenthesizedExpression';
10
- content: CalcNode;
11
- }
12
-
13
- export interface DimensionExpression {
14
- type:
15
- | 'LengthValue'
16
- | 'AngleValue'
17
- | 'TimeValue'
18
- | 'FrequencyValue'
19
- | 'PercentageValue'
20
- | 'ResolutionValue'
21
- | 'EmValue'
22
- | 'ExValue'
23
- | 'ChValue'
24
- | 'RemValue'
25
- | 'VhValue'
26
- | 'SvhValue'
27
- | 'LvhValue'
28
- | 'DvhValue'
29
- | 'VwValue'
30
- | 'SvwValue'
31
- | 'LvwValue'
32
- | 'DvwValue'
33
- | 'VminValue'
34
- | 'SvminValue'
35
- | 'LvminValue'
36
- | 'DvminValue'
37
- | 'VmaxValue'
38
- | 'SvmaxValue'
39
- | 'LvmaxValue'
40
- | 'DvmaxValue'
41
- | 'VbValue'
42
- | 'SvbValue'
43
- | 'LvbValue'
44
- | 'DvbValue'
45
- | 'ViValue'
46
- | 'SviValue'
47
- | 'LviValue'
48
- | 'DviValue'
49
- | 'CqwValue'
50
- | 'CqhValue'
51
- | 'CqbValue'
52
- | 'CqiValue'
53
- | 'CqminValue'
54
- | 'CqmaxValue'
55
- | 'LhValue'
56
- | 'RlhValue';
57
- value: number;
58
- unit: string;
59
- }
60
-
61
- export interface NumberExpression {
62
- type: 'Number';
63
- value: number;
64
- }
65
-
66
- export interface FunctionExpression {
67
- type: 'Function';
68
- value: string;
69
- }
70
-
71
- export type ValueExpression = DimensionExpression | NumberExpression;
72
-
73
- export type CalcNode = MathExpression | ValueExpression | FunctionExpression | ParenthesizedExpression;
74
-
75
- export interface Parser {
76
- parse: (arg: string) => CalcNode;
77
- }
78
-
79
- export const parser: Parser;