Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/daemon/selectors-build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { SnapshotNode } from '../utils/snapshot.ts';
import { extractNodeText, normalizeType } from './snapshot-processing.ts';
import { uniqueStrings } from './action-utils.ts';
import { isNodeVisible } from './selectors-match.ts';

export function buildSelectorChainForNode(
node: SnapshotNode,
_platform: 'ios' | 'android',
options: { action?: 'click' | 'fill' | 'get' } = {},
): string[] {
const chain: string[] = [];
const role = normalizeType(node.type ?? '');
const id = normalizeSelectorText(node.identifier);
const label = normalizeSelectorText(node.label);
const value = normalizeSelectorText(node.value);
const text = normalizeSelectorText(extractNodeText(node));
const requireEditable = options.action === 'fill';

if (id) {
chain.push(`id=${quoteSelectorValue(id)}`);
}
if (role && label) {
chain.push(
requireEditable
? `role=${quoteSelectorValue(role)} label=${quoteSelectorValue(label)} editable=true`
: `role=${quoteSelectorValue(role)} label=${quoteSelectorValue(label)}`,
);
}
if (label) {
chain.push(requireEditable ? `label=${quoteSelectorValue(label)} editable=true` : `label=${quoteSelectorValue(label)}`);
}
if (value) {
chain.push(requireEditable ? `value=${quoteSelectorValue(value)} editable=true` : `value=${quoteSelectorValue(value)}`);
}
if (text && text !== label && text !== value) {
chain.push(requireEditable ? `text=${quoteSelectorValue(text)} editable=true` : `text=${quoteSelectorValue(text)}`);
}
if (role && requireEditable && !chain.some((entry) => entry.includes('editable=true'))) {
chain.push(`role=${quoteSelectorValue(role)} editable=true`);
}

const deduped = uniqueStrings(chain);
if (deduped.length === 0 && role) {
deduped.push(requireEditable ? `role=${quoteSelectorValue(role)} editable=true` : `role=${quoteSelectorValue(role)}`);
}
if (deduped.length === 0) {
const visible = isNodeVisible(node);
if (visible) deduped.push('visible=true');
}
return deduped;
}

function quoteSelectorValue(value: string): string {
return JSON.stringify(value);
}

function normalizeSelectorText(value: string | undefined): string | null {
if (!value) return null;
const trimmed = value.trim();
if (!trimmed) return null;
return trimmed;
}
65 changes: 65 additions & 0 deletions src/daemon/selectors-match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { SnapshotNode } from '../utils/snapshot.ts';
import { extractNodeText, isFillableType, normalizeType } from './snapshot-processing.ts';
import type { Selector, SelectorTerm } from './selectors-parse.ts';

export function matchesSelector(node: SnapshotNode, selector: Selector, platform: 'ios' | 'android'): boolean {
return selector.terms.every((term) => matchesTerm(node, term, platform));
}

export function isNodeVisible(node: SnapshotNode): boolean {
if (node.hittable === true) return true;
if (!node.rect) return false;
return node.rect.width > 0 && node.rect.height > 0;
}

export function isNodeEditable(node: SnapshotNode, platform: 'ios' | 'android'): boolean {
const type = node.type ?? '';
return isFillableType(type, platform) && node.enabled !== false;
}

function matchesTerm(node: SnapshotNode, term: SelectorTerm, platform: 'ios' | 'android'): boolean {
switch (term.key) {
case 'id':
return textEquals(node.identifier, String(term.value));
case 'role':
return roleEquals(node.type, String(term.value));
case 'label':
return textEquals(node.label, String(term.value));
case 'value':
return textEquals(node.value, String(term.value));
case 'text': {
const query = normalizeText(String(term.value));
return normalizeText(extractNodeText(node)) === query;
}
case 'visible':
return isNodeVisible(node) === Boolean(term.value);
case 'hidden':
return (!isNodeVisible(node)) === Boolean(term.value);
case 'editable':
return isNodeEditable(node, platform) === Boolean(term.value);
case 'selected':
return Boolean(node.selected === true) === Boolean(term.value);
case 'enabled':
return Boolean(node.enabled !== false) === Boolean(term.value);
case 'hittable':
return Boolean(node.hittable === true) === Boolean(term.value);
default:
return false;
}
}

