source: trip-planner-front/node_modules/postcss-values-parser/lib/node.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.0 KB
Line 
1'use strict';
2
3let cloneNode = function (obj, parent) {
4 let cloned = new obj.constructor();
5
6 for (let i in obj) {
7 if (!obj.hasOwnProperty(i)) continue;
8
9 let value = obj[i],
10 type = typeof value;
11
12 if (i === 'parent' && type === 'object') {
13 if (parent) cloned[i] = parent;
14 }
15 else if (i === 'source') {
16 cloned[i] = value;
17 }
18 else if (value instanceof Array) {
19 cloned[i] = value.map(j => cloneNode(j, cloned));
20 }
21 else if (i !== 'before' && i !== 'after' && i !== 'between' && i !== 'semicolon') {
22 if (type === 'object' && value !== null) value = cloneNode(value);
23 cloned[i] = value;
24 }
25 }
26
27 return cloned;
28};
29
30module.exports = class Node {
31
32 constructor (defaults) {
33 defaults = defaults || {};
34 this.raws = { before: '', after: '' };
35
36 for (let name in defaults) {
37 this[name] = defaults[name];
38 }
39 }
40
41 remove () {
42 if (this.parent) {
43 this.parent.removeChild(this);
44 }
45
46 this.parent = undefined;
47
48 return this;
49 }
50
51 toString () {
52 return [
53 this.raws.before,
54 String(this.value),
55 this.raws.after
56 ].join('');
57 }
58
59 clone (overrides) {
60 overrides = overrides || {};
61
62 let cloned = cloneNode(this);
63
64 for (let name in overrides) {
65 cloned[name] = overrides[name];
66 }
67
68 return cloned;
69 }
70
71 cloneBefore (overrides) {
72 overrides = overrides || {};
73
74 let cloned = this.clone(overrides);
75
76 this.parent.insertBefore(this, cloned);
77 return cloned;
78 }
79
80 cloneAfter (overrides) {
81 overrides = overrides || {};
82
83 let cloned = this.clone(overrides);
84
85 this.parent.insertAfter(this, cloned);
86 return cloned;
87 }
88
89 replaceWith () {
90 let nodes = Array.prototype.slice.call(arguments);
91
92 if (this.parent) {
93 for (let node of nodes) {
94 this.parent.insertBefore(this, node);
95 }
96
97 this.remove();
98 }
99
100 return this;
101 }
102
103 moveTo (container) {
104 this.cleanRaws(this.root() === container.root());
105 this.remove();
106
107 container.append(this);
108
109 return this;
110 }
111
112 moveBefore (node) {
113 this.cleanRaws(this.root() === node.root());
114 this.remove();
115
116 node.parent.insertBefore(node, this);
117
118 return this;
119 }
120
121 moveAfter (node) {
122 this.cleanRaws(this.root() === node.root());
123 this.remove();
124 node.parent.insertAfter(node, this);
125 return this;
126 }
127
128 next () {
129 let index = this.parent.index(this);
130
131 return this.parent.nodes[index + 1];
132 }
133
134 prev () {
135 let index = this.parent.index(this);
136
137 return this.parent.nodes[index - 1];
138 }
139
140 toJSON () {
141 let fixed = { };
142
143 for (let name in this) {
144 if (!this.hasOwnProperty(name)) continue;
145 if (name === 'parent') continue;
146 let value = this[name];
147
148 if (value instanceof Array) {
149 fixed[name] = value.map(i => {
150 if (typeof i === 'object' && i.toJSON) {
151 return i.toJSON();
152 }
153 else {
154 return i;
155 }
156 });
157 }
158 else if (typeof value === 'object' && value.toJSON) {
159 fixed[name] = value.toJSON();
160 }
161 else {
162 fixed[name] = value;
163 }
164 }
165
166 return fixed;
167 }
168
169 root () {
170 let result = this;
171
172 while (result.parent) result = result.parent;
173
174 return result;
175 }
176
177 cleanRaws (keepBetween) {
178 delete this.raws.before;
179 delete this.raws.after;
180 if (!keepBetween) delete this.raws.between;
181 }
182
183 positionInside (index) {
184 let string = this.toString(),
185 column = this.source.start.column,
186 line = this.source.start.line;
187
188 for (let i = 0; i < index; i++) {
189 if (string[i] === '\n') {
190 column = 1;
191 line += 1;
192 }
193 else {
194 column += 1;
195 }
196 }
197
198 return { line, column };
199 }
200
201 positionBy (opts) {
202 let pos = this.source.start;
203
204 if (Object(opts).index) {
205 pos = this.positionInside(opts.index);
206 }
207 else if (Object(opts).word) {
208 let index = this.toString().indexOf(opts.word);
209 if (index !== -1) pos = this.positionInside(index);
210 }
211
212 return pos;
213 }
214};
Note: See TracBrowser for help on using the repository browser.