Bug 1777562 [wpt PR 34663] - [FedCM] Rename FederatedCredential to IdentityCredential...
[gecko.git] / accessible / mac / mozTextAccessible.mm
blob704de9afc8d38327934b3a031c9c79d7543b005f
1 /* clang-format off */
2 /* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 /* clang-format on */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "AccAttributes.h"
9 #include "HyperTextAccessible-inl.h"
10 #include "LocalAccessible-inl.h"
11 #include "mozilla/a11y/PDocAccessible.h"
12 #include "nsCocoaUtils.h"
13 #include "nsObjCExceptions.h"
14 #include "TextLeafAccessible.h"
16 #import "mozTextAccessible.h"
17 #import "GeckoTextMarker.h"
18 #import "MOXTextMarkerDelegate.h"
20 using namespace mozilla;
21 using namespace mozilla::a11y;
23 inline bool ToNSRange(id aValue, NSRange* aRange) {
24   MOZ_ASSERT(aRange, "aRange is nil");
26   if ([aValue isKindOfClass:[NSValue class]] &&
27       strcmp([(NSValue*)aValue objCType], @encode(NSRange)) == 0) {
28     *aRange = [aValue rangeValue];
29     return true;
30   }
32   return false;
35 inline NSString* ToNSString(id aValue) {
36   if ([aValue isKindOfClass:[NSString class]]) {
37     return aValue;
38   }
40   return nil;
43 @interface mozTextAccessible ()
44 - (long)textLength;
45 - (BOOL)isReadOnly;
46 - (NSString*)text;
47 - (GeckoTextMarkerRange)selection;
48 - (GeckoTextMarkerRange)textMarkerRangeFromRange:(NSValue*)range;
49 @end
51 @implementation mozTextAccessible
53 - (NSString*)moxTitle {
54   return @"";
57 - (id)moxValue {
58   // Apple's SpeechSynthesisServer expects AXValue to return an AXStaticText
59   // object's AXSelectedText attribute. See bug 674612 for details.
60   // Also if there is no selected text, we return the full text.
61   // See bug 369710 for details.
62   if ([[self moxRole] isEqualToString:NSAccessibilityStaticTextRole]) {
63     NSString* selectedText = [self moxSelectedText];
64     return (selectedText && [selectedText length]) ? selectedText : [self text];
65   }
67   return [self text];
70 - (id)moxRequired {
71   return @([self stateWithMask:states::REQUIRED] != 0);
74 - (NSString*)moxInvalid {
75   if ([self stateWithMask:states::INVALID] != 0) {
76     // If the attribute exists, it has one of four values: true, false,
77     // grammar, or spelling. We query the attribute value here in order
78     // to find the correct string to return.
79     RefPtr<AccAttributes> attributes;
80     HyperTextAccessibleBase* text = mGeckoAccessible->AsHyperTextBase();
81     if (text && mGeckoAccessible->IsTextRole()) {
82       attributes = text->DefaultTextAttributes();
83     }
85     nsAutoString invalidStr;
86     if (!attributes ||
87         !attributes->GetAttribute(nsGkAtoms::invalid, invalidStr)) {
88       return @"true";
89     }
90     return nsCocoaUtils::ToNSString(invalidStr);
91   }
93   // If the flag is not set, we return false.
94   return @"false";
97 - (NSNumber*)moxInsertionPointLineNumber {
98   MOZ_ASSERT(mGeckoAccessible);
100   int32_t lineNumber = -1;
101   if (mGeckoAccessible->IsLocal()) {
102     if (HyperTextAccessible* textAcc =
103             mGeckoAccessible->AsLocal()->AsHyperText()) {
104       lineNumber = textAcc->CaretLineNumber() - 1;
105     }
106   } else {
107     lineNumber = mGeckoAccessible->AsRemote()->CaretLineNumber() - 1;
108   }
110   return (lineNumber >= 0) ? [NSNumber numberWithInt:lineNumber] : nil;
113 - (NSString*)moxRole {
114   if ([self stateWithMask:states::MULTI_LINE]) {
115     return NSAccessibilityTextAreaRole;
116   }
118   return [super moxRole];
121 - (NSString*)moxSubrole {
122   MOZ_ASSERT(mGeckoAccessible);
124   if (mRole == roles::PASSWORD_TEXT) {
125     return NSAccessibilitySecureTextFieldSubrole;
126   }
128   if (mRole == roles::ENTRY) {
129     LocalAccessible* acc = mGeckoAccessible->AsLocal();
130     RemoteAccessible* proxy = mGeckoAccessible->AsRemote();
131     if ((acc && acc->IsSearchbox()) || (proxy && proxy->IsSearchbox())) {
132       return @"AXSearchField";
133     }
134   }
136   return nil;
139 - (NSNumber*)moxNumberOfCharacters {
140   return @([self textLength]);
143 - (NSString*)moxSelectedText {
144   GeckoTextMarkerRange selection = [self selection];
145   if (!selection.IsValid()) {
146     return nil;
147   }
149   return selection.Text();
152 - (NSValue*)moxSelectedTextRange {
153   GeckoTextMarkerRange selection = [self selection];
154   if (!selection.IsValid()) {
155     return nil;
156   }
158   GeckoTextMarkerRange fromStartToSelection(
159       GeckoTextMarker(mGeckoAccessible, 0), selection.mStart);
161   return [NSValue valueWithRange:NSMakeRange(fromStartToSelection.Length(),
162                                              selection.Length())];
165 - (NSValue*)moxVisibleCharacterRange {
166   // XXX this won't work with Textarea and such as we actually don't give
167   // the visible character range.
168   return [NSValue valueWithRange:NSMakeRange(0, [self textLength])];
171 - (BOOL)moxBlockSelector:(SEL)selector {
172   if (selector == @selector(moxSetValue:) && [self isReadOnly]) {
173     return YES;
174   }
176   return [super moxBlockSelector:selector];
179 - (void)moxSetValue:(id)value {
180   MOZ_ASSERT(mGeckoAccessible);
182   nsString text;
183   nsCocoaUtils::GetStringForNSString(value, text);
184   if (mGeckoAccessible->IsLocal()) {
185     if (HyperTextAccessible* textAcc =
186             mGeckoAccessible->AsLocal()->AsHyperText()) {
187       textAcc->ReplaceText(text);
188     }
189   } else {
190     mGeckoAccessible->AsRemote()->ReplaceText(text);
191   }
194 - (void)moxSetSelectedText:(NSString*)selectedText {
195   MOZ_ASSERT(mGeckoAccessible);
197   NSString* stringValue = ToNSString(selectedText);
198   if (!stringValue) {
199     return;
200   }
202   int32_t start = 0, end = 0;
203   nsString text;
204   if (mGeckoAccessible->IsLocal()) {
205     if (HyperTextAccessible* textAcc =
206             mGeckoAccessible->AsLocal()->AsHyperText()) {
207       textAcc->SelectionBoundsAt(0, &start, &end);
208       textAcc->DeleteText(start, end - start);
209       nsCocoaUtils::GetStringForNSString(stringValue, text);
210       textAcc->InsertText(text, start);
211     }
212   } else {
213     RemoteAccessible* proxy = mGeckoAccessible->AsRemote();
214     nsString data;
215     proxy->SelectionBoundsAt(0, data, &start, &end);
216     proxy->DeleteText(start, end - start);
217     nsCocoaUtils::GetStringForNSString(stringValue, text);
218     proxy->InsertText(text, start);
219   }
222 - (void)moxSetSelectedTextRange:(NSValue*)selectedTextRange {
223   GeckoTextMarkerRange markerRange =
224       [self textMarkerRangeFromRange:selectedTextRange];
226   if (markerRange.IsValid()) {
227     markerRange.Select();
228   }
231 - (void)moxSetVisibleCharacterRange:(NSValue*)visibleCharacterRange {
232   MOZ_ASSERT(mGeckoAccessible);
234   NSRange range;
235   if (!ToNSRange(visibleCharacterRange, &range)) {
236     return;
237   }
239   if (mGeckoAccessible->IsLocal()) {
240     if (HyperTextAccessible* textAcc =
241             mGeckoAccessible->AsLocal()->AsHyperText()) {
242       textAcc->ScrollSubstringTo(range.location, range.location + range.length,
243                                  nsIAccessibleScrollType::SCROLL_TYPE_TOP_EDGE);
244     }
245   } else {
246     mGeckoAccessible->AsRemote()->ScrollSubstringTo(
247         range.location, range.location + range.length,
248         nsIAccessibleScrollType::SCROLL_TYPE_TOP_EDGE);
249   }
252 - (NSString*)moxStringForRange:(NSValue*)range {
253   GeckoTextMarkerRange markerRange = [self textMarkerRangeFromRange:range];
255   if (!markerRange.IsValid()) {
256     return nil;
257   }
259   return markerRange.Text();
262 - (NSAttributedString*)moxAttributedStringForRange:(NSValue*)range {
263   GeckoTextMarkerRange markerRange = [self textMarkerRangeFromRange:range];
265   if (!markerRange.IsValid()) {
266     return nil;
267   }
269   return markerRange.AttributedText();
272 - (NSValue*)moxRangeForLine:(NSNumber*)line {
273   // XXX: actually get the integer value for the line #
274   return [NSValue valueWithRange:NSMakeRange(0, [self textLength])];
277 - (NSNumber*)moxLineForIndex:(NSNumber*)index {
278   // XXX: actually return the line #
279   return @0;
282 - (NSValue*)moxBoundsForRange:(NSValue*)range {
283   GeckoTextMarkerRange markerRange = [self textMarkerRangeFromRange:range];
285   if (!markerRange.IsValid()) {
286     return nil;
287   }
289   return markerRange.Bounds();
292 #pragma mark - mozAccessible
294 - (void)handleAccessibleTextChangeEvent:(NSString*)change
295                                inserted:(BOOL)isInserted
296                             inContainer:(Accessible*)container
297                                      at:(int32_t)start {
298   GeckoTextMarker startMarker(container, start);
299   NSDictionary* userInfo = @{
300     @"AXTextChangeElement" : self,
301     @"AXTextStateChangeType" : @(AXTextStateChangeTypeEdit),
302     @"AXTextChangeValues" : @[ @{
303       @"AXTextChangeValue" : (change ? change : @""),
304       @"AXTextChangeValueStartMarker" :
305           (__bridge id)startMarker.CreateAXTextMarker(),
306       @"AXTextEditType" : isInserted ? @(AXTextEditTypeTyping)
307                                      : @(AXTextEditTypeDelete)
308     } ]
309   };
311   mozAccessible* webArea = [self topWebArea];
312   [webArea moxPostNotification:NSAccessibilityValueChangedNotification
313                   withUserInfo:userInfo];
314   [self moxPostNotification:NSAccessibilityValueChangedNotification
315                withUserInfo:userInfo];
317   [self moxPostNotification:NSAccessibilityValueChangedNotification];
320 - (void)handleAccessibleEvent:(uint32_t)eventType {
321   switch (eventType) {
322     default:
323       [super handleAccessibleEvent:eventType];
324       break;
325   }
328 #pragma mark -
330 - (long)textLength {
331   return [[self text] length];
334 - (BOOL)isReadOnly {
335   return [self stateWithMask:states::EDITABLE] == 0;
338 - (NSString*)text {
339   // A password text field returns an empty value
340   if (mRole == roles::PASSWORD_TEXT) {
341     return @"";
342   }
344   id<MOXTextMarkerSupport> delegate = [self moxTextMarkerDelegate];
345   return [delegate
346       moxStringForTextMarkerRange:[delegate
347                                       moxTextMarkerRangeForUIElement:self]];
350 - (GeckoTextMarkerRange)selection {
351   MOZ_ASSERT(mGeckoAccessible);
353   id<MOXTextMarkerSupport> delegate = [self moxTextMarkerDelegate];
354   GeckoTextMarkerRange selection =
355       [static_cast<MOXTextMarkerDelegate*>(delegate) selection];
357   if (!selection.IsValid() || !selection.Crop(mGeckoAccessible)) {
358     // The selection is not in this accessible. Return invalid range.
359     return GeckoTextMarkerRange();
360   }
362   return selection;
365 - (GeckoTextMarkerRange)textMarkerRangeFromRange:(NSValue*)range {
366   NSRange r = [range rangeValue];
368   GeckoTextMarker startMarker =
369       GeckoTextMarker::MarkerFromIndex(mGeckoAccessible, r.location);
371   GeckoTextMarker endMarker =
372       GeckoTextMarker::MarkerFromIndex(mGeckoAccessible, r.location + r.length);
374   return GeckoTextMarkerRange(startMarker, endMarker);
377 @end
379 @implementation mozTextLeafAccessible
381 - (BOOL)moxBlockSelector:(SEL)selector {
382   if (selector == @selector(moxChildren) || selector == @selector
383                                                 (moxTitleUIElement)) {
384     return YES;
385   }
387   return [super moxBlockSelector:selector];
390 - (NSString*)moxValue {
391   return [super moxTitle];
394 - (NSString*)moxTitle {
395   return nil;
398 - (NSString*)moxLabel {
399   return nil;
402 - (NSString*)moxStringForRange:(NSValue*)range {
403   MOZ_ASSERT(mGeckoAccessible);
405   NSRange r = [range rangeValue];
406   GeckoTextMarkerRange textMarkerRange(mGeckoAccessible);
407   textMarkerRange.mStart.mOffset += r.location;
408   textMarkerRange.mEnd.mOffset =
409       textMarkerRange.mStart.mOffset + r.location + r.length;
411   return textMarkerRange.Text();
414 - (NSAttributedString*)moxAttributedStringForRange:(NSValue*)range {
415   MOZ_ASSERT(mGeckoAccessible);
417   NSRange r = [range rangeValue];
418   GeckoTextMarkerRange textMarkerRange(mGeckoAccessible);
419   textMarkerRange.mStart.mOffset += r.location;
420   textMarkerRange.mEnd.mOffset =
421       textMarkerRange.mStart.mOffset + r.location + r.length;
423   return textMarkerRange.AttributedText();
426 - (NSValue*)moxBoundsForRange:(NSValue*)range {
427   MOZ_ASSERT(mGeckoAccessible);
429   NSRange r = [range rangeValue];
430   GeckoTextMarkerRange textMarkerRange(mGeckoAccessible);
432   textMarkerRange.mStart.mOffset += r.location;
433   textMarkerRange.mEnd.mOffset = textMarkerRange.mStart.mOffset + r.length;
435   return textMarkerRange.Bounds();
438 @end