Skip to content

Commit f3ac9ef

Browse files
committed
refactor: tighten iOS snapshot presentation rules
1 parent d8092e4 commit f3ac9ef

7 files changed

Lines changed: 89 additions & 50 deletions

File tree

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ export function collectIosImplicitScrollableActions(
1212
nodes: RawSnapshotNode[],
1313
context: SnapshotTreeRuleContext,
1414
): void {
15-
const byIndex = new Map(nodes.map((node) => [node.index, node]));
16-
const viewport = findLargestViewportRect(byIndex.values());
15+
const viewport = findLargestViewportRect(context.sourceNodesByIndex.values());
1716
for (const node of nodes) {
18-
if (!isImplicitScrollableAction(node, byIndex, viewport)) {
17+
if (!isImplicitScrollableAction(node, context.sourceNodesByIndex, viewport)) {
1918
continue;
2019
}
2120
mergeReplacement(context.replacements, node, { type: 'Cell' });
@@ -24,7 +23,7 @@ export function collectIosImplicitScrollableActions(
2423

2524
function isImplicitScrollableAction(
2625
node: RawSnapshotNode,
27-
byIndex: Map<number, RawSnapshotNode>,
26+
byIndex: ReadonlyMap<number, RawSnapshotNode>,
2827
viewport: RawSnapshotNode['rect'],
2928
): boolean {
3029
if (normalizeType(node.type ?? '') !== 'other') {

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
const IOS_PRESENTATION_RULES: Array<
1212
(nodes: RawSnapshotNode[], context: SnapshotTreeRuleContext) => void
1313
> = [
14+
// Semantic representatives must be collected before duplicate-label suppression.
1415
collectIosWebSemanticPresentation,
1516
collectIosPresentationNoiseSuppression,
1617
collectIosImplicitScrollableActions,
@@ -34,14 +35,22 @@ export function buildIosInteractiveSnapshotPresentation(
3435
}
3536

3637
const replacements = new Map<number, RawSnapshotNode>();
38+
const semanticRepresentativeIndexes = new Set<number>();
39+
const sourceNodesByIndex = new Map(nodes.map((node) => [node.index, node]));
3740
const suppressedIndexes = new Set<number>();
41+
const ruleContext: SnapshotTreeRuleContext = {
42+
replacements,
43+
semanticRepresentativeIndexes,
44+
sourceNodesByIndex,
45+
suppressedIndexes,
46+
};
3847

3948
for (const rule of IOS_PRESENTATION_RULES) {
40-
rule(nodes, { replacements, suppressedIndexes });
49+
rule(nodes, ruleContext);
4150
}
4251

4352
if (suppressedIndexes.size === 0 && replacements.size === 0) {
44-
return { nodes, sourceNodes: new Map(nodes.map((node) => [node.index, node])) };
53+
return { nodes, sourceNodes: sourceNodesByIndex };
4554
}
4655

4756
const presentedSourceNodes = nodes

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

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ export function collectIosPresentationNoiseSuppression(
2525
context: SnapshotTreeRuleContext,
2626
): void {
2727
const { suppressedIndexes } = context;
28-
collectIosOffscreenKeyboardSuppression(nodes, suppressedIndexes);
28+
collectIosOffscreenKeyboardSuppression(nodes, context.sourceNodesByIndex, suppressedIndexes);
2929
collectIosStructuralIdentifierSuppression(nodes, suppressedIndexes);
3030
collectIosScrollIndicatorPresentation(nodes, context);
31-
collectIosSearchToolbarSuppression(nodes, suppressedIndexes);
31+
collectIosSearchToolbarSuppression(nodes, context.sourceNodesByIndex, suppressedIndexes);
3232
collectIosActionWrapperSuppression(nodes, suppressedIndexes);
3333
collectIosReactNativeOverlayActionPresentation(nodes, context.replacements);
3434
collectIosReactNativeOverlayWrapperSuppression(nodes, suppressedIndexes);
35-
collectIosRepeatedStaticSuppression(nodes, suppressedIndexes, context.replacements);
35+
collectIosRepeatedStaticSuppression(
36+
nodes,
37+
suppressedIndexes,
38+
context.semanticRepresentativeIndexes,
39+
);
3640
}
3741

3842
function collectIosReactNativeOverlayActionPresentation(
@@ -133,7 +137,7 @@ function collectDescendantNodes(nodes: RawSnapshotNode[], position: number): Raw
133137
function collectIosRepeatedStaticSuppression(
134138
nodes: RawSnapshotNode[],
135139
suppressedIndexes: Set<number>,
136-
replacements: Map<number, RawSnapshotNode>,
140+
semanticRepresentativeIndexes: ReadonlySet<number>,
137141
): void {
138142
for (let position = 0; position < nodes.length; position += 1) {
139143
const node = nodes[position];
@@ -148,7 +152,7 @@ function collectIosRepeatedStaticSuppression(
148152
node,
149153
nodeLabel,
150154
suppressedIndexes,
151-
replacements,
155+
semanticRepresentativeIndexes,
152156
);
153157
}
154158
}
@@ -159,11 +163,17 @@ function collectRepeatedStaticSuppressionForNode(
159163
node: RawSnapshotNode,
160164
nodeLabel: string,
161165
suppressedIndexes: Set<number>,
162-
replacements: Map<number, RawSnapshotNode>,
166+
semanticRepresentativeIndexes: ReadonlySet<number>,
163167
): void {
164168
const type = normalizeType(node.type ?? '');
165169
if (type === 'statictext' || type === 'link') {
166-
suppressRepeatedStaticDescendants(nodes, position, nodeLabel, suppressedIndexes, replacements);
170+
suppressRepeatedStaticDescendants(
171+
nodes,
172+
position,
173+
nodeLabel,
174+
suppressedIndexes,
175+
semanticRepresentativeIndexes,
176+
);
167177
return;
168178
}
169179
if (type !== 'other') {
@@ -173,7 +183,13 @@ function collectRepeatedStaticSuppressionForNode(
173183
suppressedIndexes.add(node.index);
174184
return;
175185
}
176-
suppressRepeatedStaticDescendants(nodes, position, nodeLabel, suppressedIndexes, replacements);
186+
suppressRepeatedStaticDescendants(
187+
nodes,
188+
position,
189+
nodeLabel,
190+
suppressedIndexes,
191+
semanticRepresentativeIndexes,
192+
);
177193
}
178194

179195
function hasEquivalentSemanticDescendant(
@@ -197,27 +213,18 @@ function suppressRepeatedStaticDescendants(
197213
position: number,
198214
label: string,
199215
suppressedIndexes: Set<number>,
200-
replacements: Map<number, RawSnapshotNode>,
216+
semanticRepresentativeIndexes: ReadonlySet<number>,
201217
): void {
202218
forEachDescendant(nodes, position, (descendant) => {
203219
if (
204-
!hasWebSemanticReplacement(descendant, replacements.get(descendant.index)) &&
220+
!semanticRepresentativeIndexes.has(descendant.index) &&
205221
isRepeatedStaticNode(descendant, label)
206222
) {
207223
suppressedIndexes.add(descendant.index);
208224
}
209225
});
210226
}
211227

212-
function hasWebSemanticReplacement(
213-
source: RawSnapshotNode,
214-
replacement: RawSnapshotNode | undefined,
215-
): boolean {
216-
if (normalizeType(source.type ?? '') !== 'other') return false;
217-
const replacementType = normalizeType(replacement?.type ?? '');
218-
return replacementType === 'heading' || replacementType === 'statictext';
219-
}
220-
221228
function collectIosActionWrapperSuppression(
222229
nodes: RawSnapshotNode[],
223230
suppressedIndexes: Set<number>,
@@ -276,6 +283,7 @@ function isFullscreenActionLabelWrapper(
276283

277284
function collectIosOffscreenKeyboardSuppression(
278285
nodes: RawSnapshotNode[],
286+
sourceNodesByIndex: ReadonlyMap<number, RawSnapshotNode>,
279287
suppressedIndexes: Set<number>,
280288
): void {
281289
const viewport = findLargestViewportRect(nodes);
@@ -289,7 +297,7 @@ function collectIosOffscreenKeyboardSuppression(
289297
continue;
290298
}
291299
suppressedIndexes.add(node.index);
292-
suppressOffscreenKeyboardAncestors(node, nodes, suppressedIndexes, screenBottom);
300+
suppressOffscreenKeyboardAncestors(node, sourceNodesByIndex, suppressedIndexes, screenBottom);
293301
forEachDescendant(nodes, position, (descendant) => {
294302
suppressedIndexes.add(descendant.index);
295303
});
@@ -305,16 +313,18 @@ function isOffscreenKeyboardNode(node: RawSnapshotNode, screenBottom: number): b
305313

306314
function suppressOffscreenKeyboardAncestors(
307315
node: RawSnapshotNode,
308-
nodes: RawSnapshotNode[],
316+
sourceNodesByIndex: ReadonlyMap<number, RawSnapshotNode>,
309317
suppressedIndexes: Set<number>,
310318
screenBottom: number,
311319
): void {
312-
const byIndex = new Map(nodes.map((candidate) => [candidate.index, candidate]));
313-
let current = typeof node.parentIndex === 'number' ? byIndex.get(node.parentIndex) : undefined;
320+
let current =
321+
typeof node.parentIndex === 'number' ? sourceNodesByIndex.get(node.parentIndex) : undefined;
314322
while (current?.rect && current.rect.y >= screenBottom) {
315323
suppressedIndexes.add(current.index);
316324
current =
317-
typeof current.parentIndex === 'number' ? byIndex.get(current.parentIndex) : undefined;
325+
typeof current.parentIndex === 'number'
326+
? sourceNodesByIndex.get(current.parentIndex)
327+
: undefined;
318328
}
319329
}
320330

@@ -338,6 +348,7 @@ function collectIosStructuralIdentifierSuppression(
338348

339349
function collectIosSearchToolbarSuppression(
340350
nodes: RawSnapshotNode[],
351+
sourceNodesByIndex: ReadonlyMap<number, RawSnapshotNode>,
341352
suppressedIndexes: Set<number>,
342353
): void {
343354
for (let position = 0; position < nodes.length; position += 1) {
@@ -360,7 +371,7 @@ function collectIosSearchToolbarSuppression(
360371
}
361372

362373
suppressedIndexes.add(node.index);
363-
suppressToolbarAncestors(node, nodes, suppressedIndexes);
374+
suppressToolbarAncestors(node, sourceNodesByIndex, suppressedIndexes);
364375
suppressSearchToolbarDescendants(nodes, position, innerSearch.index, suppressedIndexes);
365376
}
366377
}
@@ -392,13 +403,12 @@ function suppressSearchToolbarDescendants(
392403

393404
function suppressToolbarAncestors(
394405
node: RawSnapshotNode,
395-
nodes: RawSnapshotNode[],
406+
sourceNodesByIndex: ReadonlyMap<number, RawSnapshotNode>,
396407
suppressedIndexes: Set<number>,
397408
): void {
398-
const byIndex = new Map(nodes.map((candidate) => [candidate.index, candidate]));
399409
let current = node;
400410
while (typeof current.parentIndex === 'number') {
401-
const parent = byIndex.get(current.parentIndex);
411+
const parent = sourceNodesByIndex.get(current.parentIndex);
402412
if (!parent || parent.label !== 'Toolbar') {
403413
return;
404414
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ export function collectIosScrollIndicatorPresentation(
1414
nodes: RawSnapshotNode[],
1515
context: SnapshotTreeRuleContext,
1616
): void {
17-
const byIndex = new Map(nodes.map((node) => [node.index, node]));
1817
for (const node of nodes) {
1918
if (!isIosScrollIndicatorNode(node)) {
2019
continue;
2120
}
22-
collectIosScrollIndicatorNodePresentation(node, byIndex, context);
21+
collectIosScrollIndicatorNodePresentation(node, context.sourceNodesByIndex, context);
2322
}
2423
}
2524

@@ -30,7 +29,7 @@ function isIosScrollIndicatorNode(node: RawSnapshotNode): boolean {
3029

3130
function collectIosScrollIndicatorNodePresentation(
3231
node: RawSnapshotNode,
33-
byIndex: Map<number, RawSnapshotNode>,
32+
byIndex: ReadonlyMap<number, RawSnapshotNode>,
3433
context: SnapshotTreeRuleContext,
3534
): void {
3635
if (!isScrollableSnapshotType(node.type)) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ test('projects iOS WebKit heading and text wrappers to semantic roles', () => {
2424
).toEqual([
2525
['Application', 'Example', undefined, undefined],
2626
['WebView', 'Example page', undefined, 0],
27-
['Heading', 'Welcome', '1', 1],
27+
['Heading', 'Welcome', undefined, 1],
2828
['StaticText', 'Introduction', undefined, 1],
2929
['Link', 'Read more', undefined, 1],
3030
]);
31+
expect(nodes[3]?.value).toBe('1');
3132
});
3233

3334
test('recognizes numeric WebView types from retained older runners', () => {

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,58 @@ import { findNearestAncestor, mergeReplacement, type SnapshotTreeRuleContext } f
99
*
1010
* HTML headings use the same pair and expose their level as the wrapper value.
1111
* XCTest does not publish the level as a dedicated field, so the public role is
12-
* `Heading` while the existing value remains available in the raw node.
12+
* `Heading` while the backend-specific level remains available only in the raw
13+
* diagnostic node.
1314
*/
1415
export function collectIosWebSemanticPresentation(
1516
nodes: RawSnapshotNode[],
1617
context: SnapshotTreeRuleContext,
1718
): void {
18-
const byIndex = new Map(nodes.map((node) => [node.index, node]));
1919
const repeatedStaticTextByParent = collectRepeatedStaticTextByParent(nodes);
2020

2121
for (const node of nodes) {
2222
if (normalizeType(node.type ?? '') === 'element(58)') {
2323
mergeReplacement(context.replacements, node, { type: 'WebView' });
2424
continue;
2525
}
26-
const presentation = classifyIosWebTextWrapper(node, byIndex, repeatedStaticTextByParent);
27-
if (presentation === 'suppress') {
26+
const presentation = classifyIosWebTextWrapper(
27+
node,
28+
context.sourceNodesByIndex,
29+
repeatedStaticTextByParent,
30+
);
31+
if (presentation?.kind === 'suppress') {
2832
context.suppressedIndexes.add(node.index);
2933
continue;
3034
}
3135
if (presentation) {
32-
mergeReplacement(context.replacements, node, { type: presentation });
36+
mergeReplacement(context.replacements, node, {
37+
type: presentation.type,
38+
...(presentation.type === 'Heading' ? { value: undefined } : {}),
39+
});
40+
context.semanticRepresentativeIndexes.add(node.index);
3341
}
3442
}
3543
}
3644

45+
type IosWebTextWrapperPresentation =
46+
| { kind: 'semantic'; type: 'Heading' | 'StaticText' }
47+
| { kind: 'suppress' };
48+
3749
function classifyIosWebTextWrapper(
3850
node: RawSnapshotNode,
39-
byIndex: Map<number, RawSnapshotNode>,
51+
byIndex: ReadonlyMap<number, RawSnapshotNode>,
4052
repeatedStaticTextByParent: Map<number, Set<string>>,
41-
): 'Heading' | 'StaticText' | 'suppress' | null {
53+
): IosWebTextWrapperPresentation | null {
4254
if (normalizeType(node.type ?? '') !== 'other') return null;
4355
const webView = findNearestAncestor(node, byIndex, isWebView);
4456
const label = node.label?.trim();
4557
if (!webView || !label) return null;
46-
if (isDocumentTitleWrapper(node, webView, label)) return 'suppress';
58+
if (isDocumentTitleWrapper(node, webView, label)) return { kind: 'suppress' };
4759
if (!repeatedStaticTextByParent.get(node.index)?.has(label)) return null;
48-
return isHtmlHeadingLevel(node.value) ? 'Heading' : 'StaticText';
60+
return {
61+
kind: 'semantic',
62+
type: isHtmlHeadingLevel(node.value) ? 'Heading' : 'StaticText',
63+
};
4964
}
5065

5166
function isDocumentTitleWrapper(

‎src/daemon/snapshot-presentation/tree.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ export { areRectsApproximatelyEqual } from '../../utils/rect-center.ts';
44

55
export type SnapshotTreeRuleContext = {
66
replacements: Map<number, RawSnapshotNode>;
7+
/** Projected label owners that repeated-text suppression must retain. */
8+
semanticRepresentativeIndexes: Set<number>;
9+
sourceNodesByIndex: ReadonlyMap<number, RawSnapshotNode>;
710
suppressedIndexes: Set<number>;
811
};
912

@@ -71,7 +74,10 @@ function getDescendantEndPositions(nodes: RawSnapshotNode[]): number[] {
7174
return endPositions;
7275
}
7376

74-
function collectAncestors<T extends RawSnapshotNode>(node: T, byIndex: Map<number, T>): T[] {
77+
function collectAncestors<T extends RawSnapshotNode>(
78+
node: T,
79+
byIndex: ReadonlyMap<number, T>,
80+
): T[] {
7581
const ancestors: T[] = [];
7682
let current = typeof node.parentIndex === 'number' ? byIndex.get(node.parentIndex) : undefined;
7783
const visited = new Set<number>();
@@ -86,7 +92,7 @@ function collectAncestors<T extends RawSnapshotNode>(node: T, byIndex: Map<numbe
8692

8793
export function findNearestAncestor<T extends RawSnapshotNode>(
8894
node: T,
89-
byIndex: Map<number, T>,
95+
byIndex: ReadonlyMap<number, T>,
9096
predicate: (ancestor: T) => boolean,
9197
): T | null {
9298
for (const ancestor of collectAncestors(node, byIndex)) {
@@ -99,7 +105,7 @@ export function findNearestAncestor<T extends RawSnapshotNode>(
99105

100106
export function findNearestScrollableContainer<T extends RawSnapshotNode>(
101107
node: T,
102-
byIndex: Map<number, T>,
108+
byIndex: ReadonlyMap<number, T>,
103109
options: { includeSelf?: boolean } = {},
104110
): T | null {
105111
const self = options.includeSelf === true && isScrollableSnapshotType(node.type) ? node : null;

0 commit comments

Comments
 (0)