2 /* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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];
35 inline NSString* ToNSString(id aValue) {
36 if ([aValue isKindOfClass:[NSString class]]) {
43 @interface mozTextAccessible ()
47 - (GeckoTextMarkerRange)selection;
48 - (GeckoTextMarkerRange)textMarkerRangeFromRange:(NSValue*)range;
51 @implementation mozTextAccessible
53 - (NSString*)moxTitle {
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];
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();
85 nsAutoString invalidStr;
87 !attributes->GetAttribute(nsGkAtoms::invalid, invalidStr)) {
90 return nsCocoaUtils::ToNSString(invalidStr);
93 // If the flag is not set, we 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;
107 lineNumber = mGeckoAccessible->AsRemote()->CaretLineNumber() - 1;
110 return (lineNumber >= 0) ? [NSNumber numberWithInt:lineNumber] : nil;
113 - (NSString*)moxRole {
114 if ([self stateWithMask:states::MULTI_LINE]) {
115 return NSAccessibilityTextAreaRole;
118 return [super moxRole];
121 - (NSString*)moxSubrole {
122 MOZ_ASSERT(mGeckoAccessible);
124 if (mRole == roles::PASSWORD_TEXT) {
125 return NSAccessibilitySecureTextFieldSubrole;
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";
139 - (NSNumber*)moxNumberOfCharacters {
140 return @([self textLength]);
143 - (NSString*)moxSelectedText {
144 GeckoTextMarkerRange selection = [self selection];
145 if (!selection.IsValid()) {
149 return selection.Text();
152 - (NSValue*)moxSelectedTextRange {
153 GeckoTextMarkerRange selection = [self selection];
154 if (!selection.IsValid()) {
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]) {
176 return [super moxBlockSelector:selector];
179 - (void)moxSetValue:(id)value {
180 MOZ_ASSERT(mGeckoAccessible);
183 nsCocoaUtils::GetStringForNSString(value, text);
184 if (mGeckoAccessible->IsLocal()) {
185 if (HyperTextAccessible* textAcc =
186 mGeckoAccessible->AsLocal()->AsHyperText()) {
187 textAcc->ReplaceText(text);
190 mGeckoAccessible->AsRemote()->ReplaceText(text);
194 - (void)moxSetSelectedText:(NSString*)selectedText {
195 MOZ_ASSERT(mGeckoAccessible);
197 NSString* stringValue = ToNSString(selectedText);
202 int32_t start = 0, end = 0;
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);
213 RemoteAccessible* proxy = mGeckoAccessible->AsRemote();
215 proxy->SelectionBoundsAt(0, data, &start, &end);
216 proxy->DeleteText(start, end - start);
217 nsCocoaUtils::GetStringForNSString(stringValue, text);
218 proxy->InsertText(text, start);
222 - (void)moxSetSelectedTextRange:(NSValue*)selectedTextRange {
223 GeckoTextMarkerRange markerRange =
224 [self textMarkerRangeFromRange:selectedTextRange];
226 if (markerRange.IsValid()) {
227 markerRange.Select();
231 - (void)moxSetVisibleCharacterRange:(NSValue*)visibleCharacterRange {
232 MOZ_ASSERT(mGeckoAccessible);
235 if (!ToNSRange(visibleCharacterRange, &range)) {
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);
246 mGeckoAccessible->AsRemote()->ScrollSubstringTo(
247 range.location, range.location + range.length,
248 nsIAccessibleScrollType::SCROLL_TYPE_TOP_EDGE);
252 - (NSString*)moxStringForRange:(NSValue*)range {
253 GeckoTextMarkerRange markerRange = [self textMarkerRangeFromRange:range];
255 if (!markerRange.IsValid()) {
259 return markerRange.Text();
262 - (NSAttributedString*)moxAttributedStringForRange:(NSValue*)range {
263 GeckoTextMarkerRange markerRange = [self textMarkerRangeFromRange:range];
265 if (!markerRange.IsValid()) {
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 #
282 - (NSValue*)moxBoundsForRange:(NSValue*)range {
283 GeckoTextMarkerRange markerRange = [self textMarkerRangeFromRange:range];
285 if (!markerRange.IsValid()) {
289 return markerRange.Bounds();
292 #pragma mark - mozAccessible
294 - (void)handleAccessibleTextChangeEvent:(NSString*)change
295 inserted:(BOOL)isInserted
296 inContainer:(Accessible*)container
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)
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 {
323 [super handleAccessibleEvent:eventType];
331 return [[self text] length];
335 return [self stateWithMask:states::EDITABLE] == 0;
339 // A password text field returns an empty value
340 if (mRole == roles::PASSWORD_TEXT) {
344 id<MOXTextMarkerSupport> delegate = [self moxTextMarkerDelegate];
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();
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);
379 @implementation mozTextLeafAccessible
381 - (BOOL)moxBlockSelector:(SEL)selector {
382 if (selector == @selector(moxChildren) || selector == @selector
383 (moxTitleUIElement)) {
387 return [super moxBlockSelector:selector];
390 - (NSString*)moxValue {
391 return [super moxTitle];
394 - (NSString*)moxTitle {
398 - (NSString*)moxLabel {
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();