postcss-calc 11.1.1 → 11.2.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 (47) hide show
  1. package/README.md +40 -18
  2. package/package.json +16 -11
  3. package/src/index.js +8 -5
  4. package/src/lib/analyze.js +228 -0
  5. package/src/lib/block-index.js +118 -0
  6. package/src/lib/calculation-type.js +25 -0
  7. package/src/lib/compile.js +80 -0
  8. package/src/lib/convertUnits.js +15 -1
  9. package/src/lib/functions.js +364 -0
  10. package/src/lib/limits.js +49 -0
  11. package/src/lib/node.js +85 -39
  12. package/src/lib/opaque.js +20 -0
  13. package/src/lib/parser.js +493 -257
  14. package/src/lib/print.js +75 -0
  15. package/src/lib/regex.js +4 -0
  16. package/src/lib/scan.js +61 -0
  17. package/src/lib/serialize.js +574 -207
  18. package/src/lib/simplify/abs.js +1 -1
  19. package/src/lib/simplify/bucket.js +18 -23
  20. package/src/lib/simplify/call.js +6 -92
  21. package/src/lib/simplify/product.js +7 -4
  22. package/src/lib/simplify/round.js +4 -4
  23. package/src/lib/simplify/sum.js +22 -10
  24. package/src/lib/simplify.js +20 -5
  25. package/src/reduce.js +46 -118
  26. package/types/index.d.ts +4 -0
  27. package/types/lib/analyze.d.ts +18 -0
  28. package/types/lib/block-index.d.ts +35 -0
  29. package/types/lib/calculation-type.d.ts +18 -0
  30. package/types/lib/compile.d.ts +37 -0
  31. package/types/lib/convertUnits.d.ts +8 -1
  32. package/types/lib/functions.d.ts +40 -0
  33. package/types/lib/limits.d.ts +8 -0
  34. package/types/lib/node.d.ts +31 -9
  35. package/types/lib/opaque.d.ts +9 -0
  36. package/types/lib/parser.d.ts +47 -49
  37. package/types/lib/print.d.ts +16 -0
  38. package/types/lib/regex.d.ts +2 -0
  39. package/types/lib/scan.d.ts +35 -0
  40. package/types/lib/serialize.d.ts +19 -2
  41. package/types/lib/simplify/bucket.d.ts +2 -2
  42. package/types/lib/simplify/call.d.ts +4 -18
  43. package/types/lib/simplify/round.d.ts +3 -1
  44. package/types/lib/simplify.d.ts +5 -2
  45. package/types/reduce.d.ts +47 -6
  46. package/src/lib/tokenizer.js +0 -139
  47. package/types/lib/tokenizer.d.ts +0 -32
