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 (HyperTextAccessibleBase* textAcc = mGeckoAccessible->AsHyperTextBase()) {
102 lineNumber = textAcc->CaretLineNumber() - 1;
105 return (lineNumber >= 0) ? [NSNumber numberWithInt:lineNumber] : nil;
108 - (NSString*)moxRole {
109 if (mRole == roles::ENTRY && [self stateWithMask:states::MULTI_LINE]) {
110 return NSAccessibilityTextAreaRole;
113 return [super moxRole];
116 - (NSString*)moxSubrole {
117 MOZ_ASSERT(mGeckoAccessible);
119 if (mRole == roles::PASSWORD_TEXT) {
120 return NSAccessibilitySecureTextFieldSubrole;
123 if (mRole == roles::ENTRY && mGeckoAccessible->IsSearchbox()) {
124 return @"AXSearchField";
130 - (NSNumber*)moxNumberOfCharacters {
131 return @([self textLength]);
134 - (NSString*)moxSelectedText {
135 GeckoTextMarkerRange selection = [self selection];
136 if (!selection.IsValid()) {
140 return selection.Text();
143 - (NSValue*)moxSelectedTextRange {
144 GeckoTextMarkerRange selection = [self selection];
145 if (!selection.IsValid()) {
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]) {
167 return [super moxBlockSelector:selector];
170 - (void)moxSetValue:(id)value {
171 MOZ_ASSERT(mGeckoAccessible);
174 nsCocoaUtils::GetStringForNSString(value, text);
175 if (HyperTextAccessibleBase* textAcc = mGeckoAccessible->AsHyperTextBase()) {
176 textAcc->ReplaceText(text);
180 - (void)moxSetSelectedText:(NSString*)selectedText {
181 MOZ_ASSERT(mGeckoAccessible);
183 NSString* stringValue = ToNSString(selectedText);
188 HyperTextAccessibleBase* textAcc = mGeckoAccessible->AsHyperTextBase();
192 int32_t start = 0, end = 0;
193 textAcc->SelectionBoundsAt(0, &start, &end);
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();
210 - (void)moxSetVisibleCharacterRange:(NSValue*)visibleCharacterRange {
211 MOZ_ASSERT(mGeckoAccessible);
214 if (!ToNSRange(visibleCharacterRange, &range)) {
218 if (HyperTextAccessibleBase* textAcc = mGeckoAccessible->AsHyperTextBase()) {
219 textAcc->ScrollSubstringTo(range.location, range.location + range.length,
220 nsIAccessibleScrollType::SCROLL_TYPE_TOP_EDGE);
224 - (NSString*)moxStringForRange:(NSValue*)range {
225 GeckoTextMarkerRange markerRange = [self textMarkerRangeFromRange:range];
227 if (!markerRange.IsValid()) {
231 return markerRange.Text();
234 - (NSAttributedString*)moxAttributedStringForRange:(NSValue*)range {
235 GeckoTextMarkerRange markerRange = [self textMarkerRangeFromRange:range];
237 if (!markerRange.IsValid()) {
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 #
254 - (NSValue*)moxBoundsForRange:(NSValue*)range {
255 GeckoTextMarkerRange markerRange = [self textMarkerRangeFromRange:range];
257 if (!markerRange.IsValid()) {
261 return markerRange.Bounds();
264 #pragma mark - mozAccessible
266 - (void)handleAccessibleTextChangeEvent:(NSString*)change
267 inserted:(BOOL)isInserted
268 inContainer:(Accessible*)container
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)
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 {
295 [super handleAccessibleEvent:eventType];
303 return [[self text] length];
307 return [self stateWithMask:states::EDITABLE] == 0;
311 // A password text field returns an empty value
312 if (mRole == roles::PASSWORD_TEXT) {
316 id<MOXTextMarkerSupport> delegate = [self moxTextMarkerDelegate];
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();
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);
351 @implementation mozTextLeafAccessible
353 - (BOOL)moxBlockSelector:(SEL)selector {
354 if (selector == @selector(moxChildren) || selector == @selector
355 (moxTitleUIElement)) {
359 return [super moxBlockSelector:selector];
362 - (NSString*)moxValue {
363 NSString* val = [super moxTitle];
364 return [val length] ? val : nil;
367 - (NSString*)moxTitle {
371 - (NSString*)moxLabel {
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,
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();