postcss-normalize-positions 4.0.1 → 4.0.2

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/dist/index.js CHANGED
@@ -34,77 +34,116 @@ const vertical = {
34
34
  top: '0'
35
35
  };
36
36
 
37
- function transform(decl) {
38
- const values = (0, _postcssValueParser2.default)(decl.value);
39
- const args = (0, _cssnanoUtilGetArguments2.default)(values);
37
+ function transform(value) {
38
+ const parsed = (0, _postcssValueParser2.default)(value);
39
+ const args = (0, _cssnanoUtilGetArguments2.default)(parsed);
40
40
  const relevant = [];
41
+
41
42
  args.forEach(arg => {
42
43
  relevant.push({
43
44
  start: null,
44
45
  end: null
45
46
  });
47
+
46
48
  arg.forEach((part, index) => {
47
49
  const isPosition = ~directions.indexOf(part.value.toLowerCase()) || (0, _postcssValueParser.unit)(part.value);
48
50
  const len = relevant.length - 1;
51
+
49
52
  if (relevant[len].start === null && isPosition) {
50
53
  relevant[len].start = index;
51
54
  relevant[len].end = index;
55
+
52
56
  return;
53
57
  }
58
+
54
59
  if (relevant[len].start !== null) {
55
60
  if (part.type === 'space') {
56
61
  return;
57
62
  } else if (isPosition) {
58
63
  relevant[len].end = index;
64
+
59
65
  return;
60
66
  }
67
+
61
68
  return;
62
69
  }
63
70
  });
64
71
  });
72
+
65
73
  relevant.forEach((range, index) => {
66
74
  if (range.start === null) {
67
75
  return;
68
76
  }
77
+
69
78
  const position = args[index].slice(range.start, range.end + 1);
79
+
70
80
  if (position.length > 3) {
71
81
  return;
72
82
  }
83
+
73
84
  const firstValue = position[0].value.toLowerCase();
74
85
  const secondValue = position[2] && position[2].value ? position[2].value.toLowerCase() : null;
86
+
75
87
  if (position.length === 1 || secondValue === 'center') {
76
88
  if (secondValue) {
77
89
  position[2].value = position[1].value = '';
78
90
  }
91
+
79
92
  const map = Object.assign({}, horizontal, {
80
93
  center
81
94
  });
95
+
82
96
  if ((0, _has2.default)(map, firstValue)) {
83
97
  position[0].value = map[firstValue];
84
98
  }
99
+
85
100
  return;
86
101
  }
102
+
87
103
  if (firstValue === 'center' && ~directions.indexOf(secondValue)) {
88
104
  position[0].value = position[1].value = '';
105
+
89
106
  if ((0, _has2.default)(horizontal, secondValue)) {
90
107
  position[2].value = horizontal[secondValue];
91
108
  }
109
+
92
110
  return;
93
111
  }
112
+
94
113
  if ((0, _has2.default)(horizontal, firstValue) && (0, _has2.default)(vertical, secondValue)) {
95
114
  position[0].value = horizontal[firstValue];
96
115
  position[2].value = vertical[secondValue];
116
+
97
117
  return;
98
118
  } else if ((0, _has2.default)(vertical, firstValue) && (0, _has2.default)(horizontal, secondValue)) {
99
119
  position[0].value = horizontal[secondValue];
100
120
  position[2].value = vertical[firstValue];
121
+
101
122
  return;
102
123
  }
103
124
  });
104
- decl.value = values.toString();
125
+
126
+ return parsed.toString();
105
127
  }
106
128
 
107
129
  exports.default = (0, _postcss.plugin)('postcss-normalize-positions', () => {
108
- return css => css.walkDecls(/^(background(-position)?|(-webkit-)?perspective-origin)$/i, transform);
130
+ return css => {
131
+ const cache = {};
132
+
133
+ css.walkDecls(/^(background(-position)?|(-webkit-)?perspective-origin)$/i, decl => {
134
+ const value = decl.value;
135
+
136
+ if (cache[value]) {
137
+ decl.value = cache[value];
138
+
139
+ return;
140
+ }
141
+
142
+ const result = transform(value);
143
+
144
+ decl.value = result;
145
+ cache[value] = result;
146
+ });
147
+ };
109
148
  });
110
149
  module.exports = exports['default'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-normalize-positions",
3
- "version": "4.0.1",
3
+ "version": "4.0.2",
4
4
  "description": "Normalize keyword values for position into length values.",
5
5
  "main": "dist/index.js",
6
6
  "files": [
package/CHANGELOG.md DELETED
@@ -1,4 +0,0 @@
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.