source: trip-planner-front/node_modules/postcss-normalize-string/dist/index.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[6a3a178]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12/*
13 * Constants (parser usage)
14 */
15const SINGLE_QUOTE = "'".charCodeAt(0);
16const DOUBLE_QUOTE = '"'.charCodeAt(0);
17const BACKSLASH = '\\'.charCodeAt(0);
18const NEWLINE = '\n'.charCodeAt(0);
19const SPACE = ' '.charCodeAt(0);
20const FEED = '\f'.charCodeAt(0);
21const TAB = '\t'.charCodeAt(0);
22const CR = '\r'.charCodeAt(0);
23const WORD_END = /[ \n\t\r\f'"\\]/g;
24/*
25 * Constants (node type strings)
26 */
27
28const C_STRING = 'string';
29const C_ESCAPED_SINGLE_QUOTE = 'escapedSingleQuote';
30const C_ESCAPED_DOUBLE_QUOTE = 'escapedDoubleQuote';
31const C_SINGLE_QUOTE = 'singleQuote';
32const C_DOUBLE_QUOTE = 'doubleQuote';
33const C_NEWLINE = 'newline';
34const C_SINGLE = 'single';
35/*
36 * Literals
37 */
38
39const L_SINGLE_QUOTE = `'`;
40const L_DOUBLE_QUOTE = `"`;
41const L_NEWLINE = `\\\n`;
42/*
43 * Parser nodes
44 */
45
46const T_ESCAPED_SINGLE_QUOTE = {
47 type: C_ESCAPED_SINGLE_QUOTE,
48 value: `\\'`
49};
50const T_ESCAPED_DOUBLE_QUOTE = {
51 type: C_ESCAPED_DOUBLE_QUOTE,
52 value: `\\"`
53};
54const T_SINGLE_QUOTE = {
55 type: C_SINGLE_QUOTE,
56 value: L_SINGLE_QUOTE
57};
58const T_DOUBLE_QUOTE = {
59 type: C_DOUBLE_QUOTE,
60 value: L_DOUBLE_QUOTE
61};
62const T_NEWLINE = {
63 type: C_NEWLINE,
64 value: L_NEWLINE
65};
66
67function 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
80function 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
179function 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
209function 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
233const params = {
234 rule: 'selector',
235 decl: 'value',
236 atrule: 'params'
237};
238
239function 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
274pluginCreator.postcss = true;
275var _default = pluginCreator;
276exports.default = _default;
277module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.