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