source: trip-planner-front/node_modules/postcss-selector-parser/dist/parser.js@ 6a3a178

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

initial commit

  • Property mode set to 100644
File size: 37.5 KB
Line 
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = void 0;
5
6var _root = _interopRequireDefault(require("./selectors/root"));
7
8var _selector = _interopRequireDefault(require("./selectors/selector"));
9
10var _className = _interopRequireDefault(require("./selectors/className"));
11
12var _comment = _interopRequireDefault(require("./selectors/comment"));
13
14var _id = _interopRequireDefault(require("./selectors/id"));
15
16var _tag = _interopRequireDefault(require("./selectors/tag"));
17
18var _string = _interopRequireDefault(require("./selectors/string"));
19
20var _pseudo = _interopRequireDefault(require("./selectors/pseudo"));
21
22var _attribute = _interopRequireWildcard(require("./selectors/attribute"));
23
24var _universal = _interopRequireDefault(require("./selectors/universal"));
25
26var _combinator = _interopRequireDefault(require("./selectors/combinator"));
27
28var _nesting = _interopRequireDefault(require("./selectors/nesting"));
29
30var _sortAscending = _interopRequireDefault(require("./sortAscending"));
31
32var _tokenize = _interopRequireWildcard(require("./tokenize"));
33
34var tokens = _interopRequireWildcard(require("./tokenTypes"));
35
36var types = _interopRequireWildcard(require("./selectors/types"));
37
38var _util = require("./util");
39
40var _WHITESPACE_TOKENS, _Object$assign;
41
42function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
43
44function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
45
46function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
47
48function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
49
50function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
51
52var WHITESPACE_TOKENS = (_WHITESPACE_TOKENS = {}, _WHITESPACE_TOKENS[tokens.space] = true, _WHITESPACE_TOKENS[tokens.cr] = true, _WHITESPACE_TOKENS[tokens.feed] = true, _WHITESPACE_TOKENS[tokens.newline] = true, _WHITESPACE_TOKENS[tokens.tab] = true, _WHITESPACE_TOKENS);
53var WHITESPACE_EQUIV_TOKENS = Object.assign({}, WHITESPACE_TOKENS, (_Object$assign = {}, _Object$assign[tokens.comment] = true, _Object$assign));
54
55function tokenStart(token) {
56 return {
57 line: token[_tokenize.FIELDS.START_LINE],
58 column: token[_tokenize.FIELDS.START_COL]
59 };
60}
61
62function tokenEnd(token) {
63 return {
64 line: token[_tokenize.FIELDS.END_LINE],
65 column: token[_tokenize.FIELDS.END_COL]
66 };
67}
68
69function getSource(startLine, startColumn, endLine, endColumn) {
70 return {
71 start: {
72 line: startLine,
73 column: startColumn
74 },
75 end: {
76 line: endLine,
77 column: endColumn
78 }
79 };
80}
81
82function getTokenSource(token) {
83 return getSource(token[_tokenize.FIELDS.START_LINE], token[_tokenize.FIELDS.START_COL], token[_tokenize.FIELDS.END_LINE], token[_tokenize.FIELDS.END_COL]);
84}
85
86function getTokenSourceSpan(startToken, endToken) {
87 if (!startToken) {
88 return undefined;
89 }
90
91 return getSource(startToken[_tokenize.FIELDS.START_LINE], startToken[_tokenize.FIELDS.START_COL], endToken[_tokenize.FIELDS.END_LINE], endToken[_tokenize.FIELDS.END_COL]);
92}
93
94function unescapeProp(node, prop) {
95 var value = node[prop];
96
97 if (typeof value !== "string") {
98 return;
99 }
100
101 if (value.indexOf("\\") !== -1) {
102 (0, _util.ensureObject)(node, 'raws');
103 node[prop] = (0, _util.unesc)(value);
104
105 if (node.raws[prop] === undefined) {
106 node.raws[prop] = value;
107 }
108 }
109
110 return node;
111}
112
113function indexesOf(array, item) {
114 var i = -1;
115 var indexes = [];
116
117 while ((i = array.indexOf(item, i + 1)) !== -1) {
118 indexes.push(i);
119 }
120
121 return indexes;
122}
123
124function uniqs() {
125 var list = Array.prototype.concat.apply([], arguments);
126 return list.filter(function (item, i) {
127 return i === list.indexOf(item);
128 });
129}
130
131var Parser = /*#__PURE__*/function () {
132 function Parser(rule, options) {
133 if (options === void 0) {
134 options = {};
135 }
136
137 this.rule = rule;
138 this.options = Object.assign({
139 lossy: false,
140 safe: false
141 }, options);
142 this.position = 0;
143 this.css = typeof this.rule === 'string' ? this.rule : this.rule.selector;
144 this.tokens = (0, _tokenize["default"])({
145 css: this.css,
146 error: this._errorGenerator(),
147 safe: this.options.safe
148 });
149 var rootSource = getTokenSourceSpan(this.tokens[0], this.tokens[this.tokens.length - 1]);
150 this.root = new _root["default"]({
151 source: rootSource
152 });
153 this.root.errorGenerator = this._errorGenerator();
154 var selector = new _selector["default"]({
155 source: {
156 start: {
157 line: 1,
158 column: 1
159 }
160 }
161 });
162 this.root.append(selector);
163 this.current = selector;
164 this.loop();
165 }
166
167 var _proto = Parser.prototype;
168
169 _proto._errorGenerator = function _errorGenerator() {
170 var _this = this;
171
172 return function (message, errorOptions) {
173 if (typeof _this.rule === 'string') {
174 return new Error(message);
175 }
176
177 return _this.rule.error(message, errorOptions);
178 };
179 };
180
181 _proto.attribute = function attribute() {
182 var attr = [];
183 var startingToken = this.currToken;
184 this.position++;
185
186 while (this.position < this.tokens.length && this.currToken[_tokenize.FIELDS.TYPE] !== tokens.closeSquare) {
187 attr.push(this.currToken);
188 this.position++;
189 }
190
191 if (this.currToken[_tokenize.FIELDS.TYPE] !== tokens.closeSquare) {
192 return this.expected('closing square bracket', this.currToken[_tokenize.FIELDS.START_POS]);
193 }
194
195 var len = attr.length;
196 var node = {
197 source: getSource(startingToken[1], startingToken[2], this.currToken[3], this.currToken[4]),
198 sourceIndex: startingToken[_tokenize.FIELDS.START_POS]
199 };
200
201 if (len === 1 && !~[tokens.word].indexOf(attr[0][_tokenize.FIELDS.TYPE])) {
202 return this.expected('attribute', attr[0][_tokenize.FIELDS.START_POS]);
203 }
204
205 var pos = 0;
206 var spaceBefore = '';
207 var commentBefore = '';
208 var lastAdded = null;
209 var spaceAfterMeaningfulToken = false;
210
211 while (pos < len) {
212 var token = attr[pos];
213 var content = this.content(token);
214 var next = attr[pos + 1];
215
216 switch (token[_tokenize.FIELDS.TYPE]) {
217 case tokens.space:
218 // if (
219 // len === 1 ||
220 // pos === 0 && this.content(next) === '|'
221 // ) {
222 // return this.expected('attribute', token[TOKEN.START_POS], content);
223 // }
224 spaceAfterMeaningfulToken = true;
225
226 if (this.options.lossy) {
227 break;
228 }
229
230 if (lastAdded) {
231 (0, _util.ensureObject)(node, 'spaces', lastAdded);
232 var prevContent = node.spaces[lastAdded].after || '';
233 node.spaces[lastAdded].after = prevContent + content;
234 var existingComment = (0, _util.getProp)(node, 'raws', 'spaces', lastAdded, 'after') || null;
235
236 if (existingComment) {
237 node.raws.spaces[lastAdded].after = existingComment + content;
238 }
239 } else {
240 spaceBefore = spaceBefore + content;
241 commentBefore = commentBefore + content;
242 }
243
244 break;
245
246 case tokens.asterisk:
247 if (next[_tokenize.FIELDS.TYPE] === tokens.equals) {
248 node.operator = content;
249 lastAdded = 'operator';
250 } else if ((!node.namespace || lastAdded === "namespace" && !spaceAfterMeaningfulToken) && next) {
251 if (spaceBefore) {
252 (0, _util.ensureObject)(node, 'spaces', 'attribute');
253 node.spaces.attribute.before = spaceBefore;
254 spaceBefore = '';
255 }
256
257 if (commentBefore) {
258 (0, _util.ensureObject)(node, 'raws', 'spaces', 'attribute');
259 node.raws.spaces.attribute.before = spaceBefore;
260 commentBefore = '';
261 }
262
263 node.namespace = (node.namespace || "") + content;
264 var rawValue = (0, _util.getProp)(node, 'raws', 'namespace') || null;
265
266 if (rawValue) {
267 node.raws.namespace += content;
268 }
269
270 lastAdded = 'namespace';
271 }
272
273 spaceAfterMeaningfulToken = false;
274 break;
275
276 case tokens.dollar:
277 if (lastAdded === "value") {
278 var oldRawValue = (0, _util.getProp)(node, 'raws', 'value');
279 node.value += "$";
280
281 if (oldRawValue) {
282 node.raws.value = oldRawValue + "$";
283 }
284
285 break;
286 }
287
288 // Falls through
289
290 case tokens.caret:
291 if (next[_tokenize.FIELDS.TYPE] === tokens.equals) {
292 node.operator = content;
293 lastAdded = 'operator';
294 }
295
296 spaceAfterMeaningfulToken = false;
297 break;
298
299 case tokens.combinator:
300 if (content === '~' && next[_tokenize.FIELDS.TYPE] === tokens.equals) {
301 node.operator = content;
302 lastAdded = 'operator';
303 }
304
305 if (content !== '|') {
306 spaceAfterMeaningfulToken = false;
307 break;
308 }
309
310 if (next[_tokenize.FIELDS.TYPE] === tokens.equals) {
311 node.operator = content;
312 lastAdded = 'operator';
313 } else if (!node.namespace && !node.attribute) {
314 node.namespace = true;
315 }
316
317 spaceAfterMeaningfulToken = false;
318 break;
319
320 case tokens.word:
321 if (next && this.content(next) === '|' && attr[pos + 2] && attr[pos + 2][_tokenize.FIELDS.TYPE] !== tokens.equals && // this look-ahead probably fails with comment nodes involved.
322 !node.operator && !node.namespace) {
323 node.namespace = content;
324 lastAdded = 'namespace';
325 } else if (!node.attribute || lastAdded === "attribute" && !spaceAfterMeaningfulToken) {
326 if (spaceBefore) {
327 (0, _util.ensureObject)(node, 'spaces', 'attribute');
328 node.spaces.attribute.before = spaceBefore;
329 spaceBefore = '';
330 }
331
332 if (commentBefore) {
333 (0, _util.ensureObject)(node, 'raws', 'spaces', 'attribute');
334 node.raws.spaces.attribute.before = commentBefore;
335 commentBefore = '';
336 }
337
338 node.attribute = (node.attribute || "") + content;
339
340 var _rawValue = (0, _util.getProp)(node, 'raws', 'attribute') || null;
341
342 if (_rawValue) {
343 node.raws.attribute += content;
344 }
345
346 lastAdded = 'attribute';
347 } else if (!node.value && node.value !== "" || lastAdded === "value" && !spaceAfterMeaningfulToken) {
348 var _unescaped = (0, _util.unesc)(content);
349
350 var _oldRawValue = (0, _util.getProp)(node, 'raws', 'value') || '';
351
352 var oldValue = node.value || '';
353 node.value = oldValue + _unescaped;
354 node.quoteMark = null;
355
356 if (_unescaped !== content || _oldRawValue) {
357 (0, _util.ensureObject)(node, 'raws');
358 node.raws.value = (_oldRawValue || oldValue) + content;
359 }
360
361 lastAdded = 'value';
362 } else {
363 var insensitive = content === 'i' || content === "I";
364
365 if ((node.value || node.value === '') && (node.quoteMark || spaceAfterMeaningfulToken)) {
366 node.insensitive = insensitive;
367
368 if (!insensitive || content === "I") {
369 (0, _util.ensureObject)(node, 'raws');
370 node.raws.insensitiveFlag = content;
371 }
372
373 lastAdded = 'insensitive';
374
375 if (spaceBefore) {
376 (0, _util.ensureObject)(node, 'spaces', 'insensitive');
377 node.spaces.insensitive.before = spaceBefore;
378 spaceBefore = '';
379 }
380
381 if (commentBefore) {
382 (0, _util.ensureObject)(node, 'raws', 'spaces', 'insensitive');
383 node.raws.spaces.insensitive.before = commentBefore;
384 commentBefore = '';
385 }
386 } else if (node.value || node.value === '') {
387 lastAdded = 'value';
388 node.value += content;
389
390 if (node.raws.value) {
391 node.raws.value += content;
392 }
393 }
394 }
395
396 spaceAfterMeaningfulToken = false;
397 break;
398
399 case tokens.str:
400 if (!node.attribute || !node.operator) {
401 return this.error("Expected an attribute followed by an operator preceding the string.", {
402 index: token[_tokenize.FIELDS.START_POS]
403 });
404 }
405
406 var _unescapeValue = (0, _attribute.unescapeValue)(content),
407 unescaped = _unescapeValue.unescaped,
408 quoteMark = _unescapeValue.quoteMark;
409
410 node.value = unescaped;
411 node.quoteMark = quoteMark;
412 lastAdded = 'value';
413 (0, _util.ensureObject)(node, 'raws');
414 node.raws.value = content;
415 spaceAfterMeaningfulToken = false;
416 break;
417
418 case tokens.equals:
419 if (!node.attribute) {
420 return this.expected('attribute', token[_tokenize.FIELDS.START_POS], content);
421 }
422
423 if (node.value) {
424 return this.error('Unexpected "=" found; an operator was already defined.', {
425 index: token[_tokenize.FIELDS.START_POS]
426 });
427 }
428
429 node.operator = node.operator ? node.operator + content : content;
430 lastAdded = 'operator';
431 spaceAfterMeaningfulToken = false;
432 break;
433
434 case tokens.comment:
435 if (lastAdded) {
436 if (spaceAfterMeaningfulToken || next && next[_tokenize.FIELDS.TYPE] === tokens.space || lastAdded === 'insensitive') {
437 var lastComment = (0, _util.getProp)(node, 'spaces', lastAdded, 'after') || '';
438 var rawLastComment = (0, _util.getProp)(node, 'raws', 'spaces', lastAdded, 'after') || lastComment;
439 (0, _util.ensureObject)(node, 'raws', 'spaces', lastAdded);
440 node.raws.spaces[lastAdded].after = rawLastComment + content;
441 } else {
442 var lastValue = node[lastAdded] || '';
443 var rawLastValue = (0, _util.getProp)(node, 'raws', lastAdded) || lastValue;
444 (0, _util.ensureObject)(node, 'raws');
445 node.raws[lastAdded] = rawLastValue + content;
446 }
447 } else {
448 commentBefore = commentBefore + content;
449 }
450
451 break;
452
453 default:
454 return this.error("Unexpected \"" + content + "\" found.", {
455 index: token[_tokenize.FIELDS.START_POS]
456 });
457 }
458
459 pos++;
460 }
461
462 unescapeProp(node, "attribute");
463 unescapeProp(node, "namespace");
464 this.newNode(new _attribute["default"](node));
465 this.position++;
466 }
467 /**
468 * return a node containing meaningless garbage up to (but not including) the specified token position.
469 * if the token position is negative, all remaining tokens are consumed.
470 *
471 * This returns an array containing a single string node if all whitespace,
472 * otherwise an array of comment nodes with space before and after.
473 *
474 * These tokens are not added to the current selector, the caller can add them or use them to amend
475 * a previous node's space metadata.
476 *
477 * In lossy mode, this returns only comments.
478 */
479 ;
480
481 _proto.parseWhitespaceEquivalentTokens = function parseWhitespaceEquivalentTokens(stopPosition) {
482 if (stopPosition < 0) {
483 stopPosition = this.tokens.length;
484 }
485
486 var startPosition = this.position;
487 var nodes = [];
488 var space = "";
489 var lastComment = undefined;
490
491 do {
492 if (WHITESPACE_TOKENS[this.currToken[_tokenize.FIELDS.TYPE]]) {
493 if (!this.options.lossy) {
494 space += this.content();
495 }
496 } else if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.comment) {
497 var spaces = {};
498
499 if (space) {
500 spaces.before = space;
501 space = "";
502 }
503
504 lastComment = new _comment["default"]({
505 value: this.content(),
506 source: getTokenSource(this.currToken),
507 sourceIndex: this.currToken[_tokenize.FIELDS.START_POS],
508 spaces: spaces
509 });
510 nodes.push(lastComment);
511 }
512 } while (++this.position < stopPosition);
513
514 if (space) {
515 if (lastComment) {
516 lastComment.spaces.after = space;
517 } else if (!this.options.lossy) {
518 var firstToken = this.tokens[startPosition];
519 var lastToken = this.tokens[this.position - 1];
520 nodes.push(new _string["default"]({
521 value: '',
522 source: getSource(firstToken[_tokenize.FIELDS.START_LINE], firstToken[_tokenize.FIELDS.START_COL], lastToken[_tokenize.FIELDS.END_LINE], lastToken[_tokenize.FIELDS.END_COL]),
523 sourceIndex: firstToken[_tokenize.FIELDS.START_POS],
524 spaces: {
525 before: space,
526 after: ''
527 }
528 }));
529 }
530 }
531
532 return nodes;
533 }
534 /**
535 *
536 * @param {*} nodes
537 */
538 ;
539
540 _proto.convertWhitespaceNodesToSpace = function convertWhitespaceNodesToSpace(nodes, requiredSpace) {
541 var _this2 = this;
542
543 if (requiredSpace === void 0) {
544 requiredSpace = false;
545 }
546
547 var space = "";
548 var rawSpace = "";
549 nodes.forEach(function (n) {
550 var spaceBefore = _this2.lossySpace(n.spaces.before, requiredSpace);
551
552 var rawSpaceBefore = _this2.lossySpace(n.rawSpaceBefore, requiredSpace);
553
554 space += spaceBefore + _this2.lossySpace(n.spaces.after, requiredSpace && spaceBefore.length === 0);
555 rawSpace += spaceBefore + n.value + _this2.lossySpace(n.rawSpaceAfter, requiredSpace && rawSpaceBefore.length === 0);
556 });
557
558 if (rawSpace === space) {
559 rawSpace = undefined;
560 }
561
562 var result = {
563 space: space,
564 rawSpace: rawSpace
565 };
566 return result;
567 };
568
569 _proto.isNamedCombinator = function isNamedCombinator(position) {
570 if (position === void 0) {
571 position = this.position;
572 }
573
574 return this.tokens[position + 0] && this.tokens[position + 0][_tokenize.FIELDS.TYPE] === tokens.slash && this.tokens[position + 1] && this.tokens[position + 1][_tokenize.FIELDS.TYPE] === tokens.word && this.tokens[position + 2] && this.tokens[position + 2][_tokenize.FIELDS.TYPE] === tokens.slash;
575 };
576
577 _proto.namedCombinator = function namedCombinator() {
578 if (this.isNamedCombinator()) {
579 var nameRaw = this.content(this.tokens[this.position + 1]);
580 var name = (0, _util.unesc)(nameRaw).toLowerCase();
581 var raws = {};
582
583 if (name !== nameRaw) {
584 raws.value = "/" + nameRaw + "/";
585 }
586
587 var node = new _combinator["default"]({
588 value: "/" + name + "/",
589 source: getSource(this.currToken[_tokenize.FIELDS.START_LINE], this.currToken[_tokenize.FIELDS.START_COL], this.tokens[this.position + 2][_tokenize.FIELDS.END_LINE], this.tokens[this.position + 2][_tokenize.FIELDS.END_COL]),
590 sourceIndex: this.currToken[_tokenize.FIELDS.START_POS],
591 raws: raws
592 });
593 this.position = this.position + 3;
594 return node;
595 } else {
596 this.unexpected();
597 }
598 };
599
600 _proto.combinator = function combinator() {
601 var _this3 = this;
602
603 if (this.content() === '|') {
604 return this.namespace();
605 } // We need to decide between a space that's a descendant combinator and meaningless whitespace at the end of a selector.
606
607
608 var nextSigTokenPos = this.locateNextMeaningfulToken(this.position);
609
610 if (nextSigTokenPos < 0 || this.tokens[nextSigTokenPos][_tokenize.FIELDS.TYPE] === tokens.comma) {
611 var nodes = this.parseWhitespaceEquivalentTokens(nextSigTokenPos);
612
613 if (nodes.length > 0) {
614 var last = this.current.last;
615
616 if (last) {
617 var _this$convertWhitespa = this.convertWhitespaceNodesToSpace(nodes),
618 space = _this$convertWhitespa.space,
619 rawSpace = _this$convertWhitespa.rawSpace;
620
621 if (rawSpace !== undefined) {
622 last.rawSpaceAfter += rawSpace;
623 }
624
625 last.spaces.after += space;
626 } else {
627 nodes.forEach(function (n) {
628 return _this3.newNode(n);
629 });
630 }
631 }
632
633 return;
634 }
635
636 var firstToken = this.currToken;
637 var spaceOrDescendantSelectorNodes = undefined;
638
639 if (nextSigTokenPos > this.position) {
640 spaceOrDescendantSelectorNodes = this.parseWhitespaceEquivalentTokens(nextSigTokenPos);
641 }
642
643 var node;
644
645 if (this.isNamedCombinator()) {
646 node = this.namedCombinator();
647 } else if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.combinator) {
648 node = new _combinator["default"]({
649 value: this.content(),
650 source: getTokenSource(this.currToken),
651 sourceIndex: this.currToken[_tokenize.FIELDS.START_POS]
652 });
653 this.position++;
654 } else if (WHITESPACE_TOKENS[this.currToken[_tokenize.FIELDS.TYPE]]) {// pass
655 } else if (!spaceOrDescendantSelectorNodes) {
656 this.unexpected();
657 }
658
659 if (node) {
660 if (spaceOrDescendantSelectorNodes) {
661 var _this$convertWhitespa2 = this.convertWhitespaceNodesToSpace(spaceOrDescendantSelectorNodes),
662 _space = _this$convertWhitespa2.space,
663 _rawSpace = _this$convertWhitespa2.rawSpace;
664
665 node.spaces.before = _space;
666 node.rawSpaceBefore = _rawSpace;
667 }
668 } else {
669 // descendant combinator
670 var _this$convertWhitespa3 = this.convertWhitespaceNodesToSpace(spaceOrDescendantSelectorNodes, true),
671 _space2 = _this$convertWhitespa3.space,
672 _rawSpace2 = _this$convertWhitespa3.rawSpace;
673
674 if (!_rawSpace2) {
675 _rawSpace2 = _space2;
676 }
677
678 var spaces = {};
679 var raws = {
680 spaces: {}
681 };
682
683 if (_space2.endsWith(' ') && _rawSpace2.endsWith(' ')) {
684 spaces.before = _space2.slice(0, _space2.length - 1);
685 raws.spaces.before = _rawSpace2.slice(0, _rawSpace2.length - 1);
686 } else if (_space2.startsWith(' ') && _rawSpace2.startsWith(' ')) {
687 spaces.after = _space2.slice(1);
688 raws.spaces.after = _rawSpace2.slice(1);
689 } else {
690 raws.value = _rawSpace2;
691 }
692
693 node = new _combinator["default"]({
694 value: ' ',
695 source: getTokenSourceSpan(firstToken, this.tokens[this.position - 1]),
696 sourceIndex: firstToken[_tokenize.FIELDS.START_POS],
697 spaces: spaces,
698 raws: raws
699 });
700 }
701
702 if (this.currToken && this.currToken[_tokenize.FIELDS.TYPE] === tokens.space) {
703 node.spaces.after = this.optionalSpace(this.content());
704 this.position++;
705 }
706
707 return this.newNode(node);
708 };
709
710 _proto.comma = function comma() {
711 if (this.position === this.tokens.length - 1) {
712 this.root.trailingComma = true;
713 this.position++;
714 return;
715 }
716
717 this.current._inferEndPosition();
718
719 var selector = new _selector["default"]({
720 source: {
721 start: tokenStart(this.tokens[this.position + 1])
722 }
723 });
724 this.current.parent.append(selector);
725 this.current = selector;
726 this.position++;
727 };
728
729 _proto.comment = function comment() {
730 var current = this.currToken;
731 this.newNode(new _comment["default"]({
732 value: this.content(),
733 source: getTokenSource(current),
734 sourceIndex: current[_tokenize.FIELDS.START_POS]
735 }));
736 this.position++;
737 };
738
739 _proto.error = function error(message, opts) {
740 throw this.root.error(message, opts);
741 };
742
743 _proto.missingBackslash = function missingBackslash() {
744 return this.error('Expected a backslash preceding the semicolon.', {
745 index: this.currToken[_tokenize.FIELDS.START_POS]
746 });
747 };
748
749 _proto.missingParenthesis = function missingParenthesis() {
750 return this.expected('opening parenthesis', this.currToken[_tokenize.FIELDS.START_POS]);
751 };
752
753 _proto.missingSquareBracket = function missingSquareBracket() {
754 return this.expected('opening square bracket', this.currToken[_tokenize.FIELDS.START_POS]);
755 };
756
757 _proto.unexpected = function unexpected() {
758 return this.error("Unexpected '" + this.content() + "'. Escaping special characters with \\ may help.", this.currToken[_tokenize.FIELDS.START_POS]);
759 };
760
761 _proto.namespace = function namespace() {
762 var before = this.prevToken && this.content(this.prevToken) || true;
763
764 if (this.nextToken[_tokenize.FIELDS.TYPE] === tokens.word) {
765 this.position++;
766 return this.word(before);
767 } else if (this.nextToken[_tokenize.FIELDS.TYPE] === tokens.asterisk) {
768 this.position++;
769 return this.universal(before);
770 }
771 };
772
773 _proto.nesting = function nesting() {
774 if (this.nextToken) {
775 var nextContent = this.content(this.nextToken);
776
777 if (nextContent === "|") {
778 this.position++;
779 return;
780 }
781 }
782
783 var current = this.currToken;
784 this.newNode(new _nesting["default"]({
785 value: this.content(),
786 source: getTokenSource(current),
787 sourceIndex: current[_tokenize.FIELDS.START_POS]
788 }));
789 this.position++;
790 };
791
792 _proto.parentheses = function parentheses() {
793 var last = this.current.last;
794 var unbalanced = 1;
795 this.position++;
796
797 if (last && last.type === types.PSEUDO) {
798 var selector = new _selector["default"]({
799 source: {
800 start: tokenStart(this.tokens[this.position - 1])
801 }
802 });
803 var cache = this.current;
804 last.append(selector);
805 this.current = selector;
806
807 while (this.position < this.tokens.length && unbalanced) {
808 if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.openParenthesis) {
809 unbalanced++;
810 }
811
812 if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.closeParenthesis) {
813 unbalanced--;
814 }
815
816 if (unbalanced) {
817 this.parse();
818 } else {
819 this.current.source.end = tokenEnd(this.currToken);
820 this.current.parent.source.end = tokenEnd(this.currToken);
821 this.position++;
822 }
823 }
824
825 this.current = cache;
826 } else {
827 // I think this case should be an error. It's used to implement a basic parse of media queries
828 // but I don't think it's a good idea.
829 var parenStart = this.currToken;
830 var parenValue = "(";
831 var parenEnd;
832
833 while (this.position < this.tokens.length && unbalanced) {
834 if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.openParenthesis) {
835 unbalanced++;
836 }
837
838 if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.closeParenthesis) {
839 unbalanced--;
840 }
841
842 parenEnd = this.currToken;
843 parenValue += this.parseParenthesisToken(this.currToken);
844 this.position++;
845 }
846
847 if (last) {
848 last.appendToPropertyAndEscape("value", parenValue, parenValue);
849 } else {
850 this.newNode(new _string["default"]({
851 value: parenValue,
852 source: getSource(parenStart[_tokenize.FIELDS.START_LINE], parenStart[_tokenize.FIELDS.START_COL], parenEnd[_tokenize.FIELDS.END_LINE], parenEnd[_tokenize.FIELDS.END_COL]),
853 sourceIndex: parenStart[_tokenize.FIELDS.START_POS]
854 }));
855 }
856 }
857
858 if (unbalanced) {
859 return this.expected('closing parenthesis', this.currToken[_tokenize.FIELDS.START_POS]);
860 }
861 };
862
863 _proto.pseudo = function pseudo() {
864 var _this4 = this;
865
866 var pseudoStr = '';
867 var startingToken = this.currToken;
868
869 while (this.currToken && this.currToken[_tokenize.FIELDS.TYPE] === tokens.colon) {
870 pseudoStr += this.content();
871 this.position++;
872 }
873
874 if (!this.currToken) {
875 return this.expected(['pseudo-class', 'pseudo-element'], this.position - 1);
876 }
877
878 if (this.currToken[_tokenize.FIELDS.TYPE] === tokens.word) {
879 this.splitWord(false, function (first, length) {
880 pseudoStr += first;
881
882 _this4.newNode(new _pseudo["default"]({
883 value: pseudoStr,
884 source: getTokenSourceSpan(startingToken, _this4.currToken),
885 sourceIndex: startingToken[_tokenize.FIELDS.START_POS]
886 }));
887
888 if (length > 1 && _this4.nextToken && _this4.nextToken[_tokenize.FIELDS.TYPE] === tokens.openParenthesis) {
889 _this4.error('Misplaced parenthesis.', {
890 index: _this4.nextToken[_tokenize.FIELDS.START_POS]
891 });
892 }
893 });
894 } else {
895 return this.expected(['pseudo-class', 'pseudo-element'], this.currToken[_tokenize.FIELDS.START_POS]);
896 }
897 };
898
899 _proto.space = function space() {
900 var content = this.content(); // Handle space before and after the selector
901
902 if (this.position === 0 || this.prevToken[_tokenize.FIELDS.TYPE] === tokens.comma || this.prevToken[_tokenize.FIELDS.TYPE] === tokens.openParenthesis || this.current.nodes.every(function (node) {
903 return node.type === 'comment';
904 })) {
905 this.spaces = this.optionalSpace(content);
906 this.position++;
907 } else if (this.position === this.tokens.length - 1 || this.nextToken[_tokenize.FIELDS.TYPE] === tokens.comma || this.nextToken[_tokenize.FIELDS.TYPE] === tokens.closeParenthesis) {
908 this.current.last.spaces.after = this.optionalSpace(content);
909 this.position++;
910 } else {
911 this.combinator();
912 }
913 };
914
915 _proto.string = function string() {
916 var current = this.currToken;
917 this.newNode(new _string["default"]({
918 value: this.content(),
919 source: getTokenSource(current),
920 sourceIndex: current[_tokenize.FIELDS.START_POS]
921 }));
922 this.position++;
923 };
924
925 _proto.universal = function universal(namespace) {
926 var nextToken = this.nextToken;
927
928 if (nextToken && this.content(nextToken) === '|') {
929 this.position++;
930 return this.namespace();
931 }
932
933 var current = this.currToken;
934 this.newNode(new _universal["default"]({
935 value: this.content(),
936 source: getTokenSource(current),
937 sourceIndex: current[_tokenize.FIELDS.START_POS]
938 }), namespace);
939 this.position++;
940 };
941
942 _proto.splitWord = function splitWord(namespace, firstCallback) {
943 var _this5 = this;
944
945 var nextToken = this.nextToken;
946 var word = this.content();
947
948 while (nextToken && ~[tokens.dollar, tokens.caret, tokens.equals, tokens.word].indexOf(nextToken[_tokenize.FIELDS.TYPE])) {
949 this.position++;
950 var current = this.content();
951 word += current;
952
953 if (current.lastIndexOf('\\') === current.length - 1) {
954 var next = this.nextToken;
955
956 if (next && next[_tokenize.FIELDS.TYPE] === tokens.space) {
957 word += this.requiredSpace(this.content(next));
958 this.position++;
959 }
960 }
961
962 nextToken = this.nextToken;
963 }
964
965 var hasClass = indexesOf(word, '.').filter(function (i) {
966 return word[i - 1] !== '\\';
967 });
968 var hasId = indexesOf(word, '#').filter(function (i) {
969 return word[i - 1] !== '\\';
970 }); // Eliminate Sass interpolations from the list of id indexes
971
972 var interpolations = indexesOf(word, '#{');
973
974 if (interpolations.length) {
975 hasId = hasId.filter(function (hashIndex) {
976 return !~interpolations.indexOf(hashIndex);
977 });
978 }
979
980 var indices = (0, _sortAscending["default"])(uniqs([0].concat(hasClass, hasId)));
981 indices.forEach(function (ind, i) {
982 var index = indices[i + 1] || word.length;
983 var value = word.slice(ind, index);
984
985 if (i === 0 && firstCallback) {
986 return firstCallback.call(_this5, value, indices.length);
987 }
988
989 var node;
990 var current = _this5.currToken;
991 var sourceIndex = current[_tokenize.FIELDS.START_POS] + indices[i];
992 var source = getSource(current[1], current[2] + ind, current[3], current[2] + (index - 1));
993
994 if (~hasClass.indexOf(ind)) {
995 var classNameOpts = {
996 value: value.slice(1),
997 source: source,
998 sourceIndex: sourceIndex
999 };
1000 node = new _className["default"](unescapeProp(classNameOpts, "value"));
1001 } else if (~hasId.indexOf(ind)) {
1002 var idOpts = {
1003 value: value.slice(1),
1004 source: source,
1005 sourceIndex: sourceIndex
1006 };
1007 node = new _id["default"](unescapeProp(idOpts, "value"));
1008 } else {
1009 var tagOpts = {
1010 value: value,
1011 source: source,
1012 sourceIndex: sourceIndex
1013 };
1014 unescapeProp(tagOpts, "value");
1015 node = new _tag["default"](tagOpts);
1016 }
1017
1018 _this5.newNode(node, namespace); // Ensure that the namespace is used only once
1019
1020
1021 namespace = null;
1022 });
1023 this.position++;
1024 };
1025
1026 _proto.word = function word(namespace) {
1027 var nextToken = this.nextToken;
1028
1029 if (nextToken && this.content(nextToken) === '|') {
1030 this.position++;
1031 return this.namespace();
1032 }
1033
1034 return this.splitWord(namespace);
1035 };
1036
1037 _proto.loop = function loop() {
1038 while (this.position < this.tokens.length) {
1039 this.parse(true);
1040 }
1041
1042 this.current._inferEndPosition();
1043
1044 return this.root;
1045 };
1046
1047 _proto.parse = function parse(throwOnParenthesis) {
1048 switch (this.currToken[_tokenize.FIELDS.TYPE]) {
1049 case tokens.space:
1050 this.space();
1051 break;
1052
1053 case tokens.comment:
1054 this.comment();
1055 break;
1056
1057 case tokens.openParenthesis:
1058 this.parentheses();
1059 break;
1060
1061 case tokens.closeParenthesis:
1062 if (throwOnParenthesis) {
1063 this.missingParenthesis();
1064 }
1065
1066 break;
1067
1068 case tokens.openSquare:
1069 this.attribute();
1070 break;
1071
1072 case tokens.dollar:
1073 case tokens.caret:
1074 case tokens.equals:
1075 case tokens.word:
1076 this.word();
1077 break;
1078
1079 case tokens.colon:
1080 this.pseudo();
1081 break;
1082
1083 case tokens.comma:
1084 this.comma();
1085 break;
1086
1087 case tokens.asterisk:
1088 this.universal();
1089 break;
1090
1091 case tokens.ampersand:
1092 this.nesting();
1093 break;
1094
1095 case tokens.slash:
1096 case tokens.combinator:
1097 this.combinator();
1098 break;
1099
1100 case tokens.str:
1101 this.string();
1102 break;
1103 // These cases throw; no break needed.
1104
1105 case tokens.closeSquare:
1106 this.missingSquareBracket();
1107
1108 case tokens.semicolon:
1109 this.missingBackslash();
1110
1111 default:
1112 this.unexpected();
1113 }
1114 }
1115 /**
1116 * Helpers
1117 */
1118 ;
1119
1120 _proto.expected = function expected(description, index, found) {
1121 if (Array.isArray(description)) {
1122 var last = description.pop();
1123 description = description.join(', ') + " or " + last;
1124 }
1125
1126 var an = /^[aeiou]/.test(description[0]) ? 'an' : 'a';
1127
1128 if (!found) {
1129 return this.error("Expected " + an + " " + description + ".", {
1130 index: index
1131 });
1132 }
1133
1134 return this.error("Expected " + an + " " + description + ", found \"" + found + "\" instead.", {
1135 index: index
1136 });
1137 };
1138
1139 _proto.requiredSpace = function requiredSpace(space) {
1140 return this.options.lossy ? ' ' : space;
1141 };
1142
1143 _proto.optionalSpace = function optionalSpace(space) {
1144 return this.options.lossy ? '' : space;
1145 };
1146
1147 _proto.lossySpace = function lossySpace(space, required) {
1148 if (this.options.lossy) {
1149 return required ? ' ' : '';
1150 } else {
1151 return space;
1152 }
1153 };
1154
1155 _proto.parseParenthesisToken = function parseParenthesisToken(token) {
1156 var content = this.content(token);
1157
1158 if (token[_tokenize.FIELDS.TYPE] === tokens.space) {
1159 return this.requiredSpace(content);
1160 } else {
1161 return content;
1162 }
1163 };
1164
1165 _proto.newNode = function newNode(node, namespace) {
1166 if (namespace) {
1167 if (/^ +$/.test(namespace)) {
1168 if (!this.options.lossy) {
1169 this.spaces = (this.spaces || '') + namespace;
1170 }
1171
1172 namespace = true;
1173 }
1174
1175 node.namespace = namespace;
1176 unescapeProp(node, "namespace");
1177 }
1178
1179 if (this.spaces) {
1180 node.spaces.before = this.spaces;
1181 this.spaces = '';
1182 }
1183
1184 return this.current.append(node);
1185 };
1186
1187 _proto.content = function content(token) {
1188 if (token === void 0) {
1189 token = this.currToken;
1190 }
1191
1192 return this.css.slice(token[_tokenize.FIELDS.START_POS], token[_tokenize.FIELDS.END_POS]);
1193 };
1194
1195 /**
1196 * returns the index of the next non-whitespace, non-comment token.
1197 * returns -1 if no meaningful token is found.
1198 */
1199 _proto.locateNextMeaningfulToken = function locateNextMeaningfulToken(startPosition) {
1200 if (startPosition === void 0) {
1201 startPosition = this.position + 1;
1202 }
1203
1204 var searchPosition = startPosition;
1205
1206 while (searchPosition < this.tokens.length) {
1207 if (WHITESPACE_EQUIV_TOKENS[this.tokens[searchPosition][_tokenize.FIELDS.TYPE]]) {
1208 searchPosition++;
1209 continue;
1210 } else {
1211 return searchPosition;
1212 }
1213 }
1214
1215 return -1;
1216 };
1217
1218 _createClass(Parser, [{
1219 key: "currToken",
1220 get: function get() {
1221 return this.tokens[this.position];
1222 }
1223 }, {
1224 key: "nextToken",
1225 get: function get() {
1226 return this.tokens[this.position + 1];
1227 }
1228 }, {
1229 key: "prevToken",
1230 get: function get() {
1231 return this.tokens[this.position - 1];
1232 }
1233 }]);
1234
1235 return Parser;
1236}();
1237
1238exports["default"] = Parser;
1239module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.