📄 accessibilityuielementmac.mm
字号:
/* * Copyright (C) 2008 Apple Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#import "DumpRenderTree.h"#import "AccessibilityUIElement.h"#import <Foundation/Foundation.h>#import <JavaScriptCore/JSStringRef.h>#import <JavaScriptCore/JSStringRefCF.h>#import <WebKit/WebFrame.h>#import <WebKit/WebHTMLView.h>#import <WebKit/WebTypesInternal.h>#import <wtf/RetainPtr.h>#import <wtf/Vector.h>@interface NSObject (WebKitAccessibilityArrayCategory)- (NSArray *)accessibilityArrayAttributeValues:(NSString *)attribute index:(NSUInteger)index maxCount:(NSUInteger)maxCount;@endAccessibilityUIElement::AccessibilityUIElement(PlatformUIElement element) : m_element(element){ [m_element retain];}AccessibilityUIElement::AccessibilityUIElement(const AccessibilityUIElement& other) : m_element(other.m_element){ [m_element retain];}AccessibilityUIElement::~AccessibilityUIElement(){ [m_element release];}@interface NSString (JSStringRefAdditions)+ (NSString *)stringWithJSStringRef:(JSStringRef)jsStringRef;- (JSStringRef)createJSStringRef;@end@implementation NSString (JSStringRefAdditions)+ (NSString *)stringWithJSStringRef:(JSStringRef)jsStringRef{ if (!jsStringRef) return NULL; CFStringRef cfString = JSStringCopyCFString(kCFAllocatorDefault, jsStringRef); return [(NSString *)cfString autorelease];}- (JSStringRef)createJSStringRef{ return JSStringCreateWithCFString((CFStringRef)self);}@endstatic NSString* descriptionOfValue(id valueObject, id focusedAccessibilityObject){ if (!valueObject) return NULL; if ([valueObject isKindOfClass:[NSArray class]]) return [NSString stringWithFormat:@"<array of size %d>", [(NSArray*)valueObject count]]; if ([valueObject isKindOfClass:[NSNumber class]]) return [(NSNumber*)valueObject stringValue]; if ([valueObject isKindOfClass:[NSValue class]]) { NSString* type = [NSString stringWithCString:[valueObject objCType] encoding:NSASCIIStringEncoding]; NSValue* value = (NSValue*)valueObject; if ([type rangeOfString:@"NSRect"].length > 0) return [NSString stringWithFormat:@"NSRect: %@", NSStringFromRect([value rectValue])]; if ([type rangeOfString:@"NSPoint"].length > 0) return [NSString stringWithFormat:@"NSPoint: %@", NSStringFromPoint([value pointValue])]; if ([type rangeOfString:@"NSSize"].length > 0) return [NSString stringWithFormat:@"NSSize: %@", NSStringFromSize([value sizeValue])]; if ([type rangeOfString:@"NSRange"].length > 0) return [NSString stringWithFormat:@"NSRange: %@", NSStringFromRange([value rangeValue])]; } // Strip absolute URL paths NSString* description = [valueObject description]; NSRange range = [description rangeOfString:@"LayoutTests"]; if (range.length) return [description substringFromIndex:range.location]; // Strip pointer locations if ([description rangeOfString:@"0x"].length) { NSString* role = [focusedAccessibilityObject accessibilityAttributeValue:@"AXRole"]; NSString* title = [focusedAccessibilityObject accessibilityAttributeValue:@"AXTitle"]; if ([title length]) return [NSString stringWithFormat:@"<%@: '%@'>", role, title]; return [NSString stringWithFormat:@"<%@>", role]; } return [valueObject description];}static NSString* attributesOfElement(id accessibilityObject){ NSArray* supportedAttributes = [accessibilityObject accessibilityAttributeNames]; NSMutableString* attributesString = [NSMutableString string]; for (NSUInteger i = 0; i < [supportedAttributes count]; ++i) { NSString* attribute = [supportedAttributes objectAtIndex:i]; // Right now, position provides useless and screen-specific information, so we do not // want to include it for the sake of universally passing tests. if ([attribute isEqualToString:@"AXPosition"]) continue; // accessibilityAttributeValue: can throw an if an attribute is not returned. // For DumpRenderTree's purpose, we should ignore those exceptions @try { id valueObject = [accessibilityObject accessibilityAttributeValue:attribute]; NSString* value = descriptionOfValue(valueObject, accessibilityObject); [attributesString appendFormat:@"%@: %@\n", attribute, value]; } @catch (NSException* e) { } } return attributesString;}static JSStringRef concatenateAttributeAndValue(NSString* attribute, NSString* value){ Vector<UniChar> buffer([attribute length]); [attribute getCharacters:buffer.data()]; buffer.append(':'); buffer.append(' '); Vector<UniChar> valueBuffer([value length]); [value getCharacters:valueBuffer.data()]; buffer.append(valueBuffer); return JSStringCreateWithCharacters(buffer.data(), buffer.size());}static void convertNSArrayToVector(NSArray* array, Vector<AccessibilityUIElement>& elementVector){ NSUInteger count = [array count]; for (NSUInteger i = 0; i < count; ++i) elementVector.append(AccessibilityUIElement([array objectAtIndex:i]));}static JSStringRef descriptionOfElements(Vector<AccessibilityUIElement>& elementVector){ NSMutableString* allElementString = [NSMutableString string]; size_t size = elementVector.size(); for (size_t i = 0; i < size; ++i) { NSString* attributes = attributesOfElement(elementVector[i].platformUIElement()); [allElementString appendFormat:@"%@\n------------\n", attributes]; } return [allElementString createJSStringRef];}void AccessibilityUIElement::getLinkedUIElements(Vector<AccessibilityUIElement>& elementVector){ NSArray* linkedElements = [m_element accessibilityAttributeValue:NSAccessibilityLinkedUIElementsAttribute]; convertNSArrayToVector(linkedElements, elementVector);}void AccessibilityUIElement::getDocumentLinks(Vector<AccessibilityUIElement>& elementVector){ NSArray* linkElements = [m_element accessibilityAttributeValue:@"AXLinkUIElements"]; convertNSArrayToVector(linkElements, elementVector);}void AccessibilityUIElement::getChildren(Vector<AccessibilityUIElement>& elementVector){ NSArray* children = [m_element accessibilityAttributeValue:NSAccessibilityChildrenAttribute]; convertNSArrayToVector(children, elementVector);}void AccessibilityUIElement::getChildrenWithRange(Vector<AccessibilityUIElement>& elementVector, unsigned location, unsigned length){ NSArray* children = [m_element accessibilityArrayAttributeValues:NSAccessibilityChildrenAttribute index:location maxCount:length]; convertNSArrayToVector(children, elementVector);}AccessibilityUIElement AccessibilityUIElement::getChildAtIndex(unsigned index){ Vector<AccessibilityUIElement> children; getChildrenWithRange(children, index, 1); if (children.size() == 1) return children[0]; return nil;}AccessibilityUIElement AccessibilityUIElement::titleUIElement(){ id accessibilityObject = [m_element accessibilityAttributeValue:NSAccessibilityTitleUIElementAttribute]; if (accessibilityObject) return AccessibilityUIElement(accessibilityObject); return nil;}AccessibilityUIElement AccessibilityUIElement::parentElement(){ id accessibilityObject = [m_element accessibilityAttributeValue:NSAccessibilityParentAttribute]; if (accessibilityObject) return AccessibilityUIElement(accessibilityObject); return nil;}JSStringRef AccessibilityUIElement::attributesOfLinkedUIElements(){ Vector<AccessibilityUIElement> linkedElements;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -