.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / postcss-media-query-parser / dist / nodes / Container.js
1 'use strict';
2
3 Object.defineProperty(exports, "__esModule", {
4   value: true
5 });
6
7 var _Node = require('./Node');
8
9 var _Node2 = _interopRequireDefault(_Node);
10
11 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13 function Container(opts) {
14   var _this = this;
15
16   this.constructor(opts);
17
18   this.nodes = opts.nodes;
19
20   if (this.after === undefined) {
21     this.after = this.nodes.length > 0 ? this.nodes[this.nodes.length - 1].after : '';
22   }
23
24   if (this.before === undefined) {
25     this.before = this.nodes.length > 0 ? this.nodes[0].before : '';
26   }
27
28   if (this.sourceIndex === undefined) {
29     this.sourceIndex = this.before.length;
30   }
31
32   this.nodes.forEach(function (node) {
33     node.parent = _this; // eslint-disable-line no-param-reassign
34   });
35 } /**\r
36    * A node that contains other nodes and support traversing over them\r
37    */
38
39 Container.prototype = Object.create(_Node2.default.prototype);
40 Container.constructor = _Node2.default;
41
42 /**\r
43  * Iterate over descendant nodes of the node\r
44  *\r
45  * @param {RegExp|string} filter - Optional. Only nodes with node.type that\r
46  *    satisfies the filter will be traversed over\r
47  * @param {function} cb - callback to call on each node. Takes theese params:\r
48  *    node - the node being processed, i - it's index, nodes - the array\r
49  *    of all nodes\r
50  *    If false is returned, the iteration breaks\r
51  *\r
52  * @return (boolean) false, if the iteration was broken\r
53  */
54 Container.prototype.walk = function walk(filter, cb) {
55   var hasFilter = typeof filter === 'string' || filter instanceof RegExp;
56   var callback = hasFilter ? cb : filter;
57   var filterReg = typeof filter === 'string' ? new RegExp(filter) : filter;
58
59   for (var i = 0; i < this.nodes.length; i++) {
60     var node = this.nodes[i];
61     var filtered = hasFilter ? filterReg.test(node.type) : true;
62     if (filtered && callback && callback(node, i, this.nodes) === false) {
63       return false;
64     }
65     if (node.nodes && node.walk(filter, cb) === false) {
66       return false;
67     }
68   }
69   return true;
70 };
71
72 /**\r
73  * Iterate over immediate children of the node\r
74  *\r
75  * @param {function} cb - callback to call on each node. Takes theese params:\r
76  *    node - the node being processed, i - it's index, nodes - the array\r
77  *    of all nodes\r
78  *    If false is returned, the iteration breaks\r
79  *\r
80  * @return (boolean) false, if the iteration was broken\r
81  */
82 Container.prototype.each = function each() {
83   var cb = arguments.length <= 0 || arguments[0] === undefined ? function () {} : arguments[0];
84
85   for (var i = 0; i < this.nodes.length; i++) {
86     var node = this.nodes[i];
87     if (cb(node, i, this.nodes) === false) {
88       return false;
89     }
90   }
91   return true;
92 };
93
94 exports.default = Container;