mapObservationLocatorTree function
List<UiElement>
mapObservationLocatorTree({
- required List<
UiElement> elements, - required ObserverLocatorResolver resolve,
Applies locator resolution to a tree while consistently deriving scopes and sibling occurrences for both live and diagnostic observations.
Implementation
List<UiElement> mapObservationLocatorTree({
required List<UiElement> elements,
required ObserverLocatorResolver resolve,
}) {
return RunnerBenchmark.sync('observer', 'mapObservationLocatorTree', () {
UiElement visit(
UiElement element, {
ElementLocator? parentScope,
int? iconOccurrenceAmongSiblings,
int? iconSiblingCount,
int? occurrenceAmongSiblings,
}) {
final result = resolve(
element: element,
parentScope: parentScope,
iconOccurrenceAmongSiblings: iconOccurrenceAmongSiblings,
iconSiblingCount: iconSiblingCount,
occurrenceAmongSiblings: occurrenceAmongSiblings,
);
// Inert cards still scope nested icons via caption+role (no card sel).
final scope =
result.locator ?? containerScopeLocator(element) ?? parentScope;
final iconCount = element.children
.where((child) => (child.type ?? '').toLowerCase() == 'icon')
.length;
final siblingCandidates = [
for (final child in element.children)
cheapSuggestedLocator(child, parentScope: scope),
];
final siblingGroups = <String, List<int>>{};
for (var i = 0; i < siblingCandidates.length; i++) {
final candidate = siblingCandidates[i];
if (candidate == null) continue;
siblingGroups
.putIfAbsent(candidate.toJson().toString(), () => <int>[])
.add(i);
}
final occurrences = <int, int>{};
for (final indexes in siblingGroups.values) {
if (indexes.length < 2) continue;
for (var occurrence = 0; occurrence < indexes.length; occurrence++) {
occurrences[indexes[occurrence]] = occurrence;
}
}
final children = <UiElement>[];
var iconIndex = 0;
for (var i = 0; i < element.children.length; i++) {
final child = element.children[i];
final isIcon = (child.type ?? '').toLowerCase() == 'icon';
children.add(
visit(
child,
parentScope: scope,
iconOccurrenceAmongSiblings:
isIcon && iconCount > 1 ? iconIndex : null,
iconSiblingCount: isIcon ? iconCount : null,
occurrenceAmongSiblings: occurrences[i],
),
);
if (isIcon) iconIndex++;
}
return element.copyWith(
children: children,
suggestedLocator: result.locator,
clearSuggestedLocator: true,
locatorWarning: result.warning,
clearLocatorWarning: true,
);
}
return [for (final element in elements) visit(element)];
});
}