@@ -1,139 +0,0 @@
1
- // Folds @csstools/css-tokenizer output into the calc() token subset.
2
-
3
- import {
4
- tokenize as tokenizeCss,
5
- TokenType as CssType,
6
- } from '@csstools/css-tokenizer';
7
- /**
8
- * @typedef {'number' | 'dimension' | 'ident' | 'punct' | 'eof'} TokenType
9
- * @typedef {object} Token
10
- * @property {TokenType} type
11
- * @property {string} value
12
- * @property {string} [unit] Present on `dimension` tokens; `%` for percentages.
13
- * @property {number} pos
14
- * @property {boolean} ws Whitespace immediately before — drives the §10.1 `+`/`-` rule.
15
- */
16
-
17
- const PUNCT_DELIMS = new Set(['+', '-', '*', '/']);
18
-
19
- const NUMERIC_RAW = /^[+-]?(?:\d+(?:\.\d+)?|\.\d+)(?:[eE][+-]?\d+)?/;
20
-
21
- /**
22
- * @param {string} input
23
- * @return {Token[]}
24
- */
25
- function tokenize(input) {
26
- return tokenizeTokens(tokenizeCss({ css: input }), input.length);
27
- }
28
-
29
- /**
30
- * CSS absorbs leading signs (`-5px` is one token); the parser expects
31
- * punct sign + unsigned numeric, so split them back out.
32
- * @param {Token[]} tokens
33
- * @param {string} raw
34
- * @param {string | undefined} unit
35
- * @param {number} pos
36
- * @param {boolean} ws
37
- * @return {void}
38
- */
39
- function pushNumeric(tokens, raw, unit, pos, ws) {
40
- let value = /** @type {RegExpExecArray} */ (NUMERIC_RAW.exec(raw))[0];
41
- const sign = value[0];
42
- if (sign === '+' || sign === '-') {
43
- tokens.push({ type: 'punct', value: sign, pos, ws });
44
- value = value.slice(1);
45
- pos += 1;
46
- ws = false;
47
- }
48
- if (unit === undefined) {
49
- tokens.push({ type: 'number', value, pos, ws });
50
- } else {
51
- tokens.push({ type: 'dimension', value, unit, pos, ws });
52
- }
53
- }
54
-
55
- /**
56
- * Convert a slice of an existing CSS token stream into the token subset used
57
- * by the calculation parser. Token positions remain relative to the original
58
- * source text, which keeps parse errors useful to adapter callers.
59
- *
60
- * @param {import('@csstools/css-tokenizer').CSSToken[]} cssTokens
61
- * @param {number} eofPosition
62
- * @param {number} [start]
63
- * @param {number} [end]
64
- * @return {Token[]}
65
- */
66
- function tokenizeTokens(
67
- cssTokens,
68
- eofPosition,
69
- start = 0,
70
- end = cssTokens.length
71
- ) {
72
- /** @type {Token[]} */
73
- const tokens = [];
74
- let ws = true;
75
-
76
- for (let i = start; i < end; i++) {
77
- const t = cssTokens[i];
78
- switch (t[0]) {
79
- case CssType.Whitespace:
80
- case CssType.Comment:
81
- ws = true;
82
- continue;
83
- case CssType.Number:
84
- pushNumeric(tokens, t[1], undefined, t[2], ws);
85
- ws = false;
86
- continue;
87
- case CssType.Dimension:
88
- pushNumeric(tokens, t[1], t[4].unit, t[2], ws);
89
- ws = false;
90
- continue;
91
- case CssType.Percentage:
92
- pushNumeric(tokens, t[1], '%', t[2], ws);
93
- ws = false;
94
- continue;
95
- case CssType.Ident:
96
- tokens.push({ type: 'ident', value: t[4].value, pos: t[2], ws });
97
- break;
98
- case CssType.Function:
99
- tokens.push({ type: 'ident', value: t[4].value, pos: t[2], ws });
100
- tokens.push({
101
- type: 'punct',
102
- value: '(',
103
- pos: t[2] + t[1].length - 1,
104
- ws: false,
105
- });
106
- break;
107
- case CssType.OpenParen:
108
- tokens.push({ type: 'punct', value: '(', pos: t[2], ws });
109
- break;
110
- case CssType.CloseParen:
111
- tokens.push({ type: 'punct', value: ')', pos: t[2], ws });
112
- break;
113
- case CssType.Comma:
114
- tokens.push({ type: 'punct', value: ',', pos: t[2], ws });
115
- break;
116
- case CssType.Delim:
117
- if (!PUNCT_DELIMS.has(t[4].value)) {
118
- throw new Error(
119
- `Unexpected character "${t[4].value}" at position ${t[2]}`
120
- );
121
- }
122
- tokens.push({ type: 'punct', value: t[4].value, pos: t[2], ws });
123
- break;
124
- case CssType.EOF:
125
- continue;
126
- default:
127
- throw new Error(
128
- `Unexpected character "${t[1][0] ?? ''}" at position ${t[2]}`
129
- );
130
- }
131
- ws = false;
132
- }
133
-
134
- tokens.push({ type: 'eof', value: '', pos: eofPosition, ws });
135
-
136
- return tokens;
137
- }
138
-
139
- export { tokenize, tokenizeTokens };
@@ -1,32 +0,0 @@
1
- export type TokenType = 'number' | 'dimension' | 'ident' | 'punct' | 'eof';
2
- export type Token = {
3
- type: TokenType;
4
- value: string;
5
- /**
6
- * Present on `dimension` tokens; `%` for percentages.
7
- */
8
- unit?: string;
9
- pos: number;
10
- /**
11
- * Whitespace immediately before — drives the §10.1 `+`/`-` rule.
12
- */
13
- ws: boolean;
14
- };
15
- /**
16
- * @param {string} input
17
- * @return {Token[]}
18
- */
19
- declare function tokenize(input: string): Token[];
20
- /**
21
- * Convert a slice of an existing CSS token stream into the token subset used
22
- * by the calculation parser. Token positions remain relative to the original
23
- * source text, which keeps parse errors useful to adapter callers.
24
- *
25
- * @param {import('@csstools/css-tokenizer').CSSToken[]} cssTokens
26
- * @param {number} eofPosition
27
- * @param {number} [start]
28
- * @param {number} [end]
29
- * @return {Token[]}
30
- */
31
- declare function tokenizeTokens(cssTokens: import('@csstools/css-tokenizer').CSSToken[], eofPosition: number, start?: number, end?: number): Token[];
32
- export { tokenize, tokenizeTokens };