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 }; }
|
---|
11 |
|
---|
12 | /*
|
---|
13 | * Constants (parser usage)
|
---|
14 | */
|
---|
15 | const SINGLE_QUOTE = "'".charCodeAt(0);
|
---|
16 | const DOUBLE_QUOTE = '"'.charCodeAt(0);
|
---|
17 | const BACKSLASH = '\\'.charCodeAt(0);
|
---|
18 | const NEWLINE = '\n'.charCodeAt(0);
|
---|
19 | const SPACE = ' '.charCodeAt(0);
|
---|
20 | const FEED = '\f'.charCodeAt(0);
|
---|
21 | const TAB = '\t'.charCodeAt(0);
|
---|
22 | const CR = '\r'.charCodeAt(0);
|
---|
23 | const WORD_END = /[ \n\t\r\f'"\\]/g;
|
---|
24 | /*
|
---|
25 | * Constants (node type strings)
|
---|
26 | */
|
---|
27 |
|
---|
28 | const C_STRING = 'string';
|
---|
29 | const C_ESCAPED_SINGLE_QUOTE = 'escapedSingleQuote';
|
---|
30 | const C_ESCAPED_DOUBLE_QUOTE = 'escapedDoubleQuote';
|
---|
31 | const C_SINGLE_QUOTE = 'singleQuote';
|
---|
32 | const C_DOUBLE_QUOTE = 'doubleQuote';
|
---|
33 | const C_NEWLINE = 'newline';
|
---|
34 | const C_SINGLE = 'single';
|
---|
35 | /*
|
---|
36 | * Literals
|
---|
37 | */
|
---|
38 |
|
---|
39 | const L_SINGLE_QUOTE = `'`;
|
---|
40 | const L_DOUBLE_QUOTE = `"`;
|
---|
41 | const L_NEWLINE = `\\\n`;
|
---|
42 | /*
|
---|
43 | * Parser nodes
|
---|
44 | */
|
---|
45 |
|
---|
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 | };
|
---|
66 |
|
---|
67 | function stringify(ast) {
|
---|
68 | return ast.nodes.reduce((str, {
|
---|
69 | value
|
---|
70 | }) => {
|
---|
71 | // Collapse multiple line strings automatically
|
---|
72 | if (value === L_NEWLINE) {
|
---|
73 | return str;
|
---|
74 | }
|
---|
75 |
|
---|
76 | return str + value;
|
---|
77 | }, '');
|
---|
78 | }
|
---|
79 |
|
---|
80 | function parse(str) {
|
---|
81 | let code, next, value;
|
---|
82 | let pos = 0;
|
---|
83 | let len = str.length;
|
---|
84 | const ast = {
|
---|
85 | nodes: [],
|
---|
86 | types: {
|
---|
87 | escapedSingleQuote: 0,
|
---|
88 | escapedDoubleQuote: 0,
|
---|
89 | singleQuote: 0,
|
---|
90 | doubleQuote: 0
|
---|
91 | },
|
---|
92 | quotes: false
|
---|
93 | };
|
---|
94 |
|
---|
95 | while (pos < len) {
|
---|
96 | code = str.charCodeAt(pos);
|
---|
97 |
|
---|
98 | switch (code) {
|
---|
99 | case SPACE:
|
---|
100 | case TAB:
|
---|
101 | case CR:
|
---|
102 | case FEED:
|
---|
103 | next = pos;
|
---|
104 |
|
---|
105 | do {
|
---|
106 | next += 1;
|
---|
107 | code = str.charCodeAt(next);
|
---|
108 | } while (code === SPACE || code === NEWLINE || code === TAB || code === CR || code === FEED);
|
---|
109 |
|
---|
110 | ast.nodes.push({
|
---|
111 | type: 'space',
|
---|
112 | value: str.slice(pos, next)
|
---|
113 | });
|
---|
114 | pos = next - 1;
|
---|
115 | break;
|
---|
116 |
|
---|
117 | case SINGLE_QUOTE:
|
---|
118 | ast.nodes.push(T_SINGLE_QUOTE);
|
---|
119 | ast.types[C_SINGLE_QUOTE]++;
|
---|
120 | ast.quotes = true;
|
---|
121 | break;
|
---|
122 |
|
---|
123 | case DOUBLE_QUOTE:
|
---|
124 | ast.nodes.push(T_DOUBLE_QUOTE);
|
---|
125 | ast.types[C_DOUBLE_QUOTE]++;
|
---|
126 | ast.quotes = true;
|
---|
127 | break;
|
---|
128 |
|
---|
129 | case BACKSLASH:
|
---|
130 | next = pos + 1;
|
---|
131 |
|
---|
132 | if (str.charCodeAt(next) === SINGLE_QUOTE) {
|
---|
133 | ast.nodes.push(T_ESCAPED_SINGLE_QUOTE);
|
---|
134 | ast.types[C_ESCAPED_SINGLE_QUOTE]++;
|
---|
135 | ast.quotes = true;
|
---|
136 | pos = next;
|
---|
137 | break;
|
---|
138 | } else if (str.charCodeAt(next) === DOUBLE_QUOTE) {
|
---|
139 | ast.nodes.push(T_ESCAPED_DOUBLE_QUOTE);
|
---|
140 | ast.types[C_ESCAPED_DOUBLE_QUOTE]++;
|
---|
141 | ast.quotes = true;
|
---|
142 | pos = next;
|
---|
143 | break;
|
---|
144 | } else if (str.charCodeAt(next) === NEWLINE) {
|
---|
145 | ast.nodes.push(T_NEWLINE);
|
---|
146 | pos = next;
|
---|
147 | break;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * We need to fall through here to handle the token as
|
---|
152 | * a whole word. The missing 'break' is intentional.
|
---|
153 | */
|
---|
154 |
|
---|
155 | default:
|
---|
156 | WORD_END.lastIndex = pos + 1;
|
---|
157 | WORD_END.test(str);
|
---|
158 |
|
---|
159 | if (WORD_END.lastIndex === 0) {
|
---|
160 | next = len - 1;
|
---|
161 | } else {
|
---|
162 | next = WORD_END.lastIndex - 2;
|
---|
163 | }
|
---|
164 |
|
---|
165 | value = str.slice(pos, next + 1);
|
---|
166 | ast.nodes.push({
|
---|
167 | type: C_STRING,
|
---|
168 | value
|
---|
169 | });
|
---|
170 | pos = next;
|
---|
171 | }
|
---|
172 |
|
---|
173 | pos++;
|
---|
174 | }
|
---|
175 |
|
---|
176 | return ast;
|
---|
177 | }
|
---|
178 |
|
---|
179 | function changeWrappingQuotes(node, ast) {
|
---|
180 | const {
|
---|
181 | types
|
---|
182 | } = ast;
|
---|
183 |
|
---|
184 | if (types[C_SINGLE_QUOTE] || types[C_DOUBLE_QUOTE]) {
|
---|
185 | return;
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (node.quote === L_SINGLE_QUOTE && types[C_ESCAPED_SINGLE_QUOTE] > 0 && !types[C_ESCAPED_DOUBLE_QUOTE]) {
|
---|
189 | node.quote = L_DOUBLE_QUOTE;
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (node.quote === L_DOUBLE_QUOTE && types[C_ESCAPED_DOUBLE_QUOTE] > 0 && !types[C_ESCAPED_SINGLE_QUOTE]) {
|
---|
193 | node.quote = L_SINGLE_QUOTE;
|
---|
194 | }
|
---|
195 |
|
---|
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];
|
---|
203 | }
|
---|
204 |
|
---|
205 | return [...newAst, child];
|
---|
206 | }, []);
|
---|
207 | }
|
---|
208 |
|
---|
209 | function normalize(value, preferredQuote) {
|
---|
210 | if (!value || !value.length) {
|
---|
211 | return value;
|
---|
212 | }
|
---|
213 |
|
---|
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();
|
---|
231 | }
|
---|
232 |
|
---|
233 | const params = {
|
---|
234 | rule: 'selector',
|
---|
235 | decl: 'value',
|
---|
236 | atrule: 'params'
|
---|
237 | };
|
---|
238 |
|
---|
239 | function pluginCreator(opts) {
|
---|
240 | const {
|
---|
241 | preferredQuote
|
---|
242 | } = Object.assign({}, {
|
---|
243 | preferredQuote: 'double'
|
---|
244 | }, opts);
|
---|
245 | return {
|
---|
246 | postcssPlugin: 'postcss-normalize-string',
|
---|
247 |
|
---|
248 | 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;
|
---|
267 | }
|
---|
268 | });
|
---|
269 | }
|
---|
270 |
|
---|
271 | };
|
---|
272 | }
|
---|
273 |
|
---|
274 | pluginCreator.postcss = true;
|
---|
275 | var _default = pluginCreator;
|
---|
276 | exports.default = _default;
|
---|
277 | module.exports = exports.default; |
---|