1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports["default"] = void 0;
|
---|
5 |
|
---|
6 | var _node = _interopRequireDefault(require("./node"));
|
---|
7 |
|
---|
8 | var types = _interopRequireWildcard(require("./types"));
|
---|
9 |
|
---|
10 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
|
---|
11 |
|
---|
12 | 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; }
|
---|
13 |
|
---|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
---|
15 |
|
---|
16 | 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); }
|
---|
17 |
|
---|
18 | 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); }
|
---|
19 |
|
---|
20 | 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; }
|
---|
21 |
|
---|
22 | 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); } }
|
---|
23 |
|
---|
24 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
---|
25 |
|
---|
26 | function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
|
---|
27 |
|
---|
28 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
---|
29 |
|
---|
30 | var Container = /*#__PURE__*/function (_Node) {
|
---|
31 | _inheritsLoose(Container, _Node);
|
---|
32 |
|
---|
33 | function Container(opts) {
|
---|
34 | var _this;
|
---|
35 |
|
---|
36 | _this = _Node.call(this, opts) || this;
|
---|
37 |
|
---|
38 | if (!_this.nodes) {
|
---|
39 | _this.nodes = [];
|
---|
40 | }
|
---|
41 |
|
---|
42 | return _this;
|
---|
43 | }
|
---|
44 |
|
---|
45 | var _proto = Container.prototype;
|
---|
46 |
|
---|
47 | _proto.append = function append(selector) {
|
---|
48 | selector.parent = this;
|
---|
49 | this.nodes.push(selector);
|
---|
50 | return this;
|
---|
51 | };
|
---|
52 |
|
---|
53 | _proto.prepend = function prepend(selector) {
|
---|
54 | selector.parent = this;
|
---|
55 | this.nodes.unshift(selector);
|
---|
56 | return this;
|
---|
57 | };
|
---|
58 |
|
---|
59 | _proto.at = function at(index) {
|
---|
60 | return this.nodes[index];
|
---|
61 | };
|
---|
62 |
|
---|
63 | _proto.index = function index(child) {
|
---|
64 | if (typeof child === 'number') {
|
---|
65 | return child;
|
---|
66 | }
|
---|
67 |
|
---|
68 | return this.nodes.indexOf(child);
|
---|
69 | };
|
---|
70 |
|
---|
71 | _proto.removeChild = function removeChild(child) {
|
---|
72 | child = this.index(child);
|
---|
73 | this.at(child).parent = undefined;
|
---|
74 | this.nodes.splice(child, 1);
|
---|
75 | var index;
|
---|
76 |
|
---|
77 | for (var id in this.indexes) {
|
---|
78 | index = this.indexes[id];
|
---|
79 |
|
---|
80 | if (index >= child) {
|
---|
81 | this.indexes[id] = index - 1;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | return this;
|
---|
86 | };
|
---|
87 |
|
---|
88 | _proto.removeAll = function removeAll() {
|
---|
89 | for (var _iterator = _createForOfIteratorHelperLoose(this.nodes), _step; !(_step = _iterator()).done;) {
|
---|
90 | var node = _step.value;
|
---|
91 | node.parent = undefined;
|
---|
92 | }
|
---|
93 |
|
---|
94 | this.nodes = [];
|
---|
95 | return this;
|
---|
96 | };
|
---|
97 |
|
---|
98 | _proto.empty = function empty() {
|
---|
99 | return this.removeAll();
|
---|
100 | };
|
---|
101 |
|
---|
102 | _proto.insertAfter = function insertAfter(oldNode, newNode) {
|
---|
103 | newNode.parent = this;
|
---|
104 | var oldIndex = this.index(oldNode);
|
---|
105 | this.nodes.splice(oldIndex + 1, 0, newNode);
|
---|
106 | newNode.parent = this;
|
---|
107 | var index;
|
---|
108 |
|
---|
109 | for (var id in this.indexes) {
|
---|
110 | index = this.indexes[id];
|
---|
111 |
|
---|
112 | if (oldIndex <= index) {
|
---|
113 | this.indexes[id] = index + 1;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | return this;
|
---|
118 | };
|
---|
119 |
|
---|
120 | _proto.insertBefore = function insertBefore(oldNode, newNode) {
|
---|
121 | newNode.parent = this;
|
---|
122 | var oldIndex = this.index(oldNode);
|
---|
123 | this.nodes.splice(oldIndex, 0, newNode);
|
---|
124 | newNode.parent = this;
|
---|
125 | var index;
|
---|
126 |
|
---|
127 | for (var id in this.indexes) {
|
---|
128 | index = this.indexes[id];
|
---|
129 |
|
---|
130 | if (index <= oldIndex) {
|
---|
131 | this.indexes[id] = index + 1;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | return this;
|
---|
136 | };
|
---|
137 |
|
---|
138 | _proto._findChildAtPosition = function _findChildAtPosition(line, col) {
|
---|
139 | var found = undefined;
|
---|
140 | this.each(function (node) {
|
---|
141 | if (node.atPosition) {
|
---|
142 | var foundChild = node.atPosition(line, col);
|
---|
143 |
|
---|
144 | if (foundChild) {
|
---|
145 | found = foundChild;
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 | } else if (node.isAtPosition(line, col)) {
|
---|
149 | found = node;
|
---|
150 | return false;
|
---|
151 | }
|
---|
152 | });
|
---|
153 | return found;
|
---|
154 | }
|
---|
155 | /**
|
---|
156 | * Return the most specific node at the line and column number given.
|
---|
157 | * The source location is based on the original parsed location, locations aren't
|
---|
158 | * updated as selector nodes are mutated.
|
---|
159 | *
|
---|
160 | * Note that this location is relative to the location of the first character
|
---|
161 | * of the selector, and not the location of the selector in the overall document
|
---|
162 | * when used in conjunction with postcss.
|
---|
163 | *
|
---|
164 | * If not found, returns undefined.
|
---|
165 | * @param {number} line The line number of the node to find. (1-based index)
|
---|
166 | * @param {number} col The column number of the node to find. (1-based index)
|
---|
167 | */
|
---|
168 | ;
|
---|
169 |
|
---|
170 | _proto.atPosition = function atPosition(line, col) {
|
---|
171 | if (this.isAtPosition(line, col)) {
|
---|
172 | return this._findChildAtPosition(line, col) || this;
|
---|
173 | } else {
|
---|
174 | return undefined;
|
---|
175 | }
|
---|
176 | };
|
---|
177 |
|
---|
178 | _proto._inferEndPosition = function _inferEndPosition() {
|
---|
179 | if (this.last && this.last.source && this.last.source.end) {
|
---|
180 | this.source = this.source || {};
|
---|
181 | this.source.end = this.source.end || {};
|
---|
182 | Object.assign(this.source.end, this.last.source.end);
|
---|
183 | }
|
---|
184 | };
|
---|
185 |
|
---|
186 | _proto.each = function each(callback) {
|
---|
187 | if (!this.lastEach) {
|
---|
188 | this.lastEach = 0;
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (!this.indexes) {
|
---|
192 | this.indexes = {};
|
---|
193 | }
|
---|
194 |
|
---|
195 | this.lastEach++;
|
---|
196 | var id = this.lastEach;
|
---|
197 | this.indexes[id] = 0;
|
---|
198 |
|
---|
199 | if (!this.length) {
|
---|
200 | return undefined;
|
---|
201 | }
|
---|
202 |
|
---|
203 | var index, result;
|
---|
204 |
|
---|
205 | while (this.indexes[id] < this.length) {
|
---|
206 | index = this.indexes[id];
|
---|
207 | result = callback(this.at(index), index);
|
---|
208 |
|
---|
209 | if (result === false) {
|
---|
210 | break;
|
---|
211 | }
|
---|
212 |
|
---|
213 | this.indexes[id] += 1;
|
---|
214 | }
|
---|
215 |
|
---|
216 | delete this.indexes[id];
|
---|
217 |
|
---|
218 | if (result === false) {
|
---|
219 | return false;
|
---|
220 | }
|
---|
221 | };
|
---|
222 |
|
---|
223 | _proto.walk = function walk(callback) {
|
---|
224 | return this.each(function (node, i) {
|
---|
225 | var result = callback(node, i);
|
---|
226 |
|
---|
227 | if (result !== false && node.length) {
|
---|
228 | result = node.walk(callback);
|
---|
229 | }
|
---|
230 |
|
---|
231 | if (result === false) {
|
---|
232 | return false;
|
---|
233 | }
|
---|
234 | });
|
---|
235 | };
|
---|
236 |
|
---|
237 | _proto.walkAttributes = function walkAttributes(callback) {
|
---|
238 | var _this2 = this;
|
---|
239 |
|
---|
240 | return this.walk(function (selector) {
|
---|
241 | if (selector.type === types.ATTRIBUTE) {
|
---|
242 | return callback.call(_this2, selector);
|
---|
243 | }
|
---|
244 | });
|
---|
245 | };
|
---|
246 |
|
---|
247 | _proto.walkClasses = function walkClasses(callback) {
|
---|
248 | var _this3 = this;
|
---|
249 |
|
---|
250 | return this.walk(function (selector) {
|
---|
251 | if (selector.type === types.CLASS) {
|
---|
252 | return callback.call(_this3, selector);
|
---|
253 | }
|
---|
254 | });
|
---|
255 | };
|
---|
256 |
|
---|
257 | _proto.walkCombinators = function walkCombinators(callback) {
|
---|
258 | var _this4 = this;
|
---|
259 |
|
---|
260 | return this.walk(function (selector) {
|
---|
261 | if (selector.type === types.COMBINATOR) {
|
---|
262 | return callback.call(_this4, selector);
|
---|
263 | }
|
---|
264 | });
|
---|
265 | };
|
---|
266 |
|
---|
267 | _proto.walkComments = function walkComments(callback) {
|
---|
268 | var _this5 = this;
|
---|
269 |
|
---|
270 | return this.walk(function (selector) {
|
---|
271 | if (selector.type === types.COMMENT) {
|
---|
272 | return callback.call(_this5, selector);
|
---|
273 | }
|
---|
274 | });
|
---|
275 | };
|
---|
276 |
|
---|
277 | _proto.walkIds = function walkIds(callback) {
|
---|
278 | var _this6 = this;
|
---|
279 |
|
---|
280 | return this.walk(function (selector) {
|
---|
281 | if (selector.type === types.ID) {
|
---|
282 | return callback.call(_this6, selector);
|
---|
283 | }
|
---|
284 | });
|
---|
285 | };
|
---|
286 |
|
---|
287 | _proto.walkNesting = function walkNesting(callback) {
|
---|
288 | var _this7 = this;
|
---|
289 |
|
---|
290 | return this.walk(function (selector) {
|
---|
291 | if (selector.type === types.NESTING) {
|
---|
292 | return callback.call(_this7, selector);
|
---|
293 | }
|
---|
294 | });
|
---|
295 | };
|
---|
296 |
|
---|
297 | _proto.walkPseudos = function walkPseudos(callback) {
|
---|
298 | var _this8 = this;
|
---|
299 |
|
---|
300 | return this.walk(function (selector) {
|
---|
301 | if (selector.type === types.PSEUDO) {
|
---|
302 | return callback.call(_this8, selector);
|
---|
303 | }
|
---|
304 | });
|
---|
305 | };
|
---|
306 |
|
---|
307 | _proto.walkTags = function walkTags(callback) {
|
---|
308 | var _this9 = this;
|
---|
309 |
|
---|
310 | return this.walk(function (selector) {
|
---|
311 | if (selector.type === types.TAG) {
|
---|
312 | return callback.call(_this9, selector);
|
---|
313 | }
|
---|
314 | });
|
---|
315 | };
|
---|
316 |
|
---|
317 | _proto.walkUniversals = function walkUniversals(callback) {
|
---|
318 | var _this10 = this;
|
---|
319 |
|
---|
320 | return this.walk(function (selector) {
|
---|
321 | if (selector.type === types.UNIVERSAL) {
|
---|
322 | return callback.call(_this10, selector);
|
---|
323 | }
|
---|
324 | });
|
---|
325 | };
|
---|
326 |
|
---|
327 | _proto.split = function split(callback) {
|
---|
328 | var _this11 = this;
|
---|
329 |
|
---|
330 | var current = [];
|
---|
331 | return this.reduce(function (memo, node, index) {
|
---|
332 | var split = callback.call(_this11, node);
|
---|
333 | current.push(node);
|
---|
334 |
|
---|
335 | if (split) {
|
---|
336 | memo.push(current);
|
---|
337 | current = [];
|
---|
338 | } else if (index === _this11.length - 1) {
|
---|
339 | memo.push(current);
|
---|
340 | }
|
---|
341 |
|
---|
342 | return memo;
|
---|
343 | }, []);
|
---|
344 | };
|
---|
345 |
|
---|
346 | _proto.map = function map(callback) {
|
---|
347 | return this.nodes.map(callback);
|
---|
348 | };
|
---|
349 |
|
---|
350 | _proto.reduce = function reduce(callback, memo) {
|
---|
351 | return this.nodes.reduce(callback, memo);
|
---|
352 | };
|
---|
353 |
|
---|
354 | _proto.every = function every(callback) {
|
---|
355 | return this.nodes.every(callback);
|
---|
356 | };
|
---|
357 |
|
---|
358 | _proto.some = function some(callback) {
|
---|
359 | return this.nodes.some(callback);
|
---|
360 | };
|
---|
361 |
|
---|
362 | _proto.filter = function filter(callback) {
|
---|
363 | return this.nodes.filter(callback);
|
---|
364 | };
|
---|
365 |
|
---|
366 | _proto.sort = function sort(callback) {
|
---|
367 | return this.nodes.sort(callback);
|
---|
368 | };
|
---|
369 |
|
---|
370 | _proto.toString = function toString() {
|
---|
371 | return this.map(String).join('');
|
---|
372 | };
|
---|
373 |
|
---|
374 | _createClass(Container, [{
|
---|
375 | key: "first",
|
---|
376 | get: function get() {
|
---|
377 | return this.at(0);
|
---|
378 | }
|
---|
379 | }, {
|
---|
380 | key: "last",
|
---|
381 | get: function get() {
|
---|
382 | return this.at(this.length - 1);
|
---|
383 | }
|
---|
384 | }, {
|
---|
385 | key: "length",
|
---|
386 | get: function get() {
|
---|
387 | return this.nodes.length;
|
---|
388 | }
|
---|
389 | }]);
|
---|
390 |
|
---|
391 | return Container;
|
---|
392 | }(_node["default"]);
|
---|
393 |
|
---|
394 | exports["default"] = Container;
|
---|
395 | module.exports = exports.default; |
---|