container.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  18. if (k2 === undefined) k2 = k;
  19. var desc = Object.getOwnPropertyDescriptor(m, k);
  20. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  21. desc = { enumerable: true, get: function() { return m[k]; } };
  22. }
  23. Object.defineProperty(o, k2, desc);
  24. }) : (function(o, m, k, k2) {
  25. if (k2 === undefined) k2 = k;
  26. o[k2] = m[k];
  27. }));
  28. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  29. Object.defineProperty(o, "default", { enumerable: true, value: v });
  30. }) : function(o, v) {
  31. o["default"] = v;
  32. });
  33. var __importStar = (this && this.__importStar) || (function () {
  34. var ownKeys = function(o) {
  35. ownKeys = Object.getOwnPropertyNames || function (o) {
  36. var ar = [];
  37. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  38. return ar;
  39. };
  40. return ownKeys(o);
  41. };
  42. return function (mod) {
  43. if (mod && mod.__esModule) return mod;
  44. var result = {};
  45. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  46. __setModuleDefault(result, mod);
  47. return result;
  48. };
  49. })();
  50. var __values = (this && this.__values) || function(o) {
  51. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  52. if (m) return m.call(o);
  53. if (o && typeof o.length === "number") return {
  54. next: function () {
  55. if (o && i >= o.length) o = void 0;
  56. return { value: o && o[i++], done: !o };
  57. }
  58. };
  59. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  60. };
  61. var __read = (this && this.__read) || function (o, n) {
  62. var m = typeof Symbol === "function" && o[Symbol.iterator];
  63. if (!m) return o;
  64. var i = m.call(o), r, ar = [], e;
  65. try {
  66. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  67. }
  68. catch (error) { e = { error: error }; }
  69. finally {
  70. try {
  71. if (r && !r.done && (m = i["return"])) m.call(i);
  72. }
  73. finally { if (e) throw e.error; }
  74. }
  75. return ar;
  76. };
  77. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  78. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  79. if (ar || !(i in from)) {
  80. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  81. ar[i] = from[i];
  82. }
  83. }
  84. return to.concat(ar || Array.prototype.slice.call(from));
  85. };
  86. var __importDefault = (this && this.__importDefault) || function (mod) {
  87. return (mod && mod.__esModule) ? mod : { "default": mod };
  88. };
  89. Object.defineProperty(exports, "__esModule", { value: true });
  90. var util_1 = require("../util");
  91. var node_1 = __importDefault(require("./node"));
  92. var types = __importStar(require("./types"));
  93. var Container = /** @class */ (function (_super) {
  94. __extends(Container, _super);
  95. function Container(opts) {
  96. var _this = _super.call(this, opts) || this;
  97. if (!_this.nodes) {
  98. _this.nodes = [];
  99. }
  100. return _this;
  101. }
  102. Container.prototype.append = function (selector) {
  103. selector.parent = this;
  104. this.nodes.push(selector);
  105. return this;
  106. };
  107. Container.prototype.prepend = function (selector) {
  108. selector.parent = this;
  109. this.nodes.unshift(selector);
  110. for (var id in this.indexes) {
  111. this.indexes[id]++;
  112. }
  113. return this;
  114. };
  115. Container.prototype.at = function (index) {
  116. return this.nodes[index];
  117. };
  118. Container.prototype.index = function (child) {
  119. if (typeof child === "number") {
  120. return child;
  121. }
  122. return this.nodes.indexOf(child);
  123. };
  124. Object.defineProperty(Container.prototype, "first", {
  125. get: function () {
  126. return this.at(0);
  127. },
  128. enumerable: false,
  129. configurable: true
  130. });
  131. Object.defineProperty(Container.prototype, "last", {
  132. get: function () {
  133. return this.at(this.length - 1);
  134. },
  135. enumerable: false,
  136. configurable: true
  137. });
  138. Object.defineProperty(Container.prototype, "length", {
  139. get: function () {
  140. return this.nodes.length;
  141. },
  142. enumerable: false,
  143. configurable: true
  144. });
  145. Container.prototype.removeChild = function (child) {
  146. child = this.index(child);
  147. this.at(child).parent = undefined;
  148. this.nodes.splice(child, 1);
  149. var index;
  150. for (var id in this.indexes) {
  151. index = this.indexes[id];
  152. if (index >= child) {
  153. this.indexes[id] = index - 1;
  154. }
  155. }
  156. return this;
  157. };
  158. Container.prototype.removeAll = function () {
  159. var e_1, _a;
  160. try {
  161. for (var _b = __values(this.nodes), _c = _b.next(); !_c.done; _c = _b.next()) {
  162. var node = _c.value;
  163. node.parent = undefined;
  164. }
  165. }
  166. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  167. finally {
  168. try {
  169. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  170. }
  171. finally { if (e_1) throw e_1.error; }
  172. }
  173. this.nodes = [];
  174. return this;
  175. };
  176. Container.prototype.empty = function () {
  177. return this.removeAll();
  178. };
  179. Container.prototype.insertAfter = function (oldNode, newNode) {
  180. var _a;
  181. newNode.parent = this;
  182. var oldIndex = this.index(oldNode);
  183. var resetNode = [];
  184. for (var i = 2; i < arguments.length; i++) {
  185. resetNode.push(arguments[i]);
  186. }
  187. (_a = this.nodes).splice.apply(_a, __spreadArray([oldIndex + 1, 0, newNode], __read(resetNode), false));
  188. newNode.parent = this;
  189. var index;
  190. for (var id in this.indexes) {
  191. index = this.indexes[id];
  192. if (oldIndex < index) {
  193. this.indexes[id] = index + arguments.length - 1;
  194. }
  195. }
  196. return this;
  197. };
  198. Container.prototype.insertBefore = function (oldNode, newNode) {
  199. var _a;
  200. newNode.parent = this;
  201. var oldIndex = this.index(oldNode);
  202. var resetNode = [];
  203. for (var i = 2; i < arguments.length; i++) {
  204. resetNode.push(arguments[i]);
  205. }
  206. (_a = this.nodes).splice.apply(_a, __spreadArray([oldIndex, 0, newNode], __read(resetNode), false));
  207. newNode.parent = this;
  208. var index;
  209. for (var id in this.indexes) {
  210. index = this.indexes[id];
  211. if (index >= oldIndex) {
  212. this.indexes[id] = index + arguments.length - 1;
  213. }
  214. }
  215. return this;
  216. };
  217. Container.prototype._findChildAtPosition = function (line, col) {
  218. var found = undefined;
  219. this.each(function (node) {
  220. if (node.atPosition) {
  221. var foundChild = node.atPosition(line, col);
  222. if (foundChild) {
  223. found = foundChild;
  224. return false;
  225. }
  226. }
  227. else if (node.isAtPosition(line, col)) {
  228. found = node;
  229. return false;
  230. }
  231. });
  232. return found;
  233. };
  234. /**
  235. * Return the most specific node at the line and column number given.
  236. * The source location is based on the original parsed location, locations aren't
  237. * updated as selector nodes are mutated.
  238. *
  239. * Note that this location is relative to the location of the first character
  240. * of the selector, and not the location of the selector in the overall document
  241. * when used in conjunction with postcss.
  242. *
  243. * If not found, returns undefined.
  244. * @param {number} line The line number of the node to find. (1-based index)
  245. * @param {number} col The column number of the node to find. (1-based index)
  246. */
  247. Container.prototype.atPosition = function (line, col) {
  248. if (this.isAtPosition(line, col)) {
  249. return this._findChildAtPosition(line, col) || this;
  250. }
  251. else {
  252. return undefined;
  253. }
  254. };
  255. Container.prototype._inferEndPosition = function () {
  256. if (this.last && this.last.source && this.last.source.end) {
  257. this.source = this.source || {};
  258. this.source.end = this.source.end || {};
  259. Object.assign(this.source.end, this.last.source.end);
  260. }
  261. };
  262. Container.prototype.each = function (callback) {
  263. if (!this.lastEach) {
  264. this.lastEach = 0;
  265. }
  266. if (!this.indexes) {
  267. this.indexes = {};
  268. }
  269. this.lastEach++;
  270. var id = this.lastEach;
  271. this.indexes[id] = 0;
  272. if (!this.length) {
  273. return undefined;
  274. }
  275. var index, result;
  276. while (this.indexes[id] < this.length) {
  277. index = this.indexes[id];
  278. result = callback(this.at(index), index);
  279. if (result === false) {
  280. break;
  281. }
  282. this.indexes[id] += 1;
  283. }
  284. delete this.indexes[id];
  285. if (result === false) {
  286. return false;
  287. }
  288. };
  289. Container.prototype.walk = function (callback, depth) {
  290. if (depth === void 0) { depth = 0; }
  291. // Bound recursion so a pathologically deep node tree raises a catchable
  292. // error instead of overflowing the call stack (CVE-2026-9358 / CWE-674).
  293. if (depth > util_1.MAX_NESTING_DEPTH) {
  294. throw new Error("Cannot walk selector: nesting depth exceeds the maximum of ".concat(util_1.MAX_NESTING_DEPTH, "."));
  295. }
  296. return this.each(function (node, i) {
  297. var result = callback(node, i);
  298. if (result !== false && node.length) {
  299. result = node.walk(callback, depth + 1);
  300. }
  301. if (result === false) {
  302. return false;
  303. }
  304. });
  305. };
  306. Container.prototype.walkAttributes = function (callback) {
  307. var _this = this;
  308. return this.walk(function (selector) {
  309. if (selector.type === types.ATTRIBUTE) {
  310. return callback.call(_this, selector);
  311. }
  312. });
  313. };
  314. Container.prototype.walkClasses = function (callback) {
  315. var _this = this;
  316. return this.walk(function (selector) {
  317. if (selector.type === types.CLASS) {
  318. return callback.call(_this, selector);
  319. }
  320. });
  321. };
  322. Container.prototype.walkCombinators = function (callback) {
  323. var _this = this;
  324. return this.walk(function (selector) {
  325. if (selector.type === types.COMBINATOR) {
  326. return callback.call(_this, selector);
  327. }
  328. });
  329. };
  330. Container.prototype.walkComments = function (callback) {
  331. var _this = this;
  332. return this.walk(function (selector) {
  333. if (selector.type === types.COMMENT) {
  334. return callback.call(_this, selector);
  335. }
  336. });
  337. };
  338. Container.prototype.walkIds = function (callback) {
  339. var _this = this;
  340. return this.walk(function (selector) {
  341. if (selector.type === types.ID) {
  342. return callback.call(_this, selector);
  343. }
  344. });
  345. };
  346. Container.prototype.walkNesting = function (callback) {
  347. var _this = this;
  348. return this.walk(function (selector) {
  349. if (selector.type === types.NESTING) {
  350. return callback.call(_this, selector);
  351. }
  352. });
  353. };
  354. Container.prototype.walkPseudos = function (callback) {
  355. var _this = this;
  356. return this.walk(function (selector) {
  357. if (selector.type === types.PSEUDO) {
  358. return callback.call(_this, selector);
  359. }
  360. });
  361. };
  362. Container.prototype.walkTags = function (callback) {
  363. var _this = this;
  364. return this.walk(function (selector) {
  365. if (selector.type === types.TAG) {
  366. return callback.call(_this, selector);
  367. }
  368. });
  369. };
  370. Container.prototype.walkUniversals = function (callback) {
  371. var _this = this;
  372. return this.walk(function (selector) {
  373. if (selector.type === types.UNIVERSAL) {
  374. return callback.call(_this, selector);
  375. }
  376. });
  377. };
  378. Container.prototype.split = function (callback) {
  379. var _this = this;
  380. var current = [];
  381. return this.reduce(function (memo, node, index) {
  382. var split = callback.call(_this, node);
  383. current.push(node);
  384. if (split) {
  385. memo.push(current);
  386. current = [];
  387. }
  388. else if (index === _this.length - 1) {
  389. memo.push(current);
  390. }
  391. return memo;
  392. }, []);
  393. };
  394. Container.prototype.map = function (callback) {
  395. return this.nodes.map(callback);
  396. };
  397. Container.prototype.reduce = function (callback, memo) {
  398. return this.nodes.reduce(callback, memo);
  399. };
  400. Container.prototype.every = function (callback) {
  401. return this.nodes.every(callback);
  402. };
  403. Container.prototype.some = function (callback) {
  404. return this.nodes.some(callback);
  405. };
  406. Container.prototype.filter = function (callback) {
  407. return this.nodes.filter(callback);
  408. };
  409. Container.prototype.sort = function (callback) {
  410. return this.nodes.sort(callback);
  411. };
  412. Container.prototype.toString = function (options) {
  413. if (options === void 0) { options = {}; }
  414. return this._stringify(options, 0, (0, util_1.resolveMaxNestingDepth)(options.maxNestingDepth));
  415. };
  416. Container.prototype._stringify = function (options, depth, max) {
  417. var _this = this;
  418. return this.map(function (child) { return _this._stringifyChild(child, options, depth, max); }).join("");
  419. };
  420. // Serialize a child node. Historically `toString` used `this.map(String)`,
  421. // which leniently coerced anything — including raw arrays inserted via
  422. // `replaceWith(array)` / `insertBefore` / `insertAfter` (e.g. Tailwind's
  423. // `:merge()` expansion). Fall back to `String(child)` for values that are not
  424. // parser nodes so that behaviour is preserved.
  425. Container.prototype._stringifyChild = function (child, options, depth, max) {
  426. return typeof child._stringify === "function"
  427. ? child._stringify(options, depth, max)
  428. : String(child);
  429. };
  430. return Container;
  431. }(node_1.default));
  432. exports.default = Container;
  433. //# sourceMappingURL=container.js.map