postcss-normalize-positions 5.0.3 → 5.1.1
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 +7 -10
- package/src/index.js +248 -0
- package/types/index.d.ts +9 -0
- package/dist/index.js +0 -202
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-normalize-positions",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.1.1",
|
|
4
4
|
"description": "Normalize keyword values for position into length values.",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"types": "types/index.d.ts",
|
|
6
7
|
"files": [
|
|
7
|
-
"
|
|
8
|
-
"LICENSE-MIT"
|
|
8
|
+
"src",
|
|
9
|
+
"LICENSE-MIT",
|
|
10
|
+
"types"
|
|
9
11
|
],
|
|
10
12
|
"keywords": [
|
|
11
13
|
"css",
|
|
@@ -34,10 +36,5 @@
|
|
|
34
36
|
},
|
|
35
37
|
"peerDependencies": {
|
|
36
38
|
"postcss": "^8.2.15"
|
|
37
|
-
}
|
|
38
|
-
"scripts": {
|
|
39
|
-
"prebuild": "rimraf dist",
|
|
40
|
-
"build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
|
|
41
|
-
},
|
|
42
|
-
"readme": "# [postcss][postcss]-normalize-positions\n\n> Normalize positions with PostCSS.\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-normalize-positions) do:\n\n```\nnpm install postcss-normalize-positions --save\n```\n\n## Example\n\n### Input\n\n```css\ndiv {\n background-position: bottom left;\n}\n```\n\n### Output\n\n```css\ndiv {\n background-position:0 100%;\n}\n``` \n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n## License\n\nMIT © [Ben Briggs](http://beneb.info)\n\n[postcss]: https://github.com/postcss/postcss\n"
|
|
39
|
+
}
|
|
43
40
|
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const valueParser = require('postcss-value-parser');
|
|
3
|
+
|
|
4
|
+
const directionKeywords = new Set(['top', 'right', 'bottom', 'left', 'center']);
|
|
5
|
+
|
|
6
|
+
const center = '50%';
|
|
7
|
+
const horizontal = new Map([
|
|
8
|
+
['right', '100%'],
|
|
9
|
+
['left', '0'],
|
|
10
|
+
]);
|
|
11
|
+
const verticalValue = new Map([
|
|
12
|
+
['bottom', '100%'],
|
|
13
|
+
['top', '0'],
|
|
14
|
+
]);
|
|
15
|
+
const mathFunctions = new Set(['calc', 'min', 'max', 'clamp']);
|
|
16
|
+
const variableFunctions = new Set(['var', 'env', 'constant']);
|
|
17
|
+
/**
|
|
18
|
+
* @param {valueParser.Node} node
|
|
19
|
+
* @return {boolean}
|
|
20
|
+
*/
|
|
21
|
+
function isCommaNode(node) {
|
|
22
|
+
return node.type === 'div' && node.value === ',';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {valueParser.Node} node
|
|
27
|
+
* @return {boolean}
|
|
28
|
+
*/
|
|
29
|
+
function isVariableFunctionNode(node) {
|
|
30
|
+
if (node.type !== 'function') {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return variableFunctions.has(node.value.toLowerCase());
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {valueParser.Node} node
|
|
39
|
+
* @return {boolean}
|
|
40
|
+
*/
|
|
41
|
+
function isMathFunctionNode(node) {
|
|
42
|
+
if (node.type !== 'function') {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
return mathFunctions.has(node.value.toLowerCase());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @param {valueParser.Node} node
|
|
50
|
+
* @return {boolean}
|
|
51
|
+
*/
|
|
52
|
+
function isNumberNode(node) {
|
|
53
|
+
if (node.type !== 'word') {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const value = parseFloat(node.value);
|
|
58
|
+
|
|
59
|
+
return !isNaN(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param {valueParser.Node} node
|
|
64
|
+
* @return {boolean}
|
|
65
|
+
*/
|
|
66
|
+
function isDimensionNode(node) {
|
|
67
|
+
if (node.type !== 'word') {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const parsed = valueParser.unit(node.value);
|
|
72
|
+
|
|
73
|
+
if (!parsed) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return parsed.unit !== '';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @param {string} value
|
|
82
|
+
* @return {string}
|
|
83
|
+
*/
|
|
84
|
+
function transform(value) {
|
|
85
|
+
const parsed = valueParser(value);
|
|
86
|
+
/** @type {({start: number, end: number} | {start: null, end: null})[]} */
|
|
87
|
+
const ranges = [];
|
|
88
|
+
let rangeIndex = 0;
|
|
89
|
+
let shouldContinue = true;
|
|
90
|
+
|
|
91
|
+
parsed.nodes.forEach((node, index) => {
|
|
92
|
+
// After comma (`,`) follows next background
|
|
93
|
+
if (isCommaNode(node)) {
|
|
94
|
+
rangeIndex += 1;
|
|
95
|
+
shouldContinue = true;
|
|
96
|
+
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!shouldContinue) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// After separator (`/`) follows `background-size` values
|
|
105
|
+
// Avoid them
|
|
106
|
+
if (node.type === 'div' && node.value === '/') {
|
|
107
|
+
shouldContinue = false;
|
|
108
|
+
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!ranges[rangeIndex]) {
|
|
113
|
+
ranges[rangeIndex] = {
|
|
114
|
+
start: null,
|
|
115
|
+
end: null,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Do not try to be processed `var and `env` function inside background
|
|
120
|
+
if (isVariableFunctionNode(node)) {
|
|
121
|
+
shouldContinue = false;
|
|
122
|
+
ranges[rangeIndex].start = null;
|
|
123
|
+
ranges[rangeIndex].end = null;
|
|
124
|
+
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const isPositionKeyword =
|
|
129
|
+
(node.type === 'word' &&
|
|
130
|
+
directionKeywords.has(node.value.toLowerCase())) ||
|
|
131
|
+
isDimensionNode(node) ||
|
|
132
|
+
isNumberNode(node) ||
|
|
133
|
+
isMathFunctionNode(node);
|
|
134
|
+
|
|
135
|
+
if (ranges[rangeIndex].start === null && isPositionKeyword) {
|
|
136
|
+
ranges[rangeIndex].start = index;
|
|
137
|
+
ranges[rangeIndex].end = index;
|
|
138
|
+
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (ranges[rangeIndex].start !== null) {
|
|
143
|
+
if (node.type === 'space') {
|
|
144
|
+
return;
|
|
145
|
+
} else if (isPositionKeyword) {
|
|
146
|
+
ranges[rangeIndex].end = index;
|
|
147
|
+
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
ranges.forEach((range) => {
|
|
156
|
+
if (range.start === null) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const nodes = parsed.nodes.slice(range.start, range.end + 1);
|
|
161
|
+
|
|
162
|
+
if (nodes.length > 3) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const firstNode = nodes[0].value.toLowerCase();
|
|
167
|
+
const secondNode =
|
|
168
|
+
nodes[2] && nodes[2].value ? nodes[2].value.toLowerCase() : null;
|
|
169
|
+
|
|
170
|
+
if (nodes.length === 1 || secondNode === 'center') {
|
|
171
|
+
if (secondNode) {
|
|
172
|
+
nodes[2].value = nodes[1].value = '';
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const map = new Map([...horizontal, ['center', center]]);
|
|
176
|
+
|
|
177
|
+
if (map.has(firstNode)) {
|
|
178
|
+
nodes[0].value = /** @type {string}*/ (map.get(firstNode));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (secondNode !== null) {
|
|
185
|
+
if (firstNode === 'center' && directionKeywords.has(secondNode)) {
|
|
186
|
+
nodes[0].value = nodes[1].value = '';
|
|
187
|
+
|
|
188
|
+
if (horizontal.has(secondNode)) {
|
|
189
|
+
nodes[2].value = /** @type {string} */ (horizontal.get(secondNode));
|
|
190
|
+
}
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (horizontal.has(firstNode) && verticalValue.has(secondNode)) {
|
|
195
|
+
nodes[0].value = /** @type {string} */ (horizontal.get(firstNode));
|
|
196
|
+
nodes[2].value = /** @type {string} */ (verticalValue.get(secondNode));
|
|
197
|
+
|
|
198
|
+
return;
|
|
199
|
+
} else if (verticalValue.has(firstNode) && horizontal.has(secondNode)) {
|
|
200
|
+
nodes[0].value = /** @type {string} */ (horizontal.get(secondNode));
|
|
201
|
+
nodes[2].value = /** @type {string} */ (verticalValue.get(firstNode));
|
|
202
|
+
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
return parsed.toString();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* @type {import('postcss').PluginCreator<void>}
|
|
213
|
+
* @return {import('postcss').Plugin}
|
|
214
|
+
*/
|
|
215
|
+
function pluginCreator() {
|
|
216
|
+
return {
|
|
217
|
+
postcssPlugin: 'postcss-normalize-positions',
|
|
218
|
+
|
|
219
|
+
OnceExit(css) {
|
|
220
|
+
const cache = new Map();
|
|
221
|
+
|
|
222
|
+
css.walkDecls(
|
|
223
|
+
/^(background(-position)?|(-\w+-)?perspective-origin)$/i,
|
|
224
|
+
(decl) => {
|
|
225
|
+
const value = decl.value;
|
|
226
|
+
|
|
227
|
+
if (!value) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (cache.has(value)) {
|
|
232
|
+
decl.value = cache.get(value);
|
|
233
|
+
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const result = transform(value);
|
|
238
|
+
|
|
239
|
+
decl.value = result;
|
|
240
|
+
cache.set(value, result);
|
|
241
|
+
}
|
|
242
|
+
);
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
pluginCreator.postcss = true;
|
|
248
|
+
module.exports = pluginCreator;
|
package/types/index.d.ts
ADDED
package/dist/index.js
DELETED
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _postcssValueParser = _interopRequireWildcard(require("postcss-value-parser"));
|
|
9
|
-
|
|
10
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
11
|
-
|
|
12
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
13
|
-
|
|
14
|
-
const directionKeywords = new Set(['top', 'right', 'bottom', 'left', 'center']);
|
|
15
|
-
const center = '50%';
|
|
16
|
-
const horizontal = new Map([['right', '100%'], ['left', '0']]);
|
|
17
|
-
const verticalValue = new Map([['bottom', '100%'], ['top', '0']]);
|
|
18
|
-
const mathFunctions = new Set(['calc', 'min', 'max', 'clamp']);
|
|
19
|
-
|
|
20
|
-
function isCommaNode(node) {
|
|
21
|
-
return node.type === 'div' && node.value === ',';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function isVariableFunctionNode(node) {
|
|
25
|
-
if (node.type !== 'function') {
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return ['var', 'env'].includes(node.value.toLowerCase());
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function isMathFunctionNode(node) {
|
|
33
|
-
if (node.type !== 'function') {
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return mathFunctions.has(node.value.toLowerCase());
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function isNumberNode(node) {
|
|
41
|
-
if (node.type !== 'word') {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const value = parseFloat(node.value);
|
|
46
|
-
return !isNaN(value);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function isDimensionNode(node) {
|
|
50
|
-
if (node.type !== 'word') {
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const parsed = (0, _postcssValueParser.unit)(node.value);
|
|
55
|
-
|
|
56
|
-
if (!parsed) {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return parsed.unit !== '';
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function transform(value) {
|
|
64
|
-
const parsed = (0, _postcssValueParser.default)(value);
|
|
65
|
-
const ranges = [];
|
|
66
|
-
let rangeIndex = 0;
|
|
67
|
-
let shouldContinue = true;
|
|
68
|
-
parsed.nodes.forEach((node, index) => {
|
|
69
|
-
// After comma (`,`) follows next background
|
|
70
|
-
if (isCommaNode(node)) {
|
|
71
|
-
rangeIndex += 1;
|
|
72
|
-
shouldContinue = true;
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (!shouldContinue) {
|
|
77
|
-
return;
|
|
78
|
-
} // After separator (`/`) follows `background-size` values
|
|
79
|
-
// Avoid them
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (node.type === 'div' && node.value === '/') {
|
|
83
|
-
shouldContinue = false;
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (!ranges[rangeIndex]) {
|
|
88
|
-
ranges[rangeIndex] = {
|
|
89
|
-
start: null,
|
|
90
|
-
end: null
|
|
91
|
-
};
|
|
92
|
-
} // Do not try to be processed `var and `env` function inside background
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if (isVariableFunctionNode(node)) {
|
|
96
|
-
shouldContinue = false;
|
|
97
|
-
ranges[rangeIndex].start = null;
|
|
98
|
-
ranges[rangeIndex].end = null;
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const isPositionKeyword = node.type === 'word' && directionKeywords.has(node.value.toLowerCase()) || isDimensionNode(node) || isNumberNode(node) || isMathFunctionNode(node);
|
|
103
|
-
|
|
104
|
-
if (ranges[rangeIndex].start === null && isPositionKeyword) {
|
|
105
|
-
ranges[rangeIndex].start = index;
|
|
106
|
-
ranges[rangeIndex].end = index;
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (ranges[rangeIndex].start !== null) {
|
|
111
|
-
if (node.type === 'space') {
|
|
112
|
-
return;
|
|
113
|
-
} else if (isPositionKeyword) {
|
|
114
|
-
ranges[rangeIndex].end = index;
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
ranges.forEach(range => {
|
|
122
|
-
if (range.start === null) {
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const nodes = parsed.nodes.slice(range.start, range.end + 1);
|
|
127
|
-
|
|
128
|
-
if (nodes.length > 3) {
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const firstNode = nodes[0].value.toLowerCase();
|
|
133
|
-
const secondNode = nodes[2] && nodes[2].value ? nodes[2].value.toLowerCase() : null;
|
|
134
|
-
|
|
135
|
-
if (nodes.length === 1 || secondNode === 'center') {
|
|
136
|
-
if (secondNode) {
|
|
137
|
-
nodes[2].value = nodes[1].value = '';
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const map = new Map([...horizontal, ['center', center]]);
|
|
141
|
-
|
|
142
|
-
if (map.has(firstNode)) {
|
|
143
|
-
nodes[0].value = map.get(firstNode);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (firstNode === 'center' && directionKeywords.has(secondNode)) {
|
|
150
|
-
nodes[0].value = nodes[1].value = '';
|
|
151
|
-
|
|
152
|
-
if (horizontal.has(secondNode)) {
|
|
153
|
-
nodes[2].value = horizontal.get(secondNode);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (horizontal.has(firstNode) && verticalValue.has(secondNode)) {
|
|
160
|
-
nodes[0].value = horizontal.get(firstNode);
|
|
161
|
-
nodes[2].value = verticalValue.get(secondNode);
|
|
162
|
-
return;
|
|
163
|
-
} else if (verticalValue.has(firstNode) && horizontal.has(secondNode)) {
|
|
164
|
-
nodes[0].value = horizontal.get(secondNode);
|
|
165
|
-
nodes[2].value = verticalValue.get(firstNode);
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
return parsed.toString();
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function pluginCreator() {
|
|
173
|
-
return {
|
|
174
|
-
postcssPlugin: 'postcss-normalize-positions',
|
|
175
|
-
|
|
176
|
-
OnceExit(css) {
|
|
177
|
-
const cache = new Map();
|
|
178
|
-
css.walkDecls(/^(background(-position)?|(-\w+-)?perspective-origin)$/i, decl => {
|
|
179
|
-
const value = decl.value;
|
|
180
|
-
|
|
181
|
-
if (!value) {
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (cache.has(value)) {
|
|
186
|
-
decl.value = cache.get(value);
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const result = transform(value);
|
|
191
|
-
decl.value = result;
|
|
192
|
-
cache.set(value, result);
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
pluginCreator.postcss = true;
|
|
200
|
-
var _default = pluginCreator;
|
|
201
|
-
exports.default = _default;
|
|
202
|
-
module.exports = exports.default;
|