getParentElement function

Element? getParentElement(
  1. Element element, {
  2. bool validator(
    1. Element parent
    )?,
  3. int maxLevels = 1000,
})

Returns the parent of element applying validator and maxLevels.

Implementation

Element? getParentElement(Element element,
    {bool Function(Element parent)? validator, int maxLevels = 1000}) {
  if (maxLevels < 1) return null;

  for (var level = 1; level <= maxLevels; ++level) {
    var parent = element.parent;
    if (parent != null) {
      if (validator != null) {
        if (validator(parent)) {
          return parent;
        }
      } else {
        return parent;
      }

      element = parent;
    } else {
      break;
    }
  }

  return null;
}