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
package/dist/lib/convertUnit.js
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @type {{[key:string]: {[key:string]: number}}}
|
|
10
|
-
*/
|
|
11
|
-
const conversions = {
|
|
12
|
-
// Absolute length units
|
|
13
|
-
'px': {
|
|
14
|
-
'px': 1,
|
|
15
|
-
'cm': 96 / 2.54,
|
|
16
|
-
'mm': 96 / 25.4,
|
|
17
|
-
'q': 96 / 101.6,
|
|
18
|
-
'in': 96,
|
|
19
|
-
'pt': 96 / 72,
|
|
20
|
-
'pc': 16
|
|
21
|
-
},
|
|
22
|
-
'cm': {
|
|
23
|
-
'px': 2.54 / 96,
|
|
24
|
-
'cm': 1,
|
|
25
|
-
'mm': 0.1,
|
|
26
|
-
'q': 0.025,
|
|
27
|
-
'in': 2.54,
|
|
28
|
-
'pt': 2.54 / 72,
|
|
29
|
-
'pc': 2.54 / 6
|
|
30
|
-
},
|
|
31
|
-
'mm': {
|
|
32
|
-
'px': 25.4 / 96,
|
|
33
|
-
'cm': 10,
|
|
34
|
-
'mm': 1,
|
|
35
|
-
'q': 0.25,
|
|
36
|
-
'in': 25.4,
|
|
37
|
-
'pt': 25.4 / 72,
|
|
38
|
-
'pc': 25.4 / 6
|
|
39
|
-
},
|
|
40
|
-
'q': {
|
|
41
|
-
'px': 101.6 / 96,
|
|
42
|
-
'cm': 40,
|
|
43
|
-
'mm': 4,
|
|
44
|
-
'q': 1,
|
|
45
|
-
'in': 101.6,
|
|
46
|
-
'pt': 101.6 / 72,
|
|
47
|
-
'pc': 101.6 / 6
|
|
48
|
-
},
|
|
49
|
-
'in': {
|
|
50
|
-
'px': 1 / 96,
|
|
51
|
-
'cm': 1 / 2.54,
|
|
52
|
-
'mm': 1 / 25.4,
|
|
53
|
-
'q': 1 / 101.6,
|
|
54
|
-
'in': 1,
|
|
55
|
-
'pt': 1 / 72,
|
|
56
|
-
'pc': 1 / 6
|
|
57
|
-
},
|
|
58
|
-
'pt': {
|
|
59
|
-
'px': 0.75,
|
|
60
|
-
'cm': 72 / 2.54,
|
|
61
|
-
'mm': 72 / 25.4,
|
|
62
|
-
'q': 72 / 101.6,
|
|
63
|
-
'in': 72,
|
|
64
|
-
'pt': 1,
|
|
65
|
-
'pc': 12
|
|
66
|
-
},
|
|
67
|
-
'pc': {
|
|
68
|
-
'px': 0.0625,
|
|
69
|
-
'cm': 6 / 2.54,
|
|
70
|
-
'mm': 6 / 25.4,
|
|
71
|
-
'q': 6 / 101.6,
|
|
72
|
-
'in': 6,
|
|
73
|
-
'pt': 6 / 72,
|
|
74
|
-
'pc': 1
|
|
75
|
-
},
|
|
76
|
-
// Angle units
|
|
77
|
-
'deg': {
|
|
78
|
-
'deg': 1,
|
|
79
|
-
'grad': 0.9,
|
|
80
|
-
'rad': 180 / Math.PI,
|
|
81
|
-
'turn': 360
|
|
82
|
-
},
|
|
83
|
-
'grad': {
|
|
84
|
-
'deg': 400 / 360,
|
|
85
|
-
'grad': 1,
|
|
86
|
-
'rad': 200 / Math.PI,
|
|
87
|
-
'turn': 400
|
|
88
|
-
},
|
|
89
|
-
'rad': {
|
|
90
|
-
'deg': Math.PI / 180,
|
|
91
|
-
'grad': Math.PI / 200,
|
|
92
|
-
'rad': 1,
|
|
93
|
-
'turn': Math.PI * 2
|
|
94
|
-
},
|
|
95
|
-
'turn': {
|
|
96
|
-
'deg': 1 / 360,
|
|
97
|
-
'grad': 0.0025,
|
|
98
|
-
'rad': 0.5 / Math.PI,
|
|
99
|
-
'turn': 1
|
|
100
|
-
},
|
|
101
|
-
// Duration units
|
|
102
|
-
's': {
|
|
103
|
-
's': 1,
|
|
104
|
-
'ms': 0.001
|
|
105
|
-
},
|
|
106
|
-
'ms': {
|
|
107
|
-
's': 1000,
|
|
108
|
-
'ms': 1
|
|
109
|
-
},
|
|
110
|
-
// Frequency units
|
|
111
|
-
'hz': {
|
|
112
|
-
'hz': 1,
|
|
113
|
-
'khz': 1000
|
|
114
|
-
},
|
|
115
|
-
'khz': {
|
|
116
|
-
'hz': 0.001,
|
|
117
|
-
'khz': 1
|
|
118
|
-
},
|
|
119
|
-
// Resolution units
|
|
120
|
-
'dpi': {
|
|
121
|
-
'dpi': 1,
|
|
122
|
-
'dpcm': 1 / 2.54,
|
|
123
|
-
'dppx': 1 / 96
|
|
124
|
-
},
|
|
125
|
-
'dpcm': {
|
|
126
|
-
'dpi': 2.54,
|
|
127
|
-
'dpcm': 1,
|
|
128
|
-
'dppx': 2.54 / 96
|
|
129
|
-
},
|
|
130
|
-
'dppx': {
|
|
131
|
-
'dpi': 96,
|
|
132
|
-
'dpcm': 96 / 2.54,
|
|
133
|
-
'dppx': 1
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
/**
|
|
137
|
-
* @param {number} value
|
|
138
|
-
* @param {string} sourceUnit
|
|
139
|
-
* @param {string} targetUnit
|
|
140
|
-
* @param {number|false} precision
|
|
141
|
-
*/
|
|
142
|
-
|
|
143
|
-
function convertUnit(value, sourceUnit, targetUnit, precision) {
|
|
144
|
-
const sourceUnitNormalized = sourceUnit.toLowerCase();
|
|
145
|
-
const targetUnitNormalized = targetUnit.toLowerCase();
|
|
146
|
-
|
|
147
|
-
if (!conversions[targetUnitNormalized]) {
|
|
148
|
-
throw new Error("Cannot convert to " + targetUnit);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (!conversions[targetUnitNormalized][sourceUnitNormalized]) {
|
|
152
|
-
throw new Error("Cannot convert from " + sourceUnit + " to " + targetUnit);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const converted = conversions[targetUnitNormalized][sourceUnitNormalized] * value;
|
|
156
|
-
|
|
157
|
-
if (precision !== false) {
|
|
158
|
-
precision = Math.pow(10, Math.ceil(precision) || 5);
|
|
159
|
-
return Math.round(converted * precision) / precision;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return converted;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
var _default = convertUnit;
|
|
166
|
-
exports.default = _default;
|
|
167
|
-
module.exports = exports.default;
|
package/dist/lib/transform.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
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
|
-
* @param {string} value
|
|
24
|
-
* @param {{precision: number, warnWhenCannotResolve: boolean}} options
|
|
25
|
-
* @param {import("postcss").Result} result
|
|
26
|
-
* @param {import("postcss").ChildNode} item
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
function transformValue(value, options, result, item) {
|
|
30
|
-
return (0, _postcssValueParser.default)(value).walk(node => {
|
|
31
|
-
// skip anything which isn't a calc() function
|
|
32
|
-
if (node.type !== "function" || !MATCH_CALC.test(node.value)) {
|
|
33
|
-
return;
|
|
34
|
-
} // stringify calc expression and produce an AST
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const contents = _postcssValueParser.default.stringify(node.nodes);
|
|
38
|
-
|
|
39
|
-
const ast = _parser.parser.parse(contents); // reduce AST to its simplest form, that is, either to a single value
|
|
40
|
-
// or a simplified calc expression
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const reducedAst = (0, _reducer.default)(ast, options.precision); // stringify AST and write it back
|
|
44
|
-
|
|
45
|
-
/** @type {valueParser.Node} */
|
|
46
|
-
node.type = "word";
|
|
47
|
-
node.value = (0, _stringifier.default)(node.value, reducedAst, value, options, result, item);
|
|
48
|
-
return false;
|
|
49
|
-
}).toString();
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* @param {import("postcss-selector-parser").Selectors} value
|
|
53
|
-
* @param {{precision: number, warnWhenCannotResolve: boolean}} options
|
|
54
|
-
* @param {import("postcss").Result} result
|
|
55
|
-
* @param {import("postcss").ChildNode} item
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
function transformSelector(value, options, result, item) {
|
|
60
|
-
return (0, _postcssSelectorParser.default)(selectors => {
|
|
61
|
-
selectors.walk(node => {
|
|
62
|
-
// attribute value
|
|
63
|
-
// e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
|
|
64
|
-
if (node.type === "attribute" && node.value) {
|
|
65
|
-
node.setValue(transformValue(node.value, options, result, item));
|
|
66
|
-
} // tag value
|
|
67
|
-
// e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (node.type === "tag") {
|
|
71
|
-
node.value = transformValue(node.value, options, result, item);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return;
|
|
75
|
-
});
|
|
76
|
-
}).processSync(value);
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* @param {any} node
|
|
80
|
-
* @param {{precision: number, preserve: boolean, warnWhenCannotResolve: boolean}} options
|
|
81
|
-
* @param {'value'|'params'|'selector'} property
|
|
82
|
-
* @param {import("postcss").Result} result
|
|
83
|
-
*/
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
var _default = (node, property, options, result) => {
|
|
87
|
-
let value = node[property];
|
|
88
|
-
|
|
89
|
-
try {
|
|
90
|
-
value = property === "selector" ? transformSelector(node[property], options, result, node) : transformValue(node[property], options, result, node);
|
|
91
|
-
} catch (error) {
|
|
92
|
-
if (error instanceof Error) {
|
|
93
|
-
result.warn(error.message, {
|
|
94
|
-
node
|
|
95
|
-
});
|
|
96
|
-
} else {
|
|
97
|
-
result.warn('Error', {
|
|
98
|
-
node
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return;
|
|
103
|
-
} // if the preserve option is enabled and the value has changed, write the
|
|
104
|
-
// transformed value into a cloned node which is inserted before the current
|
|
105
|
-
// node, preserving the original value. Otherwise, overwrite the original
|
|
106
|
-
// value.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (options.preserve && node[property] !== value) {
|
|
110
|
-
const clone = node.clone();
|
|
111
|
-
clone[property] = value;
|
|
112
|
-
node.parent.insertBefore(node, clone);
|
|
113
|
-
} else {
|
|
114
|
-
node[property] = value;
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
exports.default = _default;
|
|
119
|
-
module.exports = exports.default;
|