source: node_modules/postcss/lib/node.d.ts@ 2058e5c

Last change on this file since 2058e5c was 2058e5c, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Working / before login

  • Property mode set to 100644
File size: 14.4 KB
Line 
1import AtRule = require('./at-rule.js')
2import { AtRuleProps } from './at-rule.js'
3import Comment, { CommentProps } from './comment.js'
4import Container, { NewChild } from './container.js'
5import CssSyntaxError from './css-syntax-error.js'
6import Declaration, { DeclarationProps } from './declaration.js'
7import Document from './document.js'
8import Input from './input.js'
9import { Stringifier, Syntax } from './postcss.js'
10import Result from './result.js'
11import Root from './root.js'
12import Rule, { RuleProps } from './rule.js'
13import Warning, { WarningOptions } from './warning.js'
14
15declare namespace Node {
16 export type ChildNode = AtRule.default | Comment | Declaration | Rule
17
18 export type AnyNode =
19 | AtRule.default
20 | Comment
21 | Declaration
22 | Document
23 | Root
24 | Rule
25
26 export type ChildProps =
27 | AtRuleProps
28 | CommentProps
29 | DeclarationProps
30 | RuleProps
31
32 export interface Position {
33 /**
34 * Source line in file. In contrast to `offset` it starts from 1.
35 */
36 column: number
37
38 /**
39 * Source column in file.
40 */
41 line: number
42
43 /**
44 * Source offset in file. It starts from 0.
45 */
46 offset: number
47 }
48
49 export interface Range {
50 /**
51 * End position, exclusive.
52 */
53 end: Position
54
55 /**
56 * Start position, inclusive.
57 */
58 start: Position
59 }
60
61 /**
62 * Source represents an interface for the {@link Node.source} property.
63 */
64 export interface Source {
65 /**
66 * The inclusive ending position for the source
67 * code of a node.
68 *
69 * However, `end.offset` of a non `Root` node is the exclusive position.
70 * See https://github.com/postcss/postcss/pull/1879 for details.
71 *
72 * ```js
73 * const root = postcss.parse('a { color: black }')
74 * const a = root.first
75 * const color = a.first
76 *
77 * // The offset of `Root` node is the inclusive position
78 * css.source.end // { line: 1, column: 19, offset: 18 }
79 *
80 * // The offset of non `Root` node is the exclusive position
81 * a.source.end // { line: 1, column: 18, offset: 18 }
82 * color.source.end // { line: 1, column: 16, offset: 16 }
83 * ```
84 */
85 end?: Position
86
87 /**
88 * The source file from where a node has originated.
89 */
90 input: Input
91
92 /**
93 * The inclusive starting position for the source
94 * code of a node.
95 */
96 start?: Position
97 }
98
99 /**
100 * Interface represents an interface for an object received
101 * as parameter by Node class constructor.
102 */
103 export interface NodeProps {
104 source?: Source
105 }
106
107 export interface NodeErrorOptions {
108 /**
109 * An ending index inside a node's string that should be highlighted as
110 * source of error.
111 */
112 endIndex?: number
113 /**
114 * An index inside a node's string that should be highlighted as source
115 * of error.
116 */
117 index?: number
118 /**
119 * Plugin name that created this error. PostCSS will set it automatically.
120 */
121 plugin?: string
122 /**
123 * A word inside a node's string, that should be highlighted as source
124 * of error.
125 */
126 word?: string
127 }
128
129 // eslint-disable-next-line @typescript-eslint/no-shadow
130 class Node extends Node_ {}
131 export { Node as default }
132}
133
134/**
135 * It represents an abstract class that handles common
136 * methods for other CSS abstract syntax tree nodes.
137 *
138 * Any node that represents CSS selector or value should
139 * not extend the `Node` class.
140 */
141declare abstract class Node_ {
142 /**
143 * It represents parent of the current node.
144 *
145 * ```js
146 * root.nodes[0].parent === root //=> true
147 * ```
148 */
149 parent: Container | Document | undefined
150
151 /**
152 * It represents unnecessary whitespace and characters present
153 * in the css source code.
154 *
155 * Information to generate byte-to-byte equal node string as it was
156 * in the origin input.
157 *
158 * The properties of the raws object are decided by parser,
159 * the default parser uses the following properties:
160 *
161 * * `before`: the space symbols before the node. It also stores `*`
162 * and `_` symbols before the declaration (IE hack).
163 * * `after`: the space symbols after the last child of the node
164 * to the end of the node.
165 * * `between`: the symbols between the property and value
166 * for declarations, selector and `{` for rules, or last parameter
167 * and `{` for at-rules.
168 * * `semicolon`: contains true if the last child has
169 * an (optional) semicolon.
170 * * `afterName`: the space between the at-rule name and its parameters.
171 * * `left`: the space symbols between `/*` and the comment’s text.
172 * * `right`: the space symbols between the comment’s text
173 * and <code>*&#47;</code>.
174 * - `important`: the content of the important statement,
175 * if it is not just `!important`.
176 *
177 * PostCSS filters out the comments inside selectors, declaration values
178 * and at-rule parameters but it stores the origin content in raws.
179 *
180 * ```js
181 * const root = postcss.parse('a {\n color:black\n}')
182 * root.first.first.raws //=> { before: '\n ', between: ':' }
183 * ```
184 */
185 raws: any
186
187 /**
188 * It represents information related to origin of a node and is required
189 * for generating source maps.
190 *
191 * The nodes that are created manually using the public APIs
192 * provided by PostCSS will have `source` undefined and
193 * will be absent in the source map.
194 *
195 * For this reason, the plugin developer should consider
196 * duplicating nodes as the duplicate node will have the
197 * same source as the original node by default or assign
198 * source to a node created manually.
199 *
200 * ```js
201 * decl.source.input.from //=> '/home/ai/source.css'
202 * decl.source.start //=> { line: 10, column: 2 }
203 * decl.source.end //=> { line: 10, column: 12 }
204 * ```
205 *
206 * ```js
207 * // Incorrect method, source not specified!
208 * const prefixed = postcss.decl({
209 * prop: '-moz-' + decl.prop,
210 * value: decl.value
211 * })
212 *
213 * // Correct method, source is inherited when duplicating.
214 * const prefixed = decl.clone({
215 * prop: '-moz-' + decl.prop
216 * })
217 * ```
218 *
219 * ```js
220 * if (atrule.name === 'add-link') {
221 * const rule = postcss.rule({
222 * selector: 'a',
223 * source: atrule.source
224 * })
225 *
226 * atrule.parent.insertBefore(atrule, rule)
227 * }
228 * ```
229 */
230 source?: Node.Source
231
232 /**
233 * It represents type of a node in
234 * an abstract syntax tree.
235 *
236 * A type of node helps in identification of a node
237 * and perform operation based on it's type.
238 *
239 * ```js
240 * const declaration = new Declaration({
241 * prop: 'color',
242 * value: 'black'
243 * })
244 *
245 * declaration.type //=> 'decl'
246 * ```
247 */
248 type: string
249
250 constructor(defaults?: object)
251
252 /**
253 * Insert new node after current node to current node’s parent.
254 *
255 * Just alias for `node.parent.insertAfter(node, add)`.
256 *
257 * ```js
258 * decl.after('color: black')
259 * ```
260 *
261 * @param newNode New node.
262 * @return This node for methods chain.
263 */
264 after(
265 newNode: Node | Node.ChildProps | readonly Node[] | string | undefined
266 ): this
267
268 /**
269 * It assigns properties to an existing node instance.
270 *
271 * ```js
272 * decl.assign({ prop: 'word-wrap', value: 'break-word' })
273 * ```
274 *
275 * @param overrides New properties to override the node.
276 *
277 * @return `this` for method chaining.
278 */
279 assign(overrides: object): this
280
281 /**
282 * Insert new node before current node to current node’s parent.
283 *
284 * Just alias for `node.parent.insertBefore(node, add)`.
285 *
286 * ```js
287 * decl.before('content: ""')
288 * ```
289 *
290 * @param newNode New node.
291 * @return This node for methods chain.
292 */
293 before(
294 newNode: Node | Node.ChildProps | readonly Node[] | string | undefined
295 ): this
296
297 /**
298 * Clear the code style properties for the node and its children.
299 *
300 * ```js
301 * node.raws.before //=> ' '
302 * node.cleanRaws()
303 * node.raws.before //=> undefined
304 * ```
305 *
306 * @param keepBetween Keep the `raws.between` symbols.
307 */
308 cleanRaws(keepBetween?: boolean): void
309
310 /**
311 * It creates clone of an existing node, which includes all the properties
312 * and their values, that includes `raws` but not `type`.
313 *
314 * ```js
315 * decl.raws.before //=> "\n "
316 * const cloned = decl.clone({ prop: '-moz-' + decl.prop })
317 * cloned.raws.before //=> "\n "
318 * cloned.toString() //=> -moz-transform: scale(0)
319 * ```
320 *
321 * @param overrides New properties to override in the clone.
322 *
323 * @return Duplicate of the node instance.
324 */
325 clone(overrides?: object): this
326
327 /**
328 * Shortcut to clone the node and insert the resulting cloned node
329 * after the current node.
330 *
331 * @param overrides New properties to override in the clone.
332 * @return New node.
333 */
334 cloneAfter(overrides?: object): this
335
336 /**
337 * Shortcut to clone the node and insert the resulting cloned node
338 * before the current node.
339 *
340 * ```js
341 * decl.cloneBefore({ prop: '-moz-' + decl.prop })
342 * ```
343 *
344 * @param overrides Mew properties to override in the clone.
345 *
346 * @return New node
347 */
348 cloneBefore(overrides?: object): this
349
350 /**
351 * It creates an instance of the class `CssSyntaxError` and parameters passed
352 * to this method are assigned to the error instance.
353 *
354 * The error instance will have description for the
355 * error, original position of the node in the
356 * source, showing line and column number.
357 *
358 * If any previous map is present, it would be used
359 * to get original position of the source.
360 *
361 * The Previous Map here is referred to the source map
362 * generated by previous compilation, example: Less,
363 * Stylus and Sass.
364 *
365 * This method returns the error instance instead of
366 * throwing it.
367 *
368 * ```js
369 * if (!variables[name]) {
370 * throw decl.error(`Unknown variable ${name}`, { word: name })
371 * // CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
372 * // color: $black
373 * // a
374 * // ^
375 * // background: white
376 * }
377 * ```
378 *
379 * @param message Description for the error instance.
380 * @param options Options for the error instance.
381 *
382 * @return Error instance is returned.
383 */
384 error(message: string, options?: Node.NodeErrorOptions): CssSyntaxError
385
386 /**
387 * Returns the next child of the node’s parent.
388 * Returns `undefined` if the current node is the last child.
389 *
390 * ```js
391 * if (comment.text === 'delete next') {
392 * const next = comment.next()
393 * if (next) {
394 * next.remove()
395 * }
396 * }
397 * ```
398 *
399 * @return Next node.
400 */
401 next(): Node.ChildNode | undefined
402
403 /**
404 * Get the position for a word or an index inside the node.
405 *
406 * @param opts Options.
407 * @return Position.
408 */
409 positionBy(opts?: Pick<WarningOptions, 'index' | 'word'>): Node.Position
410
411 /**
412 * Convert string index to line/column.
413 *
414 * @param index The symbol number in the node’s string.
415 * @return Symbol position in file.
416 */
417 positionInside(index: number): Node.Position
418
419 /**
420 * Returns the previous child of the node’s parent.
421 * Returns `undefined` if the current node is the first child.
422 *
423 * ```js
424 * const annotation = decl.prev()
425 * if (annotation.type === 'comment') {
426 * readAnnotation(annotation.text)
427 * }
428 * ```
429 *
430 * @return Previous node.
431 */
432 prev(): Node.ChildNode | undefined
433
434 /**
435 * Get the range for a word or start and end index inside the node.
436 * The start index is inclusive; the end index is exclusive.
437 *
438 * @param opts Options.
439 * @return Range.
440 */
441 rangeBy(
442 opts?: Pick<WarningOptions, 'end' | 'endIndex' | 'index' | 'start' | 'word'>
443 ): Node.Range
444
445 /**
446 * Returns a `raws` value. If the node is missing
447 * the code style property (because the node was manually built or cloned),
448 * PostCSS will try to autodetect the code style property by looking
449 * at other nodes in the tree.
450 *
451 * ```js
452 * const root = postcss.parse('a { background: white }')
453 * root.nodes[0].append({ prop: 'color', value: 'black' })
454 * root.nodes[0].nodes[1].raws.before //=> undefined
455 * root.nodes[0].nodes[1].raw('before') //=> ' '
456 * ```
457 *
458 * @param prop Name of code style property.
459 * @param defaultType Name of default value, it can be missed
460 * if the value is the same as prop.
461 * @return {string} Code style value.
462 */
463 raw(prop: string, defaultType?: string): string
464
465 /**
466 * It removes the node from its parent and deletes its parent property.
467 *
468 * ```js
469 * if (decl.prop.match(/^-webkit-/)) {
470 * decl.remove()
471 * }
472 * ```
473 *
474 * @return `this` for method chaining.
475 */
476 remove(): this
477
478 /**
479 * Inserts node(s) before the current node and removes the current node.
480 *
481 * ```js
482 * AtRule: {
483 * mixin: atrule => {
484 * atrule.replaceWith(mixinRules[atrule.params])
485 * }
486 * }
487 * ```
488 *
489 * @param nodes Mode(s) to replace current one.
490 * @return Current node to methods chain.
491 */
492 replaceWith(...nodes: NewChild[]): this
493
494 /**
495 * Finds the Root instance of the node’s tree.
496 *
497 * ```js
498 * root.nodes[0].nodes[0].root() === root
499 * ```
500 *
501 * @return Root parent.
502 */
503 root(): Root
504
505 /**
506 * Fix circular links on `JSON.stringify()`.
507 *
508 * @return Cleaned object.
509 */
510 toJSON(): object
511
512 /**
513 * It compiles the node to browser readable cascading style sheets string
514 * depending on it's type.
515 *
516 * ```js
517 * new Rule({ selector: 'a' }).toString() //=> "a {}"
518 * ```
519 *
520 * @param stringifier A syntax to use in string generation.
521 * @return CSS string of this node.
522 */
523 toString(stringifier?: Stringifier | Syntax): string
524
525 /**
526 * It is a wrapper for {@link Result#warn}, providing convenient
527 * way of generating warnings.
528 *
529 * ```js
530 * Declaration: {
531 * bad: (decl, { result }) => {
532 * decl.warn(result, 'Deprecated property: bad')
533 * }
534 * }
535 * ```
536 *
537 * @param result The `Result` instance that will receive the warning.
538 * @param message Description for the warning.
539 * @param options Options for the warning.
540 *
541 * @return `Warning` instance is returned
542 */
543 warn(result: Result, message: string, options?: WarningOptions): Warning
544
545 /**
546 * If this node isn't already dirty, marks it and its ancestors as such. This
547 * indicates to the LazyResult processor that the {@link Root} has been
548 * modified by the current plugin and may need to be processed again by other
549 * plugins.
550 */
551 protected markDirty(): void
552}
553
554declare class Node extends Node_ {}
555
556export = Node
Note: See TracBrowser for help on using the repository browser.