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