postcss-normalize-positions 5.0.2 → 5.1.0

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