1 | import Declaration, { DeclarationProps } from './declaration.js'
|
2 | import Comment, { CommentProps } from './comment.js'
|
3 | import { Stringifier, Syntax } from './postcss.js'
|
4 | import AtRule, { AtRuleProps } from './at-rule.js'
|
5 | import Rule, { RuleProps } from './rule.js'
|
6 | import { WarningOptions } from './warning.js'
|
7 | import CssSyntaxError from './css-syntax-error.js'
|
8 | import Container from './container.js'
|
9 | import Result from './result.js'
|
10 | import Input from './input.js'
|
11 | import Root from './root.js'
|
12 |
|
13 | export type ChildNode = AtRule | Rule | Declaration | Comment
|
14 |
|
15 | export type AnyNode = AtRule | Rule | Declaration | Comment | Root
|
16 |
|
17 | export type ChildProps =
|
18 | | AtRuleProps
|
19 | | RuleProps
|
20 | | DeclarationProps
|
21 | | CommentProps
|
22 |
|
23 | export interface Position {
|
24 | |
25 |
|
26 |
|
27 | offset: number
|
28 |
|
29 | |
30 |
|
31 |
|
32 | column: number
|
33 |
|
34 | |
35 |
|
36 |
|
37 | line: number
|
38 | }
|
39 |
|
40 | export interface Source {
|
41 | |
42 |
|
43 |
|
44 | input: Input
|
45 | |
46 |
|
47 |
|
48 | start?: Position
|
49 | |
50 |
|
51 |
|
52 | end?: Position
|
53 | }
|
54 |
|
55 | export interface NodeProps {
|
56 | source?: Source
|
57 | }
|
58 |
|
59 | interface NodeErrorOptions {
|
60 | |
61 |
|
62 |
|
63 | plugin?: string
|
64 | |
65 |
|
66 |
|
67 |
|
68 | word?: string
|
69 | |
70 |
|
71 |
|
72 |
|
73 | index?: number
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | export default abstract class Node {
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | type: string
|
92 |
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | parent: Container | undefined
|
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 | source?: Source
|
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 | raws: any
|
174 |
|
175 | |
176 |
|
177 |
|
178 | constructor (defaults?: object)
|
179 |
|
180 | /**
|
181 | * Returns a `CssSyntaxError` instance containing the original position
|
182 | * of the node in the source, showing line and column numbers and also
|
183 | * a small excerpt to facilitate debugging.
|
184 | *
|
185 | * If present, an input source map will be used to get the original position
|
186 | * of the source, even from a previous compilation step
|
187 | * (e.g., from Sass compilation).
|
188 | *
|
189 | * This method produces very useful error messages.
|
190 | *
|
191 | * ```js
|
192 | * if (!variables[name]) {
|
193 | * throw decl.error(`Unknown variable ${name}`, { word: name })
|
194 | *
|
195 | *
|
196 | *
|
197 | *
|
198 | *
|
199 | * }
|
200 | * ```
|
201 | *
|
202 | * @param message Error description.
|
203 | * @param opts Options.
|
204 | *
|
205 | * @return Error object to throw it.
|
206 | */
|
207 | error (message: string, options?: NodeErrorOptions): CssSyntaxError
|
208 |
|
209 | /**
|
210 | * This method is provided as a convenience wrapper for `Result#warn`.
|
211 | *
|
212 | * ```js
|
213 | * Declaration: {
|
214 | * bad: (decl, { result }) => {
|
215 | * decl.warn(result, 'Deprecated property bad')
|
216 | * }
|
217 | * }
|
218 | * ```
|
219 | *
|
220 | * @param result The `Result` instance that will receive the warning.
|
221 | * @param text Warning message.
|
222 | * @param opts Warning Options.
|
223 | *
|
224 | * @return Created warning object.
|
225 | */
|
226 | warn (result: Result, text: string, opts?: WarningOptions): void
|
227 |
|
228 | /**
|
229 | * Removes the node from its parent and cleans the parent properties
|
230 | * from the node and its children.
|
231 | *
|
232 | * ```js
|
233 | * if (decl.prop.match(/^-webkit-/)) {
|
234 | * decl.remove()
|
235 | * }
|
236 | * ```
|
237 | *
|
238 | * @return Node to make calls chain.
|
239 | */
|
240 | remove (): this
|
241 |
|
242 | /**
|
243 | * Returns a CSS string representing the node.
|
244 | *
|
245 | * ```js
|
246 | * new Rule({ selector: 'a' }).toString()
|
247 | * ```
|
248 | *
|
249 | * @param stringifier A syntax to use in string generation.
|
250 | * @return CSS string of this node.
|
251 | */
|
252 | toString (stringifier?: Stringifier | Syntax): string
|
253 |
|
254 | /**
|
255 | * Returns an exact clone of the node.
|
256 | *
|
257 | * The resulting cloned node and its (cloned) children will retain
|
258 | * code style properties.
|
259 | *
|
260 | * ```js
|
261 | * decl.raws.before
|
262 | * const cloned = decl.clone({ prop: '-moz-' + decl.prop })
|
263 | * cloned.raws.before
|
264 | * cloned.toString()
|
265 | * ```
|
266 | *
|
267 | * @param overrides New properties to override in the clone.
|
268 | * @return Clone of the node.
|
269 | */
|
270 | clone (overrides?: object): this
|
271 |
|
272 | /**
|
273 | * Shortcut to clone the node and insert the resulting cloned node
|
274 | * before the current node.
|
275 | *
|
276 | * ```js
|
277 | * decl.cloneBefore({ prop: '-moz-' + decl.prop })
|
278 | * ```
|
279 | *
|
280 | * @param overrides Mew properties to override in the clone.
|
281 | *
|
282 | * @return New node
|
283 | */
|
284 | cloneBefore (overrides?: object): this
|
285 |
|
286 | /**
|
287 | * Shortcut to clone the node and insert the resulting cloned node
|
288 | * after the current node.
|
289 | *
|
290 | * @param overrides New properties to override in the clone.
|
291 | * @return New node.
|
292 | */
|
293 | cloneAfter (overrides?: object): this
|
294 |
|
295 | /**
|
296 | * Inserts node(s) before the current node and removes the current node.
|
297 | *
|
298 | * ```js
|
299 | * AtRule: {
|
300 | * mixin: atrule => {
|
301 | * atrule.replaceWith(mixinRules[atrule.params])
|
302 | * }
|
303 | * }
|
304 | * ```
|
305 | *
|
306 | * @param nodes Mode(s) to replace current one.
|
307 | * @return Current node to methods chain.
|
308 | */
|
309 | replaceWith (
|
310 | ...nodes: (ChildNode | ChildProps | ChildNode[] | ChildProps[])[]
|
311 | ): this
|
312 |
|
313 | /**
|
314 | * Returns the next child of the node’s parent.
|
315 | * Returns `undefined` if the current node is the last child.
|
316 | *
|
317 | * ```js
|
318 | * if (comment.text === 'delete next') {
|
319 | * const next = comment.next()
|
320 | * if (next) {
|
321 | * next.remove()
|
322 | * }
|
323 | * }
|
324 | * ```
|
325 | *
|
326 | * @return Next node.
|
327 | */
|
328 | next (): ChildNode | undefined
|
329 |
|
330 | /**
|
331 | * Returns the previous child of the node’s parent.
|
332 | * Returns `undefined` if the current node is the first child.
|
333 | *
|
334 | * ```js
|
335 | * const annotation = decl.prev()
|
336 | * if (annotation.type === 'comment') {
|
337 | * readAnnotation(annotation.text)
|
338 | * }
|
339 | * ```
|
340 | *
|
341 | * @return Previous node.
|
342 | */
|
343 | prev (): ChildNode | undefined
|
344 |
|
345 | /**
|
346 | * Insert new node before current node to current node’s parent.
|
347 | *
|
348 | * Just alias for `node.parent.insertBefore(node, add)`.
|
349 | *
|
350 | * ```js
|
351 | * decl.before('content: ""')
|
352 | * ```
|
353 | *
|
354 | * @param newNode New node.
|
355 | * @return This node for methods chain.
|
356 | */
|
357 | before (newNode: Node | ChildProps | string | Node[]): this
|
358 |
|
359 | /**
|
360 | * Insert new node after current node to current node’s parent.
|
361 | *
|
362 | * Just alias for `node.parent.insertAfter(node, add)`.
|
363 | *
|
364 | * ```js
|
365 | * decl.after('color: black')
|
366 | * ```
|
367 | *
|
368 | * @param newNode New node.
|
369 | * @return This node for methods chain.
|
370 | */
|
371 | after (newNode: Node | ChildProps | string | Node[]): this
|
372 |
|
373 | /**
|
374 | * Finds the Root instance of the node’s tree.
|
375 | *
|
376 | * ```js
|
377 | * root.nodes[0].nodes[0].root() === root
|
378 | * ```
|
379 | *
|
380 | * @return Root parent.
|
381 | */
|
382 | root (): Root
|
383 |
|
384 | /**
|
385 | * Returns a `Node#raws` value. If the node is missing
|
386 | * the code style property (because the node was manually built or cloned),
|
387 | * PostCSS will try to autodetect the code style property by looking
|
388 | * at other nodes in the tree.
|
389 | *
|
390 | * ```js
|
391 | * const root = postcss.parse('a { background: white }')
|
392 | * root.nodes[0].append({ prop: 'color', value: 'black' })
|
393 | * root.nodes[0].nodes[1].raws.before
|
394 | * root.nodes[0].nodes[1].raw('before')
|
395 | * ```
|
396 | *
|
397 | * @param prop Name of code style property.
|
398 | * @param defaultType Name of default value, it can be missed
|
399 | * if the value is the same as prop.
|
400 | * @return {string} Code style value.
|
401 | */
|
402 | raw (prop: string, defaultType?: string): string
|
403 |
|
404 | /**
|
405 | * Clear the code style properties for the node and its children.
|
406 | *
|
407 | * ```js
|
408 | * node.raws.before
|
409 | * node.cleanRaws()
|
410 | * node.raws.before
|
411 | * ```
|
412 | *
|
413 | * @param keepBetween Keep the `raws.between` symbols.
|
414 | */
|
415 | cleanRaws (keepBetween?: boolean): void
|
416 |
|
417 | /**
|
418 | * Fix circular links on `JSON.stringify()`.
|
419 | *
|
420 | * @return Cleaned object.
|
421 | */
|
422 | toJSON (): object
|
423 |
|
424 | /**
|
425 | * Convert string index to line/column.
|
426 | *
|
427 | * @param index The symbol number in the node’s string.
|
428 | * @return Symbol position in file.
|
429 | */
|
430 | positionInside (index: number): Position
|
431 | }
|
432 |
|
\ | No newline at end of file |