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