function textEquals(value: string | undefined, query: string): boolean {
return normalizeText(value ?? '') === normalizeText(query);
}

function roleEquals(value: string | undefined, query: string): boolean {
return normalizeRole(value ?? '') === normalizeRole(query);
}

function normalizeText(value: string): string {
return value.trim().toLowerCase().replace(/\s+/g, ' ');
}

function normalizeRole(value: string): string {
return normalizeType(value);
}
253 changes: 253 additions & 0 deletions src/daemon/selectors-parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
import { AppError } from '../utils/errors.ts';

export type SelectorKey =
| 'id'
| 'role'
| 'text'
| 'label'
| 'value'
| 'visible'
| 'hidden'
| 'editable'
| 'selected'
| 'enabled'
| 'hittable';

export type SelectorTerm = {
key: SelectorKey;
value: string | boolean;
};

export type Selector = {
raw: string;
terms: SelectorTerm[];
};

export type SelectorChain = {
raw: string;
selectors: Selector[];
};

const TEXT_KEYS = new Set<SelectorKey>(['id', 'role', 'text', 'label', 'value']);
const BOOLEAN_KEYS = new Set<SelectorKey>([
'visible',
'hidden',
'editable',
'selected',
'enabled',
'hittable',
]);
const ALL_KEYS = new Set<SelectorKey>([...TEXT_KEYS, ...BOOLEAN_KEYS]);

export function parseSelectorChain(expression: string): SelectorChain {
const raw = expression.trim();
if (!raw) {
throw new AppError('INVALID_ARGS', 'Selector expression cannot be empty');
}
const segments = splitByFallback(raw);
if (segments.length === 0) {
throw new AppError('INVALID_ARGS', 'Selector expression cannot be empty');
}
return {
raw,
selectors: segments.map((segment) => parseSelector(segment)),
};
}

export function tryParseSelectorChain(expression: string): SelectorChain | null {
try {
return parseSelectorChain(expression);
} catch {
return null;
}
}

export function isSelectorToken(token: string): boolean {
const trimmed = token.trim();
if (!trimmed) return false;
if (trimmed === '||') return true;
const equalsIdx = trimmed.indexOf('=');
if (equalsIdx !== -1) {
const key = trimmed.slice(0, equalsIdx).trim().toLowerCase() as SelectorKey;
return ALL_KEYS.has(key);
}
return ALL_KEYS.has(trimmed.toLowerCase() as SelectorKey);
}

export function splitSelectorFromArgs(
args: string[],
options: { preferTrailingValue?: boolean } = {},
): { selectorExpression: string; rest: string[] } | null {
if (args.length === 0) return null;
const preferTrailingValue = options.preferTrailingValue ?? false;
let i = 0;
const boundaries: number[] = [];
while (i < args.length && isSelectorToken(args[i])) {
i += 1;
const candidate = args.slice(0, i).join(' ').trim();
if (!candidate) continue;
if (tryParseSelectorChain(candidate)) {
boundaries.push(i);
}
}
if (boundaries.length === 0) return null;
let boundary = boundaries[boundaries.length - 1];
if (preferTrailingValue) {
for (let j = boundaries.length - 1; j >= 0; j -= 1) {
if (boundaries[j] < args.length) {
boundary = boundaries[j];
break;
}
}
}
const selectorExpression = args.slice(0, boundary).join(' ').trim();
if (!selectorExpression) return null;
return {
selectorExpression,
rest: args.slice(boundary),
};
}

export function splitIsSelectorArgs(positionals: string[]): {
predicate: string;
split: { selectorExpression: string; rest: string[] } | null;
} {
const predicate = positionals[0] ?? '';
const split = splitSelectorFromArgs(positionals.slice(1), {
preferTrailingValue: predicate === 'text',
});
return { predicate, split };
}

