source: trip-planner-front/node_modules/postcss-values-parser/lib/container.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.1 KB
Line 
1'use strict';
2
3const Node = require('./node');
4
5class Container extends Node {
6
7 constructor (opts) {
8 super(opts);
9
10 if (!this.nodes) {
11 this.nodes = [];
12 }
13 }
14
15 push (child) {
16 child.parent = this;
17 this.nodes.push(child);
18 return this;
19 }
20
21 each (callback) {
22 if (!this.lastEach) this.lastEach = 0;
23 if (!this.indexes) this.indexes = { };
24
25 this.lastEach += 1;
26
27 let id = this.lastEach,
28 index,
29 result;
30
31 this.indexes[id] = 0;
32
33 if (!this.nodes) return undefined;
34
35 while (this.indexes[id] < this.nodes.length) {
36 index = this.indexes[id];
37 result = callback(this.nodes[index], index);
38 if (result === false) break;
39
40 this.indexes[id] += 1;
41 }
42
43 delete this.indexes[id];
44
45 return result;
46 }
47
48 walk (callback) {
49 return this.each((child, i) => {
50 let result = callback(child, i);
51 if (result !== false && child.walk) {
52 result = child.walk(callback);
53 }
54 return result;
55 });
56 }
57
58 walkType (type, callback) {
59 if (!type || !callback) {
60 throw new Error('Parameters {type} and {callback} are required.');
61 }
62
63 // allow users to pass a constructor, or node type string; eg. Word.
64 const isTypeCallable = typeof type === 'function';
65
66 return this.walk((node, index) => {
67 if (isTypeCallable && node instanceof type || !isTypeCallable && node.type === type) {
68 return callback.call(this, node, index);
69 }
70 });
71 }
72
73 append (node) {
74 node.parent = this;
75 this.nodes.push(node);
76 return this;
77 }
78
79 prepend (node) {
80 node.parent = this;
81 this.nodes.unshift(node);
82 return this;
83 }
84
85 cleanRaws (keepBetween) {
86 super.cleanRaws(keepBetween);
87 if (this.nodes) {
88 for (let node of this.nodes) node.cleanRaws(keepBetween);
89 }
90 }
91
92 insertAfter (oldNode, newNode) {
93 let oldIndex = this.index(oldNode),
94 index;
95
96 this.nodes.splice(oldIndex + 1, 0, newNode);
97
98 for (let id in this.indexes) {
99 index = this.indexes[id];
100 if (oldIndex <= index) {
101 this.indexes[id] = index + this.nodes.length;
102 }
103 }
104
105 return this;
106 }
107
108 insertBefore (oldNode, newNode) {
109 let oldIndex = this.index(oldNode),
110 index;
111
112 this.nodes.splice(oldIndex, 0, newNode);
113
114 for (let id in this.indexes) {
115 index = this.indexes[id];
116 if (oldIndex <= index) {
117 this.indexes[id] = index + this.nodes.length;
118 }
119 }
120
121 return this;
122 }
123
124 removeChild (child) {
125 child = this.index(child);
126 this.nodes[child].parent = undefined;
127 this.nodes.splice(child, 1);
128
129 let index;
130 for (let id in this.indexes) {
131 index = this.indexes[id];
132 if (index >= child) {
133 this.indexes[id] = index - 1;
134 }
135 }
136
137 return this;
138 }
139
140 removeAll () {
141 for (let node of this.nodes) node.parent = undefined;
142 this.nodes = [];
143 return this;
144 }
145
146 every (condition) {
147 return this.nodes.every(condition);
148 }
149
150 some (condition) {
151 return this.nodes.some(condition);
152 }
153
154 index (child) {
155 if (typeof child === 'number') {
156 return child;
157 }
158 else {
159 return this.nodes.indexOf(child);
160 }
161 }
162
163 get first () {
164 if (!this.nodes) return undefined;
165 return this.nodes[0];
166 }
167
168 get last () {
169 if (!this.nodes) return undefined;
170 return this.nodes[this.nodes.length - 1];
171 }
172
173 toString () {
174 let result = this.nodes.map(String).join('');
175
176 if (this.value) {
177 result = this.value + result;
178 }
179
180 if (this.raws.before) {
181 result = this.raws.before + result;
182 }
183
184 if (this.raws.after) {
185 result += this.raws.after;
186 }
187
188 return result;
189 }
190}
191
192Container.registerWalker = (constructor) => {
193 let walkerName = 'walk' + constructor.name;
194
195 // plural sugar
196 if (walkerName.lastIndexOf('s') !== walkerName.length - 1) {
197 walkerName += 's';
198 }
199
200 if (Container.prototype[walkerName]) {
201 return;
202 }
203
204 // we need access to `this` so we can't use an arrow function
205 Container.prototype[walkerName] = function (callback) {
206 return this.walkType(constructor, callback);
207 };
208};
209
210module.exports = Container;
Note: See TracBrowser for help on using the repository browser.