mirror of
https://github.com/OliveTin/OliveTin
synced 2025-12-18 12:05:32 +00:00
23 lines
488 B
JavaScript
23 lines
488 B
JavaScript
import { isRoot } from './typeGuards.mjs';
|
|
|
|
/** @import { Node } from 'postcss' */
|
|
|
|
export const STOP = 'STOP';
|
|
|
|
/**
|
|
* Iterates over each node up to the root node.
|
|
*
|
|
* @param {Node} node
|
|
* @param {(node: Node) => void | STOP} callback
|
|
* @returns {void}
|
|
*/
|
|
export default function eachNodeUpToRoot(node, callback) {
|
|
let currentNode = node.parent;
|
|
|
|
while (currentNode && !isRoot(currentNode)) {
|
|
if (callback(currentNode) === STOP) break;
|
|
|
|
currentNode = currentNode.parent;
|
|
}
|
|
}
|