forked from postcss/postcss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.es6
525 lines (488 loc) · 14.5 KB
/
node.es6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import CssSyntaxError from './css-syntax-error'
import Stringifier from './stringifier'
import stringify from './stringify'
function cloneNode (obj, parent) {
let cloned = new obj.constructor()
for (let i in obj) {
if (!obj.hasOwnProperty(i)) continue
let value = obj[i]
let type = typeof value
if (i === 'parent' && type === 'object') {
if (parent) cloned[i] = parent
} else if (i === 'source') {
cloned[i] = value
} else if (value instanceof Array) {
cloned[i] = value.map(j => cloneNode(j, cloned))
} else {
if (type === 'object' && value !== null) value = cloneNode(value)
cloned[i] = value
}
}
return cloned
}
/**
* All node classes inherit the following common methods.
*
* @abstract
*/
class Node {
/**
* @param {object} [defaults] Value for node properties.
*/
constructor (defaults = { }) {
this.raws = { }
if (process.env.NODE_ENV !== 'production') {
if (typeof defaults !== 'object' && typeof defaults !== 'undefined') {
throw new Error(
'PostCSS nodes constructor accepts object, not ' +
JSON.stringify(defaults)
)
}
}
for (let name in defaults) {
this[name] = defaults[name]
}
}
/**
* Returns a `CssSyntaxError` instance containing the original position
* of the node in the source, showing line and column numbers and also
* a small excerpt to facilitate debugging.
*
* If present, an input source map will be used to get the original position
* of the source, even from a previous compilation step
* (e.g., from Sass compilation).
*
* This method produces very useful error messages.
*
* @param {string} message Error description.
* @param {object} [opts] Options.
* @param {string} opts.plugin Plugin name that created this error.
* PostCSS will set it automatically.
* @param {string} opts.word A word inside a node’s string that should
* be highlighted as the source of the error.
* @param {number} opts.index An index inside a node’s string that should
* be highlighted as the source of the error.
*
* @return {CssSyntaxError} Error object to throw it.
*
* @example
* if (!variables[name]) {
* throw decl.error('Unknown variable ' + name, { word: name })
* // CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
* // color: $black
* // a
* // ^
* // background: white
* }
*/
error (message, opts = { }) {
if (this.source) {
let pos = this.positionBy(opts)
return this.source.input.error(message, pos.line, pos.column, opts)
}
return new CssSyntaxError(message)
}
/**
* This method is provided as a convenience wrapper for {@link Result#warn}.
*
* @param {Result} result The {@link Result} instance
* that will receive the warning.
* @param {string} text Warning message.
* @param {object} [opts] Options
* @param {string} opts.plugin Plugin name that created this warning.
* PostCSS will set it automatically.
* @param {string} opts.word A word inside a node’s string that should
* be highlighted as the source of the warning.
* @param {number} opts.index An index inside a node’s string that should
* be highlighted as the source of the warning.
*
* @return {Warning} Created warning object.
*
* @example
* const plugin = postcss.plugin('postcss-deprecated', () => {
* return (root, result) => {
* root.walkDecls('bad', decl => {
* decl.warn(result, 'Deprecated property bad')
* })
* }
* })
*/
warn (result, text, opts) {
let data = { node: this }
for (let i in opts) data[i] = opts[i]
return result.warn(text, data)
}
/**
* Removes the node from its parent and cleans the parent properties
* from the node and its children.
*
* @example
* if (decl.prop.match(/^-webkit-/)) {
* decl.remove()
* }
*
* @return {Node} Node to make calls chain.
*/
remove () {
if (this.parent) {
this.parent.removeChild(this)
}
this.parent = undefined
return this
}
/**
* Returns a CSS string representing the node.
*
* @param {stringifier|syntax} [stringifier] A syntax to use
* in string generation.
*
* @return {string} CSS string of this node.
*
* @example
* postcss.rule({ selector: 'a' }).toString() //=> "a {}"
*/
toString (stringifier = stringify) {
if (stringifier.stringify) stringifier = stringifier.stringify
let result = ''
stringifier(this, i => {
result += i
})
return result
}
/**
* Returns an exact clone of the node.
*
* The resulting cloned node and its (cloned) children will retain
* code style properties.
*
* @param {object} [overrides] New properties to override in the clone.
*
* @example
* decl.raws.before //=> "\n "
* const cloned = decl.clone({ prop: '-moz-' + decl.prop })
* cloned.raws.before //=> "\n "
* cloned.toString() //=> -moz-transform: scale(0)
*
* @return {Node} Clone of the node.
*/
clone (overrides = { }) {
let cloned = cloneNode(this)
for (let name in overrides) {
cloned[name] = overrides[name]
}
return cloned
}
/**
* Shortcut to clone the node and insert the resulting cloned node
* before the current node.
*
* @param {object} [overrides] Mew properties to override in the clone.
*
* @example
* decl.cloneBefore({ prop: '-moz-' + decl.prop })
*
* @return {Node} New node
*/
cloneBefore (overrides = { }) {
let cloned = this.clone(overrides)
this.parent.insertBefore(this, cloned)
return cloned
}
/**
* Shortcut to clone the node and insert the resulting cloned node
* after the current node.
*
* @param {object} [overrides] New properties to override in the clone.
*
* @return {Node} New node.
*/
cloneAfter (overrides = { }) {
let cloned = this.clone(overrides)
this.parent.insertAfter(this, cloned)
return cloned
}
/**
* Inserts node(s) before the current node and removes the current node.
*
* @param {...Node} nodes Mode(s) to replace current one.
*
* @example
* if (atrule.name === 'mixin') {
* atrule.replaceWith(mixinRules[atrule.params])
* }
*
* @return {Node} Current node to methods chain.
*/
replaceWith (...nodes) {
if (this.parent) {
for (let node of nodes) {
this.parent.insertBefore(this, node)
}
this.remove()
}
return this
}
/**
* Returns the next child of the node’s parent.
* Returns `undefined` if the current node is the last child.
*
* @return {Node|undefined} Next node.
*
* @example
* if (comment.text === 'delete next') {
* const next = comment.next()
* if (next) {
* next.remove()
* }
* }
*/
next () {
if (!this.parent) return undefined
let index = this.parent.index(this)
return this.parent.nodes[index + 1]
}
/**
* Returns the previous child of the node’s parent.
* Returns `undefined` if the current node is the first child.
*
* @return {Node|undefined} Previous node.
*
* @example
* const annotation = decl.prev()
* if (annotation.type === 'comment') {
* readAnnotation(annotation.text)
* }
*/
prev () {
if (!this.parent) return undefined
let index = this.parent.index(this)
return this.parent.nodes[index - 1]
}
/**
* Insert new node before current node to current node’s parent.
*
* Just alias for `node.parent.insertBefore(node, add)`.
*
* @param {Node|object|string|Node[]} add New node.
*
* @return {Node} This node for methods chain.
*
* @example
* decl.before('content: ""')
*/
before (add) {
this.parent.insertBefore(this, add)
return this
}
/**
* Insert new node after current node to current node’s parent.
*
* Just alias for `node.parent.insertAfter(node, add)`.
*
* @param {Node|object|string|Node[]} add New node.
*
* @return {Node} This node for methods chain.
*
* @example
* decl.after('color: black')
*/
after (add) {
this.parent.insertAfter(this, add)
return this
}
toJSON () {
let fixed = { }
for (let name in this) {
if (!this.hasOwnProperty(name)) continue
if (name === 'parent') continue
let value = this[name]
if (value instanceof Array) {
fixed[name] = value.map(i => {
if (typeof i === 'object' && i.toJSON) {
return i.toJSON()
} else {
return i
}
})
} else if (typeof value === 'object' && value.toJSON) {
fixed[name] = value.toJSON()
} else {
fixed[name] = value
}
}
return fixed
}
/**
* Returns a {@link Node#raws} value. If the node is missing
* the code style property (because the node was manually built or cloned),
* PostCSS will try to autodetect the code style property by looking
* at other nodes in the tree.
*
* @param {string} prop Name of code style property.
* @param {string} [defaultType] Name of default value, it can be missed
* if the value is the same as prop.
*
* @example
* const root = postcss.parse('a { background: white }')
* root.nodes[0].append({ prop: 'color', value: 'black' })
* root.nodes[0].nodes[1].raws.before //=> undefined
* root.nodes[0].nodes[1].raw('before') //=> ' '
*
* @return {string} Code style value.
*/
raw (prop, defaultType) {
let str = new Stringifier()
return str.raw(this, prop, defaultType)
}
/**
* Finds the Root instance of the node’s tree.
*
* @example
* root.nodes[0].nodes[0].root() === root
*
* @return {Root} Root parent.
*/
root () {
let result = this
while (result.parent) result = result.parent
return result
}
/**
* Clear the code style properties for the node and its children.
*
* @param {boolean} [keepBetween] Keep the raws.between symbols.
*
* @return {undefined}
*
* @example
* node.raws.before //=> ' '
* node.cleanRaws()
* node.raws.before //=> undefined
*/
cleanRaws (keepBetween) {
delete this.raws.before
delete this.raws.after
if (!keepBetween) delete this.raws.between
}
positionInside (index) {
let string = this.toString()
let column = this.source.start.column
let line = this.source.start.line
for (let i = 0; i < index; i++) {
if (string[i] === '\n') {
column = 1
line += 1
} else {
column += 1
}
}
return { line, column }
}
positionBy (opts) {
let pos = this.source.start
if (opts.index) {
pos = this.positionInside(opts.index)
} else if (opts.word) {
let index = this.toString().indexOf(opts.word)
if (index !== -1) pos = this.positionInside(index)
}
return pos
}
/**
* @memberof Node#
* @member {string} type String representing the node’s type.
* Possible values are `root`, `atrule`, `rule`,
* `decl`, or `comment`.
*
* @example
* postcss.decl({ prop: 'color', value: 'black' }).type //=> 'decl'
*/
/**
* @memberof Node#
* @member {Container} parent The node’s parent node.
*
* @example
* root.nodes[0].parent === root
*/
/**
* @memberof Node#
* @member {source} source The input source of the node.
*
* The property is used in source map generation.
*
* If you create a node manually (e.g., with `postcss.decl()`),
* that node will not have a `source` property and will be absent
* from the source map. For this reason, the plugin developer should
* consider cloning nodes to create new ones (in which case the new node’s
* source will reference the original, cloned node) or setting
* the `source` property manually.
*
* ```js
* // Bad
* const prefixed = postcss.decl({
* prop: '-moz-' + decl.prop,
* value: decl.value
* })
*
* // Good
* const prefixed = decl.clone({ prop: '-moz-' + decl.prop })
* ```
*
* ```js
* if (atrule.name === 'add-link') {
* const rule = postcss.rule({ selector: 'a', source: atrule.source })
* atrule.parent.insertBefore(atrule, rule)
* }
* ```
*
* @example
* decl.source.input.from //=> '/home/ai/a.sass'
* decl.source.start //=> { line: 10, column: 2 }
* decl.source.end //=> { line: 10, column: 12 }
*/
/**
* @memberof Node#
* @member {object} raws Information to generate byte-to-byte equal
* node string as it was in the origin input.
*
* Every parser saves its own properties,
* but the default CSS parser uses:
*
* * `before`: the space symbols before the node. It also stores `*`
* and `_` symbols before the declaration (IE hack).
* * `after`: the space symbols after the last child of the node
* to the end of the node.
* * `between`: the symbols between the property and value
* for declarations, selector and `{` for rules, or last parameter
* and `{` for at-rules.
* * `semicolon`: contains true if the last child has
* an (optional) semicolon.
* * `afterName`: the space between the at-rule name and its parameters.
* * `left`: the space symbols between `/*` and the comment’s text.
* * `right`: the space symbols between the comment’s text
* and <code>*/</code>.
* * `important`: the content of the important statement,
* if it is not just `!important`.
*
* PostCSS cleans selectors, declaration values and at-rule parameters
* from comments and extra spaces, but it stores origin content in raws
* properties. As such, if you don’t change a declaration’s value,
* PostCSS will use the raw value with comments.
*
* @example
* const root = postcss.parse('a {\n color:black\n}')
* root.first.first.raws //=> { before: '\n ', between: ':' }
*/
}
export default Node
/**
* @typedef {object} position
* @property {number} line Source line in file.
* @property {number} column Source column in file.
*/
/**
* @typedef {object} source
* @property {Input} input {@link Input} with input file
* @property {position} start The starting position of the node’s source.
* @property {position} end The ending position of the node’s source.
*/