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