postcss-normalize-string 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.
Files changed (3) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/index.js +167 -206
  3. package/package.json +11 -9
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/dist/index.js CHANGED
@@ -1,22 +1,28 @@
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 = _interopRequireDefault(require("postcss"));
7
+ var _postcss = require('postcss');
9
8
 
10
- var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
9
+ var _postcss2 = _interopRequireDefault(_postcss);
11
10
 
12
- var _has = _interopRequireDefault(require("has"));
11
+ var _postcssValueParser = require('postcss-value-parser');
12
+
13
+ var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
14
+
15
+ var _has = require('has');
16
+
17
+ var _has2 = _interopRequireDefault(_has);
13
18
 
14
19
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
20
 
16
21
  /*
17
22
  * Constants (parser usage)
18
23
  */
19
- const SINGLE_QUOTE = "'".charCodeAt(0);
24
+
25
+ const SINGLE_QUOTE = '\''.charCodeAt(0);
20
26
  const DOUBLE_QUOTE = '"'.charCodeAt(0);
21
27
  const BACKSLASH = '\\'.charCodeAt(0);
22
28
  const NEWLINE = '\n'.charCodeAt(0);
@@ -24,7 +30,9 @@ const SPACE = ' '.charCodeAt(0);
24
30
  const FEED = '\f'.charCodeAt(0);
25
31
  const TAB = '\t'.charCodeAt(0);
26
32
  const CR = '\r'.charCodeAt(0);