function parseSelector(segment: string): Selector {
const raw = segment.trim();
if (!raw) throw new AppError('INVALID_ARGS', 'Selector segment cannot be empty');
const tokens = tokenize(raw);
if (tokens.length === 0) {
throw new AppError('INVALID_ARGS', `Invalid selector segment: ${segment}`);
}
const terms = tokens.map(parseTerm);
return { raw, terms };
}

function parseTerm(token: string): SelectorTerm {
const normalized = token.trim();
if (!normalized) {
throw new AppError('INVALID_ARGS', 'Empty selector term');
}
const equalsIdx = normalized.indexOf('=');
if (equalsIdx === -1) {
const key = normalized.toLowerCase() as SelectorKey;
if (!BOOLEAN_KEYS.has(key)) {
throw new AppError('INVALID_ARGS', `Invalid selector term "${token}", expected key=value`);
}
return { key, value: true };
}
const keyRaw = normalized.slice(0, equalsIdx).trim().toLowerCase() as SelectorKey;
const valueRaw = normalized.slice(equalsIdx + 1).trim();
if (!ALL_KEYS.has(keyRaw)) {
throw new AppError('INVALID_ARGS', `Unknown selector key: ${keyRaw}`);
}
if (!valueRaw) {
throw new AppError('INVALID_ARGS', `Missing selector value for key: ${keyRaw}`);
}
if (BOOLEAN_KEYS.has(keyRaw)) {
const parsedBoolean = parseBoolean(valueRaw);
if (parsedBoolean === null) {
throw new AppError('INVALID_ARGS', `Invalid boolean value for ${keyRaw}: ${valueRaw}`);
}
return { key: keyRaw, value: parsedBoolean };
}
return { key: keyRaw, value: unquote(valueRaw) };
}

function splitByFallback(expression: string): string[] {
const segments: string[] = [];
let current = '';
let quote: '"' | "'" | null = null;
for (let i = 0; i < expression.length; i += 1) {
const ch = expression[i];
if ((ch === '"' || ch === "'") && !isEscapedQuote(expression, i)) {
if (!quote) {
quote = ch;
} else if (quote === ch) {
quote = null;
}
current += ch;
continue;
}
if (!quote && ch === '|' && expression[i + 1] === '|') {
const segment = current.trim();
if (!segment) {
throw new AppError('INVALID_ARGS', `Invalid selector fallback expression: ${expression}`);
}
segments.push(segment);
current = '';
i += 1;
continue;
}
current += ch;
}
const finalSegment = current.trim();
if (!finalSegment) {
throw new AppError('INVALID_ARGS', `Invalid selector fallback expression: ${expression}`);
}
segments.push(finalSegment);
return segments;
}

function tokenize(segment: string): string[] {
const tokens: string[] = [];
let current = '';
let quote: '"' | "'" | null = null;
for (let i = 0; i < segment.length; i += 1) {
const ch = segment[i];
if ((ch === '"' || ch === "'") && !isEscapedQuote(segment, i)) {
if (!quote) {
quote = ch;
} else if (quote === ch) {
quote = null;
}
current += ch;
continue;
}
if (!quote && /\s/.test(ch)) {
if (current.trim().length > 0) {
tokens.push(current.trim());
}
current = '';
continue;
}
current += ch;
}
if (quote) {
throw new AppError('INVALID_ARGS', `Unclosed quote in selector: ${segment}`);
}
if (current.trim().length > 0) {
tokens.push(current.trim());
}
return tokens;
}

function unquote(value: string): string {
const trimmed = value.trim();
if ((trimmed.startsWith('"') && trimmed.endsWith('"')) || (trimmed.startsWith("'") && trimmed.endsWith("'"))) {
return trimmed.slice(1, -1).replace(/\\(["'])/g, '$1');
}
return trimmed;
}

function parseBoolean(value: string): boolean | null {
const normalized = unquote(value).toLowerCase();
if (normalized === 'true') return true;
if (normalized === 'false') return false;
return null;
}

function isEscapedQuote(source: string, index: number): boolean {
let backslashCount = 0;
for (let i = index - 1; i >= 0 && source[i] === '\\'; i -= 1) {
backslashCount += 1;
}
return backslashCount % 2 === 1;
}
Loading
Loading