source: node_modules/postcss/lib/node.js@ 9868304

Last change on this file since 9868304 was 57e58a3, checked in by ste08 <sjovanoska@…>, 4 months ago

Initial commit

  • Property mode set to 100644
File size: 9.7 KB
Line 
1'use strict'
2
3let CssSyntaxError = require('./css-syntax-error')
4let Stringifier = require('./stringifier')
5let stringify = require('./stringify')
6let { isClean, my } = require('./symbols')
7
8function cloneNode(obj, parent) {
9 let cloned = new obj.constructor()
10
11 for (let i in obj) {
12 if (!Object.prototype.hasOwnProperty.call(obj, i)) {
13 /* c8 ignore next 2 */
14 continue
15 }
16 if (i === 'proxyCache') continue
17 let value = obj[i]
18 let type = typeof value
19
20 if (i === 'parent' && type === 'object') {
21 if (parent) cloned[i] = parent
22 } else if (i === 'source') {
23 cloned[i] = value
24 } else if (Array.isArray(value)) {
25 cloned[i] = value.map(j => cloneNode(j, cloned))
26 } else {
27 if (type === 'object' && value !== null) value = cloneNode(value)
28 cloned[i] = value
29 }
30 }
31
32 return cloned
33}
34
35function sourceOffset(inputCSS, position) {
36 // Not all custom syntaxes support `offset` in `source.start` and `source.end`
37 if (
38 position &&
39 typeof position.offset !== 'undefined'
40 ) {
41 return position.offset;
42 }
43
44 let column = 1
45 let line = 1
46 let offset = 0
47
48 for (let i = 0; i < inputCSS.length; i++) {
49 if (line === position.line && column === position.column) {
50 offset = i
51 break
52 }
53
54 if (inputCSS[i] === '\n') {
55 column = 1
56 line += 1
57 } else {
58 column += 1
59 }
60 }
61
62 return offset
63}
64
65class Node {
66 constructor(defaults = {}) {
67 this.raws = {}
68 this[isClean] = false
69 this[my] = true
70
71 for (let name in defaults) {
72 if (name === 'nodes') {
73 this.nodes = []
74 for (let node of defaults[name]) {
75 if (typeof node.clone === 'function') {
76 this.append(node.clone())
77 } else {
78 this.append(node)
79 }
80 }
81 } else {
82 this[name] = defaults[name]
83 }
84 }
85 }
86
87 addToError(error) {
88 error.postcssNode = this
89 if (error.stack && this.source && /\n\s{4}at /.test(error.stack)) {
90 let s = this.source
91 error.stack = error.stack.replace(
92 /\n\s{4}at /,
93 `$&${s.input.from}:${s.start.line}:${s.start.column}$&`
94 )
95 }
96 return error
97 }
98
99 after(add) {
100 this.parent.insertAfter(this, add)
101 return this
102 }
103
104 assign(overrides = {}) {
105 for (let name in overrides) {
106 this[name] = overrides[name]
107 }
108 return this
109 }
110
111 before(add) {
112 this.parent.insertBefore(this, add)
113 return this
114 }
115
116 cleanRaws(keepBetween) {
117 delete this.raws.before
118 delete this.raws.after
119 if (!keepBetween) delete this.raws.between
120 }
121
122 clone(overrides = {}) {
123 let cloned = cloneNode(this)
124 for (let name in overrides) {
125 cloned[name] = overrides[name]
126 }
127 return cloned
128 }
129
130 cloneAfter(overrides = {}) {
131 let cloned = this.clone(overrides)
132 this.parent.insertAfter(this, cloned)
133 return cloned
134 }
135
136 cloneBefore(overrides = {}) {
137 let cloned = this.clone(overrides)
138 this.parent.insertBefore(this, cloned)
139 return cloned
140 }
141
142 error(message, opts = {}) {
143 if (this.source) {
144 let { end, start } = this.rangeBy(opts)
145 return this.source.input.error(
146 message,
147 { column: start.column, line: start.line },
148 { column: end.column, line: end.line },
149 opts
150 )
151 }
152 return new CssSyntaxError(message)
153 }
154
155 getProxyProcessor() {
156 return {
157 get(node, prop) {
158 if (prop === 'proxyOf') {
159 return node
160 } else if (prop === 'root') {
161 return () => node.root().toProxy()
162 } else {
163 return node[prop]
164 }
165 },
166
167 set(node, prop, value) {
168 if (node[prop] === value) return true
169 node[prop] = value
170 if (
171 prop === 'prop' ||
172 prop === 'value' ||
173 prop === 'name' ||
174 prop === 'params' ||
175 prop === 'important' ||
176 /* c8 ignore next */
177 prop === 'text'
178 ) {
179 node.markDirty()
180 }
181 return true
182 }
183 }
184 }
185
186 /* c8 ignore next 3 */
187 markClean() {
188 this[isClean] = true
189 }
190
191 markDirty() {
192 if (this[isClean]) {
193 this[isClean] = false
194 let next = this
195 while ((next = next.parent)) {
196 next[isClean] = false
197 }
198 }
199 }
200
201 next() {
202 if (!this.parent) return undefined
203 let index = this.parent.index(this)
204 return this.parent.nodes[index + 1]
205 }
206
207 positionBy(opts) {
208 let pos = this.source.start
209 if (opts.index) {
210 pos = this.positionInside(opts.index)
211 } else if (opts.word) {
212 let inputString = ('document' in this.source.input)
213 ? this.source.input.document
214 : this.source.input.css
215 let stringRepresentation = inputString.slice(
216 sourceOffset(inputString, this.source.start),
217 sourceOffset(inputString, this.source.end)
218 )
219 let index = stringRepresentation.indexOf(opts.word)
220 if (index !== -1) pos = this.positionInside(index)
221 }
222 return pos
223 }
224
225 positionInside(index) {
226 let column = this.source.start.column
227 let line = this.source.start.line
228 let inputString = ('document' in this.source.input)
229 ? this.source.input.document
230 : this.source.input.css
231 let offset = sourceOffset(inputString, this.source.start)
232 let end = offset + index
233
234 for (let i = offset; i < end; i++) {
235 if (inputString[i] === '\n') {
236 column = 1
237 line += 1
238 } else {
239 column += 1
240 }
241 }
242
243 return { column, line }
244 }
245
246 prev() {
247 if (!this.parent) return undefined
248 let index = this.parent.index(this)
249 return this.parent.nodes[index - 1]
250 }
251
252 rangeBy(opts) {
253 let start = {
254 column: this.source.start.column,
255 line: this.source.start.line
256 }
257 let end = this.source.end
258 ? {
259 column: this.source.end.column + 1,
260 line: this.source.end.line
261 }
262 : {
263 column: start.column + 1,
264 line: start.line
265 }
266
267 if (opts.word) {
268 let inputString = ('document' in this.source.input)
269 ? this.source.input.document
270 : this.source.input.css
271 let stringRepresentation = inputString.slice(
272 sourceOffset(inputString, this.source.start),
273 sourceOffset(inputString, this.source.end)
274 )
275 let index = stringRepresentation.indexOf(opts.word)
276 if (index !== -1) {
277 start = this.positionInside(index)
278 end = this.positionInside(
279 index + opts.word.length,
280 )
281 }
282 } else {
283 if (opts.start) {
284 start = {
285 column: opts.start.column,
286 line: opts.start.line
287 }
288 } else if (opts.index) {
289 start = this.positionInside(opts.index)
290 }
291
292 if (opts.end) {
293 end = {
294 column: opts.end.column,
295 line: opts.end.line
296 }
297 } else if (typeof opts.endIndex === 'number') {
298 end = this.positionInside(opts.endIndex)
299 } else if (opts.index) {
300 end = this.positionInside(opts.index + 1)
301 }
302 }
303
304 if (
305 end.line < start.line ||
306 (end.line === start.line && end.column <= start.column)
307 ) {
308 end = { column: start.column + 1, line: start.line }
309 }
310
311 return { end, start }
312 }
313
314 raw(prop, defaultType) {
315 let str = new Stringifier()
316 return str.raw(this, prop, defaultType)
317 }
318
319 remove() {
320 if (this.parent) {
321 this.parent.removeChild(this)
322 }
323 this.parent = undefined
324 return this
325 }
326
327 replaceWith(...nodes) {
328 if (this.parent) {
329 let bookmark = this
330 let foundSelf = false
331 for (let node of nodes) {
332 if (node === this) {
333 foundSelf = true
334 } else if (foundSelf) {
335 this.parent.insertAfter(bookmark, node)
336 bookmark = node
337 } else {
338 this.parent.insertBefore(bookmark, node)
339 }
340 }
341
342 if (!foundSelf) {
343 this.remove()
344 }
345 }
346
347 return this
348 }
349
350 root() {
351 let result = this
352 while (result.parent && result.parent.type !== 'document') {
353 result = result.parent
354 }
355 return result
356 }
357
358 toJSON(_, inputs) {
359 let fixed = {}
360 let emitInputs = inputs == null
361 inputs = inputs || new Map()
362 let inputsNextIndex = 0
363
364 for (let name in this) {
365 if (!Object.prototype.hasOwnProperty.call(this, name)) {
366 /* c8 ignore next 2 */
367 continue
368 }
369 if (name === 'parent' || name === 'proxyCache') continue
370 let value = this[name]
371
372 if (Array.isArray(value)) {
373 fixed[name] = value.map(i => {
374 if (typeof i === 'object' && i.toJSON) {
375 return i.toJSON(null, inputs)
376 } else {
377 return i
378 }
379 })
380 } else if (typeof value === 'object' && value.toJSON) {
381 fixed[name] = value.toJSON(null, inputs)
382 } else if (name === 'source') {
383 let inputId = inputs.get(value.input)
384 if (inputId == null) {
385 inputId = inputsNextIndex
386 inputs.set(value.input, inputsNextIndex)
387 inputsNextIndex++
388 }
389 fixed[name] = {
390 end: value.end,
391 inputId,
392 start: value.start
393 }
394 } else {
395 fixed[name] = value
396 }
397 }
398
399 if (emitInputs) {
400 fixed.inputs = [...inputs.keys()].map(input => input.toJSON())
401 }
402
403 return fixed
404 }
405
406 toProxy() {
407 if (!this.proxyCache) {
408 this.proxyCache = new Proxy(this, this.getProxyProcessor())
409 }
410 return this.proxyCache
411 }
412
413 toString(stringifier = stringify) {
414 if (stringifier.stringify) stringifier = stringifier.stringify
415 let result = ''
416 stringifier(this, i => {
417 result += i
418 })
419 return result
420 }
421
422 warn(result, text, opts) {
423 let data = { node: this }
424 for (let i in opts) data[i] = opts[i]
425 return result.warn(text, data)
426 }
427
428 get proxyOf() {
429 return this
430 }
431}
432
433module.exports = Node
434Node.default = Node
Note: See TracBrowser for help on using the repository browser.