1 | import Node, { ChildNode, NodeProps, ChildProps } from './node.js'
|
---|
2 | import Declaration from './declaration.js'
|
---|
3 | import Comment from './comment.js'
|
---|
4 | import AtRule from './at-rule.js'
|
---|
5 | import Rule from './rule.js'
|
---|
6 |
|
---|
7 | interface ValueOptions {
|
---|
8 | /**
|
---|
9 | * An array of property names.
|
---|
10 | */
|
---|
11 | props?: string[]
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * String that’s used to narrow down values and speed up the regexp search.
|
---|
15 | */
|
---|
16 | fast?: string
|
---|
17 | }
|
---|
18 |
|
---|
19 | export interface ContainerProps extends NodeProps {
|
---|
20 | nodes?: (ChildNode | ChildProps)[]
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * The `Root`, `AtRule`, and `Rule` container nodes
|
---|
25 | * inherit some common methods to help work with their children.
|
---|
26 | *
|
---|
27 | * Note that all containers can store any content. If you write a rule inside
|
---|
28 | * a rule, PostCSS will parse it.
|
---|
29 | */
|
---|
30 | export default abstract class Container<
|
---|
31 | Child extends Node = ChildNode
|
---|
32 | > extends Node {
|
---|
33 | /**
|
---|
34 | * An array containing the container’s children.
|
---|
35 | *
|
---|
36 | * ```js
|
---|
37 | * const root = postcss.parse('a { color: black }')
|
---|
38 | * root.nodes.length //=> 1
|
---|
39 | * root.nodes[0].selector //=> 'a'
|
---|
40 | * root.nodes[0].nodes[0].prop //=> 'color'
|
---|
41 | * ```
|
---|
42 | */
|
---|
43 | nodes: Child[]
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * The container’s first child.
|
---|
47 | *
|
---|
48 | * ```js
|
---|
49 | * rule.first === rules.nodes[0]
|
---|
50 | * ```
|
---|
51 | */
|
---|
52 | get first(): Child | undefined
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * The container’s last child.
|
---|
56 | *
|
---|
57 | * ```js
|
---|
58 | * rule.last === rule.nodes[rule.nodes.length - 1]
|
---|
59 | * ```
|
---|
60 | */
|
---|
61 | get last(): Child | undefined
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Iterates through the container’s immediate children,
|
---|
65 | * calling `callback` for each child.
|
---|
66 | *
|
---|
67 | * Returning `false` in the callback will break iteration.
|
---|
68 | *
|
---|
69 | * This method only iterates through the container’s immediate children.
|
---|
70 | * If you need to recursively iterate through all the container’s descendant
|
---|
71 | * nodes, use `Container#walk`.
|
---|
72 | *
|
---|
73 | * Unlike the for `{}`-cycle or `Array#forEach` this iterator is safe
|
---|
74 | * if you are mutating the array of child nodes during iteration.
|
---|
75 | * PostCSS will adjust the current index to match the mutations.
|
---|
76 | *
|
---|
77 | * ```js
|
---|
78 | * const root = postcss.parse('a { color: black; z-index: 1 }')
|
---|
79 | * const rule = root.first
|
---|
80 | *
|
---|
81 | * for (const decl of rule.nodes) {
|
---|
82 | * decl.cloneBefore({ prop: '-webkit-' + decl.prop })
|
---|
83 | * // Cycle will be infinite, because cloneBefore moves the current node
|
---|
84 | * // to the next index
|
---|
85 | * }
|
---|
86 | *
|
---|
87 | * rule.each(decl => {
|
---|
88 | * decl.cloneBefore({ prop: '-webkit-' + decl.prop })
|
---|
89 | * // Will be executed only for color and z-index
|
---|
90 | * })
|
---|
91 | * ```
|
---|
92 | *
|
---|
93 | * @param callback Iterator receives each node and index.
|
---|
94 | * @return Returns `false` if iteration was broke.
|
---|
95 | */
|
---|
96 | each(
|
---|
97 | callback: (node: Child, index: number) => false | void
|
---|
98 | ): false | undefined
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Traverses the container’s descendant nodes, calling callback
|
---|
102 | * for each node.
|
---|
103 | *
|
---|
104 | * Like container.each(), this method is safe to use
|
---|
105 | * if you are mutating arrays during iteration.
|
---|
106 | *
|
---|
107 | * If you only need to iterate through the container’s immediate children,
|
---|
108 | * use `Container#each`.
|
---|
109 | *
|
---|
110 | * ```js
|
---|
111 | * root.walk(node => {
|
---|
112 | * // Traverses all descendant nodes.
|
---|
113 | * })
|
---|
114 | * ```
|
---|
115 | *
|
---|
116 | * @param callback Iterator receives each node and index.
|
---|
117 | * @return Returns `false` if iteration was broke.
|
---|
118 | */
|
---|
119 | walk(
|
---|
120 | callback: (node: ChildNode, index: number) => false | void
|
---|
121 | ): false | undefined
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Traverses the container’s descendant nodes, calling callback
|
---|
125 | * for each declaration node.
|
---|
126 | *
|
---|
127 | * If you pass a filter, iteration will only happen over declarations
|
---|
128 | * with matching properties.
|
---|
129 | *
|
---|
130 | * ```js
|
---|
131 | * root.walkDecls(decl => {
|
---|
132 | * checkPropertySupport(decl.prop)
|
---|
133 | * })
|
---|
134 | *
|
---|
135 | * root.walkDecls('border-radius', decl => {
|
---|
136 | * decl.remove()
|
---|
137 | * })
|
---|
138 | *
|
---|
139 | * root.walkDecls(/^background/, decl => {
|
---|
140 | * decl.value = takeFirstColorFromGradient(decl.value)
|
---|
141 | * })
|
---|
142 | * ```
|
---|
143 | *
|
---|
144 | * Like `Container#each`, this method is safe
|
---|
145 | * to use if you are mutating arrays during iteration.
|
---|
146 | *
|
---|
147 | * @param prop String or regular expression to filter declarations
|
---|
148 | * by property name.
|
---|
149 | * @param callback Iterator receives each node and index.
|
---|
150 | * @return Returns `false` if iteration was broke.
|
---|
151 | */
|
---|
152 | walkDecls(
|
---|
153 | propFilter: string | RegExp,
|
---|
154 | callback: (decl: Declaration, index: number) => false | void
|
---|
155 | ): false | undefined
|
---|
156 | walkDecls(
|
---|
157 | callback: (decl: Declaration, index: number) => false | void
|
---|
158 | ): false | undefined
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Traverses the container’s descendant nodes, calling callback
|
---|
162 | * for each rule node.
|
---|
163 | *
|
---|
164 | * If you pass a filter, iteration will only happen over rules
|
---|
165 | * with matching selectors.
|
---|
166 | *
|
---|
167 | * Like `Container#each`, this method is safe
|
---|
168 | * to use if you are mutating arrays during iteration.
|
---|
169 | *
|
---|
170 | * ```js
|
---|
171 | * const selectors = []
|
---|
172 | * root.walkRules(rule => {
|
---|
173 | * selectors.push(rule.selector)
|
---|
174 | * })
|
---|
175 | * console.log(`Your CSS uses ${ selectors.length } selectors`)
|
---|
176 | * ```
|
---|
177 | *
|
---|
178 | * @param selector String or regular expression to filter rules by selector.
|
---|
179 | * @param callback Iterator receives each node and index.
|
---|
180 | * @return Returns `false` if iteration was broke.
|
---|
181 | */
|
---|
182 | walkRules(
|
---|
183 | selectorFilter: string | RegExp,
|
---|
184 | callback: (atRule: Rule, index: number) => false | void
|
---|
185 | ): false | undefined
|
---|
186 | walkRules(
|
---|
187 | callback: (atRule: Rule, index: number) => false | void
|
---|
188 | ): false | undefined
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Traverses the container’s descendant nodes, calling callback
|
---|
192 | * for each at-rule node.
|
---|
193 | *
|
---|
194 | * If you pass a filter, iteration will only happen over at-rules
|
---|
195 | * that have matching names.
|
---|
196 | *
|
---|
197 | * Like `Container#each`, this method is safe
|
---|
198 | * to use if you are mutating arrays during iteration.
|
---|
199 | *
|
---|
200 | * ```js
|
---|
201 | * root.walkAtRules(rule => {
|
---|
202 | * if (isOld(rule.name)) rule.remove()
|
---|
203 | * })
|
---|
204 | *
|
---|
205 | * let first = false
|
---|
206 | * root.walkAtRules('charset', rule => {
|
---|
207 | * if (!first) {
|
---|
208 | * first = true
|
---|
209 | * } else {
|
---|
210 | * rule.remove()
|
---|
211 | * }
|
---|
212 | * })
|
---|
213 | * ```
|
---|
214 | *
|
---|
215 | * @param name String or regular expression to filter at-rules by name.
|
---|
216 | * @param callback Iterator receives each node and index.
|
---|
217 | * @return Returns `false` if iteration was broke.
|
---|
218 | */
|
---|
219 | walkAtRules(
|
---|
220 | nameFilter: string | RegExp,
|
---|
221 | callback: (atRule: AtRule, index: number) => false | void
|
---|
222 | ): false | undefined
|
---|
223 | walkAtRules(
|
---|
224 | callback: (atRule: AtRule, index: number) => false | void
|
---|
225 | ): false | undefined
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Traverses the container’s descendant nodes, calling callback
|
---|
229 | * for each comment node.
|
---|
230 | *
|
---|
231 | * Like `Container#each`, this method is safe
|
---|
232 | * to use if you are mutating arrays during iteration.
|
---|
233 | *
|
---|
234 | * ```js
|
---|
235 | * root.walkComments(comment => {
|
---|
236 | * comment.remove()
|
---|
237 | * })
|
---|
238 | * ```
|
---|
239 | *
|
---|
240 | * @param callback Iterator receives each node and index.
|
---|
241 | * @return Returns `false` if iteration was broke.
|
---|
242 | */
|
---|
243 |
|
---|
244 | walkComments(
|
---|
245 | callback: (comment: Comment, indexed: number) => false | void
|
---|
246 | ): false | undefined
|
---|
247 | walkComments(
|
---|
248 | callback: (comment: Comment, indexed: number) => false | void
|
---|
249 | ): false | undefined
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Inserts new nodes to the end of the container.
|
---|
253 | *
|
---|
254 | * ```js
|
---|
255 | * const decl1 = new Declaration({ prop: 'color', value: 'black' })
|
---|
256 | * const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
|
---|
257 | * rule.append(decl1, decl2)
|
---|
258 | *
|
---|
259 | * root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
|
---|
260 | * root.append({ selector: 'a' }) // rule
|
---|
261 | * rule.append({ prop: 'color', value: 'black' }) // declaration
|
---|
262 | * rule.append({ text: 'Comment' }) // comment
|
---|
263 | *
|
---|
264 | * root.append('a {}')
|
---|
265 | * root.first.append('color: black; z-index: 1')
|
---|
266 | * ```
|
---|
267 | *
|
---|
268 | * @param nodes New nodes.
|
---|
269 | * @return This node for methods chain.
|
---|
270 | */
|
---|
271 | append(
|
---|
272 | ...nodes: (Node | Node[] | ChildProps | ChildProps[] | string | string[])[]
|
---|
273 | ): this
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Inserts new nodes to the start of the container.
|
---|
277 | *
|
---|
278 | * ```js
|
---|
279 | * const decl1 = new Declaration({ prop: 'color', value: 'black' })
|
---|
280 | * const decl2 = new Declaration({ prop: 'background-color', value: 'white' })
|
---|
281 | * rule.prepend(decl1, decl2)
|
---|
282 | *
|
---|
283 | * root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
|
---|
284 | * root.append({ selector: 'a' }) // rule
|
---|
285 | * rule.append({ prop: 'color', value: 'black' }) // declaration
|
---|
286 | * rule.append({ text: 'Comment' }) // comment
|
---|
287 | *
|
---|
288 | * root.append('a {}')
|
---|
289 | * root.first.append('color: black; z-index: 1')
|
---|
290 | * ```
|
---|
291 | *
|
---|
292 | * @param nodes New nodes.
|
---|
293 | * @return This node for methods chain.
|
---|
294 | */
|
---|
295 | prepend(
|
---|
296 | ...nodes: (Node | Node[] | ChildProps | ChildProps[] | string | string[])[]
|
---|
297 | ): this
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Add child to the end of the node.
|
---|
301 | *
|
---|
302 | * ```js
|
---|
303 | * rule.push(new Declaration({ prop: 'color', value: 'black' }))
|
---|
304 | * ```
|
---|
305 | *
|
---|
306 | * @param child New node.
|
---|
307 | * @return This node for methods chain.
|
---|
308 | */
|
---|
309 | push(child: Child): this
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Insert new node before old node within the container.
|
---|
313 | *
|
---|
314 | * ```js
|
---|
315 | * rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
|
---|
316 | * ```
|
---|
317 | *
|
---|
318 | * @param oldNode Child or child’s index.
|
---|
319 | * @param newNode New node.
|
---|
320 | * @return This node for methods chain.
|
---|
321 | */
|
---|
322 | insertBefore(
|
---|
323 | oldNode: Child | number,
|
---|
324 | newNode: Child | ChildProps | string | Child[] | ChildProps[] | string[]
|
---|
325 | ): this
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Insert new node after old node within the container.
|
---|
329 | *
|
---|
330 | * @param oldNode Child or child’s index.
|
---|
331 | * @param newNode New node.
|
---|
332 | * @return This node for methods chain.
|
---|
333 | */
|
---|
334 | insertAfter(
|
---|
335 | oldNode: Child | number,
|
---|
336 | newNode: Child | ChildProps | string | Child[] | ChildProps[] | string[]
|
---|
337 | ): this
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * Removes node from the container and cleans the parent properties
|
---|
341 | * from the node and its children.
|
---|
342 | *
|
---|
343 | * ```js
|
---|
344 | * rule.nodes.length //=> 5
|
---|
345 | * rule.removeChild(decl)
|
---|
346 | * rule.nodes.length //=> 4
|
---|
347 | * decl.parent //=> undefined
|
---|
348 | * ```
|
---|
349 | *
|
---|
350 | * @param child Child or child’s index.
|
---|
351 | * @return This node for methods chain.
|
---|
352 | */
|
---|
353 | removeChild(child: Child | number): this
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Removes all children from the container
|
---|
357 | * and cleans their parent properties.
|
---|
358 | *
|
---|
359 | * ```js
|
---|
360 | * rule.removeAll()
|
---|
361 | * rule.nodes.length //=> 0
|
---|
362 | * ```
|
---|
363 | *
|
---|
364 | * @return This node for methods chain.
|
---|
365 | */
|
---|
366 | removeAll(): this
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Passes all declaration values within the container that match pattern
|
---|
370 | * through callback, replacing those values with the returned result
|
---|
371 | * of callback.
|
---|
372 | *
|
---|
373 | * This method is useful if you are using a custom unit or function
|
---|
374 | * and need to iterate through all values.
|
---|
375 | *
|
---|
376 | * ```js
|
---|
377 | * root.replaceValues(/\d+rem/, { fast: 'rem' }, string => {
|
---|
378 | * return 15 * parseInt(string) + 'px'
|
---|
379 | * })
|
---|
380 | * ```
|
---|
381 | *
|
---|
382 | * @param pattern Replace pattern.
|
---|
383 | * @param {object} opts Options to speed up the search.
|
---|
384 | * @param callback String to replace pattern or callback
|
---|
385 | * that returns a new value. The callback
|
---|
386 | * will receive the same arguments
|
---|
387 | * as those passed to a function parameter
|
---|
388 | * of `String#replace`.
|
---|
389 | * @return This node for methods chain.
|
---|
390 | */
|
---|
391 | replaceValues(
|
---|
392 | pattern: string | RegExp,
|
---|
393 | options: ValueOptions,
|
---|
394 | replaced: string | { (substring: string, ...args: any[]): string }
|
---|
395 | ): this
|
---|
396 | replaceValues(
|
---|
397 | pattern: string | RegExp,
|
---|
398 | replaced: string | { (substring: string, ...args: any[]): string }
|
---|
399 | ): this
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * Returns `true` if callback returns `true`
|
---|
403 | * for all of the container’s children.
|
---|
404 | *
|
---|
405 | * ```js
|
---|
406 | * const noPrefixes = rule.every(i => i.prop[0] !== '-')
|
---|
407 | * ```
|
---|
408 | *
|
---|
409 | * @param condition Iterator returns true or false.
|
---|
410 | * @return Is every child pass condition.
|
---|
411 | */
|
---|
412 | every(
|
---|
413 | condition: (node: Child, index: number, nodes: Child[]) => boolean
|
---|
414 | ): boolean
|
---|
415 |
|
---|
416 | /**
|
---|
417 | * Returns `true` if callback returns `true` for (at least) one
|
---|
418 | * of the container’s children.
|
---|
419 | *
|
---|
420 | * ```js
|
---|
421 | * const hasPrefix = rule.some(i => i.prop[0] === '-')
|
---|
422 | * ```
|
---|
423 | *
|
---|
424 | * @param condition Iterator returns true or false.
|
---|
425 | * @return Is some child pass condition.
|
---|
426 | */
|
---|
427 | some(
|
---|
428 | condition: (node: Child, index: number, nodes: Child[]) => boolean
|
---|
429 | ): boolean
|
---|
430 |
|
---|
431 | /**
|
---|
432 | * Returns a `child`’s index within the `Container#nodes` array.
|
---|
433 | *
|
---|
434 | * ```js
|
---|
435 | * rule.index( rule.nodes[2] ) //=> 2
|
---|
436 | * ```
|
---|
437 | *
|
---|
438 | * @param child Child of the current container.
|
---|
439 | * @return Child index.
|
---|
440 | */
|
---|
441 | index(child: Child | number): number
|
---|
442 | }
|
---|