3 exports.__esModule = true;
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; }; }();
7 var _node = require('./node');
9 var _node2 = _interopRequireDefault(_node);
11 var _types = require('./types');
13 var types = _interopRequireWildcard(_types);
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; } }
17 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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; }
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; }
25 var Container = function (_Node) {
26 _inherits(Container, _Node);
28 function Container(opts) {
29 _classCallCheck(this, Container);
31 var _this = _possibleConstructorReturn(this, _Node.call(this, opts));
39 Container.prototype.append = function append(selector) {
40 selector.parent = this;
41 this.nodes.push(selector);
45 Container.prototype.prepend = function prepend(selector) {
46 selector.parent = this;
47 this.nodes.unshift(selector);
51 Container.prototype.at = function at(index) {
52 return this.nodes[index];
55 Container.prototype.index = function index(child) {
56 if (typeof child === 'number') {
59 return this.nodes.indexOf(child);
62 Container.prototype.removeChild = function removeChild(child) {
63 child = this.index(child);
64 this.at(child).parent = undefined;
65 this.nodes.splice(child, 1);
68 for (var id in this.indexes) {
69 index = this.indexes[id];
71 this.indexes[id] = index - 1;
78 Container.prototype.removeAll = function removeAll() {
79 for (var _iterator = this.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
83 if (_i >= _iterator.length) break;
84 _ref = _iterator[_i++];
86 _i = _iterator.next();
93 node.parent = undefined;
99 Container.prototype.empty = function empty() {
100 return this.removeAll();
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);
108 newNode.parent = this;
111 for (var id in this.indexes) {
112 index = this.indexes[id];
113 if (oldIndex <= index) {
114 this.indexes[id] = index + 1;
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);
126 newNode.parent = this;
129 for (var id in this.indexes) {
130 index = this.indexes[id];
131 if (index <= oldIndex) {
132 this.indexes[id] = index + 1;
139 Container.prototype.each = function each(callback) {
140 if (!this.lastEach) {
148 var id = this.lastEach;
149 this.indexes[id] = 0;
157 while (this.indexes[id] < this.length) {
158 index = this.indexes[id];
159 result = callback(this.at(index), index);
160 if (result === false) {
164 this.indexes[id] += 1;
167 delete this.indexes[id];
169 if (result === false) {
174 Container.prototype.walk = function walk(callback) {
175 return this.each(function (node, i) {
176 var result = callback(node, i);
178 if (result !== false && node.length) {
179 result = node.walk(callback);
182 if (result === false) {
188 Container.prototype.walkAttributes = function walkAttributes(callback) {
191 return this.walk(function (selector) {
192 if (selector.type === types.ATTRIBUTE) {
193 return callback.call(_this2, selector);
198 Container.prototype.walkClasses = function walkClasses(callback) {
201 return this.walk(function (selector) {
202 if (selector.type === types.CLASS) {
203 return callback.call(_this3, selector);
208 Container.prototype.walkCombinators = function walkCombinators(callback) {
211 return this.walk(function (selector) {
212 if (selector.type === types.COMBINATOR) {
213 return callback.call(_this4, selector);
218 Container.prototype.walkComments = function walkComments(callback) {
221 return this.walk(function (selector) {
222 if (selector.type === types.COMMENT) {
223 return callback.call(_this5, selector);
228 Container.prototype.walkIds = function walkIds(callback) {
231 return this.walk(function (selector) {
232 if (selector.type === types.ID) {
233 return callback.call(_this6, selector);
238 Container.prototype.walkNesting = function walkNesting(callback) {
241 return this.walk(function (selector) {
242 if (selector.type === types.NESTING) {
243 return callback.call(_this7, selector);
248 Container.prototype.walkPseudos = function walkPseudos(callback) {
251 return this.walk(function (selector) {
252 if (selector.type === types.PSEUDO) {
253 return callback.call(_this8, selector);
258 Container.prototype.walkTags = function walkTags(callback) {
261 return this.walk(function (selector) {
262 if (selector.type === types.TAG) {
263 return callback.call(_this9, selector);
268 Container.prototype.walkUniversals = function walkUniversals(callback) {
271 return this.walk(function (selector) {
272 if (selector.type === types.UNIVERSAL) {
273 return callback.call(_this10, selector);
278 Container.prototype.split = function split(callback) {
282 return this.reduce(function (memo, node, index) {
283 var split = callback.call(_this11, node);
288 } else if (index === _this11.length - 1) {
295 Container.prototype.map = function map(callback) {
296 return this.nodes.map(callback);
299 Container.prototype.reduce = function reduce(callback, memo) {
300 return this.nodes.reduce(callback, memo);
303 Container.prototype.every = function every(callback) {
304 return this.nodes.every(callback);
307 Container.prototype.some = function some(callback) {
308 return this.nodes.some(callback);
311 Container.prototype.filter = function filter(callback) {
312 return this.nodes.filter(callback);
315 Container.prototype.sort = function sort(callback) {
316 return this.nodes.sort(callback);
319 Container.prototype.toString = function toString() {
320 return this.map(String).join('');
323 _createClass(Container, [{
325 get: function get() {
330 get: function get() {
331 return this.at(this.length - 1);
335 get: function get() {
336 return this.nodes.length;
343 exports.default = Container;
344 module.exports = exports['default'];