Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/bundler/deps.ts>postcss.Container#each

A Bundler with the web in mind.
Go to Latest
method postcss.Container.prototype.each
Re-export
import { postcss } from "https://deno.land/x/bundler@0.6.2/deps.ts";
const { Container } = postcss;

Iterates through the container’s immediate children, calling callback for each child.

Returning false in the callback will break iteration.

This method only iterates through the container’s immediate children. If you need to recursively iterate through all the container’s descendant nodes, use Container#walk.

Unlike the for {}-cycle or Array#forEach this iterator is safe if you are mutating the array of child nodes during iteration. PostCSS will adjust the current index to match the mutations.

const root = postcss.parse('a { color: black; z-index: 1 }')
const rule = root.first

for (const decl of rule.nodes) {
  decl.cloneBefore({ prop: '-webkit-' + decl.prop })
  // Cycle will be infinite, because cloneBefore moves the current node
  // to the next index
}

rule.each(decl => {
  decl.cloneBefore({ prop: '-webkit-' + decl.prop })
  // Will be executed only for color and z-index
})

Parameters

callback: (node: ChildNode, index: number) => false | void

Iterator receives each node and index.

Returns

false | undefined

Returns false if iteration was broke.