You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

394 lines
11 KiB

2 years ago
  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() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
  7. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { 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."); } it = o[Symbol.iterator](); return it.next.bind(it); }
  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); 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 || 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. ;
  128. _proto.atPosition = function atPosition(line, col) {
  129. if (this.isAtPosition(line, col)) {
  130. return this._findChildAtPosition(line, col) || this;
  131. } else {
  132. return undefined;
  133. }
  134. };
  135. _proto._inferEndPosition = function _inferEndPosition() {
  136. if (this.last && this.last.source && this.last.source.end) {
  137. this.source = this.source || {};
  138. this.source.end = this.source.end || {};
  139. Object.assign(this.source.end, this.last.source.end);
  140. }
  141. };
  142. _proto.each = function each(callback) {
  143. if (!this.lastEach) {
  144. this.lastEach = 0;
  145. }
  146. if (!this.indexes) {
  147. this.indexes = {};
  148. }
  149. this.lastEach++;
  150. var id = this.lastEach;
  151. this.indexes[id] = 0;
  152. if (!this.length) {
  153. return undefined;
  154. }
  155. var index, result;
  156. while (this.indexes[id] < this.length) {
  157. index = this.indexes[id];
  158. result = callback(this.at(index), index);
  159. if (result === false) {
  160. break;
  161. }
  162. this.indexes[id] += 1;
  163. }
  164. delete this.indexes[id];
  165. if (result === false) {
  166. return false;
  167. }
  168. };
  169. _proto.walk = function walk(callback) {
  170. return this.each(function (node, i) {
  171. var result = callback(node, i);
  172. if (result !== false && node.length) {
  173. result = node.walk(callback);
  174. }
  175. if (result === false) {
  176. return false;
  177. }
  178. });
  179. };
  180. _proto.walkAttributes = function walkAttributes(callback) {
  181. var _this2 = this;
  182. return this.walk(function (selector) {
  183. if (selector.type === types.ATTRIBUTE) {
  184. return callback.call(_this2, selector);
  185. }
  186. });
  187. };
  188. _proto.walkClasses = function walkClasses(callback) {
  189. var _this3 = this;
  190. return this.walk(function (selector) {
  191. if (selector.type === types.CLASS) {
  192. return callback.call(_this3, selector);
  193. }
  194. });
  195. };
  196. _proto.walkCombinators = function walkCombinators(callback) {
  197. var _this4 = this;
  198. return this.walk(function (selector) {
  199. if (selector.type === types.COMBINATOR) {
  200. return callback.call(_this4, selector);
  201. }
  202. });
  203. };
  204. _proto.walkComments = function walkComments(callback) {
  205. var _this5 = this;
  206. return this.walk(function (selector) {
  207. if (selector.type === types.COMMENT) {
  208. return callback.call(_this5, selector);
  209. }
  210. });
  211. };
  212. _proto.walkIds = function walkIds(callback) {
  213. var _this6 = this;
  214. return this.walk(function (selector) {
  215. if (selector.type === types.ID) {
  216. return callback.call(_this6, selector);
  217. }
  218. });
  219. };
  220. _proto.walkNesting = function walkNesting(callback) {
  221. var _this7 = this;
  222. return this.walk(function (selector) {
  223. if (selector.type === types.NESTING) {
  224. return callback.call(_this7, selector);
  225. }
  226. });
  227. };
  228. _proto.walkPseudos = function walkPseudos(callback) {
  229. var _this8 = this;
  230. return this.walk(function (selector) {
  231. if (selector.type === types.PSEUDO) {
  232. return callback.call(_this8, selector);
  233. }
  234. });
  235. };
  236. _proto.walkTags = function walkTags(callback) {
  237. var _this9 = this;
  238. return this.walk(function (selector) {
  239. if (selector.type === types.TAG) {
  240. return callback.call(_this9, selector);
  241. }
  242. });
  243. };
  244. _proto.walkUniversals = function walkUniversals(callback) {
  245. var _this10 = this;
  246. return this.walk(function (selector) {
  247. if (selector.type === types.UNIVERSAL) {
  248. return callback.call(_this10, selector);
  249. }
  250. });
  251. };
  252. _proto.split = function split(callback) {
  253. var _this11 = this;
  254. var current = [];
  255. return this.reduce(function (memo, node, index) {
  256. var split = callback.call(_this11, node);
  257. current.push(node);
  258. if (split) {
  259. memo.push(current);
  260. current = [];
  261. } else if (index === _this11.length - 1) {
  262. memo.push(current);
  263. }
  264. return memo;
  265. }, []);
  266. };
  267. _proto.map = function map(callback) {
  268. return this.nodes.map(callback);
  269. };
  270. _proto.reduce = function reduce(callback, memo) {
  271. return this.nodes.reduce(callback, memo);
  272. };
  273. _proto.every = function every(callback) {
  274. return this.nodes.every(callback);
  275. };
  276. _proto.some = function some(callback) {
  277. return this.nodes.some(callback);
  278. };
  279. _proto.filter = function filter(callback) {
  280. return this.nodes.filter(callback);
  281. };
  282. _proto.sort = function sort(callback) {
  283. return this.nodes.sort(callback);
  284. };
  285. _proto.toString = function toString() {
  286. return this.map(String).join('');
  287. };
  288. _createClass(Container, [{
  289. key: "first",
  290. get: function get() {
  291. return this.at(0);
  292. }
  293. }, {
  294. key: "last",
  295. get: function get() {
  296. return this.at(this.length - 1);
  297. }
  298. }, {
  299. key: "length",
  300. get: function get() {
  301. return this.nodes.length;
  302. }
  303. }]);
  304. return Container;
  305. }(_node["default"]);
  306. exports["default"] = Container;
  307. module.exports = exports.default;