Security update for Core, with self-updated composer
[yaffs-website] / node_modules / uncss / node_modules / postcss / lib / container.js
1 'use strict';
2
3 exports.__esModule = true;
4
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; }; }();
6
7 var _declaration = require('./declaration');
8
9 var _declaration2 = _interopRequireDefault(_declaration);
10
11 var _warnOnce = require('./warn-once');
12
13 var _warnOnce2 = _interopRequireDefault(_warnOnce);
14
15 var _comment = require('./comment');
16
17 var _comment2 = _interopRequireDefault(_comment);
18
19 var _node = require('./node');
20
21 var _node2 = _interopRequireDefault(_node);
22
23 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
25 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
26
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; }
28
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; }
30
31 function cleanSource(nodes) {
32     return nodes.map(function (i) {
33         if (i.nodes) i.nodes = cleanSource(i.nodes);
34         delete i.source;
35         return i;
36     });
37 }
38
39 var Container = function (_Node) {
40     _inherits(Container, _Node);
41
42     function Container() {
43         _classCallCheck(this, Container);
44
45         return _possibleConstructorReturn(this, _Node.apply(this, arguments));
46     }
47
48     Container.prototype.push = function push(child) {
49         child.parent = this;
50         this.nodes.push(child);
51         return this;
52     };
53
54     Container.prototype.each = function each(callback) {
55         if (!this.lastEach) this.lastEach = 0;
56         if (!this.indexes) this.indexes = {};
57
58         this.lastEach += 1;
59         var id = this.lastEach;
60         this.indexes[id] = 0;
61
62         if (!this.nodes) return undefined;
63
64         var index = void 0,
65             result = void 0;
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;
70
71             this.indexes[id] += 1;
72         }
73
74         delete this.indexes[id];
75
76         return result;
77     };
78
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);
84             }
85             return result;
86         });
87     };
88
89     Container.prototype.walkDecls = function walkDecls(prop, callback) {
90         if (!callback) {
91             callback = prop;
92             return this.walk(function (child, i) {
93                 if (child.type === 'decl') {
94                     return callback(child, i);
95                 }
96             });
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);
101                 }
102             });
103         } else {
104             return this.walk(function (child, i) {
105                 if (child.type === 'decl' && child.prop === prop) {
106                     return callback(child, i);
107                 }
108             });
109         }
110     };
111
112     Container.prototype.walkRules = function walkRules(selector, callback) {
113         if (!callback) {
114             callback = selector;
115
116             return this.walk(function (child, i) {
117                 if (child.type === 'rule') {
118                     return callback(child, i);
119                 }
120             });
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);
125                 }
126             });
127         } else {
128             return this.walk(function (child, i) {
129                 if (child.type === 'rule' && child.selector === selector) {
130                     return callback(child, i);
131                 }
132             });
133         }
134     };
135
136     Container.prototype.walkAtRules = function walkAtRules(name, callback) {
137         if (!callback) {
138             callback = name;
139             return this.walk(function (child, i) {
140                 if (child.type === 'atrule') {
141                     return callback(child, i);
142                 }
143             });
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);
148                 }
149             });
150         } else {
151             return this.walk(function (child, i) {
152                 if (child.type === 'atrule' && child.name === name) {
153                     return callback(child, i);
154                 }
155             });
156         }
157     };
158
159     Container.prototype.walkComments = function walkComments(callback) {
160         return this.walk(function (child, i) {
161             if (child.type === 'comment') {
162                 return callback(child, i);
163             }
164         });
165     };
166
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];
170         }
171
172         for (var _iterator = children, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
173             var _ref;
174
175             if (_isArray) {
176                 if (_i >= _iterator.length) break;
177                 _ref = _iterator[_i++];
178             } else {
179                 _i = _iterator.next();
180                 if (_i.done) break;
181                 _ref = _i.value;
182             }
183
184             var child = _ref;
185
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]();;) {
188                 var _ref2;
189
190                 if (_isArray2) {
191                     if (_i2 >= _iterator2.length) break;
192                     _ref2 = _iterator2[_i2++];
193                 } else {
194                     _i2 = _iterator2.next();
195                     if (_i2.done) break;
196                     _ref2 = _i2.value;
197                 }
198
199                 var node = _ref2;
200                 this.nodes.push(node);
201             }
202         }
203         return this;
204     };
205
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];
209         }
210
211         children = children.reverse();
212         for (var _iterator3 = children, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
213             var _ref3;
214
215             if (_isArray3) {
216                 if (_i3 >= _iterator3.length) break;
217                 _ref3 = _iterator3[_i3++];
218             } else {
219                 _i3 = _iterator3.next();
220                 if (_i3.done) break;
221                 _ref3 = _i3.value;
222             }
223
224             var child = _ref3;
225
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]();;) {
228                 var _ref4;
229
230                 if (_isArray4) {
231                     if (_i4 >= _iterator4.length) break;
232                     _ref4 = _iterator4[_i4++];
233                 } else {
234                     _i4 = _iterator4.next();
235                     if (_i4.done) break;
236                     _ref4 = _i4.value;
237                 }
238
239                 var node = _ref4;
240                 this.nodes.unshift(node);
241             }for (var id in this.indexes) {
242                 this.indexes[id] = this.indexes[id] + nodes.length;
243             }
244         }
245         return this;
246     };
247
248     Container.prototype.cleanRaws = function cleanRaws(keepBetween) {
249         _Node.prototype.cleanRaws.call(this, keepBetween);
250         if (this.nodes) {
251             for (var _iterator5 = this.nodes, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
252                 var _ref5;
253
254                 if (_isArray5) {
255                     if (_i5 >= _iterator5.length) break;
256                     _ref5 = _iterator5[_i5++];
257                 } else {
258                     _i5 = _iterator5.next();
259                     if (_i5.done) break;
260                     _ref5 = _i5.value;
261                 }
262
263                 var node = _ref5;
264                 node.cleanRaws(keepBetween);
265             }
266         }
267     };
268
269     Container.prototype.insertBefore = function insertBefore(exist, add) {
270         exist = this.index(exist);
271
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]();;) {
275             var _ref6;
276
277             if (_isArray6) {
278                 if (_i6 >= _iterator6.length) break;
279                 _ref6 = _iterator6[_i6++];
280             } else {
281                 _i6 = _iterator6.next();
282                 if (_i6.done) break;
283                 _ref6 = _i6.value;
284             }
285
286             var node = _ref6;
287             this.nodes.splice(exist, 0, node);
288         }var index = void 0;
289         for (var id in this.indexes) {
290             index = this.indexes[id];
291             if (exist <= index) {
292                 this.indexes[id] = index + nodes.length;
293             }
294         }
295
296         return this;
297     };
298
299     Container.prototype.insertAfter = function insertAfter(exist, add) {
300         exist = this.index(exist);
301
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]();;) {
304             var _ref7;
305
306             if (_isArray7) {
307                 if (_i7 >= _iterator7.length) break;
308                 _ref7 = _iterator7[_i7++];
309             } else {
310                 _i7 = _iterator7.next();
311                 if (_i7.done) break;
312                 _ref7 = _i7.value;
313             }
314
315             var node = _ref7;
316             this.nodes.splice(exist + 1, 0, node);
317         }var index = void 0;
318         for (var id in this.indexes) {
319             index = this.indexes[id];
320             if (exist < index) {
321                 this.indexes[id] = index + nodes.length;
322             }
323         }
324
325         return this;
326     };
327
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);
333         } else {
334             _Node.prototype.remove.call(this);
335         }
336         return this;
337     };
338
339     Container.prototype.removeChild = function removeChild(child) {
340         child = this.index(child);
341         this.nodes[child].parent = undefined;
342         this.nodes.splice(child, 1);
343
344         var index = void 0;
345         for (var id in this.indexes) {
346             index = this.indexes[id];
347             if (index >= child) {
348                 this.indexes[id] = index - 1;
349             }
350         }
351
352         return this;
353     };
354
355     Container.prototype.removeAll = function removeAll() {
356         for (var _iterator8 = this.nodes, _isArray8 = Array.isArray(_iterator8), _i8 = 0, _iterator8 = _isArray8 ? _iterator8 : _iterator8[Symbol.iterator]();;) {
357             var _ref8;
358
359             if (_isArray8) {
360                 if (_i8 >= _iterator8.length) break;
361                 _ref8 = _iterator8[_i8++];
362             } else {
363                 _i8 = _iterator8.next();
364                 if (_i8.done) break;
365                 _ref8 = _i8.value;
366             }
367
368             var node = _ref8;
369             node.parent = undefined;
370         }this.nodes = [];
371         return this;
372     };
373
374     Container.prototype.replaceValues = function replaceValues(regexp, opts, callback) {
375         if (!callback) {
376             callback = opts;
377             opts = {};
378         }
379
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;
383
384             decl.value = decl.value.replace(regexp, callback);
385         });
386
387         return this;
388     };
389
390     Container.prototype.every = function every(condition) {
391         return this.nodes.every(condition);
392     };
393
394     Container.prototype.some = function some(condition) {
395         return this.nodes.some(condition);
396     };
397
398     Container.prototype.index = function index(child) {
399         if (typeof child === 'number') {
400             return child;
401         } else {
402             return this.nodes.indexOf(child);
403         }
404     };
405
406     Container.prototype.normalize = function normalize(nodes, sample) {
407         var _this2 = this;
408
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') {
414                 nodes = nodes.nodes;
415             } else if (nodes.type) {
416                 nodes = [nodes];
417             } else if (nodes.prop) {
418                 if (typeof nodes.value === 'undefined') {
419                     throw new Error('Value field is missed in node creation');
420                 }
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)];
430             } else {
431                 throw new Error('Unknown node type in node creation');
432             }
433         }
434
435         var processed = nodes.map(function (i) {
436             /* istanbul ignore if */
437             if (typeof i.raws === 'undefined') i = _this2.rebuild(i);
438
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, '');
443                 }
444             }
445             i.parent = _this2;
446             return i;
447         });
448
449         return processed;
450     };
451
452     /* istanbul ignore next */
453
454
455     Container.prototype.rebuild = function rebuild(node, parent) {
456         var _this3 = this;
457
458         var fix = void 0;
459         if (node.type === 'root') {
460             var Root = require('./root');
461             fix = new Root();
462         } else if (node.type === 'atrule') {
463             var AtRule = require('./at-rule');
464             fix = new AtRule();
465         } else if (node.type === 'rule') {
466             var Rule = require('./rule');
467             fix = new Rule();
468         } else if (node.type === 'decl') {
469             fix = new _declaration2.default();
470         } else if (node.type === 'comment') {
471             fix = new _comment2.default();
472         }
473
474         for (var i in node) {
475             if (i === 'nodes') {
476                 fix.nodes = node.nodes.map(function (j) {
477                     return _this3.rebuild(j, fix);
478                 });
479             } else if (i === 'parent' && parent) {
480                 fix.parent = parent;
481             } else if (node.hasOwnProperty(i)) {
482                 fix[i] = node[i];
483             }
484         }
485
486         return fix;
487     };
488
489     /* istanbul ignore next */
490
491
492     Container.prototype.eachInside = function eachInside(callback) {
493         (0, _warnOnce2.default)('Container#eachInside is deprecated. ' + 'Use Container#walk instead.');
494         return this.walk(callback);
495     };
496
497     /* istanbul ignore next */
498
499
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);
503     };
504
505     /* istanbul ignore next */
506
507
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);
511     };
512
513     /* istanbul ignore next */
514
515
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);
519     };
520
521     /* istanbul ignore next */
522
523
524     Container.prototype.eachComment = function eachComment(callback) {
525         (0, _warnOnce2.default)('Container#eachComment is deprecated. ' + 'Use Container#walkComments instead.');
526         return this.walkComments(callback);
527     };
528
529     /* istanbul ignore next */
530
531
532     _createClass(Container, [{
533         key: 'first',
534         get: function get() {
535             if (!this.nodes) return undefined;
536             return this.nodes[0];
537         }
538     }, {
539         key: 'last',
540         get: function get() {
541             if (!this.nodes) return undefined;
542             return this.nodes[this.nodes.length - 1];
543         }
544     }, {
545         key: 'semicolon',
546         get: function get() {
547             (0, _warnOnce2.default)('Node#semicolon is deprecated. Use Node#raws.semicolon');
548             return this.raws.semicolon;
549         }
550
551         /* istanbul ignore next */
552         ,
553         set: function set(val) {
554             (0, _warnOnce2.default)('Node#semicolon is deprecated. Use Node#raws.semicolon');
555             this.raws.semicolon = val;
556         }
557
558         /* istanbul ignore next */
559
560     }, {
561         key: 'after',
562         get: function get() {
563             (0, _warnOnce2.default)('Node#after is deprecated. Use Node#raws.after');
564             return this.raws.after;
565         }
566
567         /* istanbul ignore next */
568         ,
569         set: function set(val) {
570             (0, _warnOnce2.default)('Node#after is deprecated. Use Node#raws.after');
571             this.raws.after = val;
572         }
573     }]);
574
575     return Container;
576 }(_node2.default);
577
578 exports.default = Container;
579 module.exports = exports['default'];