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 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 | * Insert new node after current node to current node’s parent.
|
---|
239 | *
|
---|
240 | * Just alias for `node.parent.insertAfter(node, add)`.
|
---|
241 | *
|
---|
242 | * ```js
|
---|
243 | * decl.after('color: black')
|
---|
244 | * ```
|
---|
245 | *
|
---|
246 | * @param newNode New node.
|
---|
247 | * @return This node for methods chain.
|
---|
248 | */
|
---|
249 | after(newNode: Node | Node.ChildProps | Node[] | string | undefined): this
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * It assigns properties to an existing node instance.
|
---|
253 | *
|
---|
254 | * ```js
|
---|
255 | * decl.assign({ prop: 'word-wrap', value: 'break-word' })
|
---|
256 | * ```
|
---|
257 | *
|
---|
258 | * @param overrides New properties to override the node.
|
---|
259 | *
|
---|
260 | * @return `this` for method chaining.
|
---|
261 | */
|
---|
262 | assign(overrides: object): this
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Insert new node before current node to current node’s parent.
|
---|
266 | *
|
---|
267 | * Just alias for `node.parent.insertBefore(node, add)`.
|
---|
268 | *
|
---|
269 | * ```js
|
---|
270 | * decl.before('content: ""')
|
---|
271 | * ```
|
---|
272 | *
|
---|
273 | * @param newNode New node.
|
---|
274 | * @return This node for methods chain.
|
---|
275 | */
|
---|
276 | before(newNode: Node | Node.ChildProps | Node[] | string | undefined): this
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Clear the code style properties for the node and its children.
|
---|
280 | *
|
---|
281 | * ```js
|
---|
282 | * node.raws.before //=> ' '
|
---|
283 | * node.cleanRaws()
|
---|
284 | * node.raws.before //=> undefined
|
---|
285 | * ```
|
---|
286 | *
|
---|
287 | * @param keepBetween Keep the `raws.between` symbols.
|
---|
288 | */
|
---|
289 | cleanRaws(keepBetween?: boolean): void
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * It creates clone of an existing node, which includes all the properties
|
---|
293 | * and their values, that includes `raws` but not `type`.
|
---|
294 | *
|
---|
295 | * ```js
|
---|
296 | * decl.raws.before //=> "\n "
|
---|
297 | * const cloned = decl.clone({ prop: '-moz-' + decl.prop })
|
---|
298 | * cloned.raws.before //=> "\n "
|
---|
299 | * cloned.toString() //=> -moz-transform: scale(0)
|
---|
300 | * ```
|
---|
301 | *
|
---|
302 | * @param overrides New properties to override in the clone.
|
---|
303 | *
|
---|
304 | * @return Duplicate of the node instance.
|
---|
305 | */
|
---|
306 | clone(overrides?: object): Node
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Shortcut to clone the node and insert the resulting cloned node
|
---|
310 | * after the current node.
|
---|
311 | *
|
---|
312 | * @param overrides New properties to override in the clone.
|
---|
313 | * @return New node.
|
---|
314 | */
|
---|
315 | cloneAfter(overrides?: object): Node
|
---|
316 |
|
---|
317 | /**
|
---|
318 | * Shortcut to clone the node and insert the resulting cloned node
|
---|
319 | * before the current node.
|
---|
320 | *
|
---|
321 | * ```js
|
---|
322 | * decl.cloneBefore({ prop: '-moz-' + decl.prop })
|
---|
323 | * ```
|
---|
324 | *
|
---|
325 | * @param overrides Mew properties to override in the clone.
|
---|
326 | *
|
---|
327 | * @return New node
|
---|
328 | */
|
---|
329 | cloneBefore(overrides?: object): Node
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * It creates an instance of the class `CssSyntaxError` and parameters passed
|
---|
333 | * to this method are assigned to the error instance.
|
---|
334 | *
|
---|
335 | * The error instance will have description for the
|
---|
336 | * error, original position of the node in the
|
---|
337 | * source, showing line and column number.
|
---|
338 | *
|
---|
339 | * If any previous map is present, it would be used
|
---|
340 | * to get original position of the source.
|
---|
341 | *
|
---|
342 | * The Previous Map here is referred to the source map
|
---|
343 | * generated by previous compilation, example: Less,
|
---|
344 | * Stylus and Sass.
|
---|
345 | *
|
---|
346 | * This method returns the error instance instead of
|
---|
347 | * throwing it.
|
---|
348 | *
|
---|
349 | * ```js
|
---|
350 | * if (!variables[name]) {
|
---|
351 | * throw decl.error(`Unknown variable ${name}`, { word: name })
|
---|
352 | * // CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
|
---|
353 | * // color: $black
|
---|
354 | * // a
|
---|
355 | * // ^
|
---|
356 | * // background: white
|
---|
357 | * }
|
---|
358 | * ```
|
---|
359 | *
|
---|
360 | * @param message Description for the error instance.
|
---|
361 | * @param options Options for the error instance.
|
---|
362 | *
|
---|
363 | * @return Error instance is returned.
|
---|
364 | */
|
---|
365 | error(message: string, options?: Node.NodeErrorOptions): CssSyntaxError
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Returns the next child of the node’s parent.
|
---|
369 | * Returns `undefined` if the current node is the last child.
|
---|
370 | *
|
---|
371 | * ```js
|
---|
372 | * if (comment.text === 'delete next') {
|
---|
373 | * const next = comment.next()
|
---|
374 | * if (next) {
|
---|
375 | * next.remove()
|
---|
376 | * }
|
---|
377 | * }
|
---|
378 | * ```
|
---|
379 | *
|
---|
380 | * @return Next node.
|
---|
381 | */
|
---|
382 | next(): Node.ChildNode | undefined
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Get the position for a word or an index inside the node.
|
---|
386 | *
|
---|
387 | * @param opts Options.
|
---|
388 | * @return Position.
|
---|
389 | */
|
---|
390 | positionBy(opts?: Pick<WarningOptions, 'index' | 'word'>): Node.Position
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Convert string index to line/column.
|
---|
394 | *
|
---|
395 | * @param index The symbol number in the node’s string.
|
---|
396 | * @return Symbol position in file.
|
---|
397 | */
|
---|
398 | positionInside(index: number): Node.Position
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * Returns the previous child of the node’s parent.
|
---|
402 | * Returns `undefined` if the current node is the first child.
|
---|
403 | *
|
---|
404 | * ```js
|
---|
405 | * const annotation = decl.prev()
|
---|
406 | * if (annotation.type === 'comment') {
|
---|
407 | * readAnnotation(annotation.text)
|
---|
408 | * }
|
---|
409 | * ```
|
---|
410 | *
|
---|
411 | * @return Previous node.
|
---|
412 | */
|
---|
413 | prev(): Node.ChildNode | undefined
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * Get the range for a word or start and end index inside the node.
|
---|
417 | * The start index is inclusive; the end index is exclusive.
|
---|
418 | *
|
---|
419 | * @param opts Options.
|
---|
420 | * @return Range.
|
---|
421 | */
|
---|
422 | rangeBy(
|
---|
423 | opts?: Pick<WarningOptions, 'endIndex' | 'index' | 'word'>
|
---|
424 | ): Node.Range
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Returns a `raws` value. If the node is missing
|
---|
428 | * the code style property (because the node was manually built or cloned),
|
---|
429 | * PostCSS will try to autodetect the code style property by looking
|
---|
430 | * at other nodes in the tree.
|
---|
431 | *
|
---|
432 | * ```js
|
---|
433 | * const root = postcss.parse('a { background: white }')
|
---|
434 | * root.nodes[0].append({ prop: 'color', value: 'black' })
|
---|
435 | * root.nodes[0].nodes[1].raws.before //=> undefined
|
---|
436 | * root.nodes[0].nodes[1].raw('before') //=> ' '
|
---|
437 | * ```
|
---|
438 | *
|
---|
439 | * @param prop Name of code style property.
|
---|
440 | * @param defaultType Name of default value, it can be missed
|
---|
441 | * if the value is the same as prop.
|
---|
442 | * @return {string} Code style value.
|
---|
443 | */
|
---|
444 | raw(prop: string, defaultType?: string): string
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * It removes the node from its parent and deletes its parent property.
|
---|
448 | *
|
---|
449 | * ```js
|
---|
450 | * if (decl.prop.match(/^-webkit-/)) {
|
---|
451 | * decl.remove()
|
---|
452 | * }
|
---|
453 | * ```
|
---|
454 | *
|
---|
455 | * @return `this` for method chaining.
|
---|
456 | */
|
---|
457 | remove(): this
|
---|
458 |
|
---|
459 | /**
|
---|
460 | * Inserts node(s) before the current node and removes the current node.
|
---|
461 | *
|
---|
462 | * ```js
|
---|
463 | * AtRule: {
|
---|
464 | * mixin: atrule => {
|
---|
465 | * atrule.replaceWith(mixinRules[atrule.params])
|
---|
466 | * }
|
---|
467 | * }
|
---|
468 | * ```
|
---|
469 | *
|
---|
470 | * @param nodes Mode(s) to replace current one.
|
---|
471 | * @return Current node to methods chain.
|
---|
472 | */
|
---|
473 | replaceWith(
|
---|
474 | ...nodes: (
|
---|
475 | | Node.ChildNode
|
---|
476 | | Node.ChildNode[]
|
---|
477 | | Node.ChildProps
|
---|
478 | | Node.ChildProps[]
|
---|
479 | )[]
|
---|
480 | ): this
|
---|
481 |
|
---|
482 | /**
|
---|
483 | * Finds the Root instance of the node’s tree.
|
---|
484 | *
|
---|
485 | * ```js
|
---|
486 | * root.nodes[0].nodes[0].root() === root
|
---|
487 | * ```
|
---|
488 | *
|
---|
489 | * @return Root parent.
|
---|
490 | */
|
---|
491 | root(): Root
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * Fix circular links on `JSON.stringify()`.
|
---|
495 | *
|
---|
496 | * @return Cleaned object.
|
---|
497 | */
|
---|
498 | toJSON(): object
|
---|
499 |
|
---|
500 | /**
|
---|
501 | * It compiles the node to browser readable cascading style sheets string
|
---|
502 | * depending on it's type.
|
---|
503 | *
|
---|
504 | * ```js
|
---|
505 | * new Rule({ selector: 'a' }).toString() //=> "a {}"
|
---|
506 | * ```
|
---|
507 | *
|
---|
508 | * @param stringifier A syntax to use in string generation.
|
---|
509 | * @return CSS string of this node.
|
---|
510 | */
|
---|
511 | toString(stringifier?: Stringifier | Syntax): string
|
---|
512 |
|
---|
513 | /**
|
---|
514 | * It is a wrapper for {@link Result#warn}, providing convenient
|
---|
515 | * way of generating warnings.
|
---|
516 | *
|
---|
517 | * ```js
|
---|
518 | * Declaration: {
|
---|
519 | * bad: (decl, { result }) => {
|
---|
520 | * decl.warn(result, 'Deprecated property: bad')
|
---|
521 | * }
|
---|
522 | * }
|
---|
523 | * ```
|
---|
524 | *
|
---|
525 | * @param result The `Result` instance that will receive the warning.
|
---|
526 | * @param message Description for the warning.
|
---|
527 | * @param options Options for the warning.
|
---|
528 | *
|
---|
529 | * @return `Warning` instance is returned
|
---|
530 | */
|
---|
531 | warn(result: Result, message: string, options?: WarningOptions): Warning
|
---|
532 | }
|
---|
533 |
|
---|
534 | declare class Node extends Node_ {}
|
---|
535 |
|
---|
536 | export = Node
|
---|