Skip to content

Commit 6b23725

Browse files
committed
fix(kernel): keep the covering-element hint one line for app-controlled labels
Collapse whitespace and drop quote characters when rendering a covering node's label into a refusal hint, so an app-set label containing a newline or quote cannot break the hint's one-line shape; the machine detail keeps the label raw.
1 parent d98fdd1 commit 6b23725

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { test } from 'vitest';
2+
import assert from 'node:assert/strict';
3+
import { coveredInteractionHint, type SnapshotCoveringNode } from './snapshot.ts';
4+
5+
const cover: SnapshotCoveringNode = {
6+
kind: 'tab-bar',
7+
label: 'Media',
8+
rect: { x: 0.4, y: 760.2, width: 390.5, height: 84 },
9+
};
10+
11+
test('coveredInteractionHint names the cover with rounded geometry', () => {
12+
assert.match(
13+
coveredInteractionHint(cover),
14+
/^Covered by the tab-bar "Media" at \(0, 760 391x84\)\. Dismiss or close that element/,
15+
);
16+
});
17+
18+
test('coveredInteractionHint keeps the hint one line when the cover label carries newlines or quotes', () => {
19+
// The label is app-controlled text; the hint must stay a single unambiguous line, and only the
20+
// rendering is sanitized — callers can still read the raw label on the coveredBy detail.
21+
const dirty = coveredInteractionHint({
22+
...cover,
23+
label: ' Dismiss\n "now" ',
24+
});
25+
assert.match(dirty, /the tab-bar "Dismiss now" at/);
26+
assert.doesNotMatch(dirty, /\n/);
27+
});
28+
29+
test('coveredInteractionHint falls back to the bare kind when the label sanitizes to nothing', () => {
30+
assert.match(coveredInteractionHint({ ...cover, label: '""\n' }), /^Covered by the tab-bar at/);
31+
});
32+
33+
test('coveredInteractionHint keeps the generic recovery without a cover', () => {
34+
assert.equal(
35+
coveredInteractionHint(undefined),
36+
'Use a different visible target, scroll it clear of the overlay, or inspect with snapshot/screenshot before retrying.',
37+
);
38+
});

‎packages/kernel/src/snapshot.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,12 @@ export const SNAPSHOT_COVERING_LABEL_MAX_LENGTH = 80;
267267
/**
268268
* One-line human rendering of a {@link SnapshotCoveringNode} for refusal hints, e.g.
269269
* `the dialog "Cookie banner" at (16, 420 358x180)`. Coordinates are rounded; the detail payload
270-
* carries the unrounded rect.
270+
* carries the unrounded rect. The label is app-controlled text: the rendering collapses whitespace
271+
* and drops quote characters so the hint stays one unambiguous line, while the detail keeps it raw.
271272
*/
272273
function formatSnapshotCoveringNode(cover: SnapshotCoveringNode): string {
273-
const named = cover.label ? `${cover.kind} "${cover.label}"` : cover.kind;
274+
const sanitized = cover.label ? sanitizeCoveringLabelForHint(cover.label) : '';
275+
const named = sanitized ? `${cover.kind} "${sanitized}"` : cover.kind;
274276
const rect = cover.rect;
275277
if (!rect) return `the ${named}`;
276278
return (
@@ -279,6 +281,14 @@ function formatSnapshotCoveringNode(cover: SnapshotCoveringNode): string {
279281
);
280282
}
281283

284+
function sanitizeCoveringLabelForHint(label: string): string {
285+
return label
286+
.replaceAll(/[\r\n]+/g, ' ')
287+
.replaceAll(/\s+/g, ' ')
288+
.replaceAll('"', '')
289+
.trim();
290+
}
291+
282292
/**
283293
* The recovery hint every covered-interaction refusal publishes. When the scan named the cover it
284294
* says what to dismiss; a cover-less refusal (a backend-policy annotation that reports only the

0 commit comments

Comments
 (0)