postcss-normalize-positions 4.0.0-nightly.2020.9.3 → 4.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # 4.0.0-rc.0
2
+
3
+ * Initial commit. This module was previously part of other cssnano modules and
4
+ has been extracted out.
package/README.md CHANGED
@@ -16,7 +16,7 @@ npm install postcss-normalize-positions --save
16
16
 
17
17
  ```css
18
18
  div {
19
- background-position: bottom left;
19
+ background-position: bottom right
20
20
  }
21
21
  ```
22
22
 
@@ -24,7 +24,7 @@ div {
24
24
 
25
25
  ```css
26
26
  div {
27
- background-position:0 100%;
27
+ background-position: 100% 100%
28
28
  }
29
29
  ```
30
30
 
package/dist/index.js CHANGED
@@ -1,208 +1,111 @@
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;
7
6
 
8
- var _postcss = require("postcss");
7
+ var _postcss = require('postcss');
9
8
 
10
- var _postcssValueParser = _interopRequireWildcard(require("postcss-value-parser"));
9
+ var _postcssValueParser = require('postcss-value-parser');
11
10
 
12
- var _has = _interopRequireDefault(require("has"));
11
+ var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
13
12
 
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
17
-
18
- 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; }
19
-
20
- const directionKeywords = ['top', 'right', 'bottom', 'left', 'center'];
21
- const center = '50%';
22
- const horizontal = {
23
- right: '100%',
24
- left: '0'
25
- };
26
- const verticalValue = {
27
- bottom: '100%',
28
- top: '0'
29
- };
30
-
31
- function isCommaNode(node) {
32
- return node.type === 'div' && node.value === ',';
33
- }
34
-
35
- function isVariableFunctionNode(node) {
36
- if (node.type !== 'function') {
37
- return false;
38
- }
39
-
40
- return ['var', 'env'].includes(node.value.toLowerCase());
41
- }
42
-
43
- function isMathFunctionNode(node) {
44
- if (node.type !== 'function') {
45
- return false;
46
- }
47
-
48
- return ['calc', 'min', 'max', 'clamp'].includes(node.value.toLowerCase());
49
- }
50
-
51
- function isNumberNode(node) {
52
- if (node.type !== 'word') {
53
- return false;
54
- }
55
-
56
- const value = parseFloat(node.value);
57
- return !isNaN(value);
58
- }
59
-
60
- function isDimensionNode(node) {
61
- if (node.type !== 'word') {
62
- return false;
63
- }
64
-
65
- const parsed = (0, _postcssValueParser.unit)(node.value);
66
-
67
- if (!parsed) {
68
- return false;
69
- }
70
-
71
- return parsed.unit !== '';
72
- }
73
-
74
- function transform(value) {
75
- const parsed = (0, _postcssValueParser.default)(value);
76
- const ranges = [];
77
- let rangeIndex = 0;
78
- let shouldContinue = true;
79
- parsed.nodes.forEach((node, index) => {
80
- // After comma (`,`) follows next background
81
- if (isCommaNode(node)) {
82
- rangeIndex += 1;
83
- shouldContinue = true;
84
- return;
85
- }
13
+ var _cssnanoUtilGetArguments = require('cssnano-util-get-arguments');
86
14
 
87
- if (!shouldContinue) {
88
- return;
89
- } // After separator (`/`) follows `background-size` values
90
- // Avoid them
15
+ var _cssnanoUtilGetArguments2 = _interopRequireDefault(_cssnanoUtilGetArguments);
91
16
 
17
+ var _has = require('has');
92
18
 
93
- if (node.type === 'div' && node.value === '/') {
94
- shouldContinue = false;
95
- return;
96
- }
19
+ var _has2 = _interopRequireDefault(_has);
97
20
 
98
- if (!ranges[rangeIndex]) {
99
- ranges[rangeIndex] = {
100
- start: null,
101
- end: null
102
- };
103
- } // Do not try to be processed `var and `env` function inside background
104
-
105
-
106
- if (isVariableFunctionNode(node)) {
107
- shouldContinue = false;
108
- ranges[rangeIndex].start = null;
109
- ranges[rangeIndex].end = null;
110
- return;
111
- }
112
-
113
- const isPositionKeyword = node.type === 'word' && directionKeywords.includes(node.value.toLowerCase()) || isDimensionNode(node) || isNumberNode(node) || isMathFunctionNode(node);
114
-
115
- if (ranges[rangeIndex].start === null && isPositionKeyword) {
116
- ranges[rangeIndex].start = index;
117
- ranges[rangeIndex].end = index;
118
- return;
119
- }
120
-
121
- if (ranges[rangeIndex].start !== null) {
122
- if (node.type === 'space') {
123
- return;
124
- } else if (isPositionKeyword) {
125
- ranges[rangeIndex].end = index;
126
- return;
127
- }
128
-
129
- return;
130
- }
131
- });
132
- ranges.forEach(range => {
133
- if (range.start === null) {
134
- return;
135
- }
136
-
137
- const nodes = parsed.nodes.slice(range.start, range.end + 1);
138
-
139
- if (nodes.length > 3) {
140
- return;
141
- }
142
-
143
- const firstNode = nodes[0].value.toLowerCase();
144
- const secondNode = nodes[2] && nodes[2].value ? nodes[2].value.toLowerCase() : null;
145
-
146
- if (nodes.length === 1 || secondNode === 'center') {
147
- if (secondNode) {
148
- nodes[2].value = nodes[1].value = '';
149
- }
150
-
151
- const map = Object.assign({}, horizontal, {
152
- center
153
- });
154
-
155
- if ((0, _has.default)(map, firstNode)) {
156
- nodes[0].value = map[firstNode];
157
- }
21
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
158
22
 
159
- return;
160
- }
23
+ const directions = ['top', 'right', 'bottom', 'left', 'center'];
161
24
 
162
- if (firstNode === 'center' && directionKeywords.includes(secondNode)) {
163
- nodes[0].value = nodes[1].value = '';
25
+ const center = '50%';
164
26
 
165
- if ((0, _has.default)(horizontal, secondNode)) {
166
- nodes[2].value = horizontal[secondNode];
167
- }
27
+ const horizontal = {
28
+ right: '100%',
29
+ left: '0'
30
+ };
168
31
 
169
- return;
170
- }
32
+ const vertical = {
33
+ bottom: '100%',
34
+ top: '0'
35
+ };
171
36
 
172
- if ((0, _has.default)(horizontal, firstNode) && (0, _has.default)(verticalValue, secondNode)) {
173
- nodes[0].value = horizontal[firstNode];
174
- nodes[2].value = verticalValue[secondNode];
175
- return;
176
- } else if ((0, _has.default)(verticalValue, firstNode) && (0, _has.default)(horizontal, secondNode)) {
177
- nodes[0].value = horizontal[secondNode];
178
- nodes[2].value = verticalValue[firstNode];
179
- return;
180
- }
181
- });
182
- return parsed.toString();
37
+ function transform(decl) {
38
+ const values = (0, _postcssValueParser2.default)(decl.value);
39
+ const args = (0, _cssnanoUtilGetArguments2.default)(values);
40
+ const relevant = [];
41
+ args.forEach(arg => {
42
+ relevant.push({
43
+ start: null,
44
+ end: null
45
+ });
46
+ arg.forEach((part, index) => {
47
+ const isPosition = ~directions.indexOf(part.value) || (0, _postcssValueParser.unit)(part.value);
48
+ const len = relevant.length - 1;
49
+ if (relevant[len].start === null && isPosition) {
50
+ relevant[len].start = index;
51
+ relevant[len].end = index;
52
+ return;
53
+ }
54
+ if (relevant[len].start !== null) {
55
+ if (part.type === 'space') {
56
+ return;
57
+ } else if (isPosition) {
58
+ relevant[len].end = index;
59
+ return;
60
+ }
61
+ return;
62
+ }
63
+ });
64
+ });
65
+ relevant.forEach((range, index) => {
66
+ if (range.start === null) {
67
+ return;
68
+ }
69
+ const position = args[index].slice(range.start, range.end + 1);
70
+ if (position.length > 3) {
71
+ return;
72
+ }
73
+ if (position.length === 1 || position[2].value === 'center') {
74
+ if (position[2]) {
75
+ position[2].value = position[1].value = '';
76
+ }
77
+ const { value } = position[0];
78
+ const map = Object.assign({}, horizontal, {
79
+ center
80
+ });
81
+ if ((0, _has2.default)(map, value)) {
82
+ position[0].value = map[value];
83
+ }
84
+ return;
85
+ }
86
+ if (position[0].value === 'center' && ~directions.indexOf(position[2].value)) {
87
+ position[0].value = position[1].value = '';
88
+ const { value } = position[2];
89
+ if ((0, _has2.default)(horizontal, value)) {
90
+ position[2].value = horizontal[value];
91
+ }
92
+ return;
93
+ }
94
+ if ((0, _has2.default)(horizontal, position[0].value) && (0, _has2.default)(vertical, position[2].value)) {
95
+ position[0].value = horizontal[position[0].value];
96
+ position[2].value = vertical[position[2].value];
97
+ return;
98
+ } else if ((0, _has2.default)(vertical, position[0].value) && (0, _has2.default)(horizontal, position[2].value)) {
99
+ let first = position[0].value;
100
+ position[0].value = horizontal[position[2].value];
101
+ position[2].value = vertical[first];
102
+ return;
103
+ }
104
+ });
105
+ decl.value = values.toString();
183
106
  }
184
107
 
185
- var _default = (0, _postcss.plugin)('postcss-normalize-positions', () => {
186
- return css => {
187
- const cache = {};
188
- css.walkDecls(/^(background(-position)?|(-\w+-)?perspective-origin)$/i, decl => {
189
- const value = decl.value;
190
-
191
- if (!value) {
192
- return;
193
- }
194
-
195
- if (cache[value]) {
196
- decl.value = cache[value];
197
- return;
198
- }
199
-
200
- const result = transform(value);
201
- decl.value = result;
202
- cache[value] = result;
203
- });
204
- };
108
+ exports.default = (0, _postcss.plugin)('postcss-normalize-positions', () => {
109
+ return css => css.walkDecls(/^(background(-position)?|(-webkit-)?perspective-origin)$/i, transform);
205
110
  });
206
-
207
- exports.default = _default;
208
- module.exports = exports.default;
111
+ module.exports = exports['default'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-normalize-positions",
3
- "version": "4.0.0-nightly.2020.9.3",
3
+ "version": "4.0.0",
4
4
  "description": "Normalize keyword values for position into length values.",
5
5
  "main": "dist/index.js",
6
6
  "files": [
@@ -8,9 +8,7 @@
8
8
  "LICENSE-MIT"
9
9
  ],
10
10
  "scripts": {
11
- "prebuild": "",
12
- "build": "cross-env BABEL_ENV=publish babel src --config-file ../../babel.config.js --out-dir dist --ignore \"**/__tests__/\"",
13
- "prepublish": ""
11
+ "prepublish": "cross-env BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/"
14
12
  },
15
13
  "keywords": [
16
14
  "css",
@@ -18,6 +16,10 @@
18
16
  "postcss-plugin"
19
17
  ],
20
18
  "license": "MIT",
19
+ "devDependencies": {
20
+ "babel-cli": "^6.0.0",
21
+ "cross-env": "^5.0.0"
22
+ },
21
23
  "homepage": "https://github.com/cssnano/cssnano",
22
24
  "author": {
23
25
  "name": "Ben Briggs",
@@ -26,14 +28,15 @@
26
28
  },
27
29
  "repository": "cssnano/cssnano",
28
30
  "dependencies": {
29
- "has": "^1.0.3",
30
- "postcss": "^7.0.16",
31
- "postcss-value-parser": "^3.3.1"
31
+ "cssnano-util-get-arguments": "^4.0.0",
32
+ "has": "^1.0.0",
33
+ "postcss": "^6.0.0",
34
+ "postcss-value-parser": "^3.0.0"
32
35
  },
33
36
  "bugs": {
34
37
  "url": "https://github.com/cssnano/cssnano/issues"
35
38
  },
36
39
  "engines": {
37
- "node": ">=10.13.0"
40
+ "node": ">=6.9.0"
38
41
  }
39
- }
42
+ }