container.d.ts 14 KB

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