parents property Null safety

List<String> parents

Gets a chain of parent elements, including the element itself.

Implementation

List<String> get parents {
  WebElement? p = this;
  final result = <String>[];
  while (p != null) {
    var id = p.id;
    if (_parentCache.containsKey(id)) {
      break;
    }
    result.add(id);
    _parentCache[id] = (p = p.parent)?.id;
  }

  if (p != null) {
    // Hit cache in the previous loop.
    String? id = p.id;
    while (id != null) {
      result.add(id);
      id = _parentCache[id];
    }
  }

  return result;
}