postcss-normalize-string 5.0.0 → 5.0.4

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,17 +1,12 @@
1
1
  {
2
2
  "name": "postcss-normalize-string",
3
- "version": "5.0.0",
3
+ "version": "5.0.4",
4
4
  "description": "Normalize wrapping quotes for CSS string literals.",
5
- "main": "dist/index.js",
5
+ "main": "src/index.js",
6
6
  "files": [
7
- "dist",
7
+ "src",
8
8
  "LICENSE-MIT"
9
9
  ],
10
- "scripts": {
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"
14
- },
15
10
  "keywords": [
16
11
  "css",
17
12
  "postcss",
@@ -26,7 +21,7 @@
26
21
  },
27
22
  "repository": "cssnano/cssnano",
28
23
  "dependencies": {
29
- "postcss-value-parser": "^4.1.0"
24
+ "postcss-value-parser": "^4.2.0"
30
25
  },
31
26
  "bugs": {
32
27
  "url": "https://github.com/cssnano/cssnano/issues"
@@ -35,10 +30,10 @@
35
30
  "node": "^10 || ^12 || >=14.0"
36
31
  },
37
32
  "devDependencies": {
38
- "postcss": "^8.2.1"
33
+ "postcss": "^8.2.15"
39
34
  },
40
35
  "peerDependencies": {
41
- "postcss": "^8.2.1"
36
+ "postcss": "^8.2.15"
42
37
  },
43
- "gitHead": "0e2c3bf5835bafcdc8783bef66f730a24194c8f3"
44
- }
38
+ "readme": "# [postcss][postcss]-normalize-string\n\n> Normalize strings with PostCSS.\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-normalize-string) do:\n\n```\nnpm install postcss-normalize-string --save\n```\n\n## Example\n\n### Input\n\n```css\np:after{ content: '\\\\'string\\\\' is intact' }\n```\n\n### Output\n\n```css\np:after{ content:\"'string' is intact\" }\n```\n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n## API\n\n### normalize([options])\n\n#### options\n\n##### preferredQuote\n\nType: `string`\nDefault: `double`\n\nSets what type of quote to prefer. Possible values are `single` and `double`.\n\n```js\nvar css = 'p:after{content:\"\"}';\nconsole.log(postcss(normalize({preferredQuote: 'single'})).process(css).css);\n//=> p:after{content:''}\n```\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"
39
+ }
@@ -1,17 +1,10 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
9
-
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1
+ 'use strict';
2
+ const valueParser = require('postcss-value-parser');
11
3
 
12
4
  /*
13
5
  * Constants (parser usage)
14
6
  */
7
+
15
8
  const SINGLE_QUOTE = "'".charCodeAt(0);
16
9
  const DOUBLE_QUOTE = '"'.charCodeAt(0);
17
10
  const BACKSLASH = '\\'.charCodeAt(0);
@@ -20,7 +13,9 @@ const SPACE = ' '.charCodeAt(0);
20
13
  const FEED = '\f'.charCodeAt(0);
21
14
  const TAB = '\t'.charCodeAt(0);
22
15
  const CR = '\r'.charCodeAt(0);
