container.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _node = _interopRequireDefault(require("./node"));
  5. var types = _interopRequireWildcard(require("./types"));
  6. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  7. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  9. function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  10. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  11. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  12. 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); } }
  13. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  14. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
  15. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  16. var Container = /*#__PURE__*/function (_Node) {
  17. _inheritsLoose(Container, _Node);
  18. function Container(opts) {
  19. var _this;
  20. _this = _Node.call(this, opts) || this;
  21. if (!_this.nodes) {
  22. _this.nodes = [];
  23. }
  24. return _this;
  25. }
  26. var _proto = Container.prototype;
  27. _proto.append = function append(selector) {
  28. selector.parent = this;
  29. this.nodes.push(selector);
  30. return this;
  31. };
  32. _proto.prepend = function prepend(selector) {
  33. selector.parent = this;
  34. this.nodes.unshift(selector);
  35. for (var id in this.indexes) {
  36. this.indexes[id]++;
  37. }
  38. return this;
  39. };
  40. _proto.at = function at(index) {
  41. return this.nodes[index];
  42. };
  43. _proto.index = function index(child) {
  44. if (typeof child === 'number') {
  45. return child;
  46. }
  47. return this.nodes.indexOf(child);
  48. };
  49. _proto.removeChild = function removeChild(child) {
  50. child = this.index(child);
  51. this.at(child).parent = undefined;
  52. this.nodes.splice(child, 1);
  53. var index;
  54. for (var id in this.indexes) {
  55. index = this.indexes[id];
  56. if (index >= child) {
  57. this.indexes[id] = index - 1;
  58. }
  59. }
  60. return this;
  61. };
  62. _proto.removeAll = function removeAll() {
  63. for (var _iterator = _createForOfIteratorHelperLoose(this.nodes), _step; !(_step = _iterator()).done;) {
  64. var node = _step.value;
  65. node.parent = undefined;
  66. }
  67. this.nodes = [];
  68. return this;
  69. };
  70. _proto.empty = function empty() {
  71. return this.removeAll();
  72. };
  73. _proto.insertAfter = function insertAfter(oldNode, newNode) {
  74. newNode.parent = this;
  75. var oldIndex = this.index(oldNode);
  76. this.nodes.splice(oldIndex + 1, 0, newNode);
  77. newNode.parent = this;
  78. var index;
  79. for (var id in this.indexes) {
  80. index = this.indexes[id];
  81. if (oldIndex < index) {
  82. this.indexes[id] = index + 1;
  83. }
  84. }
  85. return this;
  86. };
  87. _proto.insertBefore = function insertBefore(oldNode, newNode) {
  88. newNode.parent = this;
  89. var oldIndex = this.index(oldNode);
  90. this.nodes.splice(oldIndex, 0, newNode);
  91. newNode.parent = this;
  92. var index;
  93. for (var id in this.indexes) {
  94. index = this.indexes[id];
  95. if (index >= oldIndex) {
  96. this.indexes[id] = index + 1;
  97. }
  98. }
  99. return this;
  100. };
  101. _proto._findChildAtPosition = function _findChildAtPosition(line, col) {
  102. var found = undefined;
  103. this.each(function (node) {
  104. if (node.atPosition) {
  105. var foundChild = node.atPosition(line, col);
  106. if (foundChild) {
  107. found = foundChild;
  108. return false;
  109. }
  110. } else if (node.isAtPosition(line, col)) {
  111. found = node;
  112. return false;
  113. }
  114. });
  115. return found;
  116. }
  117. /**
  118. * Return the most specific node at the line and column number given.
  119. * The source location is based on the original parsed location, locations aren't
  120. * updated as selector nodes are mutated.
  121. *
  122. * Note that this location is relative to the location of the first character
  123. * of the selector, and not the location of the selector in the overall document
  124. * when used in conjunction with postcss.
  125. *
  126. * If not found, returns undefined.
  127. * @param {number} line The line number of the node to find. (1-based index)
  128. * @param {number} col The column number of the node to find. (1-based index)
  129. */;
  130. _proto.atPosition = function atPosition(line, col) {
  131. if (this.isAtPosition(line, col)) {
  132. return this._findChildAtPosition(line, col) || this;
  133. } else {
  134. return undefined;
  135. }
  136. };
  137. _proto._inferEndPosition = function _inferEndPosition() {
  138. if (this.last && this.last.source && this.last.source.end) {
  139. this.source = this.source || {};
  140. this.source.end = this.source.end || {};
  141. Object.assign(this.source.end, this.last.source.end);
  142. }
  143. };
  144. _proto.each = function each(callback) {
  145. if (!this.lastEach) {
  146. this.lastEach = 0;
  147. }
  148. if (!this.indexes) {
  149. this.indexes = {};
  150. }
  151. this.lastEach++;
  152. var id = this.lastEach;
  153. this.indexes[id] = 0;
  154. if (!this.length) {
  155. return undefined;
  156. }
  157. var index, result;
  158. while (this.indexes[id] < this.length) {
  159. index = this.indexes[id];
  160. result = callback(this.at(index), index);
  161. if (result === false) {
  162. break;
  163. }
  164. this.indexes[id] += 1;
  165. }
  166. delete this.indexes[id];
  167. if (result === false) {
  168. return false;
  169. }
  170. };
  171. _proto.walk = function walk(callback) {
  172. return this.each(function (node, i) {
  173. var result = callback(node, i);
  174. if (result !== false && node.length) {
  175. result = node.walk(callback);
  176. }
  177. if (result === false) {
  178. return false;
  179. }
  180. });
  181. };
  182. _proto.walkAttributes = function walkAttributes(callback) {
  183. var _this2 = this;
  184. return this.walk(function (selector) {
  185. if (selector.type === types.ATTRIBUTE) {
  186. return callback.call(_this2, selector);
  187. }
  188. });
  189. };
  190. _proto.walkClasses = function walkClasses(callback) {
  191. var _this3 = this;
  192. return this.walk(function (selector) {
  193. if (selector.type === types.CLASS) {
  194. return callback.call(_this3, selector);
  195. }
  196. });
  197. };
  198. _proto.walkCombinators = function walkCombinators(callback) {
  199. var _this4 = this;
  200. return this.walk(function (selector) {
  201. if (selector.type === types.COMBINATOR) {
  202. return callback.call(_this4, selector);
  203. }
  204. });
  205. };
  206. _proto.walkComments = function walkComments(callback) {
  207. var _this5 = this;
  208. return this.walk(function (selector) {
  209. if (selector.type === types.COMMENT) {
  210. return callback.call(_this5, selector);
  211. }
  212. });
  213. };
  214. _proto.walkIds = function walkIds(callback) {
  215. var _this6 = this;
  216. return this.walk(function (selector) {
  217. if (selector.type === types.ID) {
  218. return callback.call(_this6, selector);
  219. }
  220. });
  221. };
  222. _proto.walkNesting = function walkNesting(callback) {
  223. var _this7 = this;
  224. return this.walk(function (selector) {
  225. if (selector.type === types.NESTING) {
  226. return callback.call(_this7, selector);
  227. }
  228. });
  229. };
  230. _proto.walkPseudos = function walkPseudos(callback) {
  231. var _this8 = this;
  232. return this.walk(function (selector) {
  233. if (selector.type === types.PSEUDO) {
  234. return callback.call(_this8, selector);
  235. }
  236. });
  237. };
  238. _proto.walkTags = function walkTags(callback) {
  239. var _this9 = this;
  240. return this.walk(function (selector) {
  241. if (selector.type === types.TAG) {
  242. return callback.call(_this9, selector);
  243. }
  244. });
  245. };
  246. _proto.walkUniversals = function walkUniversals(callback) {
  247. var _this10 = this;
  248. return this.walk(function (selector) {
  249. if (selector.type === types.UNIVERSAL) {
  250. return callback.call(_this10, selector);
  251. }
  252. });
  253. };
  254. _proto.split = function split(callback) {
  255. var _this11 = this;
  256. var current = [];
  257. return this.reduce(function (memo, node, index) {
  258. var split = callback.call(_this11, node);
  259. current.push(node);
  260. if (split) {
  261. memo.push(current);
  262. current = [];
  263. } else if (index === _this11.length - 1) {
  264. memo.push(current);
  265. }
  266. return memo;
  267. }, []);
  268. };
  269. _proto.map = function map(callback) {
  270. return this.nodes.map(callback);
  271. };
  272. _proto.reduce = function reduce(callback, memo) {
  273. return this.nodes.reduce(callback, memo);
  274. };
  275. _proto.every = function every(callback) {
  276. return this.nodes.every(callback);
  277. };
  278. _proto.some = function some(callback) {
  279. return this.nodes.some(callback);
  280. };
  281. _proto.filter = function filter(callback) {
  282. return this.nodes.filter(callback);
  283. };
  284. _proto.sort = function sort(callback) {
  285. return this.nodes.sort(callback);
  286. };
  287. _proto.toString = function toString() {
  288. return this.map(String).join('');
  289. };
  290. _createClass(Container, [{
  291. key: "first",
  292. get: function get() {
  293. return this.at(0);
  294. }
  295. }, {
  296. key: "last",
  297. get: function get() {
  298. return this.at(this.length - 1);
  299. }
  300. }, {
  301. key: "length",
  302. get: function get() {
  303. return this.nodes.length;
  304. }
  305. }]);
  306. return Container;
  307. }(_node["default"]);
  308. exports["default"] = Container;
  309. module.exports = exports.default;