1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
/* clang-format off */
/* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* clang-format on */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#import "MOXSearchInfo.h"
#import "MOXWebAreaAccessible.h"
#import "RotorRules.h"
#include "nsCocoaUtils.h"
#include "DocAccessibleParent.h"
using namespace mozilla::a11y;
@interface MOXSearchInfo ()
- (NSArray*)getMatchesForRule:(PivotRule&)rule;
- (NSArray<mozAccessible*>*)applyPostFilter:(NSArray<mozAccessible*>*)matches;
- (AccessibleOrProxy)rootGeckoAccessible;
- (AccessibleOrProxy)startGeckoAccessible;
- (BOOL)shouldApplyPostFilter;
@end
@implementation MOXSearchInfo
- (id)initWithParameters:(NSDictionary*)params
andRoot:(MOXAccessibleBase*)root {
if (id searchKeyParam = [params objectForKey:@"AXSearchKey"]) {
mSearchKeys = [searchKeyParam isKindOfClass:[NSString class]]
? @[ searchKeyParam ]
: searchKeyParam;
}
if (id startElemParam = [params objectForKey:@"AXStartElement"]) {
mStartElem = startElemParam;
} else {
mStartElem = root;
}
mRoot = root;
mResultLimit = [[params objectForKey:@"AXResultsLimit"] intValue];
mSearchForward =
[[params objectForKey:@"AXDirection"] isEqualToString:@"AXDirectionNext"];
mImmediateDescendantsOnly =
[[params objectForKey:@"AXImmediateDescendantsOnly"] boolValue];
mSearchText = [params objectForKey:@"AXSearchText"];
return [super init];
}
- (AccessibleOrProxy)rootGeckoAccessible {
id root =
[mRoot isKindOfClass:[mozAccessible class]] ? mRoot : [mRoot moxParent];
return [static_cast<mozAccessible*>(root) geckoAccessible];
}
- (AccessibleOrProxy)startGeckoAccessible {
if ([mStartElem isKindOfClass:[mozAccessible class]]) {
return [static_cast<mozAccessible*>(mStartElem) geckoAccessible];
}
// If it isn't a mozAccessible, it doesn't have a gecko accessible
// this is most likely the root group. Use the gecko doc as the start
// accessible.
return [self rootGeckoAccessible];
}
- (NSArray*)getMatchesForRule:(PivotRule&)rule {
// If we will apply a post-filter, don't limit search so we
// don't come up short on the final result count.
int resultLimit = [self shouldApplyPostFilter] ? -1 : mResultLimit;
NSMutableArray<mozAccessible*>* matches = [[NSMutableArray alloc] init];
AccessibleOrProxy geckoRootAcc = [self rootGeckoAccessible];
AccessibleOrProxy geckoStartAcc = [self startGeckoAccessible];
Pivot p = Pivot(geckoRootAcc);
AccessibleOrProxy match;
if (mSearchForward) {
match = p.Next(geckoStartAcc, rule);
} else {
// Search backwards
if (geckoRootAcc == geckoStartAcc) {
// If we have no explicit start accessible, start from the last match.
match = p.Last(rule);
} else {
match = p.Prev(geckoStartAcc, rule);
}
}
while (!match.IsNull() && resultLimit != 0) {
if (!mSearchForward && match == geckoRootAcc) {
// If searching backwards, don't include root.
break;
}
// we use mResultLimit != 0 to capture the case where mResultLimit is -1
// when it is set from the params dictionary. If that's true, we want
// to return all matches (ie. have no limit)
mozAccessible* nativeMatch = GetNativeFromGeckoAccessible(match);
if (nativeMatch) {
// only add/count results for which there is a matching
// native accessible
[matches addObject:nativeMatch];
resultLimit -= 1;
}
match = mSearchForward ? p.Next(match, rule) : p.Prev(match, rule);
}
return [self applyPostFilter:matches];
}
- (BOOL)shouldApplyPostFilter {
// We currently only support AXSearchText as a post-search filter.
return !!mSearchText;
}
- (NSArray<mozAccessible*>*)applyPostFilter:(NSArray<mozAccessible*>*)matches {
if (![self shouldApplyPostFilter]) {
return matches;
}
NSMutableArray<mozAccessible*>* postMatches = [[NSMutableArray alloc] init];
nsString searchText;
nsCocoaUtils::GetStringForNSString(mSearchText, searchText);
__block DocAccessibleParent* ipcDoc = nullptr;
__block nsTArray<uint64_t> accIds;
[matches enumerateObjectsUsingBlock:^(mozAccessible* match, NSUInteger idx,
BOOL* stop) {
AccessibleOrProxy geckoAcc = [match geckoAccessible];
if (geckoAcc.IsNull()) {
return;
}
switch (geckoAcc.Role()) {
case roles::LANDMARK:
case roles::COMBOBOX:
case roles::LISTITEM:
case roles::COMBOBOX_LIST:
case roles::MENUBAR:
case roles::MENUPOPUP:
case roles::DOCUMENT:
case roles::APPLICATION:
// XXX: These roles either have AXTitle/AXDescription overridden as
// empty, or should never be returned in search text results. This
// should be integrated into a pivot rule in the future, and possibly
// better mapped somewhere.
return;
default:
break;
}
if (geckoAcc.IsAccessible()) {
AccessibleWrap* acc =
static_cast<AccessibleWrap*>(geckoAcc.AsAccessible());
if (acc->ApplyPostFilter(EWhichPostFilter::eContainsText, searchText)) {
if (mozAccessible* nativePostMatch =
GetNativeFromGeckoAccessible(acc)) {
[postMatches addObject:nativePostMatch];
if (mResultLimit > 0 &&
[postMatches count] >= static_cast<NSUInteger>(mResultLimit)) {
// If we reached the result limit, alter the `stop` pointer to YES
// to stop iteration.
*stop = YES;
}
}
}
return;
}
ProxyAccessible* proxy = geckoAcc.AsProxy();
if (ipcDoc &&
((ipcDoc != proxy->Document()) || (idx + 1 == [matches count]))) {
// If the ipcDoc doesn't match the current proxy's doc, we crossed into a
// new document. ..or this is the last match. Apply the filter on the list
// of the current ipcDoc.
nsTArray<uint64_t> matchIds;
Unused << ipcDoc->GetPlatformExtension()->SendApplyPostSearchFilter(
accIds, mResultLimit, EWhichPostFilter::eContainsText, searchText,
&matchIds);
for (size_t i = 0; i < matchIds.Length(); i++) {
if (ProxyAccessible* postMatch =
ipcDoc->GetAccessible(matchIds.ElementAt(i))) {
if (mozAccessible* nativePostMatch =
GetNativeFromGeckoAccessible(postMatch)) {
[postMatches addObject:nativePostMatch];
if (mResultLimit > 0 &&
[postMatches count] >= static_cast<NSUInteger>(mResultLimit)) {
// If we reached the result limit, alter the `stop` pointer to YES
// to stop iteration.
*stop = YES;
return;
}
}
}
}
ipcDoc = nullptr;
accIds.Clear();
}
if (!ipcDoc) {
ipcDoc = proxy->Document();
}
accIds.AppendElement(proxy->ID());
}];
return postMatches;
}
- (NSArray*)performSearch {
AccessibleOrProxy geckoRootAcc = [self rootGeckoAccessible];
AccessibleOrProxy geckoStartAcc = [self startGeckoAccessible];
NSMutableArray* matches = [[NSMutableArray alloc] init];
for (id key in mSearchKeys) {
if ([key isEqualToString:@"AXAnyTypeSearchKey"]) {
RotorRule rule =
mImmediateDescendantsOnly ? RotorRule(geckoRootAcc) : RotorRule();
if ([mStartElem isKindOfClass:[MOXWebAreaAccessible class]]) {
if (id rootGroup =
[static_cast<MOXWebAreaAccessible*>(mStartElem) rootGroup]) {
// Moving forward from web area, rootgroup; if it exists, is next.
[matches addObject:rootGroup];
if (mResultLimit == 1) {
// Found one match, continue in search keys for block.
continue;
}
}
}
if (mImmediateDescendantsOnly && mStartElem != mRoot &&
[mStartElem isKindOfClass:[MOXRootGroup class]]) {
// Moving forward from root group. If we don't match descendants,
// there is no match. Continue.
continue;
}
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXHeadingSearchKey"]) {
RotorRoleRule rule = mImmediateDescendantsOnly
? RotorRoleRule(roles::HEADING, geckoRootAcc)
: RotorRoleRule(roles::HEADING);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXArticleSearchKey"]) {
RotorRoleRule rule = mImmediateDescendantsOnly
? RotorRoleRule(roles::ARTICLE, geckoRootAcc)
: RotorRoleRule(roles::ARTICLE);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXTableSearchKey"]) {
RotorRoleRule rule = mImmediateDescendantsOnly
? RotorRoleRule(roles::TABLE, geckoRootAcc)
: RotorRoleRule(roles::TABLE);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXLandmarkSearchKey"]) {
RotorRoleRule rule = mImmediateDescendantsOnly
? RotorRoleRule(roles::LANDMARK, geckoRootAcc)
: RotorRoleRule(roles::LANDMARK);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXListSearchKey"]) {
RotorRoleRule rule = mImmediateDescendantsOnly
? RotorRoleRule(roles::LIST, geckoRootAcc)
: RotorRoleRule(roles::LIST);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXLinkSearchKey"]) {
RotorLinkRule rule = mImmediateDescendantsOnly
? RotorLinkRule(geckoRootAcc)
: RotorLinkRule();
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXVisitedLinkSearchKey"]) {
RotorVisitedLinkRule rule = mImmediateDescendantsOnly
? RotorVisitedLinkRule(geckoRootAcc)
: RotorVisitedLinkRule();
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXUnvisitedLinkSearchKey"]) {
RotorUnvisitedLinkRule rule = mImmediateDescendantsOnly
? RotorUnvisitedLinkRule(geckoRootAcc)
: RotorUnvisitedLinkRule();
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXButtonSearchKey"]) {
RotorRoleRule rule = mImmediateDescendantsOnly
? RotorRoleRule(roles::PUSHBUTTON, geckoRootAcc)
: RotorRoleRule(roles::PUSHBUTTON);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXControlSearchKey"]) {
RotorControlRule rule = mImmediateDescendantsOnly
? RotorControlRule(geckoRootAcc)
: RotorControlRule();
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXSameTypeSearchKey"]) {
mozAccessible* native = GetNativeFromGeckoAccessible(geckoStartAcc);
NSString* macRole = [native moxRole];
RotorMacRoleRule rule = mImmediateDescendantsOnly
? RotorMacRoleRule(macRole, geckoRootAcc)
: RotorMacRoleRule(macRole);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXDifferentTypeSearchKey"]) {
mozAccessible* native = GetNativeFromGeckoAccessible(geckoStartAcc);
NSString* macRole = [native moxRole];
RotorNotMacRoleRule rule =
mImmediateDescendantsOnly ? RotorNotMacRoleRule(macRole, geckoRootAcc)
: RotorNotMacRoleRule(macRole);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXRadioGroupSearchKey"]) {
RotorRoleRule rule = mImmediateDescendantsOnly
? RotorRoleRule(roles::RADIO_GROUP, geckoRootAcc)
: RotorRoleRule(roles::RADIO_GROUP);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXFrameSearchKey"]) {
RotorRoleRule rule = mImmediateDescendantsOnly
? RotorRoleRule(roles::DOCUMENT, geckoRootAcc)
: RotorRoleRule(roles::DOCUMENT);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXImageSearchKey"] ||
[key isEqualToString:@"AXGraphicSearchKey"]) {
RotorRoleRule rule = mImmediateDescendantsOnly
? RotorRoleRule(roles::GRAPHIC, geckoRootAcc)
: RotorRoleRule(roles::GRAPHIC);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXCheckboxSearchKey"]) {
RotorRoleRule rule = mImmediateDescendantsOnly
? RotorRoleRule(roles::CHECKBUTTON, geckoRootAcc)
: RotorRoleRule(roles::CHECKBUTTON);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXStaticTextSearchKey"]) {
RotorStaticTextRule rule = mImmediateDescendantsOnly
? RotorStaticTextRule(geckoRootAcc)
: RotorStaticTextRule();
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXHeadingLevel1SearchKey"]) {
RotorHeadingLevelRule rule = mImmediateDescendantsOnly
? RotorHeadingLevelRule(1, geckoRootAcc)
: RotorHeadingLevelRule(1);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXHeadingLevel2SearchKey"]) {
RotorHeadingLevelRule rule = mImmediateDescendantsOnly
? RotorHeadingLevelRule(2, geckoRootAcc)
: RotorHeadingLevelRule(2);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXHeadingLevel3SearchKey"]) {
RotorHeadingLevelRule rule = mImmediateDescendantsOnly
? RotorHeadingLevelRule(3, geckoRootAcc)
: RotorHeadingLevelRule(3);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXHeadingLevel4SearchKey"]) {
RotorHeadingLevelRule rule = mImmediateDescendantsOnly
? RotorHeadingLevelRule(4, geckoRootAcc)
: RotorHeadingLevelRule(4);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXHeadingLevel5SearchKey"]) {
RotorHeadingLevelRule rule = mImmediateDescendantsOnly
? RotorHeadingLevelRule(5, geckoRootAcc)
: RotorHeadingLevelRule(5);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXHeadingLevel6SearchKey"]) {
RotorHeadingLevelRule rule = mImmediateDescendantsOnly
? RotorHeadingLevelRule(6, geckoRootAcc)
: RotorHeadingLevelRule(6);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXBlockquoteSearchKey"]) {
RotorRoleRule rule = mImmediateDescendantsOnly
? RotorRoleRule(roles::BLOCKQUOTE, geckoRootAcc)
: RotorRoleRule(roles::BLOCKQUOTE);
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXTextFieldSearchKey"]) {
RotorTextEntryRule rule = mImmediateDescendantsOnly
? RotorTextEntryRule(geckoRootAcc)
: RotorTextEntryRule();
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
if ([key isEqualToString:@"AXLiveRegionSearchKey"]) {
RotorLiveRegionRule rule = mImmediateDescendantsOnly
? RotorLiveRegionRule(geckoRootAcc)
: RotorLiveRegionRule();
[matches addObjectsFromArray:[self getMatchesForRule:rule]];
}
}
return matches;
}
- (void)dealloc {
[mSearchKeys release];
[super dealloc];
}
@end
|