index.js
7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
'use strict';
const valueParser = require('postcss-value-parser');
/*
* Constants (parser usage)
*/
const SINGLE_QUOTE = "'".charCodeAt(0);
const DOUBLE_QUOTE = '"'.charCodeAt(0);
const BACKSLASH = '\\'.charCodeAt(0);
const NEWLINE = '\n'.charCodeAt(0);
const SPACE = ' '.charCodeAt(0);
const FEED = '\f'.charCodeAt(0);
const TAB = '\t'.charCodeAt(0);
const CR = '\r'.charCodeAt(0);
const WORD_END = /[ \n\t\r\f'"\\]/g;
/*
* Constants (node type strings)
*/
const C_STRING = 'string';
const C_ESCAPED_SINGLE_QUOTE = 'escapedSingleQuote';
const C_ESCAPED_DOUBLE_QUOTE = 'escapedDoubleQuote';
const C_SINGLE_QUOTE = 'singleQuote';
const C_DOUBLE_QUOTE = 'doubleQuote';
const C_NEWLINE = 'newline';
const C_SINGLE = 'single';
/*
* Literals
*/
const L_SINGLE_QUOTE = `'`;
const L_DOUBLE_QUOTE = `"`;
const L_NEWLINE = `\\\n`;
/*
* Parser nodes
*/
const T_ESCAPED_SINGLE_QUOTE = { type: C_ESCAPED_SINGLE_QUOTE, value: `\\'` };
const T_ESCAPED_DOUBLE_QUOTE = { type: C_ESCAPED_DOUBLE_QUOTE, value: `\\"` };
const T_SINGLE_QUOTE = { type: C_SINGLE_QUOTE, value: L_SINGLE_QUOTE };
const T_DOUBLE_QUOTE = { type: C_DOUBLE_QUOTE, value: L_DOUBLE_QUOTE };
const T_NEWLINE = { type: C_NEWLINE, value: L_NEWLINE };
/** @typedef {T_ESCAPED_SINGLE_QUOTE | T_ESCAPED_DOUBLE_QUOTE | T_SINGLE_QUOTE | T_NEWLINE} StringAstNode */
/**
* @typedef {{nodes: StringAstNode[],
* types: {escapedSingleQuote: number, escapedDoubleQuote: number, singleQuote: number, doubleQuote: number},
* quotes: boolean}} StringAst
*/
/**
* @param {StringAst} ast
* @return {string}
*/
function stringify(ast) {
return ast.nodes.reduce((str, { value }) => {
// Collapse multiple line strings automatically
if (value === L_NEWLINE) {
return str;
}
return str + value;
}, '');
}
/**
* @param {string} str
* @return {StringAst}
*/
function parse(str) {
let code, next, value;
let pos = 0;
let len = str.length;
/** @type StringAst */
const ast = {
nodes: [],
types: {
escapedSingleQuote: 0,
escapedDoubleQuote: 0,
singleQuote: 0,
doubleQuote: 0,
},
quotes: false,
};
while (pos < len) {
code = str.charCodeAt(pos);
switch (code) {
case SPACE:
case TAB:
case CR:
case FEED:
next = pos;
do {
next += 1;
code = str.charCodeAt(next);
} while (
code === SPACE ||
code === NEWLINE ||
code === TAB ||
code === CR ||
code === FEED
);
ast.nodes.push({
type: 'space',
value: str.slice(pos, next),
});
pos = next - 1;
break;
case SINGLE_QUOTE:
ast.nodes.push(T_SINGLE_QUOTE);
ast.types[C_SINGLE_QUOTE]++;
ast.quotes = true;
break;
case DOUBLE_QUOTE:
ast.nodes.push(T_DOUBLE_QUOTE);
ast.types[C_DOUBLE_QUOTE]++;
ast.quotes = true;
break;
case BACKSLASH:
next = pos + 1;
if (str.charCodeAt(next) === SINGLE_QUOTE) {
ast.nodes.push(T_ESCAPED_SINGLE_QUOTE);
ast.types[C_ESCAPED_SINGLE_QUOTE]++;
ast.quotes = true;
pos = next;
break;
} else if (str.charCodeAt(next) === DOUBLE_QUOTE) {
ast.nodes.push(T_ESCAPED_DOUBLE_QUOTE);
ast.types[C_ESCAPED_DOUBLE_QUOTE]++;
ast.quotes = true;
pos = next;
break;
} else if (str.charCodeAt(next) === NEWLINE) {
ast.nodes.push(T_NEWLINE);
pos = next;
break;
}
/*
* We need to fall through here to handle the token as
* a whole word. The missing 'break' is intentional.
*/
default:
WORD_END.lastIndex = pos + 1;
WORD_END.test(str);
if (WORD_END.lastIndex === 0) {
next = len - 1;
} else {
next = WORD_END.lastIndex - 2;
}
value = str.slice(pos, next + 1);
ast.nodes.push({
type: C_STRING,
value,
});
pos = next;
}
pos++;
}
return ast;
}
/**
* @param {valueParser.StringNode} node
* @param {StringAst} ast
* @return {void}
*/
function changeWrappingQuotes(node, ast) {
const { types } = ast;
if (types[C_SINGLE_QUOTE] || types[C_DOUBLE_QUOTE]) {
return;
}
if (
node.quote === L_SINGLE_QUOTE &&
types[C_ESCAPED_SINGLE_QUOTE] > 0 &&
!types[C_ESCAPED_DOUBLE_QUOTE]
) {
node.quote = L_DOUBLE_QUOTE;
}
if (
node.quote === L_DOUBLE_QUOTE &&
types[C_ESCAPED_DOUBLE_QUOTE] > 0 &&
!types[C_ESCAPED_SINGLE_QUOTE]
) {
node.quote = L_SINGLE_QUOTE;
}
ast.nodes = changeChildQuotes(ast.nodes, node.quote);
}
/**
* @param {StringAstNode[]} childNodes
* @param {string} parentQuote
* @return {StringAstNode[]}
*/
function changeChildQuotes(childNodes, parentQuote) {
const updatedChildren = [];
for (const child of childNodes) {
if (
child.type === C_ESCAPED_DOUBLE_QUOTE &&
parentQuote === L_SINGLE_QUOTE
) {
updatedChildren.push(T_DOUBLE_QUOTE);
} else if (
child.type === C_ESCAPED_SINGLE_QUOTE &&
parentQuote === L_DOUBLE_QUOTE
) {
updatedChildren.push(T_SINGLE_QUOTE);
} else {
updatedChildren.push(child);
}
}
return updatedChildren;
}
/**
* @param {string} value
* @param {'single' | 'double'} preferredQuote
* @return {string}
*/
function normalize(value, preferredQuote) {
if (!value || !value.length) {
return value;
}
return valueParser(value)
.walk((child) => {
if (child.type !== C_STRING) {
return;
}
const ast = parse(child.value);
if (ast.quotes) {
changeWrappingQuotes(child, ast);
} else if (preferredQuote === C_SINGLE) {
child.quote = L_SINGLE_QUOTE;
} else {
child.quote = L_DOUBLE_QUOTE;
}
child.value = stringify(ast);
})
.toString();
}
/**
* @param {string} original
* @param {Map<string, string>} cache
* @param {'single' | 'double'} preferredQuote
* @return {string}
*/
function minify(original, cache, preferredQuote) {
const key = original + '|' + preferredQuote;
if (cache.has(key)) {
return /** @type {string} */ (cache.get(key));
}
const newValue = normalize(original, preferredQuote);
cache.set(key, newValue);
return newValue;
}
/** @typedef {{preferredQuote?: 'double' | 'single'}} Options */
/**
* @type {import('postcss').PluginCreator<Options>}
* @param {Options} opts
* @return {import('postcss').Plugin}
*/
function pluginCreator(opts) {
const { preferredQuote } = Object.assign(
{},
{
preferredQuote: 'double',
},
opts
);
return {
postcssPlugin: 'postcss-normalize-string',
OnceExit(css) {
const cache = new Map();
css.walk((node) => {
switch (node.type) {
case 'rule':
node.selector = minify(node.selector, cache, preferredQuote);
break;
case 'decl':
node.value = minify(node.value, cache, preferredQuote);
break;
case 'atrule':
node.params = minify(node.params, cache, preferredQuote);
break;
}
});
},
};
}
pluginCreator.postcss = true;
module.exports = pluginCreator;