container.d.ts 13 KB

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