16
+
23
17
  const WORD_END = /[ \n\t\r\f'"\\]/g;
18
+
24
19
  /*
25
20
  * Constants (node type strings)
26
21
  */
@@ -32,6 +27,7 @@ const C_SINGLE_QUOTE = 'singleQuote';
32
27
  const C_DOUBLE_QUOTE = 'doubleQuote';
33
28
  const C_NEWLINE = 'newline';
34
29
  const C_SINGLE = 'single';
30
+
35
31
  /*
36
32
  * Literals
37
33
  */
@@ -39,35 +35,19 @@ const C_SINGLE = 'single';
39
35
  const L_SINGLE_QUOTE = `'`;
40
36
  const L_DOUBLE_QUOTE = `"`;
41
37
  const L_NEWLINE = `\\\n`;
38
+
42
39
  /*
43
40
  * Parser nodes
44
41
  */
45
42
 
46
- const T_ESCAPED_SINGLE_QUOTE = {
47
- type: C_ESCAPED_SINGLE_QUOTE,
48
- value: `\\'`
49
- };
50
- const T_ESCAPED_DOUBLE_QUOTE = {
51
- type: C_ESCAPED_DOUBLE_QUOTE,
52
- value: `\\"`
53
- };
54
- const T_SINGLE_QUOTE = {
55
- type: C_SINGLE_QUOTE,
56
- value: L_SINGLE_QUOTE
57
- };
58
- const T_DOUBLE_QUOTE = {
59
- type: C_DOUBLE_QUOTE,
60
- value: L_DOUBLE_QUOTE
61
- };
62
- const T_NEWLINE = {
63
- type: C_NEWLINE,
64
- value: L_NEWLINE
65
- };
43
+ const T_ESCAPED_SINGLE_QUOTE = { type: C_ESCAPED_SINGLE_QUOTE, value: `\\'` };
44
+ const T_ESCAPED_DOUBLE_QUOTE = { type: C_ESCAPED_DOUBLE_QUOTE, value: `\\"` };
45
+ const T_SINGLE_QUOTE = { type: C_SINGLE_QUOTE, value: L_SINGLE_QUOTE };
46
+ const T_DOUBLE_QUOTE = { type: C_DOUBLE_QUOTE, value: L_DOUBLE_QUOTE };
47
+ const T_NEWLINE = { type: C_NEWLINE, value: L_NEWLINE };
66
48
 
67
49
  function stringify(ast) {
68
- return ast.nodes.reduce((str, {
69
- value
70
- }) => {
50
+ return ast.nodes.reduce((str, { value }) => {
71
51
  // Collapse multiple line strings automatically
72
52
  if (value === L_NEWLINE) {
73
53
  return str;
@@ -81,15 +61,16 @@ function parse(str) {
81
61
  let code, next, value;
82
62
  let pos = 0;
83
63
  let len = str.length;
64
+
84
65
  const ast = {
85
66
  nodes: [],
86
67
  types: {
87
68
  escapedSingleQuote: 0,
88
69
  escapedDoubleQuote: 0,
89
70
  singleQuote: 0,
90
- doubleQuote: 0
71
+ doubleQuote: 0,
91
72
  },
92
- quotes: false
73
+ quotes: false,
93
74
  };
94
75
 
95
76
  while (pos < len) {
@@ -105,27 +86,30 @@ function parse(str) {
105
86
  do {
106
87
  next += 1;
107
88
  code = str.charCodeAt(next);
108
- } while (code === SPACE || code === NEWLINE || code === TAB || code === CR || code === FEED);
89
+ } while (
90
+ code === SPACE ||
91
+ code === NEWLINE ||
92
+ code === TAB ||
93
+ code === CR ||
94
+ code === FEED
95
+ );
109
96
 
110
97
  ast.nodes.push({
111
98
  type: 'space',
112
- value: str.slice(pos, next)
99
+ value: str.slice(pos, next),
113
100
  });
114
101
  pos = next - 1;
115
102
  break;
116
-
117
103
  case SINGLE_QUOTE:
118
104
  ast.nodes.push(T_SINGLE_QUOTE);
119
105
  ast.types[C_SINGLE_QUOTE]++;
120
106
  ast.quotes = true;
121
107
  break;
122
-
123
108
  case DOUBLE_QUOTE:
124
109
  ast.nodes.push(T_DOUBLE_QUOTE);
125
110
  ast.types[C_DOUBLE_QUOTE]++;
126
111
  ast.quotes = true;
127
112
  break;
128
-
129
113
  case BACKSLASH:
130
114
  next = pos + 1;
131
115
 
@@ -146,12 +130,10 @@ function parse(str) {
146
130
  pos = next;
147
131
  break;
148
132
  }
149
-
150
133
  /*
151
134
  * We need to fall through here to handle the token as
152
135
  * a whole word. The missing 'break' is intentional.
153
136
  */
154
-
155
137
  default:
156
138
  WORD_END.lastIndex = pos + 1;
157
139
  WORD_END.test(str);
@@ -163,13 +145,14 @@ function parse(str) {
163
145
  }
164
146
 
165
147
  value = str.slice(pos, next + 1);
148
+
166
149
  ast.nodes.push({
167
150
  type: C_STRING,
168
- value
151
+ value,
169
152
  });
153
+
170
154
  pos = next;
171
155
  }
172
-
173
156
  pos++;
174
157
  }
175
158
 
@@ -177,33 +160,51 @@ function parse(str) {
177
160
  }
178
161
 
179
162
  function changeWrappingQuotes(node, ast) {
180
- const {
181
- types
182
- } = ast;
163
+ const { types } = ast;
183
164
 
184
165
  if (types[C_SINGLE_QUOTE] || types[C_DOUBLE_QUOTE]) {
185
166
  return;
186
167
  }
187
168
 
188
- if (node.quote === L_SINGLE_QUOTE && types[C_ESCAPED_SINGLE_QUOTE] > 0 && !types[C_ESCAPED_DOUBLE_QUOTE]) {
169
+ if (
170
+ node.quote === L_SINGLE_QUOTE &&
171
+ types[C_ESCAPED_SINGLE_QUOTE] > 0 &&
172
+ !types[C_ESCAPED_DOUBLE_QUOTE]
173
+ ) {
189
174
  node.quote = L_DOUBLE_QUOTE;
190
175
  }
191
176
 
192
- if (node.quote === L_DOUBLE_QUOTE && types[C_ESCAPED_DOUBLE_QUOTE] > 0 && !types[C_ESCAPED_SINGLE_QUOTE]) {
177
+ if (
178
+ node.quote === L_DOUBLE_QUOTE &&
179
+ types[C_ESCAPED_DOUBLE_QUOTE] > 0 &&
180
+ !types[C_ESCAPED_SINGLE_QUOTE]
181
+ ) {
193
182
  node.quote = L_SINGLE_QUOTE;
194
183
  }
195
184
 
196
- ast.nodes = ast.nodes.reduce((newAst, child) => {
197
- if (child.type === C_ESCAPED_DOUBLE_QUOTE && node.quote === L_SINGLE_QUOTE) {
198
- return [...newAst, T_DOUBLE_QUOTE];
199
- }
200
-
201
- if (child.type === C_ESCAPED_SINGLE_QUOTE && node.quote === L_DOUBLE_QUOTE) {
202
- return [...newAst, T_SINGLE_QUOTE];
185
+ ast.nodes = changeChildQuotes(ast.nodes, node.quote);
186
+ }
187
+ /**
188
+ * @param {string} parentQuote
189
+ */
190
+ function changeChildQuotes(childNodes, parentQuote) {
191
+ const updatedChildren = [];
192
+ for (const child of childNodes) {
193
+ if (
194
+ child.type === C_ESCAPED_DOUBLE_QUOTE &&
195
+ parentQuote === L_SINGLE_QUOTE
196
+ ) {
197
+ updatedChildren.push(T_DOUBLE_QUOTE);
198
+ } else if (
199
+ child.type === C_ESCAPED_SINGLE_QUOTE &&
200
+ parentQuote === L_DOUBLE_QUOTE
201
+ ) {
202
+ updatedChildren.push(T_SINGLE_QUOTE);
203
+ } else {
204
+ updatedChildren.push(child);
203
205
  }
204
-
205
- return [...newAst, child];
206
- }, []);
206
+ }
207
+ return updatedChildren;
207
208
  }
208
209
 
209
210
  function normalize(value, preferredQuote) {
@@ -211,67 +212,68 @@ function normalize(value, preferredQuote) {
211
212
  return value;
212
213
  }
213
214
 
214
- return (0, _postcssValueParser.default)(value).walk(child => {
215
- if (child.type !== C_STRING) {
216
- return;
217
- }
218
-
219
- const ast = parse(child.value);
220
-
221
- if (ast.quotes) {
222
- changeWrappingQuotes(child, ast);
223
- } else if (preferredQuote === C_SINGLE) {
224
- child.quote = L_SINGLE_QUOTE;
225
- } else {
226
- child.quote = L_DOUBLE_QUOTE;
227
- }
228
-
229
- child.value = stringify(ast);
230
- }).toString();
215
+ return valueParser(value)
216
+ .walk((child) => {
217
+ if (child.type !== C_STRING) {
218
+ return;
219
+ }
220
+
221
+ const ast = parse(child.value);
222
+
223
+ if (ast.quotes) {
224
+ changeWrappingQuotes(child, ast);
225
+ } else if (preferredQuote === C_SINGLE) {
226
+ child.quote = L_SINGLE_QUOTE;
227
+ } else {
228
+ child.quote = L_DOUBLE_QUOTE;
229
+ }
230
+
231
+ child.value = stringify(ast);
232
+ })
233
+ .toString();
231
234
  }
232
235
 
233
- const params = {
234
- rule: 'selector',
235
- decl: 'value',
236
- atrule: 'params'
237
- };
236
+ function minify(original, cache, preferredQuote) {
237
+ const key = original + '|' + preferredQuote;
238
+ if (cache.has(key)) {
239
+ return cache.get(key);
240
+ }
241
+ const newValue = normalize(original, preferredQuote);
242
+ cache.set(key, newValue);
243
+ return newValue;
244
+ }
238
245
 
239
246
  function pluginCreator(opts) {
240
- const {
241
- preferredQuote
242
- } = Object.assign({}, {
243
- preferredQuote: 'double'
244
- }, opts);
247
+ const { preferredQuote } = Object.assign(
248
+ {},
249
+ {
250
+ preferredQuote: 'double',
251
+ },
252
+ opts
253
+ );
254
+
245
255
  return {
246
256
  postcssPlugin: 'postcss-normalize-string',
247
257
 
248
258
  OnceExit(css) {
249
- const cache = {};
250
- css.walk(node => {
251
- const {
252
- type
253
- } = node;
254
-
255
- if (Object.prototype.hasOwnProperty.call(params, type)) {
256
- const param = params[type];
257
- const key = node[param] + '|' + preferredQuote;
258
-
259
- if (cache[key]) {
260
- node[param] = cache[key];
261
- return;
262
- }
263
-
264
- const newValue = normalize(node[param], preferredQuote);
265
- node[param] = newValue;
266
- cache[key] = newValue;
259
+ const cache = new Map();
260
+
261
+ css.walk((node) => {
262
+ switch (node.type) {
263
+ case 'rule':
264
+ node.selector = minify(node.selector, cache, preferredQuote);
265
+ break;
266
+ case 'decl':
267
+ node.value = minify(node.value, cache, preferredQuote);
268
+ break;
269
+ case 'atrule':
270
+ node.params = minify(node.params, cache, preferredQuote);
271
+ break;
267
272
  }
268
273
  });
269
- }
270
-
274
+ },
271
275
  };
272
276
  }
273
277
 
274
278
  pluginCreator.postcss = true;
275
- var _default = pluginCreator;
276
- exports.default = _default;
277
- module.exports = exports.default;
279
+ module.exports = pluginCreator;
package/CHANGELOG.md DELETED
@@ -1,69 +0,0 @@
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-string@5.0.0-rc.2...postcss-normalize-string@5.0.0) (2021-04-06)
7
-
8
- **Note:** Version bump only for package postcss-normalize-string
9
-
10
-
11
-
12
-
13
-
14
- # [5.0.0-rc.2](https://github.com/cssnano/cssnano/compare/postcss-normalize-string@5.0.0-rc.1...postcss-normalize-string@5.0.0-rc.2) (2021-03-15)
15
-
16
- **Note:** Version bump only for package postcss-normalize-string
17
-
18
-
19
-
20
-
21
-
22
- # [5.0.0-rc.1](https://github.com/cssnano/cssnano/compare/postcss-normalize-string@5.0.0-rc.0...postcss-normalize-string@5.0.0-rc.1) (2021-03-04)
23
-
24
- **Note:** Version bump only for package postcss-normalize-string
25
-
26
-
27
-
28
-
29
-
30
- # 5.0.0-rc.0 (2021-02-19)
31
-
32
-
33
- ### Bug Fixes
34
-
35
- * **postcss-ordered-values:** columns transform returning string instead of the AST ([#928](https://github.com/cssnano/cssnano/issues/928)) ([a5d6d36](https://github.com/cssnano/cssnano/commit/a5d6d364e0815ecb198a95de301f3554ccce4f78))
36
-
37
-
38
- ### chore
39
-
40
- * minimum require version of node is 10.13 ([#871](https://github.com/cssnano/cssnano/issues/871)) ([28bda24](https://github.com/cssnano/cssnano/commit/28bda243e32ce3ba89b3c358a5f78727b3732f11))
41
-
42
-
43
- ### Features
44
-
45
- * migarete to PostCSS 8 ([#975](https://github.com/cssnano/cssnano/issues/975)) ([40b82dc](https://github.com/cssnano/cssnano/commit/40b82dca7f53ac02cd4fe62846dec79b898ccb49))
46
-
47
-
48
- ### BREAKING CHANGES
49
-
50
- * minimum supported `postcss` version is `8.2.1`
51
- * minimum require version of node is 10.13
52
-
53
-
54
-
55
- ## 4.1.9 (2019-02-12)
56
-
57
-
58
- ### Performance Improvements
59
-
60
- * **postcss-normalize-string:** increase perf ([#689](https://github.com/cssnano/cssnano/issues/689)) ([07948cb](https://github.com/cssnano/cssnano/commit/07948cb3ff433825682c499516640abeeec7383a))
61
-
62
-
63
-
64
- ## 4.1.1 (2018-09-24)
65
-
66
-
67
- ### Bug Fixes
68
-
69
- * **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)