afde9a1944c095f83c4e729d271f20fa397bc9bcb377ecc3b382d6a35ed5332916fad8674edf69e060bdddc17d9de94ffaba11ee8f79eb2ea3e74bb1d3ab86 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. newNode.parent = this;
  77. var oldIndex = this.index(oldNode);
  78. this.nodes.splice(oldIndex + 1, 0, newNode);
  79. newNode.parent = this;
  80. var index = void 0;
  81. for (var id in this.indexes) {
  82. index = this.indexes[id];
  83. if (oldIndex <= index) {
  84. this.indexes[id] = index + 1;
  85. }
  86. }
  87. return this;
  88. };
  89. Container.prototype.insertBefore = function insertBefore(oldNode, newNode) {
  90. newNode.parent = this;
  91. var oldIndex = this.index(oldNode);
  92. this.nodes.splice(oldIndex, 0, newNode);
  93. newNode.parent = this;
  94. var index = void 0;
  95. for (var id in this.indexes) {
  96. index = this.indexes[id];
  97. if (index <= oldIndex) {
  98. this.indexes[id] = index + 1;
  99. }
  100. }
  101. return this;
  102. };
  103. Container.prototype.each = function each(callback) {
  104. if (!this.lastEach) {
  105. this.lastEach = 0;
  106. }
  107. if (!this.indexes) {
  108. this.indexes = {};
  109. }
  110. this.lastEach++;
  111. var id = this.lastEach;
  112. this.indexes[id] = 0;
  113. if (!this.length) {
  114. return undefined;
  115. }
  116. var index = void 0,
  117. result = void 0;
  118. while (this.indexes[id] < this.length) {
  119. index = this.indexes[id];
  120. result = callback(this.at(index), index);
  121. if (result === false) {
  122. break;
  123. }
  124. this.indexes[id] += 1;
  125. }
  126. delete this.indexes[id];
  127. if (result === false) {
  128. return false;
  129. }
  130. };
  131. Container.prototype.walk = function walk(callback) {
  132. return this.each(function (node, i) {
  133. var result = callback(node, i);
  134. if (result !== false && node.length) {
  135. result = node.walk(callback);
  136. }
  137. if (result === false) {
  138. return false;
  139. }
  140. });
  141. };
  142. Container.prototype.walkAttributes = function walkAttributes(callback) {
  143. var _this2 = this;
  144. return this.walk(function (selector) {
  145. if (selector.type === types.ATTRIBUTE) {
  146. return callback.call(_this2, selector);
  147. }
  148. });
  149. };
  150. Container.prototype.walkClasses = function walkClasses(callback) {
  151. var _this3 = this;
  152. return this.walk(function (selector) {
  153. if (selector.type === types.CLASS) {
  154. return callback.call(_this3, selector);
  155. }
  156. });
  157. };
  158. Container.prototype.walkCombinators = function walkCombinators(callback) {
  159. var _this4 = this;
  160. return this.walk(function (selector) {
  161. if (selector.type === types.COMBINATOR) {
  162. return callback.call(_this4, selector);
  163. }
  164. });
  165. };
  166. Container.prototype.walkComments = function walkComments(callback) {
  167. var _this5 = this;
  168. return this.walk(function (selector) {
  169. if (selector.type === types.COMMENT) {
  170. return callback.call(_this5, selector);
  171. }
  172. });
  173. };
  174. Container.prototype.walkIds = function walkIds(callback) {
  175. var _this6 = this;
  176. return this.walk(function (selector) {
  177. if (selector.type === types.ID) {
  178. return callback.call(_this6, selector);
  179. }
  180. });
  181. };
  182. Container.prototype.walkNesting = function walkNesting(callback) {
  183. var _this7 = this;
  184. return this.walk(function (selector) {
  185. if (selector.type === types.NESTING) {
  186. return callback.call(_this7, selector);
  187. }
  188. });
  189. };
  190. Container.prototype.walkPseudos = function walkPseudos(callback) {
  191. var _this8 = this;
  192. return this.walk(function (selector) {
  193. if (selector.type === types.PSEUDO) {
  194. return callback.call(_this8, selector);
  195. }
  196. });
  197. };
  198. Container.prototype.walkTags = function walkTags(callback) {
  199. var _this9 = this;
  200. return this.walk(function (selector) {
  201. if (selector.type === types.TAG) {
  202. return callback.call(_this9, selector);
  203. }
  204. });
  205. };
  206. Container.prototype.walkUniversals = function walkUniversals(callback) {
  207. var _this10 = this;
  208. return this.walk(function (selector) {
  209. if (selector.type === types.UNIVERSAL) {
  210. return callback.call(_this10, selector);
  211. }
  212. });
  213. };
  214. Container.prototype.split = function split(callback) {
  215. var _this11 = this;
  216. var current = [];
  217. return this.reduce(function (memo, node, index) {
  218. var split = callback.call(_this11, node);
  219. current.push(node);
  220. if (split) {
  221. memo.push(current);
  222. current = [];
  223. } else if (index === _this11.length - 1) {
  224. memo.push(current);
  225. }
  226. return memo;
  227. }, []);
  228. };
  229. Container.prototype.map = function map(callback) {
  230. return this.nodes.map(callback);
  231. };
  232. Container.prototype.reduce = function reduce(callback, memo) {
  233. return this.nodes.reduce(callback, memo);
  234. };
  235. Container.prototype.every = function every(callback) {
  236. return this.nodes.every(callback);
  237. };
  238. Container.prototype.some = function some(callback) {
  239. return this.nodes.some(callback);
  240. };
  241. Container.prototype.filter = function filter(callback) {
  242. return this.nodes.filter(callback);
  243. };
  244. Container.prototype.sort = function sort(callback) {
  245. return this.nodes.sort(callback);
  246. };
  247. Container.prototype.toString = function toString() {
  248. return this.map(String).join('');
  249. };
  250. _createClass(Container, [{
  251. key: 'first',
  252. get: function get() {
  253. return this.at(0);
  254. }
  255. }, {
  256. key: 'last',
  257. get: function get() {
  258. return this.at(this.length - 1);
  259. }
  260. }, {
  261. key: 'length',
  262. get: function get() {
  263. return this.nodes.length;
  264. }
  265. }]);
  266. return Container;
  267. }(_node2.default);
  268. exports.default = Container;
  269. module.exports = exports['default'];