3 exports.__esModule = true;
5 var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
7 var _declaration = require('./declaration');
9 var _declaration2 = _interopRequireDefault(_declaration);
11 var _warnOnce = require('./warn-once');
13 var _warnOnce2 = _interopRequireDefault(_warnOnce);
15 var _comment = require('./comment');
17 var _comment2 = _interopRequireDefault(_comment);
19 var _node = require('./node');
21 var _node2 = _interopRequireDefault(_node);
23 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
27 function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
29 function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
31 function cleanSource(nodes) {
32 return nodes.map(function (i) {
33 if (i.nodes) i.nodes = cleanSource(i.nodes);
39 var Container = function (_Node) {
40 _inherits(Container, _Node);
42 function Container() {
43 _classCallCheck(this, Container);
45 return _possibleConstructorReturn(this, _Node.apply(this, arguments));
48 Container.prototype.push = function push(child) {
50 this.nodes.push(child);
54 Container.prototype.each = function each(callback) {
55 if (!this.lastEach) this.lastEach = 0;
56 if (!this.indexes) this.indexes = {};
59 var id = this.lastEach;
62 if (!this.nodes) return undefined;
66 while (this.indexes[id] < this.nodes.length) {
67 index = this.indexes[id];
68 result = callback(this.nodes[index], index);
69 if (result === false) break;
71 this.indexes[id] += 1;
74 delete this.indexes[id];
79 Container.prototype.walk = function walk(callback) {
80 return this.each(function (child, i) {
81 var result = callback(child, i);
82 if (result !== false && child.walk) {
83 result = child.walk(callback);
89 Container.prototype.walkDecls = function walkDecls(prop, callback) {
92 return this.walk(function (child, i) {
93 if (child.type === 'decl') {
94 return callback(child, i);
97 } else if (prop instanceof RegExp) {
98 return this.walk(function (child, i) {
99 if (child.type === 'decl' && prop.test(child.prop)) {
100 return callback(child, i);
104 return this.walk(function (child, i) {
105 if (child.type === 'decl' && child.prop === prop) {
106 return callback(child, i);
112 Container.prototype.walkRules = function walkRules(selector, callback) {
116 return this.walk(function (child, i) {
117 if (child.type === 'rule') {
118 return callback(child, i);
121 } else if (selector instanceof RegExp) {
122 return this.walk(function (child, i) {
123 if (child.type === 'rule' && selector.test(child.selector)) {
124 return callback(child, i);
128 return this.walk(function (child, i) {
129 if (child.type === 'rule' && child.selector === selector) {
130 return callback(child, i);
136 Container.prototype.walkAtRules = function walkAtRules(name, callback) {
139 return this.walk(function (child, i) {
140 if (child.type === 'atrule') {
141 return callback(child, i);
144 } else if (name instanceof RegExp) {
145 return this.walk(function (child, i) {
146 if (child.type === 'atrule' && name.test(child.name)) {
147 return callback(child, i);
151 return this.walk(function (child, i) {
152 if (child.type === 'atrule' && child.name === name) {
153 return callback(child, i);
159 Container.prototype.walkComments = function walkComments(callback) {
160 return this.walk(function (child, i) {
161 if (child.type === 'comment') {
162 return callback(child, i);
167 Container.prototype.append = function append() {
168 for (var _len = arguments.length, children = Array(_len), _key = 0; _key < _len; _key++) {
169 children[_key] = arguments[_key];
172 for (var _iterator = children, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
176 if (_i >= _iterator.length) break;
177 _ref = _iterator[_i++];
179 _i = _iterator.next();
186 var nodes = this.normalize(child, this.last);
187 for (var _iterator2 = nodes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
191 if (_i2 >= _iterator2.length) break;
192 _ref2 = _iterator2[_i2++];
194 _i2 = _iterator2.next();
200 this.nodes.push(node);
206 Container.prototype.prepend = function prepend() {
207 for (var _len2 = arguments.length, children = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
208 children[_key2] = arguments[_key2];
211 children = children.reverse();
212 for (var _iterator3 = children, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
216 if (_i3 >= _iterator3.length) break;
217 _ref3 = _iterator3[_i3++];
219 _i3 = _iterator3.next();
226 var nodes = this.normalize(child, this.first, 'prepend').reverse();
227 for (var _iterator4 = nodes, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
231 if (_i4 >= _iterator4.length) break;
232 _ref4 = _iterator4[_i4++];
234 _i4 = _iterator4.next();
240 this.nodes.unshift(node);
241 }for (var id in this.indexes) {
242 this.indexes[id] = this.indexes[id] + nodes.length;
248 Container.prototype.cleanRaws = function cleanRaws(keepBetween) {
249 _Node.prototype.cleanRaws.call(this, keepBetween);
251 for (var _iterator5 = this.nodes, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
255 if (_i5 >= _iterator5.length) break;
256 _ref5 = _iterator5[_i5++];
258 _i5 = _iterator5.next();
264 node.cleanRaws(keepBetween);
269 Container.prototype.insertBefore = function insertBefore(exist, add) {
270 exist = this.index(exist);
272 var type = exist === 0 ? 'prepend' : false;
273 var nodes = this.normalize(add, this.nodes[exist], type).reverse();
274 for (var _iterator6 = nodes, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
278 if (_i6 >= _iterator6.length) break;
279 _ref6 = _iterator6[_i6++];
281 _i6 = _iterator6.next();
287 this.nodes.splice(exist, 0, node);
289 for (var id in this.indexes) {
290 index = this.indexes[id];
291 if (exist <= index) {
292 this.indexes[id] = index + nodes.length;
299 Container.prototype.insertAfter = function insertAfter(exist, add) {
300 exist = this.index(exist);
302 var nodes = this.normalize(add, this.nodes[exist]).reverse();
303 for (var _iterator7 = nodes, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : _iterator7[Symbol.iterator]();;) {
307 if (_i7 >= _iterator7.length) break;
308 _ref7 = _iterator7[_i7++];
310 _i7 = _iterator7.next();
316 this.nodes.splice(exist + 1, 0, node);
318 for (var id in this.indexes) {
319 index = this.indexes[id];
321 this.indexes[id] = index + nodes.length;
328 Container.prototype.remove = function remove(child) {
329 /* istanbul ignore if */
330 if (typeof child !== 'undefined') {
331 (0, _warnOnce2.default)('Container#remove is deprecated. ' + 'Use Container#removeChild');
332 this.removeChild(child);
334 _Node.prototype.remove.call(this);
339 Container.prototype.removeChild = function removeChild(child) {
340 child = this.index(child);
341 this.nodes[child].parent = undefined;
342 this.nodes.splice(child, 1);
345 for (var id in this.indexes) {
346 index = this.indexes[id];
347 if (index >= child) {
348 this.indexes[id] = index - 1;
355 Container.prototype.removeAll = function removeAll() {
356 for (var _iterator8 = this.nodes, _isArray8 = Array.isArray(_iterator8), _i8 = 0, _iterator8 = _isArray8 ? _iterator8 : _iterator8[Symbol.iterator]();;) {
360 if (_i8 >= _iterator8.length) break;
361 _ref8 = _iterator8[_i8++];
363 _i8 = _iterator8.next();
369 node.parent = undefined;
374 Container.prototype.replaceValues = function replaceValues(regexp, opts, callback) {
380 this.walkDecls(function (decl) {
381 if (opts.props && opts.props.indexOf(decl.prop) === -1) return;
382 if (opts.fast && decl.value.indexOf(opts.fast) === -1) return;
384 decl.value = decl.value.replace(regexp, callback);
390 Container.prototype.every = function every(condition) {
391 return this.nodes.every(condition);
394 Container.prototype.some = function some(condition) {
395 return this.nodes.some(condition);
398 Container.prototype.index = function index(child) {
399 if (typeof child === 'number') {
402 return this.nodes.indexOf(child);
406 Container.prototype.normalize = function normalize(nodes, sample) {
409 if (typeof nodes === 'string') {
410 var parse = require('./parse');
411 nodes = cleanSource(parse(nodes).nodes);
412 } else if (!Array.isArray(nodes)) {
413 if (nodes.type === 'root') {
415 } else if (nodes.type) {
417 } else if (nodes.prop) {
418 if (typeof nodes.value === 'undefined') {
419 throw new Error('Value field is missed in node creation');
421 nodes = [new _declaration2.default(nodes)];
422 } else if (nodes.selector) {
423 var Rule = require('./rule');
424 nodes = [new Rule(nodes)];
425 } else if (nodes.name) {
426 var AtRule = require('./at-rule');
427 nodes = [new AtRule(nodes)];
428 } else if (nodes.text) {
429 nodes = [new _comment2.default(nodes)];
431 throw new Error('Unknown node type in node creation');
435 var processed = nodes.map(function (i) {
436 /* istanbul ignore if */
437 if (typeof i.raws === 'undefined') i = _this2.rebuild(i);
439 if (i.parent) i = i.clone();
440 if (typeof i.raws.before === 'undefined') {
441 if (sample && typeof sample.raws.before !== 'undefined') {
442 i.raws.before = sample.raws.before.replace(/[^\s]/g, '');
452 /* istanbul ignore next */
455 Container.prototype.rebuild = function rebuild(node, parent) {
459 if (node.type === 'root') {
460 var Root = require('./root');
462 } else if (node.type === 'atrule') {
463 var AtRule = require('./at-rule');
465 } else if (node.type === 'rule') {
466 var Rule = require('./rule');
468 } else if (node.type === 'decl') {
469 fix = new _declaration2.default();
470 } else if (node.type === 'comment') {
471 fix = new _comment2.default();
474 for (var i in node) {
476 fix.nodes = node.nodes.map(function (j) {
477 return _this3.rebuild(j, fix);
479 } else if (i === 'parent' && parent) {
481 } else if (node.hasOwnProperty(i)) {
489 /* istanbul ignore next */
492 Container.prototype.eachInside = function eachInside(callback) {
493 (0, _warnOnce2.default)('Container#eachInside is deprecated. ' + 'Use Container#walk instead.');
494 return this.walk(callback);
497 /* istanbul ignore next */
500 Container.prototype.eachDecl = function eachDecl(prop, callback) {
501 (0, _warnOnce2.default)('Container#eachDecl is deprecated. ' + 'Use Container#walkDecls instead.');
502 return this.walkDecls(prop, callback);
505 /* istanbul ignore next */
508 Container.prototype.eachRule = function eachRule(selector, callback) {
509 (0, _warnOnce2.default)('Container#eachRule is deprecated. ' + 'Use Container#walkRules instead.');
510 return this.walkRules(selector, callback);
513 /* istanbul ignore next */
516 Container.prototype.eachAtRule = function eachAtRule(name, callback) {
517 (0, _warnOnce2.default)('Container#eachAtRule is deprecated. ' + 'Use Container#walkAtRules instead.');
518 return this.walkAtRules(name, callback);
521 /* istanbul ignore next */
524 Container.prototype.eachComment = function eachComment(callback) {
525 (0, _warnOnce2.default)('Container#eachComment is deprecated. ' + 'Use Container#walkComments instead.');
526 return this.walkComments(callback);
529 /* istanbul ignore next */
532 _createClass(Container, [{
534 get: function get() {
535 if (!this.nodes) return undefined;
536 return this.nodes[0];
540 get: function get() {
541 if (!this.nodes) return undefined;
542 return this.nodes[this.nodes.length - 1];
546 get: function get() {
547 (0, _warnOnce2.default)('Node#semicolon is deprecated. Use Node#raws.semicolon');
548 return this.raws.semicolon;
551 /* istanbul ignore next */
553 set: function set(val) {
554 (0, _warnOnce2.default)('Node#semicolon is deprecated. Use Node#raws.semicolon');
555 this.raws.semicolon = val;
558 /* istanbul ignore next */
562 get: function get() {
563 (0, _warnOnce2.default)('Node#after is deprecated. Use Node#raws.after');
564 return this.raws.after;
567 /* istanbul ignore next */
569 set: function set(val) {
570 (0, _warnOnce2.default)('Node#after is deprecated. Use Node#raws.after');
571 this.raws.after = val;
578 exports.default = Container;
579 module.exports = exports['default'];