Skip to content

Commit 4c65a10

Browse files
committed
fix: preserve semantic WebView containers
1 parent f3ac9ef commit 4c65a10

2 files changed

Lines changed: 41 additions & 15 deletions

File tree

‎src/daemon/snapshot-presentation/ios/web.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ test('does not infer text from a nested same-label descendant', () => {
104104
]);
105105
});
106106

107+
test('keeps a labelled WebKit container with additional semantic content', () => {
108+
const nodes: RawSnapshotNode[] = [
109+
{ index: 0, depth: 0, type: 'WebView', label: 'Example' },
110+
{ index: 1, depth: 1, parentIndex: 0, type: 'Other', label: 'News' },
111+
{ index: 2, depth: 2, parentIndex: 1, type: 'StaticText', label: 'News' },
112+
{ index: 3, depth: 2, parentIndex: 1, type: 'Link', label: 'Read more' },
113+
];
114+
115+
expect(presentIosInteractiveSnapshot(nodes).map((node) => [node.type, node.label])).toEqual([
116+
['WebView', 'Example'],
117+
['Other', 'News'],
118+
['Link', 'Read more'],
119+
]);
120+
});
121+
107122
test('does not infer semantic roles for matching wrappers outside a WebView', () => {
108123
const nodes: RawSnapshotNode[] = [
109124
{ index: 0, depth: 0, type: 'Application', label: 'Example' },

‎src/daemon/snapshot-presentation/ios/web.ts‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function collectIosWebSemanticPresentation(
1616
nodes: RawSnapshotNode[],
1717
context: SnapshotTreeRuleContext,
1818
): void {
19-
const repeatedStaticTextByParent = collectRepeatedStaticTextByParent(nodes);
19+
const childrenByParent = collectChildrenByParent(nodes);
2020

2121
for (const node of nodes) {
2222
if (normalizeType(node.type ?? '') === 'element(58)') {
@@ -26,7 +26,7 @@ export function collectIosWebSemanticPresentation(
2626
const presentation = classifyIosWebTextWrapper(
2727
node,
2828
context.sourceNodesByIndex,
29-
repeatedStaticTextByParent,
29+
childrenByParent,
3030
);
3131
if (presentation?.kind === 'suppress') {
3232
context.suppressedIndexes.add(node.index);
@@ -49,20 +49,35 @@ type IosWebTextWrapperPresentation =
4949
function classifyIosWebTextWrapper(
5050
node: RawSnapshotNode,
5151
byIndex: ReadonlyMap<number, RawSnapshotNode>,
52-
repeatedStaticTextByParent: Map<number, Set<string>>,
52+
childrenByParent: ReadonlyMap<number, RawSnapshotNode[]>,
5353
): IosWebTextWrapperPresentation | null {
5454
if (normalizeType(node.type ?? '') !== 'other') return null;
5555
const webView = findNearestAncestor(node, byIndex, isWebView);
5656
const label = node.label?.trim();
5757
if (!webView || !label) return null;
5858
if (isDocumentTitleWrapper(node, webView, label)) return { kind: 'suppress' };
59-
if (!repeatedStaticTextByParent.get(node.index)?.has(label)) return null;
59+
if (!isWebTextLeafWrapper(node, label, childrenByParent)) return null;
6060
return {
6161
kind: 'semantic',
6262
type: isHtmlHeadingLevel(node.value) ? 'Heading' : 'StaticText',
6363
};
6464
}
6565

66+
function isWebTextLeafWrapper(
67+
node: RawSnapshotNode,
68+
label: string,
69+
childrenByParent: ReadonlyMap<number, RawSnapshotNode[]>,
70+
): boolean {
71+
const children = childrenByParent.get(node.index);
72+
if (children?.length !== 1) return false;
73+
const text = children[0]!;
74+
return (
75+
normalizeType(text.type ?? '') === 'statictext' &&
76+
text.label?.trim() === label &&
77+
!childrenByParent.has(text.index)
78+
);
79+
}
80+
6681
function isDocumentTitleWrapper(
6782
node: RawSnapshotNode,
6883
webView: RawSnapshotNode,
@@ -75,19 +90,15 @@ function isDocumentTitleWrapper(
7590
);
7691
}
7792

78-
function collectRepeatedStaticTextByParent(nodes: RawSnapshotNode[]): Map<number, Set<string>> {
79-
const labelsByParent = new Map<number, Set<string>>();
93+
function collectChildrenByParent(nodes: RawSnapshotNode[]): Map<number, RawSnapshotNode[]> {
94+
const childrenByParent = new Map<number, RawSnapshotNode[]>();
8095
for (const node of nodes) {
81-
if (typeof node.parentIndex !== 'number' || normalizeType(node.type ?? '') !== 'statictext') {
82-
continue;
83-
}
84-
const label = node.label?.trim();
85-
if (!label) continue;
86-
const labels = labelsByParent.get(node.parentIndex) ?? new Set<string>();
87-
labels.add(label);
88-
labelsByParent.set(node.parentIndex, labels);
96+
if (typeof node.parentIndex !== 'number') continue;
97+
const children = childrenByParent.get(node.parentIndex) ?? [];
98+
children.push(node);
99+
childrenByParent.set(node.parentIndex, children);
89100
}
90-
return labelsByParent;
101+
return childrenByParent;
91102
}
92103

93104
function isWebView(node: RawSnapshotNode): boolean {

0 commit comments

Comments
 (0)