postcss-normalize-positions 5.0.3 → 5.0.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 +3 -7
- package/{dist → src}/index.js +59 -44
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-normalize-positions",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.4",
|
|
4
4
|
"description": "Normalize keyword values for position into length values.",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "src/index.js",
|
|
6
6
|
"files": [
|
|
7
|
-
"
|
|
7
|
+
"src",
|
|
8
8
|
"LICENSE-MIT"
|
|
9
9
|
],
|
|
10
10
|
"keywords": [
|
|
@@ -35,9 +35,5 @@
|
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"postcss": "^8.2.15"
|
|
37
37
|
},
|
|
38
|
-
"scripts": {
|
|
39
|
-
"prebuild": "rimraf dist",
|
|
40
|
-
"build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
|
|
41
|
-
},
|
|
42
38
|
"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"
|
|
43
39
|
}
|
package/{dist → src}/index.js
RENAMED
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
|
|
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; }
|
|
1
|
+
'use strict';
|
|
2
|
+
const valueParser = require('postcss-value-parser');
|
|
13
3
|
|
|
14
4
|
const directionKeywords = new Set(['top', 'right', 'bottom', 'left', 'center']);
|
|
5
|
+
|
|
15
6
|
const center = '50%';
|
|
16
|
-
const horizontal = new Map([
|
|
17
|
-
|
|
7
|
+
const horizontal = new Map([
|
|
8
|
+
['right', '100%'],
|
|
9
|
+
['left', '0'],
|
|
10
|
+
]);
|
|
11
|
+
const verticalValue = new Map([
|
|
12
|
+
['bottom', '100%'],
|
|
13
|
+
['top', '0'],
|
|
14
|
+
]);
|
|
18
15
|
const mathFunctions = new Set(['calc', 'min', 'max', 'clamp']);
|
|
19
16
|
|
|
20
17
|
function isCommaNode(node) {
|
|
@@ -33,7 +30,6 @@ function isMathFunctionNode(node) {
|
|
|
33
30
|
if (node.type !== 'function') {
|
|
34
31
|
return false;
|
|
35
32
|
}
|
|
36
|
-
|
|
37
33
|
return mathFunctions.has(node.value.toLowerCase());
|
|
38
34
|
}
|
|
39
35
|
|
|
@@ -43,6 +39,7 @@ function isNumberNode(node) {
|
|
|
43
39
|
}
|
|
44
40
|
|
|
45
41
|
const value = parseFloat(node.value);
|
|
42
|
+
|
|
46
43
|
return !isNaN(value);
|
|
47
44
|
}
|
|
48
45
|
|
|
@@ -51,7 +48,7 @@ function isDimensionNode(node) {
|
|
|
51
48
|
return false;
|
|
52
49
|
}
|
|
53
50
|
|
|
54
|
-
const parsed =
|
|
51
|
+
const parsed = valueParser.unit(node.value);
|
|
55
52
|
|
|
56
53
|
if (!parsed) {
|
|
57
54
|
return false;
|
|
@@ -61,49 +58,59 @@ function isDimensionNode(node) {
|
|
|
61
58
|
}
|
|
62
59
|
|
|
63
60
|
function transform(value) {
|
|
64
|
-
const parsed = (
|
|
61
|
+
const parsed = valueParser(value);
|
|
65
62
|
const ranges = [];
|
|
66
63
|
let rangeIndex = 0;
|
|
67
64
|
let shouldContinue = true;
|
|
65
|
+
|
|
68
66
|
parsed.nodes.forEach((node, index) => {
|
|
69
67
|
// After comma (`,`) follows next background
|
|
70
68
|
if (isCommaNode(node)) {
|
|
71
69
|
rangeIndex += 1;
|
|
72
70
|
shouldContinue = true;
|
|
71
|
+
|
|
73
72
|
return;
|
|
74
73
|
}
|
|
75
74
|
|
|
76
75
|
if (!shouldContinue) {
|
|
77
76
|
return;
|
|
78
|
-
}
|
|
79
|
-
// Avoid them
|
|
80
|
-
|
|
77
|
+
}
|
|
81
78
|
|
|
79
|
+
// After separator (`/`) follows `background-size` values
|
|
80
|
+
// Avoid them
|
|
82
81
|
if (node.type === 'div' && node.value === '/') {
|
|
83
82
|
shouldContinue = false;
|
|
83
|
+
|
|
84
84
|
return;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
if (!ranges[rangeIndex]) {
|
|
88
88
|
ranges[rangeIndex] = {
|
|
89
89
|
start: null,
|
|
90
|
-
end: null
|
|
90
|
+
end: null,
|
|
91
91
|
};
|
|
92
|
-
}
|
|
93
|
-
|
|
92
|
+
}
|
|
94
93
|
|
|
94
|
+
// Do not try to be processed `var and `env` function inside background
|
|
95
95
|
if (isVariableFunctionNode(node)) {
|
|
96
96
|
shouldContinue = false;
|
|
97
97
|
ranges[rangeIndex].start = null;
|
|
98
98
|
ranges[rangeIndex].end = null;
|
|
99
|
+
|
|
99
100
|
return;
|
|
100
101
|
}
|
|
101
102
|
|
|
102
|
-
const isPositionKeyword =
|
|
103
|
+
const isPositionKeyword =
|
|
104
|
+
(node.type === 'word' &&
|
|
105
|
+
directionKeywords.has(node.value.toLowerCase())) ||
|
|
106
|
+
isDimensionNode(node) ||
|
|
107
|
+
isNumberNode(node) ||
|
|
108
|
+
isMathFunctionNode(node);
|
|
103
109
|
|
|
104
110
|
if (ranges[rangeIndex].start === null && isPositionKeyword) {
|
|
105
111
|
ranges[rangeIndex].start = index;
|
|
106
112
|
ranges[rangeIndex].end = index;
|
|
113
|
+
|
|
107
114
|
return;
|
|
108
115
|
}
|
|
109
116
|
|
|
@@ -112,13 +119,15 @@ function transform(value) {
|
|
|
112
119
|
return;
|
|
113
120
|
} else if (isPositionKeyword) {
|
|
114
121
|
ranges[rangeIndex].end = index;
|
|
122
|
+
|
|
115
123
|
return;
|
|
116
124
|
}
|
|
117
125
|
|
|
118
126
|
return;
|
|
119
127
|
}
|
|
120
128
|
});
|
|
121
|
-
|
|
129
|
+
|
|
130
|
+
ranges.forEach((range) => {
|
|
122
131
|
if (range.start === null) {
|
|
123
132
|
return;
|
|
124
133
|
}
|
|
@@ -130,7 +139,8 @@ function transform(value) {
|
|
|
130
139
|
}
|
|
131
140
|
|
|
132
141
|
const firstNode = nodes[0].value.toLowerCase();
|
|
133
|
-
const secondNode =
|
|
142
|
+
const secondNode =
|
|
143
|
+
nodes[2] && nodes[2].value ? nodes[2].value.toLowerCase() : null;
|
|
134
144
|
|
|
135
145
|
if (nodes.length === 1 || secondNode === 'center') {
|
|
136
146
|
if (secondNode) {
|
|
@@ -152,20 +162,22 @@ function transform(value) {
|
|
|
152
162
|
if (horizontal.has(secondNode)) {
|
|
153
163
|
nodes[2].value = horizontal.get(secondNode);
|
|
154
164
|
}
|
|
155
|
-
|
|
156
165
|
return;
|
|
157
166
|
}
|
|
158
167
|
|
|
159
168
|
if (horizontal.has(firstNode) && verticalValue.has(secondNode)) {
|
|
160
169
|
nodes[0].value = horizontal.get(firstNode);
|
|
161
170
|
nodes[2].value = verticalValue.get(secondNode);
|
|
171
|
+
|
|
162
172
|
return;
|
|
163
173
|
} else if (verticalValue.has(firstNode) && horizontal.has(secondNode)) {
|
|
164
174
|
nodes[0].value = horizontal.get(secondNode);
|
|
165
175
|
nodes[2].value = verticalValue.get(firstNode);
|
|
176
|
+
|
|
166
177
|
return;
|
|
167
178
|
}
|
|
168
179
|
});
|
|
180
|
+
|
|
169
181
|
return parsed.toString();
|
|
170
182
|
}
|
|
171
183
|
|
|
@@ -175,28 +187,31 @@ function pluginCreator() {
|
|
|
175
187
|
|
|
176
188
|
OnceExit(css) {
|
|
177
189
|
const cache = new Map();
|
|
178
|
-
css.walkDecls(/^(background(-position)?|(-\w+-)?perspective-origin)$/i, decl => {
|
|
179
|
-
const value = decl.value;
|
|
180
190
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
191
|
+
css.walkDecls(
|
|
192
|
+
/^(background(-position)?|(-\w+-)?perspective-origin)$/i,
|
|
193
|
+
(decl) => {
|
|
194
|
+
const value = decl.value;
|
|
184
195
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
196
|
+
if (!value) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
189
199
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
cache.set(value, result);
|
|
193
|
-
});
|
|
194
|
-
}
|
|
200
|
+
if (cache.has(value)) {
|
|
201
|
+
decl.value = cache.get(value);
|
|
195
202
|
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const result = transform(value);
|
|
207
|
+
|
|
208
|
+
decl.value = result;
|
|
209
|
+
cache.set(value, result);
|
|
210
|
+
}
|
|
211
|
+
);
|
|
212
|
+
},
|
|
196
213
|
};
|
|
197
214
|
}
|
|
198
215
|
|
|
199
216
|
pluginCreator.postcss = true;
|
|
200
|
-
|
|
201
|
-
exports.default = _default;
|
|
202
|
-
module.exports = exports.default;
|
|
217
|
+
module.exports = pluginCreator;
|