33
+
27
34
  const WORD_END = /[ \n\t\r\f'"\\]/g;
35
+
28
36
  /*
29
37
  * Constants (node type strings)
30
38
  */
@@ -36,6 +44,7 @@ const C_SINGLE_QUOTE = 'singleQuote';
36
44
  const C_DOUBLE_QUOTE = 'doubleQuote';
37
45
  const C_NEWLINE = 'newline';
38
46
  const C_SINGLE = 'single';
47
+
39
48
  /*
40
49
  * Literals
41
50
  */
@@ -43,232 +52,184 @@ const C_SINGLE = 'single';
43
52
  const L_SINGLE_QUOTE = `'`;
44
53
  const L_DOUBLE_QUOTE = `"`;
45
54
  const L_NEWLINE = `\\\n`;
55
+
46
56
  /*
47
57
  * Parser nodes
48
58
  */
49
59
 
50
- const T_ESCAPED_SINGLE_QUOTE = {
51
- type: C_ESCAPED_SINGLE_QUOTE,
52
- value: `\\'`
53
- };
54
- const T_ESCAPED_DOUBLE_QUOTE = {
55
- type: C_ESCAPED_DOUBLE_QUOTE,
56
- value: `\\"`
57
- };
58
- const T_SINGLE_QUOTE = {
59
- type: C_SINGLE_QUOTE,
60
- value: L_SINGLE_QUOTE
61
- };
62
- const T_DOUBLE_QUOTE = {
63
- type: C_DOUBLE_QUOTE,
64
- value: L_DOUBLE_QUOTE
65
- };
66
- const T_NEWLINE = {
67
- type: C_NEWLINE,
68
- value: L_NEWLINE
69
- };
60
+ const T_ESCAPED_SINGLE_QUOTE = { type: C_ESCAPED_SINGLE_QUOTE, value: `\\'` };
61
+ const T_ESCAPED_DOUBLE_QUOTE = { type: C_ESCAPED_DOUBLE_QUOTE, value: `\\"` };
62
+ const T_SINGLE_QUOTE = { type: C_SINGLE_QUOTE, value: L_SINGLE_QUOTE };
63
+ const T_DOUBLE_QUOTE = { type: C_DOUBLE_QUOTE, value: L_DOUBLE_QUOTE };
64
+ const T_NEWLINE = { type: C_NEWLINE, value: L_NEWLINE };
70
65
 
71
66
  function stringify(ast) {
72
- return ast.nodes.reduce((str, {
73
- value
74
- }) => {
75
- // Collapse multiple line strings automatically
76
- if (value === L_NEWLINE) {
77
- return str;
78
- }
79
-
80
- return str + value;
81
- }, '');
67
+ return ast.nodes.reduce((str, { value }) => {
68
+ // Collapse multiple line strings automatically
69
+ if (value === L_NEWLINE) {
70
+ return str;
71
+ }
72
+ return str + value;
73
+ }, '');
82
74
  }
83
75
 
84
76
  function parse(str) {
85
- let code, next, value;
86
- let pos = 0;
87
- let len = str.length;
88
- const ast = {
89
- nodes: [],
90
- types: {
91
- escapedSingleQuote: 0,
92
- escapedDoubleQuote: 0,
93
- singleQuote: 0,
94
- doubleQuote: 0
95
- },
96
- quotes: false
97
- };
98
-
99
- while (pos < len) {
100
- code = str.charCodeAt(pos);
101
-
102
- switch (code) {
103
- case SPACE:
104
- case TAB:
105
- case CR:
106
- case FEED:
107
- next = pos;
108
-
109
- do {
110
- next += 1;
111
- code = str.charCodeAt(next);
112
- } while (code === SPACE || code === NEWLINE || code === TAB || code === CR || code === FEED);
113
-
114
- ast.nodes.push({
115
- type: 'space',
116
- value: str.slice(pos, next)
117
- });
118
- pos = next - 1;
119
- break;
120
-
121
- case SINGLE_QUOTE:
122
- ast.nodes.push(T_SINGLE_QUOTE);
123
- ast.types[C_SINGLE_QUOTE]++;
124
- ast.quotes = true;
125
- break;
126
-
127
- case DOUBLE_QUOTE:
128
- ast.nodes.push(T_DOUBLE_QUOTE);
129
- ast.types[C_DOUBLE_QUOTE]++;
130
- ast.quotes = true;
131
- break;
132
-
133
- case BACKSLASH:
134
- next = pos + 1;
135
-
136
- if (str.charCodeAt(next) === SINGLE_QUOTE) {
137
- ast.nodes.push(T_ESCAPED_SINGLE_QUOTE);
138
- ast.types[C_ESCAPED_SINGLE_QUOTE]++;
139
- ast.quotes = true;
140
- pos = next;
141
- break;
142
- } else if (str.charCodeAt(next) === DOUBLE_QUOTE) {
143
- ast.nodes.push(T_ESCAPED_DOUBLE_QUOTE);
144
- ast.types[C_ESCAPED_DOUBLE_QUOTE]++;
145
- ast.quotes = true;
146
- pos = next;
147
- break;
148
- } else if (str.charCodeAt(next) === NEWLINE) {
149
- ast.nodes.push(T_NEWLINE);
150
- pos = next;
151
- break;
152
- }
153
-
154
- /*
155
- * We need to fall through here to handle the token as
156
- * a whole word. The missing 'break' is intentional.
157
- */
158
-
159
- default:
160
- WORD_END.lastIndex = pos + 1;
161
- WORD_END.test(str);
162
-
163
- if (WORD_END.lastIndex === 0) {
164
- next = len - 1;
165
- } else {
166
- next = WORD_END.lastIndex - 2;
77
+ let code, next, value;
78
+ let pos = 0;
79
+ let len = str.length;
80
+
81
+ const ast = {
82
+ nodes: [],
83
+ types: {
84
+ escapedSingleQuote: 0,
85
+ escapedDoubleQuote: 0,
86
+ singleQuote: 0,
87
+ doubleQuote: 0
88
+ },
89
+ quotes: false
90
+ };
91
+
92
+ while (pos < len) {
93
+ code = str.charCodeAt(pos);
94
+ switch (code) {
95
+ case SPACE:
96
+ case TAB:
97
+ case CR:
98
+ case FEED:
99
+ next = pos;
100
+ do {
101
+ next += 1;
102
+ code = str.charCodeAt(next);
103
+ } while (code === SPACE || code === NEWLINE || code === TAB || code === CR || code === FEED);
104
+
105
+ ast.nodes.push({
106
+ type: 'space',
107
+ value: str.slice(pos, next)
108
+ });
109
+ pos = next - 1;
110
+ break;
111
+ case SINGLE_QUOTE:
112
+ ast.nodes.push(T_SINGLE_QUOTE);
113
+ ast.types[C_SINGLE_QUOTE]++;
114
+ ast.quotes = true;
115
+ break;
116
+ case DOUBLE_QUOTE:
117
+ ast.nodes.push(T_DOUBLE_QUOTE);
118
+ ast.types[C_DOUBLE_QUOTE]++;
119
+ ast.quotes = true;
120
+ break;
121
+ case BACKSLASH:
122
+ next = pos + 1;
123
+ if (str.charCodeAt(next) === SINGLE_QUOTE) {
124
+ ast.nodes.push(T_ESCAPED_SINGLE_QUOTE);
125
+ ast.types[C_ESCAPED_SINGLE_QUOTE]++;
126
+ ast.quotes = true;
127
+ pos = next;
128
+ break;
129
+ } else if (str.charCodeAt(next) === DOUBLE_QUOTE) {
130
+ ast.nodes.push(T_ESCAPED_DOUBLE_QUOTE);
131
+ ast.types[C_ESCAPED_DOUBLE_QUOTE]++;
132
+ ast.quotes = true;
133
+ pos = next;
134
+ break;
135
+ } else if (str.charCodeAt(next) === NEWLINE) {
136
+ ast.nodes.push(T_NEWLINE);
137
+ pos = next;
138
+ break;
139
+ }
140
+ /*
141
+ * We need to fall through here to handle the token as
142
+ * a whole word. The missing 'break' is intentional.
143
+ */
144
+ default:
145
+ WORD_END.lastIndex = pos + 1;
146
+ WORD_END.test(str);
147
+
148
+ if (WORD_END.lastIndex === 0) {
149
+ next = len - 1;
150
+ } else {
151
+ next = WORD_END.lastIndex - 2;
152
+ }
153
+
154
+ value = str.slice(pos, next + 1);
155
+
156
+ ast.nodes.push({
157
+ type: C_STRING,
158
+ value
159
+ });
160
+
161
+ pos = next;
167
162
  }
168
-
169
- value = str.slice(pos, next + 1);
170
- ast.nodes.push({
171
- type: C_STRING,
172
- value
173
- });
174
- pos = next;
163
+ pos++;
175
164
  }
176
165
 
177
- pos++;
178
- }
179
-
180
- return ast;
166
+ return ast;
181
167
  }
182
168
 
183
169
  function changeWrappingQuotes(node, ast) {
184
- const {
185
- types
186
- } = ast;
187
-
188
- if (types[C_SINGLE_QUOTE] || types[C_DOUBLE_QUOTE]) {
189
- return;
190
- }
191
-
192
- if (node.quote === L_SINGLE_QUOTE && types[C_ESCAPED_SINGLE_QUOTE] > 0 && !types[C_ESCAPED_DOUBLE_QUOTE]) {
193
- node.quote = L_DOUBLE_QUOTE;
194
- }
195
-
196
- if (node.quote === L_DOUBLE_QUOTE && types[C_ESCAPED_DOUBLE_QUOTE] > 0 && !types[C_ESCAPED_SINGLE_QUOTE]) {
197
- node.quote = L_SINGLE_QUOTE;
198
- }
170
+ const { types } = ast;
171
+ if (types[C_SINGLE_QUOTE] || types[C_DOUBLE_QUOTE]) {
172
+ return;
173
+ }
199
174
 
200
- ast.nodes = ast.nodes.reduce((newAst, child) => {
201
- if (child.type === C_ESCAPED_DOUBLE_QUOTE && node.quote === L_SINGLE_QUOTE) {
202
- return [...newAst, T_DOUBLE_QUOTE];
175
+ if (node.quote === L_SINGLE_QUOTE && types[C_ESCAPED_SINGLE_QUOTE] > 0 && !types[C_ESCAPED_DOUBLE_QUOTE]) {
176
+ node.quote = L_DOUBLE_QUOTE;
203
177
  }
204
178
 
205
- if (child.type === C_ESCAPED_SINGLE_QUOTE && node.quote === L_DOUBLE_QUOTE) {
206
- return [...newAst, T_SINGLE_QUOTE];
179
+ if (node.quote === L_DOUBLE_QUOTE && types[C_ESCAPED_DOUBLE_QUOTE] > 0 && !types[C_ESCAPED_SINGLE_QUOTE]) {
180
+ node.quote = L_SINGLE_QUOTE;
207
181
  }
208
182
 
209
- return [...newAst, child];
210
- }, []);
183
+ ast.nodes = ast.nodes.reduce((newAst, child) => {
184
+ if (child.type === C_ESCAPED_DOUBLE_QUOTE && node.quote === L_SINGLE_QUOTE) {
185
+ return [...newAst, T_DOUBLE_QUOTE];
186
+ }
187
+ if (child.type === C_ESCAPED_SINGLE_QUOTE && node.quote === L_DOUBLE_QUOTE) {
188
+ return [...newAst, T_SINGLE_QUOTE];
189
+ }
190
+ return [...newAst, child];
191
+ }, []);
211
192
  }
212
193
 
213
194
  function normalize(value, preferredQuote) {
214
- if (!value || !value.length) {
215
- return value;
216
- }
217
-
218
- return (0, _postcssValueParser.default)(value).walk(child => {
219
- if (child.type !== C_STRING) {
220
- return;
195
+ if (!value || !value.length) {
196
+ return value;
221
197
  }
222
-
223
- const ast = parse(child.value);
224
-
225
- if (ast.quotes) {
226
- changeWrappingQuotes(child, ast);
227
- } else if (preferredQuote === C_SINGLE) {
228
- child.quote = L_SINGLE_QUOTE;
229
- } else {
230
- child.quote = L_DOUBLE_QUOTE;
231
- }
232
-
233
- child.value = stringify(ast);
234
- }).toString();
198
+ return (0, _postcssValueParser2.default)(value).walk(child => {
199
+ if (child.type !== C_STRING) {
200
+ return;
201
+ }
202
+ const ast = parse(child.value);
203
+ if (ast.quotes) {
204
+ changeWrappingQuotes(child, ast);
205
+ } else if (preferredQuote === C_SINGLE) {
206
+ child.quote = L_SINGLE_QUOTE;
207
+ } else {
208
+ child.quote = L_DOUBLE_QUOTE;
209
+ }
210
+ child.value = stringify(ast);
211
+ }).toString();
235
212
  }
236
213
 
237
214
  const params = {
238
- rule: 'selector',
239
- decl: 'value',
240
- atrule: 'params'
215
+ rule: 'selector',
216
+ decl: 'value',
217
+ atrule: 'params'
241
218
  };
242
219
 
243
- var _default = _postcss.default.plugin('postcss-normalize-string', opts => {
244
- const {
245
- preferredQuote
246
- } = Object.assign({}, {
247
- preferredQuote: 'double'
248
- }, opts);
249
- return css => {
250
- const cache = {};
251
- css.walk(node => {
252
- const {
253
- type
254
- } = node;
255
-
256
- if ((0, _has.default)(params, type)) {
257
- const param = params[type];
258
- const key = node[param] + '|' + preferredQuote;
259
-
260
- if (cache[key]) {
261
- node[param] = cache[key];
262
- return;
263
- }
264
-
265
- const newValue = normalize(node[param], preferredQuote);
266
- node[param] = newValue;
267
- cache[key] = newValue;
268
- }
269
- });
270
- };
220
+ exports.default = _postcss2.default.plugin('postcss-normalize-string', opts => {
221
+ const { preferredQuote } = Object.assign({}, {
222
+ preferredQuote: 'double'
223
+ }, opts);
224
+
225
+ return css => {
226
+ css.walk(node => {
227
+ const { type } = node;
228
+ if ((0, _has2.default)(params, type)) {
229
+ const param = params[type];
230
+ node[param] = normalize(node[param], preferredQuote);
231
+ }
232
+ });
233
+ };
271
234
  });
272
-
273
- exports.default = _default;
274
- module.exports = exports.default;
235
+ module.exports = exports['default'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-normalize-string",
3
- "version": "4.0.0-nightly.2020.9.3",
3
+ "version": "4.0.0",
4
4
  "description": "Normalize wrapping quotes for CSS string literals.",
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,14 @@
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
+ "has": "^1.0.0",
32
+ "postcss": "^6.0.0",
33
+ "postcss-value-parser": "^3.0.0"
32
34
  },
33
35
  "bugs": {
34
36
  "url": "https://github.com/cssnano/cssnano/issues"
35
37
  },
36
38
  "engines": {
37
- "node": ">=10.13.0"
39
+ "node": ">=6.9.0"
38
40
  }
39
- }
41
+ }