AK-21/Graphite-Industrial-Intelligence
0
1import AtRule from './at-rule.js'2import Comment from './comment.js'3import Declaration from './declaration.js'4import Node, { ChildNode, ChildProps, NodeProps } from './node.js'5import { Root } from './postcss.js'6import Rule from './rule.js'7 8declare 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?: string18 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 new31 * child node.32 */33 export type NewChild =34 | ChildProps35 | Node36 | readonly ChildProps[]37 | readonly Node[]38 | readonly string[]39 | string40 | undefined41 42 export { Container_ as default }43}44 45/**46 * The `Root`, `AtRule`, and `Rule` container nodes47 * 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 inside50 * a rule, PostCSS will parse it.51 */52declare abstract class Container_<Child extends Node = ChildNode> extends Node {53 /**54 * An array containing the container’s children.55 *56 * ```js57 * const root = postcss.parse('a { color: black }')58 * root.nodes.length //=> 159 * root.nodes[0].selector //=> 'a'60 * root.nodes[0].nodes[0].prop //=> 'color'61 * ```62 */63 nodes: Child[] | undefined64 65 /**66 * The container’s first child.67 *68 * ```js69 * rule.first === rules.nodes[0]70 * ```71 */72 get first(): Child | undefined73 74 /**75 * The container’s last child.76 *77 * ```js78 * rule.last === rule.nodes[rule.nodes.length - 1]79 * ```80 */81 get last(): Child | undefined82 /**83 * Inserts new nodes to the end of the container.84 *85 * ```js86 * 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-rule91 * root.append({ selector: 'a' }) // rule92 * rule.append({ prop: 'color', value: 'black' }) // declaration93 * rule.append({ text: 'Comment' }) // comment94 *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[]): this103 assign(overrides: Container.ContainerProps | object): this104 clone(overrides?: Partial<Container.ContainerProps>): this105 106 cloneAfter(overrides?: Partial<Container.ContainerProps>): this107 108 cloneBefore(overrides?: Partial<Container.ContainerProps>): this109 /**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 descendant117 * nodes, use `Container#walk`.118 *119 * Unlike the for `{}`-cycle or `Array#forEach` this iterator is safe120 * if you are mutating the array of child nodes during iteration.121 * PostCSS will adjust the current index to match the mutations.122 *123 * ```js124 * const root = postcss.parse('a { color: black; z-index: 1 }')125 * const rule = root.first126 *127 * for (const decl of rule.nodes) {128 * decl.cloneBefore({ prop: '-webkit-' + decl.prop })129 * // Cycle will be infinite, because cloneBefore moves the current node130 * // to the next index131 * }132 *133 * rule.each(decl => {134 * decl.cloneBefore({ prop: '-webkit-' + decl.prop })135 * // Will be executed only for color and z-index136 * })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 | void144 ): false | undefined145 146 /**147 * Returns `true` if callback returns `true`148 * for all of the container’s children.149 *150 * ```js151 * 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[]) => boolean159 ): boolean160 /**161 * Returns a `child`’s index within the `Container#nodes` array.162 *163 * ```js164 * rule.index( rule.nodes[2] ) //=> 2165 * ```166 *167 * @param child Child of the current container.168 * @return Child index.169 */170 index(child: Child | number): number171 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): this180 181 /**182 * Traverses the container’s descendant nodes, calling callback183 * for each comment node.184 *185 * Like `Container#each`, this method is safe186 * to use if you are mutating arrays during iteration.187 *188 * ```js189 * 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 * ```js202 * 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): this210 /**211 * Inserts new nodes to the start of the container.212 *213 * ```js214 * 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-rule219 * root.append({ selector: 'a' }) // rule220 * rule.append({ prop: 'color', value: 'black' }) // declaration221 * rule.append({ text: 'Comment' }) // comment222 *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[]): this231 232 /**233 * Add child to the end of the node.234 *235 * ```js236 * 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): this243 244 /**245 * Removes all children from the container246 * and cleans their parent properties.247 *248 * ```js249 * rule.removeAll()250 * rule.nodes.length //=> 0251 * ```252 *253 * @return This node for methods chain.254 */255 removeAll(): this256 257 /**258 * Removes node from the container and cleans the parent properties259 * from the node and its children.260 *261 * ```js262 * rule.nodes.length //=> 5263 * rule.removeChild(decl)264 * rule.nodes.length //=> 4265 * decl.parent //=> undefined266 * ```267 *268 * @param child Child or child’s index.269 * @return This node for methods chain.270 */271 removeChild(child: Child | number): this272 273 replaceValues(274 pattern: RegExp | string,275 replaced: { (substring: string, ...args: any[]): string } | string276 ): this277 /**278 * Passes all declaration values within the container that match pattern279 * through callback, replacing those values with the returned result280 * of callback.281 *282 * This method is useful if you are using a custom unit or function283 * and need to iterate through all values.284 *285 * ```js286 * 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 callback294 * that returns a new value. The callback295 * will receive the same arguments296 * as those passed to a function parameter297 * 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 } | string304 ): this305 306 /**307 * Returns `true` if callback returns `true` for (at least) one308 * of the container’s children.309 *310 * ```js311 * 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[]) => boolean319 ): boolean320 321 /**322 * Traverses the container’s descendant nodes, calling callback323 * for each node.324 *325 * Like container.each(), this method is safe to use326 * 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 * ```js332 * 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 | void342 ): false | undefined343 344 /**345 * Traverses the container’s descendant nodes, calling callback346 * for each at-rule node.347 *348 * If you pass a filter, iteration will only happen over at-rules349 * that have matching names.350 *351 * Like `Container#each`, this method is safe352 * to use if you are mutating arrays during iteration.353 *354 * ```js355 * root.walkAtRules(rule => {356 * if (isOld(rule.name)) rule.remove()357 * })358 *359 * let first = false360 * root.walkAtRules('charset', rule => {361 * if (!first) {362 * first = true363 * } 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 | void376 ): false | undefined377 walkAtRules(378 callback: (atRule: AtRule, index: number) => false | void379 ): false | undefined380 381 walkComments(382 callback: (comment: Comment, indexed: number) => false | void383 ): false | undefined384 walkComments(385 callback: (comment: Comment, indexed: number) => false | void386 ): false | undefined387 388 /**389 * Traverses the container’s descendant nodes, calling callback390 * for each declaration node.391 *392 * If you pass a filter, iteration will only happen over declarations393 * with matching properties.394 *395 * ```js396 * 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 safe410 * to use if you are mutating arrays during iteration.411 *412 * @param prop String or regular expression to filter declarations413 * 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 | void420 ): false | undefined421 walkDecls(422 callback: (decl: Declaration, index: number) => false | void423 ): false | undefined424 /**425 * Traverses the container’s descendant nodes, calling callback426 * for each rule node.427 *428 * If you pass a filter, iteration will only happen over rules429 * with matching selectors.430 *431 * Like `Container#each`, this method is safe432 * to use if you are mutating arrays during iteration.433 *434 * ```js435 * 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 | void449 ): false | undefined450 walkRules(451 callback: (rule: Rule, index: number) => false | void452 ): false | undefined453 /**454 * An internal method that converts a {@link NewChild} into a list of actual455 * 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 use458 * 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 be462 * taken.463 * @param type This should be set to `'prepend'` if the new nodes will be464 * inserted at the beginning of the container.465 * @hidden466 */467 protected normalize(468 nodes: Container.NewChild,469 sample: Node | undefined,470 type?: 'prepend' | false471 ): Child[]472}473 474declare class Container<475 Child extends Node = ChildNode476> extends Container_<Child> {}477 478export = Container479 