Skip to content

The open alternative to Salesforce, designed for AI.

779 B
Raw Blame
import { isObject } from '@sniptt/guards';
import { isDefined } from 'twenty-shared/utils';

import { type ElementLike } from '@/polyfills/dom/types/ElementLike';

export function* iterateElementSubtree(
  rootElement: ElementLike,
): Generator<ElementLike> {
  const pendingNodes: ElementLike[] = [rootElement];

  while (pendingNodes.length > 0) {
    const currentNode = pendingNodes.pop();

    if (!isDefined(currentNode)) {
      return;
    }

    yield currentNode;

    const childNodes = currentNode.childNodes;

    if (isDefined(childNodes)) {
      for (let index = childNodes.length - 1; index >= 0; index -= 1) {
        const childNode = childNodes[index];

        if (isObject(childNode)) {
          pendingNodes.push(childNode);
        }
      }
    }
  }
}