.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / postcss-selector-parser / dist / selectors / container.js
1 'use strict';
2
3 exports.__esModule = true;
4
5 var _createClass = function () { function 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
6
7 var _node = require('./node');
8
9 var _node2 = _interopRequireDefault(_node);
10
11 var _types = require('./types');
12
13 var types = _interopRequireWildcard(_types);
14
15 function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
16
17 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
19 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
20
21 function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
22
23 function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
24
25 var Container = function (_Node) {
26     _inherits(Container, _Node);
27
28     function Container(opts) {
29         _classCallCheck(this, Container);
30
31         var _this = _possibleConstructorReturn(this, _Node.call(this, opts));
32
33         if (!_this.nodes) {
34             _this.nodes = [];
35         }
36         return _this;
37     }
38
39     Container.prototype.append = function append(selector) {
40         selector.parent = this;
41         this.nodes.push(selector);
42         return this;
43     };
44
45     Container.prototype.prepend = function prepend(selector) {
46         selector.parent = this;
47         this.nodes.unshift(selector);
48         return this;
49     };
50
51     Container.prototype.at = function at(index) {
52         return this.nodes[index];
53     };
54
55     Container.prototype.index = function index(child) {
56         if (typeof child === 'number') {
57             return child;
58         }
59         return this.nodes.indexOf(child);
60     };
61
62     Container.prototype.removeChild = function removeChild(child) {
63         child = this.index(child);
64         this.at(child).parent = undefined;
65         this.nodes.splice(child, 1);
66
67         var index = void 0;
68         for (var id in this.indexes) {
69             index = this.indexes[id];
70             if (index >= child) {
71                 this.indexes[id] = index - 1;
72             }
73         }
74
75         return this;
76     };
77
78     Container.prototype.removeAll = function removeAll() {
79         for (var _iterator = this.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
80             var _ref;
81
82             if (_isArray) {
83                 if (_i >= _iterator.length) break;
84                 _ref = _iterator[_i++];
85             } else {
86                 _i = _iterator.next();
87                 if (_i.done) break;
88                 _ref = _i.value;
89             }
90
91             var node = _ref;
92
93             node.parent = undefined;
94         }
95         this.nodes = [];
96         return this;
97     };
98
99     Container.prototype.empty = function empty() {
100         return this.removeAll();
101     };
102
103     Container.prototype.insertAfter = function insertAfter(oldNode, newNode) {
104         newNode.parent = this;
105         var oldIndex = this.index(oldNode);
106         this.nodes.splice(oldIndex + 1, 0, newNode);
107
108         newNode.parent = this;
109
110         var index = void 0;
111         for (var id in this.indexes) {
112             index = this.indexes[id];
113             if (oldIndex <= index) {
114                 this.indexes[id] = index + 1;
115             }
116         }
117
118         return this;
119     };
120
121     Container.prototype.insertBefore = function insertBefore(oldNode, newNode) {
122         newNode.parent = this;
123         var oldIndex = this.index(oldNode);
124         this.nodes.splice(oldIndex, 0, newNode);
125
126         newNode.parent = this;
127
128         var index = void 0;
129         for (var id in this.indexes) {
130             index = this.indexes[id];
131             if (index <= oldIndex) {
132                 this.indexes[id] = index + 1;
133             }
134         }
135
136         return this;
137     };
138
139     Container.prototype.each = function each(callback) {
140         if (!this.lastEach) {
141             this.lastEach = 0;
142         }
143         if (!this.indexes) {
144             this.indexes = {};
145         }
146
147         this.lastEach++;
148         var id = this.lastEach;
149         this.indexes[id] = 0;
150
151         if (!this.length) {
152             return undefined;
153         }
154
155         var index = void 0,
156             result = void 0;
157         while (this.indexes[id] < this.length) {
158             index = this.indexes[id];
159             result = callback(this.at(index), index);
160             if (result === false) {
161                 break;
162             }
163
164             this.indexes[id] += 1;
165         }
166
167         delete this.indexes[id];
168
169         if (result === false) {
170             return false;
171         }
172     };
173
174     Container.prototype.walk = function walk(callback) {
175         return this.each(function (node, i) {
176             var result = callback(node, i);
177
178             if (result !== false && node.length) {
179                 result = node.walk(callback);
180             }
181
182             if (result === false) {
183                 return false;
184             }
185         });
186     };
187
188     Container.prototype.walkAttributes = function walkAttributes(callback) {
189         var _this2 = this;
190
191         return this.walk(function (selector) {
192             if (selector.type === types.ATTRIBUTE) {
193                 return callback.call(_this2, selector);
194             }
195         });
196     };
197
198     Container.prototype.walkClasses = function walkClasses(callback) {
199         var _this3 = this;
200
201         return this.walk(function (selector) {
202             if (selector.type === types.CLASS) {
203                 return callback.call(_this3, selector);
204             }
205         });
206     };
207
208     Container.prototype.walkCombinators = function walkCombinators(callback) {
209         var _this4 = this;
210
211         return this.walk(function (selector) {
212             if (selector.type === types.COMBINATOR) {
213                 return callback.call(_this4, selector);
214             }
215         });
216     };
217
218     Container.prototype.walkComments = function walkComments(callback) {
219         var _this5 = this;
220
221         return this.walk(function (selector) {
222             if (selector.type === types.COMMENT) {
223                 return callback.call(_this5, selector);
224             }
225         });
226     };
227
228     Container.prototype.walkIds = function walkIds(callback) {
229         var _this6 = this;
230
231         return this.walk(function (selector) {
232             if (selector.type === types.ID) {
233                 return callback.call(_this6, selector);
234             }
235         });
236     };
237
238     Container.prototype.walkNesting = function walkNesting(callback) {
239         var _this7 = this;
240
241         return this.walk(function (selector) {
242             if (selector.type === types.NESTING) {
243                 return callback.call(_this7, selector);
244             }
245         });
246     };
247
248     Container.prototype.walkPseudos = function walkPseudos(callback) {
249         var _this8 = this;
250
251         return this.walk(function (selector) {
252             if (selector.type === types.PSEUDO) {
253                 return callback.call(_this8, selector);
254             }
255         });
256     };
257
258     Container.prototype.walkTags = function walkTags(callback) {
259         var _this9 = this;
260
261         return this.walk(function (selector) {
262             if (selector.type === types.TAG) {
263                 return callback.call(_this9, selector);
264             }
265         });
266     };
267
268     Container.prototype.walkUniversals = function walkUniversals(callback) {
269         var _this10 = this;
270
271         return this.walk(function (selector) {
272             if (selector.type === types.UNIVERSAL) {
273                 return callback.call(_this10, selector);
274             }
275         });
276     };
277
278     Container.prototype.split = function split(callback) {
279         var _this11 = this;
280
281         var current = [];
282         return this.reduce(function (memo, node, index) {
283             var split = callback.call(_this11, node);
284             current.push(node);
285             if (split) {
286                 memo.push(current);
287                 current = [];
288             } else if (index === _this11.length - 1) {
289                 memo.push(current);
290             }
291             return memo;
292         }, []);
293     };
294
295     Container.prototype.map = function map(callback) {
296         return this.nodes.map(callback);
297     };
298
299     Container.prototype.reduce = function reduce(callback, memo) {
300         return this.nodes.reduce(callback, memo);
301     };
302
303     Container.prototype.every = function every(callback) {
304         return this.nodes.every(callback);
305     };
306
307     Container.prototype.some = function some(callback) {
308         return this.nodes.some(callback);
309     };
310
311     Container.prototype.filter = function filter(callback) {
312         return this.nodes.filter(callback);
313     };
314
315     Container.prototype.sort = function sort(callback) {
316         return this.nodes.sort(callback);
317     };
318
319     Container.prototype.toString = function toString() {
320         return this.map(String).join('');
321     };
322
323     _createClass(Container, [{
324         key: 'first',
325         get: function get() {
326             return this.at(0);
327         }
328     }, {
329         key: 'last',
330         get: function get() {
331             return this.at(this.length - 1);
332         }
333     }, {
334         key: 'length',
335         get: function get() {
336             return this.nodes.length;
337         }
338     }]);
339
340     return Container;
341 }(_node2.default);
342
343 exports.default = Container;
344 module.exports = exports['default'];