eachNodeUpToRoot.mjs 488 B

12345678910111213141516171819202122
  1. import { isRoot } from './typeGuards.mjs';
  2. /** @import { Node } from 'postcss' */
  3. export const STOP = 'STOP';
  4. /**
  5. * Iterates over each node up to the root node.
  6. *
  7. * @param {Node} node
  8. * @param {(node: Node) => void | STOP} callback
  9. * @returns {void}
  10. */
  11. export default function eachNodeUpToRoot(node, callback) {
  12. let currentNode = node.parent;
  13. while (currentNode && !isRoot(currentNode)) {
  14. if (callback(currentNode) === STOP) break;
  15. currentNode = currentNode.parent;
  16. }
  17. }