no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / accessible / mac / mozTextAccessible.mm
blob4993e220d228af458c50fd0544b4a1235996d426
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 (HyperTextAccessibleBase* textAcc = mGeckoAccessible->AsHyperTextBase()) {
102     lineNumber = textAcc->CaretLineNumber() - 1;
103   }
105   return (lineNumber >= 0) ? [NSNumber numberWithInt:lineNumber] : nil;
108 - (NSString*)moxRole {
109   if ([self stateWithMask:states::MULTI_LINE]) {
110     return NSAccessibilityTextAreaRole;
111   }
113   return [super moxRole];
116 - (NSString*)moxSubrole {
117   MOZ_ASSERT(mGeckoAccessible);
119   if (mRole == roles::PASSWORD_TEXT) {
120     return NSAccessibilitySecureTextFieldSubrole;
121   }
123   if (mRole == roles::ENTRY && mGeckoAccessible->IsSearchbox()) {
124     return @"AXSearchField";
125   }
127   return nil;
130 - (NSNumber*)moxNumberOfCharacters {
131   return @([self textLength]);
134 - (NSString*)moxSelectedText {
135   GeckoTextMarkerRange selection = [self selection];
136   if (!selection.IsValid()) {
137     return nil;
138   }
140   return selection.Text();
143 - (NSValue*)moxSelectedTextRange {
144   GeckoTextMarkerRange selection = [self selection];
145   if (!selection.IsValid()) {
146     return nil;
147   }
149   GeckoTextMarkerRange fromStartToSelection(
150       GeckoTextMarker(mGeckoAccessible, 0), selection.Start());
152   return [NSValue valueWithRange:NSMakeRange(fromStartToSelection.Length(),
153                                              selection.Length())];
156 - (NSValue*)moxVisibleCharacterRange {
157   // XXX this won't work with Textarea and such as we actually don't give
158   // the visible character range.
159   return [NSValue valueWithRange:NSMakeRange(0, [self textLength])];
162 - (BOOL)moxBlockSelector:(SEL)selector {
163   if (selector == @selector(moxSetValue:) && [self isReadOnly]) {
164     return YES;
165   }
167   return [super moxBlockSelector:selector];
170 - (void)moxSetValue:(id)value {
171   MOZ_ASSERT(mGeckoAccessible);
173   nsString text;
174   nsCocoaUtils::GetStringForNSString(value, text);
175   if (HyperTextAccessibleBase* textAcc = mGeckoAccessible->AsHyperTextBase()) {
176     textAcc->ReplaceText(text);
177   }
180 - (void)moxSetSelectedText:(NSString*)selectedText {
181   MOZ_ASSERT(mGeckoAccessible);
183   NSString* stringValue = ToNSString(selectedText);
184   if (!stringValue) {
185     return;
186   }
188   HyperTextAccessibleBase* textAcc = mGeckoAccessible->AsHyperTextBase();
189   if (!textAcc) {
190     return;
191   }
192   int32_t start = 0, end = 0;
193   textAcc->SelectionBoundsAt(0, &start, &end);
194   nsString text;
195   nsCocoaUtils::GetStringForNSString(stringValue, text);
196   textAcc->SelectionBoundsAt(0, &start, &end);
197   textAcc->DeleteText(start, end - start);
198   textAcc->InsertText(text, start);
201 - (void)moxSetSelectedTextRange:(NSValue*)selectedTextRange {
202   GeckoTextMarkerRange markerRange =
203       [self textMarkerRangeFromRange:selectedTextRange];
205   if (markerRange.IsValid()) {
206     markerRange.Select();
207   }
210 - (void)moxSetVisibleCharacterRange:(NSValue*)visibleCharacterRange {
211   MOZ_ASSERT(mGeckoAccessible);
213   NSRange range;
214   if (!ToNSRange(visibleCharacterRange, &range)) {
215     return;
216   }
218   if (HyperTextAccessibleBase* textAcc = mGeckoAccessible->AsHyperTextBase()) {
219     textAcc->ScrollSubstringTo(range.location, range.location + range.length,
220                                nsIAccessibleScrollType::SCROLL_TYPE_TOP_EDGE);
221   }
224 - (NSString*)moxStringForRange:(NSValue*)range {
225   GeckoTextMarkerRange markerRange = [self textMarkerRangeFromRange:range];
227   if (!markerRange.IsValid()) {
228     return nil;
229   }
231   return markerRange.Text();
234 - (NSAttributedString*)moxAttributedStringForRange:(NSValue*)range {
235   GeckoTextMarkerRange markerRange = [self textMarkerRangeFromRange:range];
237   if (!markerRange.IsValid()) {
238     return nil;
239   }
241   return markerRange.AttributedText();
244 - (NSValue*)moxRangeForLine:(NSNumber*)line {
245   // XXX: actually get the integer value for the line #
246   return [NSValue valueWithRange:NSMakeRange(0, [self textLength])];
249 - (NSNumber*)moxLineForIndex:(NSNumber*)index {
250   // XXX: actually return the line #
251   return @0;
254 - (NSValue*)moxBoundsForRange:(NSValue*)range {
255   GeckoTextMarkerRange markerRange = [self textMarkerRangeFromRange:range];
257   if (!markerRange.IsValid()) {
258     return nil;
259   }
261   return markerRange.Bounds();
264 #pragma mark - mozAccessible
266 - (void)handleAccessibleTextChangeEvent:(NSString*)change
267                                inserted:(BOOL)isInserted
268                             inContainer:(Accessible*)container
269                                      at:(int32_t)start {
270   GeckoTextMarker startMarker(container, start);
271   NSDictionary* userInfo = @{
272     @"AXTextChangeElement" : self,
273     @"AXTextStateChangeType" : @(AXTextStateChangeTypeEdit),
274     @"AXTextChangeValues" : @[ @{
275       @"AXTextChangeValue" : (change ? change : @""),
276       @"AXTextChangeValueStartMarker" :
277           (__bridge id)startMarker.CreateAXTextMarker(),
278       @"AXTextEditType" : isInserted ? @(AXTextEditTypeTyping)
279                                      : @(AXTextEditTypeDelete)
280     } ]
281   };
283   mozAccessible* webArea = [self topWebArea];
284   [webArea moxPostNotification:NSAccessibilityValueChangedNotification
285                   withUserInfo:userInfo];
286   [self moxPostNotification:NSAccessibilityValueChangedNotification
287                withUserInfo:userInfo];
289   [self moxPostNotification:NSAccessibilityValueChangedNotification];
292 - (void)handleAccessibleEvent:(uint32_t)eventType {
293   switch (eventType) {
294     default:
295       [super handleAccessibleEvent:eventType];
296       break;
297   }
300 #pragma mark -
302 - (long)textLength {
303   return [[self text] length];
306 - (BOOL)isReadOnly {
307   return [self stateWithMask:states::EDITABLE] == 0;
310 - (NSString*)text {
311   // A password text field returns an empty value
312   if (mRole == roles::PASSWORD_TEXT) {
313     return @"";
314   }
316   id<MOXTextMarkerSupport> delegate = [self moxTextMarkerDelegate];
317   return [delegate
318       moxStringForTextMarkerRange:[delegate
319                                       moxTextMarkerRangeForUIElement:self]];
322 - (GeckoTextMarkerRange)selection {
323   MOZ_ASSERT(mGeckoAccessible);
325   id<MOXTextMarkerSupport> delegate = [self moxTextMarkerDelegate];
326   GeckoTextMarkerRange selection =
327       [static_cast<MOXTextMarkerDelegate*>(delegate) selection];
329   if (!selection.IsValid() || !selection.Crop(mGeckoAccessible)) {
330     // The selection is not in this accessible. Return invalid range.
331     return GeckoTextMarkerRange();
332   }
334   return selection;
337 - (GeckoTextMarkerRange)textMarkerRangeFromRange:(NSValue*)range {
338   NSRange r = [range rangeValue];
340   GeckoTextMarker startMarker =
341       GeckoTextMarker::MarkerFromIndex(mGeckoAccessible, r.location);
343   GeckoTextMarker endMarker =
344       GeckoTextMarker::MarkerFromIndex(mGeckoAccessible, r.location + r.length);
346   return GeckoTextMarkerRange(startMarker, endMarker);
349 @end
351 @implementation mozTextLeafAccessible
353 - (BOOL)moxBlockSelector:(SEL)selector {
354   if (selector == @selector(moxChildren) || selector == @selector
355                                                 (moxTitleUIElement)) {
356     return YES;
357   }
359   return [super moxBlockSelector:selector];
362 - (NSString*)moxValue {
363   NSString* val = [super moxTitle];
364   return [val length] ? val : nil;
367 - (NSString*)moxTitle {
368   return nil;
371 - (NSString*)moxLabel {
372   return nil;
375 - (BOOL)moxIgnoreWithParent:(mozAccessible*)parent {
376   // Don't render text nodes that are completely empty
377   // or those that should be ignored based on our
378   // standard ignore rules
379   return [self moxValue] == nil || [super moxIgnoreWithParent:parent];
382 static GeckoTextMarkerRange TextMarkerSubrange(Accessible* aAccessible,
383                                                NSValue* aRange) {
384   GeckoTextMarkerRange textMarkerRange(aAccessible);
385   GeckoTextMarker start = textMarkerRange.Start();
386   GeckoTextMarker end = textMarkerRange.End();
388   NSRange r = [aRange rangeValue];
389   start.Offset() += r.location;
390   end.Offset() = start.Offset() + r.length;
392   textMarkerRange = GeckoTextMarkerRange(start, end);
393   // Crop range to accessible
394   textMarkerRange.Crop(aAccessible);
396   return textMarkerRange;
399 - (NSString*)moxStringForRange:(NSValue*)range {
400   MOZ_ASSERT(mGeckoAccessible);
401   GeckoTextMarkerRange textMarkerRange =
402       TextMarkerSubrange(mGeckoAccessible, range);
404   return textMarkerRange.Text();
407 - (NSAttributedString*)moxAttributedStringForRange:(NSValue*)range {
408   MOZ_ASSERT(mGeckoAccessible);
409   GeckoTextMarkerRange textMarkerRange =
410       TextMarkerSubrange(mGeckoAccessible, range);
412   return textMarkerRange.AttributedText();
415 - (NSValue*)moxBoundsForRange:(NSValue*)range {
416   MOZ_ASSERT(mGeckoAccessible);
417   GeckoTextMarkerRange textMarkerRange =
418       TextMarkerSubrange(mGeckoAccessible, range);
420   return textMarkerRange.Bounds();
423 @end