postcss-normalize-positions 4.0.2 → 5.0.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.
Files changed (3) hide show
  1. package/CHANGELOG.md +71 -0
  2. package/dist/index.js +179 -119
  3. package/package.json +14 -12
package/CHANGELOG.md ADDED
@@ -0,0 +1,71 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [5.0.0](https://github.com/cssnano/cssnano/compare/postcss-normalize-positions@5.0.0-rc.2...postcss-normalize-positions@5.0.0) (2021-04-06)
7
+
8
+ **Note:** Version bump only for package postcss-normalize-positions
9
+
10
+
11
+
12
+
13
+
14
+ # [5.0.0-rc.2](https://github.com/cssnano/cssnano/compare/postcss-normalize-positions@5.0.0-rc.1...postcss-normalize-positions@5.0.0-rc.2) (2021-03-15)
15
+
16
+ **Note:** Version bump only for package postcss-normalize-positions
17
+
18
+
19
+
20
+
21
+
22
+ # [5.0.0-rc.1](https://github.com/cssnano/cssnano/compare/postcss-normalize-positions@5.0.0-rc.0...postcss-normalize-positions@5.0.0-rc.1) (2021-03-04)
23
+
24
+ **Note:** Version bump only for package postcss-normalize-positions
25
+
26
+
27
+
28
+
29
+
30
+ # 5.0.0-rc.0 (2021-02-19)
31
+
32
+
33
+ ### Bug Fixes
34
+
35
+ * **postcss-normalize-positions:** correct optimize math (`calc` and etc) and variable functions (`var` and `env`) ([#750](https://github.com/cssnano/cssnano/issues/750)) ([a81e8df](https://github.com/cssnano/cssnano/commit/a81e8dfc1ad26067d5a9efab8081072cd4b15c44))
36
+ * **postcss-normalize-repeat-style:** better handle css variables ([#753](https://github.com/cssnano/cssnano/issues/753)) ([7e18b0c](https://github.com/cssnano/cssnano/commit/7e18b0cbcd7cb5de58e60ab4ef1900a4d8eeefec))
37
+
38
+
39
+ ### chore
40
+
41
+ * minimum require version of node is 10.13 ([#871](https://github.com/cssnano/cssnano/issues/871)) ([28bda24](https://github.com/cssnano/cssnano/commit/28bda243e32ce3ba89b3c358a5f78727b3732f11))
42
+
43
+
44
+ ### Features
45
+
46
+ * migarete to PostCSS 8 ([#975](https://github.com/cssnano/cssnano/issues/975)) ([40b82dc](https://github.com/cssnano/cssnano/commit/40b82dca7f53ac02cd4fe62846dec79b898ccb49))
47
+
48
+
49
+ ### BREAKING CHANGES
50
+
51
+ * minimum supported `postcss` version is `8.2.1`
52
+ * minimum require version of node is 10.13
53
+
54
+
55
+
56
+ ## 4.1.9 (2019-02-12)
57
+
58
+
59
+ ### Performance Improvements
60
+
61
+ * **postcss-normalize-positions:** increase perf ([#694](https://github.com/cssnano/cssnano/issues/694)) ([95639da](https://github.com/cssnano/cssnano/commit/95639da5659aef112586b9334a1e13ac0b61a525))
62
+
63
+
64
+
65
+ ## 4.1.1 (2018-09-24)
66
+
67
+
68
+ ### Bug Fixes
69
+
70
+ * handle uppercase property and values on `postcss-normalize-positions` ([#607](https://github.com/cssnano/cssnano/issues/607)) ([c521423](https://github.com/cssnano/cssnano/commit/c521423d5939039b771ba1b12fad5fe377a6c612))
71
+ * **postcss-merge-longhand:** not mangle border output ([#555](https://github.com/cssnano/cssnano/issues/555)) ([9a70605](https://github.com/cssnano/cssnano/commit/9a706050b621e7795a9bf74eb7110b5c81804ffe)), closes [#553](https://github.com/cssnano/cssnano/issues/553) [#554](https://github.com/cssnano/cssnano/issues/554)
package/dist/index.js CHANGED
@@ -1,149 +1,209 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
- value: true
4
+ value: true
5
5
  });
6
+ exports.default = void 0;
6
7
 
7
- var _postcss = require('postcss');
8
+ var _postcssValueParser = _interopRequireWildcard(require("postcss-value-parser"));
8
9
 
9
- var _postcssValueParser = require('postcss-value-parser');
10
+ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
10
11
 
11
- var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
12
-
13
- var _cssnanoUtilGetArguments = require('cssnano-util-get-arguments');
14
-
15
- var _cssnanoUtilGetArguments2 = _interopRequireDefault(_cssnanoUtilGetArguments);
16
-
17
- var _has = require('has');
18
-
19
- var _has2 = _interopRequireDefault(_has);
20
-
21
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
-
23
- const directions = ['top', 'right', 'bottom', 'left', 'center'];
12
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }
24
13
 
14
+ const directionKeywords = ['top', 'right', 'bottom', 'left', 'center'];
25
15
  const center = '50%';
26
-
27
16
  const horizontal = {
28
- right: '100%',
29
- left: '0'
17
+ right: '100%',
18
+ left: '0'
30
19
  };
31
-
32
- const vertical = {
33
- bottom: '100%',
34
- top: '0'
20
+ const verticalValue = {
21
+ bottom: '100%',
22
+ top: '0'
35
23
  };
36
24
 
37
- function transform(value) {
38
- const parsed = (0, _postcssValueParser2.default)(value);
39
- const args = (0, _cssnanoUtilGetArguments2.default)(parsed);
40
- const relevant = [];
41
-
42
- args.forEach(arg => {
43
- relevant.push({
44
- start: null,
45
- end: null
46
- });
47
-
48
- arg.forEach((part, index) => {
49
- const isPosition = ~directions.indexOf(part.value.toLowerCase()) || (0, _postcssValueParser.unit)(part.value);
50
- const len = relevant.length - 1;
51
-
52
- if (relevant[len].start === null && isPosition) {
53
- relevant[len].start = index;
54
- relevant[len].end = index;
55
-
56
- return;
57
- }
58
-
59
- if (relevant[len].start !== null) {
60
- if (part.type === 'space') {
61
- return;
62
- } else if (isPosition) {
63
- relevant[len].end = index;
64
-
65
- return;
66
- }
67
-
68
- return;
69
- }
70
- });
71
- });
72
-
73
- relevant.forEach((range, index) => {
74
- if (range.start === null) {
75
- return;
76
- }
77
-
78
- const position = args[index].slice(range.start, range.end + 1);
79
-
80
- if (position.length > 3) {
81
- return;
82
- }
83
-
84
- const firstValue = position[0].value.toLowerCase();
85
- const secondValue = position[2] && position[2].value ? position[2].value.toLowerCase() : null;
25
+ function isCommaNode(node) {
26
+ return node.type === 'div' && node.value === ',';
27
+ }
86
28
 
87
- if (position.length === 1 || secondValue === 'center') {
88
- if (secondValue) {
89
- position[2].value = position[1].value = '';
90
- }
29
+ function isVariableFunctionNode(node) {
30
+ if (node.type !== 'function') {
31
+ return false;
32
+ }
91
33
 
92
- const map = Object.assign({}, horizontal, {
93
- center
94
- });
34
+ return ['var', 'env'].includes(node.value.toLowerCase());
35
+ }
95
36
 
96
- if ((0, _has2.default)(map, firstValue)) {
97
- position[0].value = map[firstValue];
98
- }
37
+ function isMathFunctionNode(node) {
38
+ if (node.type !== 'function') {
39
+ return false;
40
+ }
99
41
 
100
- return;
101
- }
42
+ return ['calc', 'min', 'max', 'clamp'].includes(node.value.toLowerCase());
43
+ }
102
44
 
103
- if (firstValue === 'center' && ~directions.indexOf(secondValue)) {
104
- position[0].value = position[1].value = '';
45
+ function isNumberNode(node) {
46
+ if (node.type !== 'word') {
47
+ return false;
48
+ }
105
49
 
106
- if ((0, _has2.default)(horizontal, secondValue)) {
107
- position[2].value = horizontal[secondValue];
108
- }
50
+ const value = parseFloat(node.value);
51
+ return !isNaN(value);
52
+ }
109
53
 
110
- return;
111
- }
54
+ function isDimensionNode(node) {
55
+ if (node.type !== 'word') {
56
+ return false;
57
+ }
112
58
 
113
- if ((0, _has2.default)(horizontal, firstValue) && (0, _has2.default)(vertical, secondValue)) {
114
- position[0].value = horizontal[firstValue];
115
- position[2].value = vertical[secondValue];
59
+ const parsed = (0, _postcssValueParser.unit)(node.value);
116
60
 
117
- return;
118
- } else if ((0, _has2.default)(vertical, firstValue) && (0, _has2.default)(horizontal, secondValue)) {
119
- position[0].value = horizontal[secondValue];
120
- position[2].value = vertical[firstValue];
61
+ if (!parsed) {
62
+ return false;
63
+ }
121
64
 
122
- return;
123
- }
124
- });
65
+ return parsed.unit !== '';
66
+ }
125
67
 
126
- return parsed.toString();
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();
127
177
  }
128
178
 
129
- exports.default = (0, _postcss.plugin)('postcss-normalize-positions', () => {
130
- return css => {
131
- const cache = {};
179
+ function pluginCreator() {
180
+ return {
181
+ postcssPlugin: 'postcss-normalize-positions',
132
182
 
133
- css.walkDecls(/^(background(-position)?|(-webkit-)?perspective-origin)$/i, decl => {
134
- const value = decl.value;
183
+ OnceExit(css) {
184
+ const cache = {};
185
+ css.walkDecls(/^(background(-position)?|(-\w+-)?perspective-origin)$/i, decl => {
186
+ const value = decl.value;
135
187
 
136
- if (cache[value]) {
137
- decl.value = cache[value];
188
+ if (!value) {
189
+ return;
190
+ }
138
191
 
139
- return;
140
- }
192
+ if (cache[value]) {
193
+ decl.value = cache[value];
194
+ return;
195
+ }
141
196
 
142
- const result = transform(value);
197
+ const result = transform(value);
198
+ decl.value = result;
199
+ cache[value] = result;
200
+ });
201
+ }
143
202
 
144
- decl.value = result;
145
- cache[value] = result;
146
- });
147
- };
148
- });
149
- module.exports = exports['default'];
203
+ };
204
+ }
205
+
206
+ pluginCreator.postcss = true;
207
+ var _default = pluginCreator;
208
+ exports.default = _default;
209
+ module.exports = exports.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-normalize-positions",
3
- "version": "4.0.2",
3
+ "version": "5.0.0",
4
4
  "description": "Normalize keyword values for position into length values.",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -8,7 +8,9 @@
8
8
  "LICENSE-MIT"
9
9
  ],
10
10
  "scripts": {
11
- "prepublish": "cross-env BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/"
11
+ "prebuild": "del-cli dist",
12
+ "build": "cross-env BABEL_ENV=publish babel src --config-file ../../babel.config.js --out-dir dist --ignore \"**/__tests__/\"",
13
+ "prepublish": "yarn build"
12
14
  },
13
15
  "keywords": [
14
16
  "css",
@@ -16,10 +18,6 @@
16
18
  "postcss-plugin"
17
19
  ],
18
20
  "license": "MIT",
19
- "devDependencies": {
20
- "babel-cli": "^6.0.0",
21
- "cross-env": "^5.0.0"
22
- },
23
21
  "homepage": "https://github.com/cssnano/cssnano",
24
22
  "author": {
25
23
  "name": "Ben Briggs",
@@ -28,15 +26,19 @@
28
26
  },
29
27
  "repository": "cssnano/cssnano",
30
28
  "dependencies": {
31
- "cssnano-util-get-arguments": "^4.0.0",
32
- "has": "^1.0.0",
33
- "postcss": "^7.0.0",
34
- "postcss-value-parser": "^3.0.0"
29
+ "postcss-value-parser": "^4.1.0"
35
30
  },
36
31
  "bugs": {
37
32
  "url": "https://github.com/cssnano/cssnano/issues"
38
33
  },
39
34
  "engines": {
40
- "node": ">=6.9.0"
41
- }
35
+ "node": "^10 || ^12 || >=14.0"
36
+ },
37
+ "devDependencies": {
38
+ "postcss": "^8.2.1"
39
+ },
40
+ "peerDependencies": {
41
+ "postcss": "^8.2.1"
42
+ },
43
+ "gitHead": "0e2c3bf5835bafcdc8783bef66f730a24194c8f3"
42
44
  }