XPath.createBy constructor

XPath.createBy({
  1. required Element element,
  2. required Element pageElement,
})

Implementation

factory XPath.createBy({
  required Element element,
  required Element pageElement,
}) {
  XPath xpath = XPath._(element);
  xpath._targetElement = element;

  final highLevelSet = _PathConst.highLevelSet;
  LinkedList<_ElementEntry> originalPath = LinkedList();
  originalPath.add(_ElementEntry(element));

  bool lookForTarget = true;
  element.visitAncestorElements((parent) {
    if (parent.widget is GestureDetector) {
      lookForTarget = false;
    }
    if (lookForTarget && highLevelSet.contains(parent.widget.runtimeType)) {
      xpath._targetElement = parent;
    }
    originalPath.add(_ElementEntry(parent));
    if (pageElement == parent) {
      return false;
    }
    return true;
  });

  LinkedList<PathNode> path = xpath._buildFromOriginal(xpath._targetElement, originalPath);
  xpath._shortPath(path);

  if (path.isNotEmpty) {
    path.first.isPage = true;
  }
  for (var node in path) {
    node.computeIndex();
  }
  xpath._path = path;

  return xpath;
}