container.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import Comment from './comment';
  2. import * as postcss from './postcss';
  3. import AtRule from './at-rule';
  4. import Node from './node';
  5. import Rule from './rule';
  6. /**
  7. * Containers can store any content. If you write a rule inside a rule,
  8. * PostCSS will parse it.
  9. */
  10. export default class Container extends Node implements postcss.Container {
  11. private indexes;
  12. private lastEach;
  13. /**
  14. * Contains the container's children.
  15. */
  16. nodes: Node[];
  17. /**
  18. * @param overrides New properties to override in the clone.
  19. * @returns A clone of this node. The node and its (cloned) children will
  20. * have a clean parent and code style properties.
  21. */
  22. clone(overrides?: Object): any;
  23. toJSON(): postcss.JsonContainer;
  24. push(child: any): this;
  25. /**
  26. * Iterates through the container's immediate children, calling the
  27. * callback function for each child. If you need to recursively iterate
  28. * through all the container's descendant nodes, use container.walk().
  29. * Unlike the for {} -cycle or Array#forEach() this iterator is safe if you
  30. * are mutating the array of child nodes during iteration.
  31. * @param callback Iterator. Returning false will break iteration. Safe
  32. * if you are mutating the array of child nodes during iteration. PostCSS
  33. * will adjust the current index to match the mutations.
  34. */
  35. each(callback: (node: Node, index: number) => any): boolean | void;
  36. /**
  37. * Traverses the container's descendant nodes, calling `callback` for each
  38. * node. Like container.each(), this method is safe to use if you are
  39. * mutating arrays during iteration. If you only need to iterate through
  40. * the container's immediate children, use container.each().
  41. * @param callback Iterator.
  42. */
  43. walk(callback: (node: Node, index: number) => any): boolean | void;
  44. /**
  45. * Traverses the container's descendant nodes, calling `callback` for each
  46. * declaration. Like container.each(), this method is safe to use if you
  47. * are mutating arrays during iteration.
  48. * @param propFilter Filters declarations by property name. Only those
  49. * declarations whose property matches propFilter will be iterated over.
  50. * @param callback Called for each declaration node within the container.
  51. */
  52. walkDecls(propFilter: string | RegExp, callback?: (decl: postcss.Declaration, index: number) => any): boolean | void;
  53. walkDecls(callback: (decl: postcss.Declaration, index: number) => any): boolean | void;
  54. /**
  55. * Traverses the container's descendant nodes, calling `callback` for each
  56. * rule. Like container.each(), this method is safe to use if you are
  57. * mutating arrays during iteration.
  58. * @param selectorFilter Filters rules by selector. If provided, iteration
  59. * will only happen over rules that have matching names.
  60. * @param callback Iterator called for each rule node within the
  61. * container.
  62. */
  63. walkRules(selectorFilter: string | RegExp, callback: (atRule: Rule, index: number) => any): boolean | void;
  64. walkRules(callback: (atRule: Rule, index: number) => any): boolean | void;
  65. /**
  66. * Traverses the container's descendant nodes, calling `callback` for each
  67. * at-rule. Like container.each(), this method is safe to use if you are
  68. * mutating arrays during iteration.
  69. * @param nameFilter Filters at-rules by name. If provided, iteration will
  70. * only happen over at-rules that have matching names.
  71. * @param callback Iterator called for each at-rule node within the
  72. * container.
  73. */
  74. walkAtRules(nameFilter: string | RegExp, callback: (atRule: AtRule, index: number) => any): boolean | void;
  75. walkAtRules(callback: (atRule: AtRule, index: number) => any): boolean | void;
  76. /**
  77. * Traverses the container's descendant nodes, calling `callback` for each
  78. * commennt. Like container.each(), this method is safe to use if you are
  79. * mutating arrays during iteration.
  80. * @param callback Iterator called for each comment node within the container.
  81. */
  82. walkComments(callback: (comment: Comment, indexed: number) => any): void | boolean;
  83. /**
  84. * Inserts new nodes to the end of the container.
  85. * Because each node class is identifiable by unique properties, use the
  86. * following shortcuts to create nodes in insert methods:
  87. * root.append({ name: '@charset', params: '"UTF-8"' }); // at-rule
  88. * root.append({ selector: 'a' }); // rule
  89. * rule.append({ prop: 'color', value: 'black' }); // declaration
  90. * rule.append({ text: 'Comment' }) // comment
  91. * A string containing the CSS of the new element can also be used. This
  92. * approach is slower than the above shortcuts.
  93. * root.append('a {}');
  94. * root.first.append('color: black; z-index: 1');
  95. * @param nodes New nodes.
  96. * @returns This container for chaining.
  97. */
  98. append(...nodes: (Node | Object | string)[]): this;
  99. /**
  100. * Inserts new nodes to the beginning of the container.
  101. * Because each node class is identifiable by unique properties, use the
  102. * following shortcuts to create nodes in insert methods:
  103. * root.prepend({ name: 'charset', params: '"UTF-8"' }); // at-rule
  104. * root.prepend({ selector: 'a' }); // rule
  105. * rule.prepend({ prop: 'color', value: 'black' }); // declaration
  106. * rule.prepend({ text: 'Comment' }) // comment
  107. * A string containing the CSS of the new element can also be used. This
  108. * approach is slower than the above shortcuts.
  109. * root.prepend('a {}');
  110. * root.first.prepend('color: black; z-index: 1');
  111. * @param nodes New nodes.
  112. * @returns This container for chaining.
  113. */
  114. prepend(...nodes: (Node | Object | string)[]): this;
  115. cleanRaws(keepBetween?: boolean): void;
  116. /**
  117. * Insert newNode before oldNode within the container.
  118. * @param oldNode Child or child's index.
  119. * @returns This container for chaining.
  120. */
  121. insertBefore(oldNode: Node | number, newNode: Node | Object | string): this;
  122. /**
  123. * Insert newNode after oldNode within the container.
  124. * @param oldNode Child or child's index.
  125. * @returns This container for chaining.
  126. */
  127. insertAfter(oldNode: Node | number, newNode: Node | Object | string): this;
  128. /**
  129. * Removes the container from its parent and cleans the parent property in the
  130. * container and its children.
  131. * @returns This container for chaining.
  132. */
  133. remove(): any;
  134. /**
  135. * Removes child from the container and clean the parent properties from the
  136. * node and its children.
  137. * @param child Child or child's index.
  138. * @returns This container for chaining.
  139. */
  140. removeChild(child: Node | number): this;
  141. /**
  142. * Removes all children from the container and cleans their parent
  143. * properties.
  144. * @returns This container for chaining.
  145. */
  146. removeAll(): this;
  147. /**
  148. * Passes all declaration values within the container that match pattern
  149. * through the callback, replacing those values with the returned result of
  150. * callback. This method is useful if you are using a custom unit or
  151. * function and need to iterate through all values.
  152. * @param pattern Pattern that we need to replace.
  153. * @param options Options to speed up the search.
  154. * @param callbackOrReplaceValue String to replace pattern or callback
  155. * that will return a new value. The callback will receive the same
  156. * arguments as those passed to a function parameter of String#replace.
  157. */
  158. replaceValues(pattern: string | RegExp, options: {
  159. /**
  160. * Property names. The method will only search for values that match
  161. * regexp within declarations of listed properties.
  162. */
  163. props?: string[];
  164. /**
  165. * Used to narrow down values and speed up the regexp search. Searching
  166. * every single value with a regexp can be slow. If you pass a fast
  167. * string, PostCSS will first check whether the value contains the fast
  168. * string; and only if it does will PostCSS check that value against
  169. * regexp. For example, instead of just checking for /\d+rem/ on all
  170. * values, set fast: 'rem' to first check whether a value has the rem
  171. * unit, and only if it does perform the regexp check.
  172. */
  173. fast?: string;
  174. }, callbackOrReplaceValue: string | {
  175. (substring: string, ...args: any[]): string;
  176. }): Container;
  177. replaceValues(pattern: string | RegExp, callbackOrReplaceValue: string | {
  178. (substring: string, ...args: any[]): string;
  179. }): Container;
  180. /**
  181. * Determines whether all child nodes satisfy the specified test.
  182. * @param callback A function that accepts up to three arguments. The
  183. * every method calls the callback function for each node until the
  184. * callback returns false, or until the end of the array.
  185. * @returns True if the callback returns true for all of the container's
  186. * children.
  187. */
  188. every(callback: (node: Node, index: number, nodes: Node[]) => any, thisArg?: any): boolean;
  189. /**
  190. * Determines whether the specified callback returns true for any child node.
  191. * @param callback A function that accepts up to three arguments. The some
  192. * method calls the callback for each node until the callback returns true,
  193. * or until the end of the array.
  194. * @param thisArg An object to which the this keyword can refer in the
  195. * callback function. If thisArg is omitted, undefined is used as the
  196. * this value.
  197. * @returns True if callback returns true for (at least) one of the
  198. * container's children.
  199. */
  200. some(callback: (node: Node, index: number, nodes: Node[]) => boolean, thisArg?: any): boolean;
  201. /**
  202. * @param child Child of the current container.
  203. * @returns The child's index within the container's "nodes" array.
  204. */
  205. index(child: Node | number): number;
  206. /**
  207. * @returns The container's first child.
  208. */
  209. first: Node;
  210. /**
  211. * @returns The container's last child.
  212. */
  213. last: Node;
  214. protected normalize(node: Node | string, sample?: Node, type?: string | boolean): Node[];
  215. protected normalize(props: postcss.AtRuleNewProps | postcss.RuleNewProps | postcss.DeclarationNewProps | postcss.CommentNewProps, sample?: Node, type?: string | boolean): Node[];
  216. rebuild(node: Node, parent?: Container): any;
  217. eachInside(callback: any): any;
  218. eachDecl(propFilter: any, callback?: any): any;
  219. eachRule(selectorFilter: any, callback?: any): any;
  220. eachAtRule(nameFilter: any, callback?: any): any;
  221. eachComment(selectorFilter: any, callback?: any): any;
  222. semicolon: boolean;
  223. after: string;
  224. }