container.js
9.79 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _node = _interopRequireDefault(require("./node"));
var types = _interopRequireWildcard(require("./types"));
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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
var Container =
/*#__PURE__*/
function (_Node) {
_inheritsLoose(Container, _Node);
function Container(opts) {
var _this;
_this = _Node.call(this, opts) || this;
if (!_this.nodes) {
_this.nodes = [];
}
return _this;
}
var _proto = Container.prototype;
_proto.append = function append(selector) {
selector.parent = this;
this.nodes.push(selector);
return this;
};
_proto.prepend = function prepend(selector) {
selector.parent = this;
this.nodes.unshift(selector);
return this;
};
_proto.at = function at(index) {
return this.nodes[index];
};
_proto.index = function index(child) {
if (typeof child === 'number') {
return child;
}
return this.nodes.indexOf(child);
};
_proto.removeChild = function removeChild(child) {
child = this.index(child);
this.at(child).parent = undefined;
this.nodes.splice(child, 1);
var index;
for (var id in this.indexes) {
index = this.indexes[id];
if (index >= child) {
this.indexes[id] = index - 1;
}
}
return this;
};
_proto.removeAll = function removeAll() {
for (var _iterator = this.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var node = _ref;
node.parent = undefined;
}
this.nodes = [];
return this;
};
_proto.empty = function empty() {
return this.removeAll();
};
_proto.insertAfter = function insertAfter(oldNode, newNode) {
newNode.parent = this;
var oldIndex = this.index(oldNode);
this.nodes.splice(oldIndex + 1, 0, newNode);
newNode.parent = this;
var index;
for (var id in this.indexes) {
index = this.indexes[id];
if (oldIndex <= index) {
this.indexes[id] = index + 1;
}
}
return this;
};
_proto.insertBefore = function insertBefore(oldNode, newNode) {
newNode.parent = this;
var oldIndex = this.index(oldNode);
this.nodes.splice(oldIndex, 0, newNode);
newNode.parent = this;
var index;
for (var id in this.indexes) {
index = this.indexes[id];
if (index <= oldIndex) {
this.indexes[id] = index + 1;
}
}
return this;
};
_proto._findChildAtPosition = function _findChildAtPosition(line, col) {
var found = undefined;
this.each(function (node) {
if (node.atPosition) {
var foundChild = node.atPosition(line, col);
if (foundChild) {
found = foundChild;
return false;
}
} else if (node.isAtPosition(line, col)) {
found = node;
return false;
}
});
return found;
}
/**
* Return the most specific node at the line and column number given.
* The source location is based on the original parsed location, locations aren't
* updated as selector nodes are mutated.
*
* Note that this location is relative to the location of the first character
* of the selector, and not the location of the selector in the overall document
* when used in conjunction with postcss.
*
* If not found, returns undefined.
* @param {number} line The line number of the node to find. (1-based index)
* @param {number} col The column number of the node to find. (1-based index)
*/
;
_proto.atPosition = function atPosition(line, col) {
if (this.isAtPosition(line, col)) {
return this._findChildAtPosition(line, col) || this;
} else {
return undefined;
}
};
_proto._inferEndPosition = function _inferEndPosition() {
if (this.last && this.last.source && this.last.source.end) {
this.source = this.source || {};
this.source.end = this.source.end || {};
Object.assign(this.source.end, this.last.source.end);
}
};
_proto.each = function each(callback) {
if (!this.lastEach) {
this.lastEach = 0;
}
if (!this.indexes) {
this.indexes = {};
}
this.lastEach++;
var id = this.lastEach;
this.indexes[id] = 0;
if (!this.length) {
return undefined;
}
var index, result;
while (this.indexes[id] < this.length) {
index = this.indexes[id];
result = callback(this.at(index), index);
if (result === false) {
break;
}
this.indexes[id] += 1;
}
delete this.indexes[id];
if (result === false) {
return false;
}
};
_proto.walk = function walk(callback) {
return this.each(function (node, i) {
var result = callback(node, i);
if (result !== false && node.length) {
result = node.walk(callback);
}
if (result === false) {
return false;
}
});
};
_proto.walkAttributes = function walkAttributes(callback) {
var _this2 = this;
return this.walk(function (selector) {
if (selector.type === types.ATTRIBUTE) {
return callback.call(_this2, selector);
}
});
};
_proto.walkClasses = function walkClasses(callback) {
var _this3 = this;
return this.walk(function (selector) {
if (selector.type === types.CLASS) {
return callback.call(_this3, selector);
}
});
};
_proto.walkCombinators = function walkCombinators(callback) {
var _this4 = this;
return this.walk(function (selector) {
if (selector.type === types.COMBINATOR) {
return callback.call(_this4, selector);
}
});
};
_proto.walkComments = function walkComments(callback) {
var _this5 = this;
return this.walk(function (selector) {
if (selector.type === types.COMMENT) {
return callback.call(_this5, selector);
}
});
};
_proto.walkIds = function walkIds(callback) {
var _this6 = this;
return this.walk(function (selector) {
if (selector.type === types.ID) {
return callback.call(_this6, selector);
}
});
};
_proto.walkNesting = function walkNesting(callback) {
var _this7 = this;
return this.walk(function (selector) {
if (selector.type === types.NESTING) {
return callback.call(_this7, selector);
}
});
};
_proto.walkPseudos = function walkPseudos(callback) {
var _this8 = this;
return this.walk(function (selector) {
if (selector.type === types.PSEUDO) {
return callback.call(_this8, selector);
}
});
};
_proto.walkTags = function walkTags(callback) {
var _this9 = this;
return this.walk(function (selector) {
if (selector.type === types.TAG) {
return callback.call(_this9, selector);
}
});
};
_proto.walkUniversals = function walkUniversals(callback) {
var _this10 = this;
return this.walk(function (selector) {
if (selector.type === types.UNIVERSAL) {
return callback.call(_this10, selector);
}
});
};
_proto.split = function split(callback) {
var _this11 = this;
var current = [];
return this.reduce(function (memo, node, index) {
var split = callback.call(_this11, node);
current.push(node);
if (split) {
memo.push(current);
current = [];
} else if (index === _this11.length - 1) {
memo.push(current);
}
return memo;
}, []);
};
_proto.map = function map(callback) {
return this.nodes.map(callback);
};
_proto.reduce = function reduce(callback, memo) {
return this.nodes.reduce(callback, memo);
};
_proto.every = function every(callback) {
return this.nodes.every(callback);
};
_proto.some = function some(callback) {
return this.nodes.some(callback);
};
_proto.filter = function filter(callback) {
return this.nodes.filter(callback);
};
_proto.sort = function sort(callback) {
return this.nodes.sort(callback);
};
_proto.toString = function toString() {
return this.map(String).join('');
};
_createClass(Container, [{
key: "first",
get: function get() {
return this.at(0);
}
}, {
key: "last",
get: function get() {
return this.at(this.length - 1);
}
}, {
key: "length",
get: function get() {
return this.nodes.length;
}
}]);
return Container;
}(_node.default);
exports.default = Container;
module.exports = exports.default;