container.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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._findChildAtPosition = function _findChildAtPosition(line, col) {
  104. var found = undefined;
  105. this.each(function (node) {
  106. if (node.atPosition) {
  107. var foundChild = node.atPosition(line, col);
  108. if (foundChild) {
  109. found = foundChild;
  110. return false;
  111. }
  112. } else if (node.isAtPosition(line, col)) {
  113. found = node;
  114. return false;
  115. }
  116. });
  117. return found;
  118. };
  119. /**
  120. * Return the most specific node at the line and column number given.
  121. * The source location is based on the original parsed location, locations aren't
  122. * updated as selector nodes are mutated.
  123. *
  124. * Note that this location is relative to the location of the first character
  125. * of the selector, and not the location of the selector in the overall document
  126. * when used in conjunction with postcss.
  127. *
  128. * If not found, returns undefined.
  129. * @param {number} line The line number of the node to find. (1-based index)
  130. * @param {number} col The column number of the node to find. (1-based index)
  131. */
  132. Container.prototype.atPosition = function atPosition(line, col) {
  133. if (this.isAtPosition(line, col)) {
  134. return this._findChildAtPosition(line, col) || this;
  135. } else {
  136. return undefined;
  137. }
  138. };
  139. Container.prototype._inferEndPosition = function _inferEndPosition() {
  140. if (this.last && this.last.source && this.last.source.end) {
  141. this.source = this.source || {};
  142. this.source.end = this.source.end || {};
  143. Object.assign(this.source.end, this.last.source.end);
  144. }
  145. };
  146. Container.prototype.each = function each(callback) {
  147. if (!this.lastEach) {
  148. this.lastEach = 0;
  149. }
  150. if (!this.indexes) {
  151. this.indexes = {};
  152. }
  153. this.lastEach++;
  154. var id = this.lastEach;
  155. this.indexes[id] = 0;
  156. if (!this.length) {
  157. return undefined;
  158. }
  159. var index = void 0,
  160. result = void 0;
  161. while (this.indexes[id] < this.length) {
  162. index = this.indexes[id];
  163. result = callback(this.at(index), index);
  164. if (result === false) {
  165. break;
  166. }
  167. this.indexes[id] += 1;
  168. }
  169. delete this.indexes[id];
  170. if (result === false) {
  171. return false;
  172. }
  173. };
  174. Container.prototype.walk = function walk(callback) {
  175. return this.each(function (node, i) {
  176. var result = callback(node, i);
  177. if (result !== false && node.length) {
  178. result = node.walk(callback);
  179. }
  180. if (result === false) {
  181. return false;
  182. }
  183. });
  184. };
  185. Container.prototype.walkAttributes = function walkAttributes(callback) {
  186. var _this2 = this;
  187. return this.walk(function (selector) {
  188. if (selector.type === types.ATTRIBUTE) {
  189. return callback.call(_this2, selector);
  190. }
  191. });
  192. };
  193. Container.prototype.walkClasses = function walkClasses(callback) {
  194. var _this3 = this;
  195. return this.walk(function (selector) {
  196. if (selector.type === types.CLASS) {
  197. return callback.call(_this3, selector);
  198. }
  199. });
  200. };
  201. Container.prototype.walkCombinators = function walkCombinators(callback) {
  202. var _this4 = this;
  203. return this.walk(function (selector) {
  204. if (selector.type === types.COMBINATOR) {
  205. return callback.call(_this4, selector);
  206. }
  207. });
  208. };
  209. Container.prototype.walkComments = function walkComments(callback) {
  210. var _this5 = this;
  211. return this.walk(function (selector) {
  212. if (selector.type === types.COMMENT) {
  213. return callback.call(_this5, selector);
  214. }
  215. });
  216. };
  217. Container.prototype.walkIds = function walkIds(callback) {
  218. var _this6 = this;
  219. return this.walk(function (selector) {
  220. if (selector.type === types.ID) {
  221. return callback.call(_this6, selector);
  222. }
  223. });
  224. };
  225. Container.prototype.walkNesting = function walkNesting(callback) {
  226. var _this7 = this;
  227. return this.walk(function (selector) {
  228. if (selector.type === types.NESTING) {
  229. return callback.call(_this7, selector);
  230. }
  231. });
  232. };
  233. Container.prototype.walkPseudos = function walkPseudos(callback) {
  234. var _this8 = this;
  235. return this.walk(function (selector) {
  236. if (selector.type === types.PSEUDO) {
  237. return callback.call(_this8, selector);
  238. }
  239. });
  240. };
  241. Container.prototype.walkTags = function walkTags(callback) {
  242. var _this9 = this;
  243. return this.walk(function (selector) {
  244. if (selector.type === types.TAG) {
  245. return callback.call(_this9, selector);
  246. }
  247. });
  248. };
  249. Container.prototype.walkUniversals = function walkUniversals(callback) {
  250. var _this10 = this;
  251. return this.walk(function (selector) {
  252. if (selector.type === types.UNIVERSAL) {
  253. return callback.call(_this10, selector);
  254. }
  255. });
  256. };
  257. Container.prototype.split = function split(callback) {
  258. var _this11 = this;
  259. var current = [];
  260. return this.reduce(function (memo, node, index) {
  261. var split = callback.call(_this11, node);
  262. current.push(node);
  263. if (split) {
  264. memo.push(current);
  265. current = [];
  266. } else if (index === _this11.length - 1) {
  267. memo.push(current);
  268. }
  269. return memo;
  270. }, []);
  271. };
  272. Container.prototype.map = function map(callback) {
  273. return this.nodes.map(callback);
  274. };
  275. Container.prototype.reduce = function reduce(callback, memo) {
  276. return this.nodes.reduce(callback, memo);
  277. };
  278. Container.prototype.every = function every(callback) {
  279. return this.nodes.every(callback);
  280. };
  281. Container.prototype.some = function some(callback) {
  282. return this.nodes.some(callback);
  283. };
  284. Container.prototype.filter = function filter(callback) {
  285. return this.nodes.filter(callback);
  286. };
  287. Container.prototype.sort = function sort(callback) {
  288. return this.nodes.sort(callback);
  289. };
  290. Container.prototype.toString = function toString() {
  291. return this.map(String).join('');
  292. };
  293. _createClass(Container, [{
  294. key: 'first',
  295. get: function get() {
  296. return this.at(0);
  297. }
  298. }, {
  299. key: 'last',
  300. get: function get() {
  301. return this.at(this.length - 1);
  302. }
  303. }, {
  304. key: 'length',
  305. get: function get() {
  306. return this.nodes.length;
  307. }
  308. }]);
  309. return Container;
  310. }(_node2.default);
  311. exports.default = Container;
  312. module.exports = exports['default'];