container.js 12 KB

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