postcss-normalize-positions 8.0.1 → 8.0.3

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/LICENSE-MIT CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) Ben Briggs <beneb.info@gmail.com> (http://beneb.info)
1
+ Copyright (c) Ben Briggs <beneb.info@gmail.com> (https://beneb.info)
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person
4
4
  obtaining a copy of this software and associated documentation
package/README.md CHANGED
@@ -39,6 +39,6 @@ See [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTOR
39
39
 
40
40
  ## License
41
41
 
42
- MIT © [Ben Briggs](http://beneb.info)
42
+ MIT © [Ben Briggs](https://beneb.info)
43
43
 
44
44
  [postcss]: https://github.com/postcss/postcss
package/package.json CHANGED
@@ -1,9 +1,17 @@
1
1
  {
2
2
  "name": "postcss-normalize-positions",
3
- "version": "8.0.1",
3
+ "version": "8.0.3",
4
4
  "description": "Normalize keyword values for position into length values.",
5
- "main": "src/index.js",
6
- "types": "types/index.d.ts",
5
+ "exports": {
6
+ ".": {
7
+ "types": "./types/index.d.ts",
8
+ "default": "./src/index.js"
9
+ },
10
+ "./src": "./src/index.js",
11
+ "./src/index": "./src/index.js",
12
+ "./src/index.js": "./src/index.js",
13
+ "./package.json": "./package.json"
14
+ },
7
15
  "files": [
8
16
  "src",
9
17
  "LICENSE-MIT",
@@ -19,9 +27,12 @@
19
27
  "author": {
20
28
  "name": "Ben Briggs",
21
29
  "email": "beneb.info@gmail.com",
22
- "url": "http://beneb.info"
30
+ "url": "https://beneb.info"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "cssnano/cssnano"
23
35
  },
24
- "repository": "cssnano/cssnano",
25
36
  "dependencies": {
26
37
  "postcss-value-parser": "^4.2.0"
27
38
  },
@@ -32,9 +43,12 @@
32
43
  "node": "^22.11.0 || ^24.11.0 || >=26.0"
33
44
  },
34
45
  "devDependencies": {
35
- "postcss": "^8.5.15"
46
+ "postcss": "^8.5.26"
36
47
  },
37
48
  "peerDependencies": {
38
- "postcss": "^8.5.15"
49
+ "postcss": "^8.5.26"
50
+ },
51
+ "scripts": {
52
+ "test": "node --test"
39
53
  }
40
54
  }
package/src/index.js CHANGED
@@ -14,6 +14,9 @@ const verticalValue = new Map([
14
14
  ]);
15
15
  const mathFunctions = new Set(['calc', 'min', 'max', 'clamp']);
16
16
  const variableFunctions = new Set(['var', 'env', 'constant']);
17
+ const propFilterRegex =
18
+ /^(background(-position)?|(-\w+-)?perspective-origin)$/i;
19
+
17
20
  /**
18
21
  * @param {valueParser.Node} node
19
22
  * @return {boolean}
@@ -83,22 +86,36 @@ function isDimensionNode(node) {
83
86
  */
84
87
  function transform(value) {
85
88
  const parsed = valueParser(value);
86
- /** @type {({start: number, end: number} | {start: null, end: null})[]} */
89
+ const ranges = collectRanges(parsed.nodes);
90
+
91
+ for (const range of ranges) {
92
+ normalizeRange(parsed, range);
93
+ }
94
+
95
+ return parsed.toString();
96
+ }
97
+
98
+ /**
99
+ * @param {valueParser.Node[]} nodes
100
+ * @return {{start: number | null, end: number | null}[]}
101
+ */
102
+ function collectRanges(nodes) {
103
+ /** @type {{start: number | null, end: number | null}[]} */
87
104
  const ranges = [];
88
105
  let rangeIndex = 0;
89
106
  let shouldContinue = true;
90
107
 
91
- parsed.nodes.forEach((node, index) => {
108
+ for (const [index, node] of nodes.entries()) {
92
109
  // After comma (`,`) follows next background
93
110
  if (isCommaNode(node)) {
94
111
  rangeIndex += 1;
95
112
  shouldContinue = true;
96
113
 
97
- return;
114
+ continue;
98
115
  }
99
116
 
100
117
  if (!shouldContinue) {
101
- return;
118
+ continue;
102
119
  }
103
120
 
104
121
  // After separator (`/`) follows `background-size` values
@@ -106,7 +123,7 @@ function transform(value) {
106
123
  if (node.type === 'div' && node.value === '/') {
107
124
  shouldContinue = false;
108
125
 
109
- return;
126
+ continue;
110
127
  }
111
128
 
112
129
  if (!ranges[rangeIndex]) {
@@ -122,7 +139,7 @@ function transform(value) {
122
139
  ranges[rangeIndex].start = null;
123
140
  ranges[rangeIndex].end = null;
124
141
 
125
- return;
142
+ continue;
126
143
  }
127
144
 
128
145
  const isPositionKeyword =
@@ -136,113 +153,130 @@ function transform(value) {
136
153
  ranges[rangeIndex].start = index;
137
154
  ranges[rangeIndex].end = index;
138
155
 
139
- return;
156
+ continue;
140
157
  }
141
158
 
142
159
  if (ranges[rangeIndex].start !== null) {
143
- if (node.type === 'space') {
144
- return;
145
- } else if (isPositionKeyword) {
160
+ if (node.type !== 'space' && isPositionKeyword) {
146
161
  ranges[rangeIndex].end = index;
147
-
148
- return;
149
162
  }
150
-
151
- return;
152
163
  }
153
- });
164
+ }
154
165
 
155
- ranges.forEach((range) => {
156
- if (range.start === null) {
157
- return;
158
- }
166
+ return ranges;
167
+ }
159
168
 
160
- const nodes = parsed.nodes.slice(range.start, range.end + 1);
169
+ /**
170
+ * @param {{nodes: valueParser.Node[]}} parsed
171
+ * @param {{start: number | null, end: number | null}} range
172
+ * @return {void}
173
+ */
174
+ function normalizeRange(parsed, range) {
175
+ if (range.start === null || range.end === null) {
176
+ return;
177
+ }
161
178
 
162
- if (nodes.length > 3) {
163
- return;
164
- }
179
+ const nodes = parsed.nodes.slice(range.start, range.end + 1);
165
180
 
166
- const firstNode = nodes[0].value.toLowerCase();
167
- const secondNode =
168
- nodes[2] && nodes[2].value ? nodes[2].value.toLowerCase() : null;
181
+ if (nodes.length > 3) {
182
+ return;
183
+ }
169
184
 
170
- if (nodes.length === 1 || secondNode === 'center') {
171
- if (secondNode) {
172
- nodes[2].value = nodes[1].value = '';
173
- }
185
+ const firstNode = nodes[0].value.toLowerCase();
186
+ const secondNode =
187
+ nodes[2] && nodes[2].value ? nodes[2].value.toLowerCase() : null;
174
188
 
175
- const map = new Map([...horizontal, ['center', center]]);
189
+ if (nodes.length === 1 || secondNode === 'center') {
190
+ normalizeSinglePosition(nodes, firstNode, secondNode);
191
+ return;
192
+ }
176
193
 
177
- if (map.has(firstNode)) {
178
- nodes[0].value = /** @type {string}*/ (map.get(firstNode));
179
- }
194
+ normalizePairPosition(nodes, firstNode, secondNode);
195
+ }
180
196
 
181
- return;
182
- }
197
+ /**
198
+ * @param {valueParser.Node[]} nodes
199
+ * @param {string} firstNode
200
+ * @param {string | null} secondNode
201
+ * @return {void}
202
+ */
203
+ function normalizeSinglePosition(nodes, firstNode, secondNode) {
204
+ if (secondNode) {
205
+ nodes[2].value = nodes[1].value = '';
206
+ }
183
207
 
184
- if (secondNode !== null) {
185
- if (firstNode === 'center' && directionKeywords.has(secondNode)) {
186
- nodes[0].value = nodes[1].value = '';
208
+ const map = new Map([...horizontal, ['center', center]]);
187
209
 
188
- if (horizontal.has(secondNode)) {
189
- nodes[2].value = /** @type {string} */ (horizontal.get(secondNode));
190
- }
191
- return;
192
- }
210
+ if (map.has(firstNode)) {
211
+ nodes[0].value = /** @type {string}*/ (map.get(firstNode));
212
+ }
213
+ }
193
214
 
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));
215
+ /**
216
+ * @param {valueParser.Node[]} nodes
217
+ * @param {string} firstNode
218
+ * @param {string | null} secondNode
219
+ * @return {void}
220
+ */
221
+ function normalizePairPosition(nodes, firstNode, secondNode) {
222
+ if (secondNode === null) {
223
+ return;
224
+ }
197
225
 
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));
226
+ if (firstNode === 'center' && directionKeywords.has(secondNode)) {
227
+ nodes[0].value = nodes[1].value = '';
202
228
 
203
- return;
204
- }
229
+ if (horizontal.has(secondNode)) {
230
+ nodes[2].value = /** @type {string} */ (horizontal.get(secondNode));
205
231
  }
206
- });
232
+ return;
233
+ }
207
234
 
208
- return parsed.toString();
235
+ if (horizontal.has(firstNode) && verticalValue.has(secondNode)) {
236
+ nodes[0].value = /** @type {string} */ (horizontal.get(firstNode));
237
+ nodes[2].value = /** @type {string} */ (verticalValue.get(secondNode));
238
+ } else if (verticalValue.has(firstNode) && horizontal.has(secondNode)) {
239
+ nodes[0].value = /** @type {string} */ (horizontal.get(secondNode));
240
+ nodes[2].value = /** @type {string} */ (verticalValue.get(firstNode));
241
+ }
209
242
  }
210
243
 
211
244
  /**
212
- * @type {import('postcss').PluginCreator<void>}
213
245
  * @return {import('postcss').Plugin}
214
246
  */
215
247
  function pluginCreator() {
216
248
  return {
217
249
  postcssPlugin: 'postcss-normalize-positions',
218
250
 
251
+ /**
252
+ * @param {import('postcss').Root} css
253
+ */
219
254
  OnceExit(css) {
220
255
  const cache = new Map();
221
256
 
222
- css.walkDecls(
223
- /^(background(-position)?|(-\w+-)?perspective-origin)$/i,
224
- (decl) => {
225
- const value = decl.value;
257
+ css.walkDecls(propFilterRegex, (decl) => {
258
+ const value = decl.value;
226
259
 
227
- if (!value) {
228
- return;
229
- }
260
+ if (!value) {
261
+ return;
262
+ }
230
263
 
231
- if (cache.has(value)) {
232
- decl.value = cache.get(value);
264
+ if (cache.has(value)) {
265
+ decl.value = cache.get(value);
233
266
 
234
- return;
235
- }
267
+ return;
268
+ }
236
269
 
237
- const result = transform(value);
270
+ const result = transform(value);
238
271
 
239
- decl.value = result;
240
- cache.set(value, result);
241
- }
242
- );
272
+ decl.value = result;
273
+ cache.set(value, result);
274
+ });
243
275
  },
244
276
  };
245
277
  }
246
278
 
247
279
  pluginCreator.postcss = true;
248
- module.exports = pluginCreator;
280
+ module.exports = /** @type {import('postcss').PluginCreator<void>}*/ (
281
+ pluginCreator
282
+ );
package/types/index.d.ts CHANGED
@@ -1,10 +1,3 @@
1
- export = pluginCreator;
2
- /**
3
- * @type {import('postcss').PluginCreator<void>}
4
- * @return {import('postcss').Plugin}
5
- */
6
- declare function pluginCreator(): import("postcss").Plugin;
7
- declare namespace pluginCreator {
8
- let postcss: true;
9
- }
1
+ declare const _exports: import("postcss").PluginCreator<void>;
2
+ export = _exports;
10
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";AAkNA;;;GAGG;AACH,kCAFY,OAAO,SAAS,EAAE,MAAM,CAgCnC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":""}