container.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. 'use strict';
  2. exports.__esModule = true;
  3. 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; }; }();
  4. var _node = require('./node');
  5. var _node2 = _interopRequireDefault(_node);
  6. var _types = require('./types');
  7. var types = _interopRequireWildcard(_types);
  8. 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; } }
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  11. 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; }
  12. 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; }
  13. var Container = function (_Node) {
  14. _inherits(Container, _Node);
  15. function Container(opts) {
  16. _classCallCheck(this, Container);
  17. var _this = _possibleConstructorReturn(this, _Node.call(this, opts));
  18. if (!_this.nodes) {
  19. _this.nodes = [];
  20. }
  21. return _this;
  22. }
  23. Container.prototype.append = function append(selector) {
  24. selector.parent = this;
  25. this.nodes.push(selector);
  26. return this;
  27. };
  28. Container.prototype.prepend = function prepend(selector) {
  29. selector.parent = this;
  30. this.nodes.unshift(selector);
  31. return this;
  32. };
  33. Container.prototype.at = function at(index) {
  34. return this.nodes[index];
  35. };
  36. Container.prototype.index = function index(child) {
  37. if (typeof child === 'number') {
  38. return child;
  39. }
  40. return this.nodes.indexOf(child);
  41. };
  42. Container.prototype.removeChild = function removeChild(child) {
  43. child = this.index(child);
  44. this.at(child).parent = undefined;
  45. this.nodes.splice(child, 1);
  46. var index = void 0;
  47. for (var id in this.indexes) {
  48. index = this.indexes[id];
  49. if (index >= child) {
  50. this.indexes[id] = index - 1;
  51. }
  52. }
  53. return this;
  54. };
  55. Container.prototype.removeAll = function removeAll() {
  56. for (var _iterator = this.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  57. var _ref;
  58. if (_isArray) {
  59. if (_i >= _iterator.length) break;
  60. _ref = _iterator[_i++];
  61. } else {
  62. _i = _iterator.next();
  63. if (_i.done) break;
  64. _ref = _i.value;
  65. }
  66. var node = _ref;
  67. node.parent = undefined;
  68. }
  69. this.nodes = [];
  70. return this;
  71. };
  72. Container.prototype.empty = function empty() {
  73. return this.removeAll();
  74. };
  75. Container.prototype.insertAfter = function insertAfter(oldNode, newNode) {
  76. var oldIndex = this.index(oldNode);
  77. this.nodes.splice(oldIndex + 1, 0, newNode);
  78. var index = void 0;
  79. for (var id in this.indexes) {
  80. index = this.indexes[id];
  81. if (oldIndex <= index) {
  82. this.indexes[id] = index + this.nodes.length;
  83. }
  84. }
  85. return this;
  86. };
  87. Container.prototype.insertBefore = function insertBefore(oldNode, newNode) {
  88. var oldIndex = this.index(oldNode);
  89. this.nodes.splice(oldIndex, 0, newNode);
  90. var index = void 0;
  91. for (var id in this.indexes) {
  92. index = this.indexes[id];
  93. if (oldIndex <= index) {
  94. this.indexes[id] = index + this.nodes.length;
  95. }
  96. }
  97. return this;
  98. };
  99. Container.prototype.each = function each(callback) {
  100. if (!this.lastEach) {
  101. this.lastEach = 0;
  102. }
  103. if (!this.indexes) {
  104. this.indexes = {};
  105. }
  106. this.lastEach++;
  107. var id = this.lastEach;
  108. this.indexes[id] = 0;
  109. if (!this.length) {
  110. return undefined;
  111. }
  112. var index = void 0,
  113. result = void 0;
  114. while (this.indexes[id] < this.length) {
  115. index = this.indexes[id];
  116. result = callback(this.at(index), index);
  117. if (result === false) {
  118. break;
  119. }
  120. this.indexes[id] += 1;
  121. }
  122. delete this.indexes[id];
  123. if (result === false) {
  124. return false;
  125. }
  126. };
  127. Container.prototype.walk = function walk(callback) {
  128. return this.each(function (node, i) {
  129. var result = callback(node, i);
  130. if (result !== false && node.length) {
  131. result = node.walk(callback);
  132. }
  133. if (result === false) {
  134. return false;
  135. }
  136. });
  137. };
  138. Container.prototype.walkAttributes = function walkAttributes(callback) {
  139. var _this2 = this;
  140. return this.walk(function (selector) {
  141. if (selector.type === types.ATTRIBUTE) {
  142. return callback.call(_this2, selector);
  143. }
  144. });
  145. };
  146. Container.prototype.walkClasses = function walkClasses(callback) {
  147. var _this3 = this;
  148. return this.walk(function (selector) {
  149. if (selector.type === types.CLASS) {
  150. return callback.call(_this3, selector);
  151. }
  152. });
  153. };
  154. Container.prototype.walkCombinators = function walkCombinators(callback) {
  155. var _this4 = this;
  156. return this.walk(function (selector) {
  157. if (selector.type === types.COMBINATOR) {
  158. return callback.call(_this4, selector);
  159. }
  160. });
  161. };
  162. Container.prototype.walkComments = function walkComments(callback) {
  163. var _this5 = this;
  164. return this.walk(function (selector) {
  165. if (selector.type === types.COMMENT) {
  166. return callback.call(_this5, selector);
  167. }
  168. });
  169. };
  170. Container.prototype.walkIds = function walkIds(callback) {
  171. var _this6 = this;
  172. return this.walk(function (selector) {
  173. if (selector.type === types.ID) {
  174. return callback.call(_this6, selector);
  175. }
  176. });
  177. };
  178. Container.prototype.walkNesting = function walkNesting(callback) {
  179. var _this7 = this;
  180. return this.walk(function (selector) {
  181. if (selector.type === types.NESTING) {
  182. return callback.call(_this7, selector);
  183. }
  184. });
  185. };
  186. Container.prototype.walkPseudos = function walkPseudos(callback) {
  187. var _this8 = this;
  188. return this.walk(function (selector) {
  189. if (selector.type === types.PSEUDO) {
  190. return callback.call(_this8, selector);
  191. }
  192. });
  193. };
  194. Container.prototype.walkTags = function walkTags(callback) {
  195. var _this9 = this;
  196. return this.walk(function (selector) {
  197. if (selector.type === types.TAG) {
  198. return callback.call(_this9, selector);
  199. }
  200. });
  201. };
  202. Container.prototype.walkUniversals = function walkUniversals(callback) {
  203. var _this10 = this;
  204. return this.walk(function (selector) {
  205. if (selector.type === types.UNIVERSAL) {
  206. return callback.call(_this10, selector);
  207. }
  208. });
  209. };
  210. Container.prototype.split = function split(callback) {
  211. var _this11 = this;
  212. var current = [];
  213. return this.reduce(function (memo, node, index) {
  214. var split = callback.call(_this11, node);
  215. current.push(node);
  216. if (split) {
  217. memo.push(current);
  218. current = [];
  219. } else if (index === _this11.length - 1) {
  220. memo.push(current);
  221. }
  222. return memo;
  223. }, []);
  224. };
  225. Container.prototype.map = function map(callback) {
  226. return this.nodes.map(callback);
  227. };
  228. Container.prototype.reduce = function reduce(callback, memo) {
  229. return this.nodes.reduce(callback, memo);
  230. };
  231. Container.prototype.every = function every(callback) {
  232. return this.nodes.every(callback);
  233. };
  234. Container.prototype.some = function some(callback) {
  235. return this.nodes.some(callback);
  236. };
  237. Container.prototype.filter = function filter(callback) {
  238. return this.nodes.filter(callback);
  239. };
  240. Container.prototype.sort = function sort(callback) {
  241. return this.nodes.sort(callback);
  242. };
  243. Container.prototype.toString = function toString() {
  244. return this.map(String).join('');
  245. };
  246. _createClass(Container, [{
  247. key: 'first',
  248. get: function get() {
  249. return this.at(0);
  250. }
  251. }, {
  252. key: 'last',
  253. get: function get() {
  254. return this.at(this.length - 1);
  255. }
  256. }, {
  257. key: 'length',
  258. get: function get() {
  259. return this.nodes.length;
  260. }
  261. }]);
  262. return Container;
  263. }(_node2.default);
  264. exports.default = Container;
  265. module.exports = exports['default'];