postcss-calc 8.2.3 → 8.2.4
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/package.json +19 -16
- package/src/__tests__/convertUnit.js +421 -0
- package/src/__tests__/index.js +903 -0
- package/src/index.js +51 -0
- package/src/lib/convertUnit.js +160 -0
- package/{dist → src}/lib/reducer.js +108 -142
- package/{dist → src}/lib/stringifier.js +36 -51
- package/src/lib/transform.js +109 -0
- package/src/parser.d.ts +51 -0
- package/src/parser.jison +111 -0
- package/{dist → src}/parser.js +0 -0
- package/types/index.d.ts +14 -13
- package/types/lib/convertUnit.d.ts +1 -1
- package/types/lib/reducer.d.ts +8 -5
- package/types/lib/stringifier.d.ts +2 -11
- package/types/lib/transform.d.ts +2 -2
- package/dist/index.js +0 -64
- package/dist/lib/convertUnit.js +0 -167
- package/dist/lib/transform.js +0 -119
|
@@ -1,77 +1,64 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = _default;
|
|
1
|
+
'use strict';
|
|
7
2
|
const order = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
'*': 0,
|
|
4
|
+
'/': 0,
|
|
5
|
+
'+': 1,
|
|
6
|
+
'-': 1,
|
|
12
7
|
};
|
|
8
|
+
|
|
13
9
|
/**
|
|
14
10
|
* @param {number} value
|
|
15
11
|
* @param {number | false} prec
|
|
16
12
|
*/
|
|
17
|
-
|
|
18
13
|
function round(value, prec) {
|
|
19
14
|
if (prec !== false) {
|
|
20
15
|
const precision = Math.pow(10, prec);
|
|
21
16
|
return Math.round(value * precision) / precision;
|
|
22
17
|
}
|
|
23
|
-
|
|
24
18
|
return value;
|
|
25
19
|
}
|
|
20
|
+
|
|
26
21
|
/**
|
|
27
22
|
* @param {number | false} prec
|
|
28
23
|
* @param {import('../parser').CalcNode} node
|
|
29
24
|
*
|
|
30
25
|
* @return {string}
|
|
31
26
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
27
|
function stringify(node, prec) {
|
|
35
28
|
switch (node.type) {
|
|
36
|
-
case
|
|
37
|
-
{
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (left.type === 'MathExpression' && order[op] < order[left.operator]) {
|
|
46
|
-
str += `(${stringify(left, prec)})`;
|
|
47
|
-
} else {
|
|
48
|
-
str += stringify(left, prec);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
str += order[op] ? ` ${node.operator} ` : node.operator;
|
|
29
|
+
case 'MathExpression': {
|
|
30
|
+
const { left, right, operator: op } = node;
|
|
31
|
+
let str = '';
|
|
32
|
+
if (left.type === 'MathExpression' && order[op] < order[left.operator]) {
|
|
33
|
+
str += `(${stringify(left, prec)})`;
|
|
34
|
+
} else {
|
|
35
|
+
str += stringify(left, prec);
|
|
36
|
+
}
|
|
52
37
|
|
|
53
|
-
|
|
54
|
-
str += `(${stringify(right, prec)})`;
|
|
55
|
-
} else {
|
|
56
|
-
str += stringify(right, prec);
|
|
57
|
-
}
|
|
38
|
+
str += order[op] ? ` ${node.operator} ` : node.operator;
|
|
58
39
|
|
|
59
|
-
|
|
40
|
+
if (
|
|
41
|
+
right.type === 'MathExpression' &&
|
|
42
|
+
order[op] < order[right.operator]
|
|
43
|
+
) {
|
|
44
|
+
str += `(${stringify(right, prec)})`;
|
|
45
|
+
} else {
|
|
46
|
+
str += stringify(right, prec);
|
|
60
47
|
}
|
|
61
48
|
|
|
49
|
+
return str;
|
|
50
|
+
}
|
|
62
51
|
case 'Number':
|
|
63
52
|
return round(node.value, prec).toString();
|
|
64
|
-
|
|
65
53
|
case 'Function':
|
|
66
54
|
return node.value.toString();
|
|
67
|
-
|
|
68
55
|
case 'ParenthesizedExpression':
|
|
69
56
|
return `(${stringify(node.content, prec)})`;
|
|
70
|
-
|
|
71
57
|
default:
|
|
72
58
|
return round(node.value, prec) + node.unit;
|
|
73
59
|
}
|
|
74
60
|
}
|
|
61
|
+
|
|
75
62
|
/**
|
|
76
63
|
* @param {string} calc
|
|
77
64
|
* @param {import('../parser').CalcNode} node
|
|
@@ -82,27 +69,25 @@ function stringify(node, prec) {
|
|
|
82
69
|
*
|
|
83
70
|
* @returns {string}
|
|
84
71
|
*/
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
function _default(calc, node, originalValue, options, result, item) {
|
|
72
|
+
module.exports = function (calc, node, originalValue, options, result, item) {
|
|
88
73
|
let str = stringify(node, options.precision);
|
|
89
|
-
|
|
74
|
+
|
|
75
|
+
const shouldPrintCalc =
|
|
76
|
+
node.type === 'MathExpression' || node.type === 'Function';
|
|
90
77
|
|
|
91
78
|
if (shouldPrintCalc) {
|
|
92
79
|
// if calc expression couldn't be resolved to a single value, re-wrap it as
|
|
93
80
|
// a calc()
|
|
94
|
-
str = `${calc}(${str})`;
|
|
95
|
-
// expression could not be resolved to a single value
|
|
81
|
+
str = `${calc}(${str})`;
|
|
96
82
|
|
|
83
|
+
// if the warnWhenCannotResolve option is on, inform the user that the calc
|
|
84
|
+
// expression could not be resolved to a single value
|
|
97
85
|
if (options.warnWhenCannotResolve) {
|
|
98
|
-
result.warn(
|
|
86
|
+
result.warn('Could not reduce expression: ' + originalValue, {
|
|
99
87
|
plugin: 'postcss-calc',
|
|
100
|
-
node: item
|
|
88
|
+
node: item,
|
|
101
89
|
});
|
|
102
90
|
}
|
|
103
91
|
}
|
|
104
|
-
|
|
105
92
|
return str;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
module.exports = exports.default;
|
|
93
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
| 'VwValue'
|
|
27
|
+
| 'VminValue'
|
|
28
|
+
| 'VmaxValue';
|
|
29
|
+
value: number;
|
|
30
|
+
unit: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface NumberExpression {
|
|
34
|
+
type: 'Number';
|
|
35
|
+
value: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface FunctionExpression {
|
|
39
|
+
type: 'Function';
|
|
40
|
+
value: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type ValueExpression = DimensionExpression | NumberExpression;
|
|
44
|
+
|
|
45
|
+
export type CalcNode = MathExpression | ValueExpression | FunctionExpression | ParenthesizedExpression;
|
|
46
|
+
|
|
47
|
+
export interface Parser {
|
|
48
|
+
parse: (arg: string) => CalcNode;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const parser: Parser;
|
package/src/parser.jison
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/* description: Parses expressions. */
|
|
2
|
+
|
|
3
|
+
/* lexical grammar */
|
|
4
|
+
%lex
|
|
5
|
+
|
|
6
|
+
%options case-insensitive
|
|
7
|
+
|
|
8
|
+
%%
|
|
9
|
+
\s+ /* skip whitespace */
|
|
10
|
+
|
|
11
|
+
(\-(webkit|moz)\-)?calc\b return 'CALC';
|
|
12
|
+
|
|
13
|
+
[a-z][a-z0-9-]*\s*\((?:(?:\"(?:\\.|[^\"\\])*\"|\'(?:\\.|[^\'\\])*\')|\([^)]*\)|[^\(\)]*)*\) return 'FUNCTION';
|
|
14
|
+
|
|
15
|
+
"*" return 'MUL';
|
|
16
|
+
"/" return 'DIV';
|
|
17
|
+
"+" return 'ADD';
|
|
18
|
+
"-" return 'SUB';
|
|
19
|
+
|
|
20
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)em\b return 'EMS';
|
|
21
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)ex\b return 'EXS';
|
|
22
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)ch\b return 'CHS';
|
|
23
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)rem\b return 'REMS';
|
|
24
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vw\b return 'VWS';
|
|
25
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vh\b return 'VHS';
|
|
26
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vmin\b return 'VMINS';
|
|
27
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)vmax\b return 'VMAXS';
|
|
28
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)cm\b return 'LENGTH';
|
|
29
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)mm\b return 'LENGTH';
|
|
30
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)Q\b return 'LENGTH';
|
|
31
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)in\b return 'LENGTH';
|
|
32
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)pt\b return 'LENGTH';
|
|
33
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)pc\b return 'LENGTH';
|
|
34
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)px\b return 'LENGTH';
|
|
35
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)deg\b return 'ANGLE';
|
|
36
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)grad\b return 'ANGLE';
|
|
37
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)rad\b return 'ANGLE';
|
|
38
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)turn\b return 'ANGLE';
|
|
39
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)s\b return 'TIME';
|
|
40
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)ms\b return 'TIME';
|
|
41
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)Hz\b return 'FREQ';
|
|
42
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)kHz\b return 'FREQ';
|
|
43
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)dpi\b return 'RES';
|
|
44
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)dpcm\b return 'RES';
|
|
45
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)dppx\b return 'RES';
|
|
46
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)\% return 'PERCENTAGE';
|
|
47
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)\b return 'NUMBER';
|
|
48
|
+
|
|
49
|
+
(([0-9]+("."[0-9]+)?|"."[0-9]+)(e(\+|-)[0-9]+)?)-?([a-zA-Z_]|[\240-\377]|(\\[0-9a-fA-F]{1,6}(\r\n|[ \t\r\n\f])?|\\[^\r\n\f0-9a-fA-F]))([a-zA-Z0-9_-]|[\240-\377]|(\\[0-9a-fA-F]{1,6}(\r\n|[ \t\r\n\f])?|\\[^\r\n\f0-9a-fA-F]))*\b return 'UNKNOWN_DIMENSION';
|
|
50
|
+
|
|
51
|
+
"(" return 'LPAREN';
|
|
52
|
+
")" return 'RPAREN';
|
|
53
|
+
|
|
54
|
+
<<EOF>> return 'EOF';
|
|
55
|
+
|
|
56
|
+
/lex
|
|
57
|
+
|
|
58
|
+
%left ADD SUB
|
|
59
|
+
%left MUL DIV
|
|
60
|
+
%left UPREC
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
%start expression
|
|
64
|
+
|
|
65
|
+
%%
|
|
66
|
+
|
|
67
|
+
expression
|
|
68
|
+
: math_expression EOF { return $1; }
|
|
69
|
+
;
|
|
70
|
+
|
|
71
|
+
math_expression
|
|
72
|
+
: CALC LPAREN math_expression RPAREN { $$ = $3; }
|
|
73
|
+
| math_expression ADD math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
|
|
74
|
+
| math_expression SUB math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
|
|
75
|
+
| math_expression MUL math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
|
|
76
|
+
| math_expression DIV math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
|
|
77
|
+
| LPAREN math_expression RPAREN { $$ = { type: 'ParenthesizedExpression', content: $2 }; }
|
|
78
|
+
| function { $$ = $1; }
|
|
79
|
+
| dimension { $$ = $1; }
|
|
80
|
+
| number { $$ = $1; }
|
|
81
|
+
;
|
|
82
|
+
|
|
83
|
+
function
|
|
84
|
+
: FUNCTION { $$ = { type: 'Function', value: $1 }; }
|
|
85
|
+
;
|
|
86
|
+
|
|
87
|
+
dimension
|
|
88
|
+
: LENGTH { $$ = { type: 'LengthValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
|
|
89
|
+
| ANGLE { $$ = { type: 'AngleValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
|
|
90
|
+
| TIME { $$ = { type: 'TimeValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
|
|
91
|
+
| FREQ { $$ = { type: 'FrequencyValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
|
|
92
|
+
| RES { $$ = { type: 'ResolutionValue', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
|
|
93
|
+
| UNKNOWN_DIMENSION { $$ = { type: 'UnknownDimension', value: parseFloat($1), unit: /[a-z]+$/i.exec($1)[0] }; }
|
|
94
|
+
| EMS { $$ = { type: 'EmValue', value: parseFloat($1), unit: 'em' }; }
|
|
95
|
+
| EXS { $$ = { type: 'ExValue', value: parseFloat($1), unit: 'ex' }; }
|
|
96
|
+
| CHS { $$ = { type: 'ChValue', value: parseFloat($1), unit: 'ch' }; }
|
|
97
|
+
| REMS { $$ = { type: 'RemValue', value: parseFloat($1), unit: 'rem' }; }
|
|
98
|
+
| VHS { $$ = { type: 'VhValue', value: parseFloat($1), unit: 'vh' }; }
|
|
99
|
+
| VWS { $$ = { type: 'VwValue', value: parseFloat($1), unit: 'vw' }; }
|
|
100
|
+
| VMINS { $$ = { type: 'VminValue', value: parseFloat($1), unit: 'vmin' }; }
|
|
101
|
+
| VMAXS { $$ = { type: 'VmaxValue', value: parseFloat($1), unit: 'vmax' }; }
|
|
102
|
+
| PERCENTAGE { $$ = { type: 'PercentageValue', value: parseFloat($1), unit: '%' }; }
|
|
103
|
+
| ADD dimension { var prev = $2; $$ = prev; }
|
|
104
|
+
| SUB dimension { var prev = $2; prev.value *= -1; $$ = prev; }
|
|
105
|
+
;
|
|
106
|
+
|
|
107
|
+
number
|
|
108
|
+
: NUMBER { $$ = { type: 'Number', value: parseFloat($1) }; }
|
|
109
|
+
| ADD NUMBER { $$ = { type: 'Number', value: parseFloat($2) }; }
|
|
110
|
+
| SUB NUMBER { $$ = { type: 'Number', value: parseFloat($2) * -1 }; }
|
|
111
|
+
;
|
package/{dist → src}/parser.js
RENAMED
|
File without changes
|
package/types/index.d.ts
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
export type PostCssCalcOptions = {
|
|
3
|
-
precision?: number | false;
|
|
4
|
-
preserve?: boolean;
|
|
5
|
-
warnWhenCannotResolve?: boolean;
|
|
6
|
-
mediaQueries?: boolean;
|
|
7
|
-
selectors?: boolean;
|
|
8
|
-
};
|
|
1
|
+
export = pluginCreator;
|
|
9
2
|
/**
|
|
10
3
|
* @typedef {{precision?: number | false,
|
|
11
4
|
* preserve?: boolean,
|
|
@@ -14,11 +7,19 @@ export type PostCssCalcOptions = {
|
|
|
14
7
|
* selectors?: boolean}} PostCssCalcOptions
|
|
15
8
|
*/
|
|
16
9
|
/**
|
|
17
|
-
* @type {import('postcss').PluginCreator<PostCssCalcOptions>}
|
|
18
|
-
* @param {PostCssCalcOptions} opts
|
|
19
|
-
* @return {import('postcss').Plugin}
|
|
20
|
-
*/
|
|
10
|
+
* @type {import('postcss').PluginCreator<PostCssCalcOptions>}
|
|
11
|
+
* @param {PostCssCalcOptions} opts
|
|
12
|
+
* @return {import('postcss').Plugin}
|
|
13
|
+
*/
|
|
21
14
|
declare function pluginCreator(opts: PostCssCalcOptions): import('postcss').Plugin;
|
|
22
15
|
declare namespace pluginCreator {
|
|
23
|
-
|
|
16
|
+
export { postcss, PostCssCalcOptions };
|
|
24
17
|
}
|
|
18
|
+
type PostCssCalcOptions = {
|
|
19
|
+
precision?: number | false;
|
|
20
|
+
preserve?: boolean;
|
|
21
|
+
warnWhenCannotResolve?: boolean;
|
|
22
|
+
mediaQueries?: boolean;
|
|
23
|
+
selectors?: boolean;
|
|
24
|
+
};
|
|
25
|
+
declare var postcss: true;
|
package/types/lib/reducer.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
export
|
|
2
|
-
export type Collectible = {
|
|
3
|
-
preOperator: '+' | '-';
|
|
4
|
-
node: import('../parser').CalcNode;
|
|
5
|
-
};
|
|
1
|
+
export = reduce;
|
|
6
2
|
/**
|
|
7
3
|
* @param {import('../parser').CalcNode} node
|
|
8
4
|
* @param {number} precision
|
|
9
5
|
* @return {import('../parser').CalcNode}
|
|
10
6
|
*/
|
|
11
7
|
declare function reduce(node: import('../parser').CalcNode, precision: number): import('../parser').CalcNode;
|
|
8
|
+
declare namespace reduce {
|
|
9
|
+
export { Collectible };
|
|
10
|
+
}
|
|
11
|
+
type Collectible = {
|
|
12
|
+
preOperator: '+' | '-';
|
|
13
|
+
node: import('../parser').CalcNode;
|
|
14
|
+
};
|
|
@@ -1,14 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
* @param {string} calc
|
|
3
|
-
* @param {import('../parser').CalcNode} node
|
|
4
|
-
* @param {string} originalValue
|
|
5
|
-
* @param {{precision: number | false, warnWhenCannotResolve: boolean}} options
|
|
6
|
-
* @param {import("postcss").Result} result
|
|
7
|
-
* @param {import("postcss").ChildNode} item
|
|
8
|
-
*
|
|
9
|
-
* @returns {string}
|
|
10
|
-
*/
|
|
11
|
-
export default function _default(calc: string, node: import('../parser').CalcNode, originalValue: string, options: {
|
|
1
|
+
declare function _exports(calc: string, node: import('../parser').CalcNode, originalValue: string, options: {
|
|
12
2
|
precision: number | false;
|
|
13
3
|
warnWhenCannotResolve: boolean;
|
|
14
4
|
}, result: import("postcss").Result, item: import("postcss").ChildNode): string;
|
|
5
|
+
export = _exports;
|
package/types/lib/transform.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
declare function
|
|
1
|
+
declare function _exports(node: any, property: 'value' | 'params' | 'selector', options: {
|
|
2
2
|
precision: number;
|
|
3
3
|
preserve: boolean;
|
|
4
4
|
warnWhenCannotResolve: boolean;
|
|
5
5
|
}, result: import("postcss").Result): void;
|
|
6
|
-
export
|
|
6
|
+
export = _exports;
|
package/dist/index.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _transform = _interopRequireDefault(require("./lib/transform"));
|
|
9
|
-
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @typedef {{precision?: number | false,
|
|
14
|
-
* preserve?: boolean,
|
|
15
|
-
* warnWhenCannotResolve?: boolean,
|
|
16
|
-
* mediaQueries?: boolean,
|
|
17
|
-
* selectors?: boolean}} PostCssCalcOptions
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @type {import('postcss').PluginCreator<PostCssCalcOptions>}
|
|
22
|
-
* @param {PostCssCalcOptions} opts
|
|
23
|
-
* @return {import('postcss').Plugin}
|
|
24
|
-
*/
|
|
25
|
-
function pluginCreator(opts) {
|
|
26
|
-
const options = Object.assign({
|
|
27
|
-
precision: 5,
|
|
28
|
-
preserve: false,
|
|
29
|
-
warnWhenCannotResolve: false,
|
|
30
|
-
mediaQueries: false,
|
|
31
|
-
selectors: false
|
|
32
|
-
}, opts);
|
|
33
|
-
return {
|
|
34
|
-
postcssPlugin: 'postcss-calc',
|
|
35
|
-
|
|
36
|
-
OnceExit(css, {
|
|
37
|
-
result
|
|
38
|
-
}) {
|
|
39
|
-
css.walk(node => {
|
|
40
|
-
const {
|
|
41
|
-
type
|
|
42
|
-
} = node;
|
|
43
|
-
|
|
44
|
-
if (type === 'decl') {
|
|
45
|
-
(0, _transform.default)(node, "value", options, result);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (type === 'atrule' && options.mediaQueries) {
|
|
49
|
-
(0, _transform.default)(node, "params", options, result);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (type === 'rule' && options.selectors) {
|
|
53
|
-
(0, _transform.default)(node, "selector", options, result);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
pluginCreator.postcss = true;
|
|
62
|
-
var _default = pluginCreator;
|
|
63
|
-
exports.default = _default;
|
|
64
|
-
module.exports = exports.default;
|