container.js 12 KB

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