This repository has been archived by the owner on Sep 4, 2018. It is now read-only.
/
Viewer.m
178 lines (146 loc) · 5.74 KB
/
Viewer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#import <UIKit/UIKit.h>
#import "Viewer.h"
static Viewer *sharedViewer = nil;
@interface NSObject (UIAccessibilityViewer)
-(void)appendToXml:(NSMutableString*)xml;
-(void)appendTraitsToXml:(NSMutableString*)xml;
-(void)appendFrameToXml:(NSMutableString*)xml;
-(void)appendOpenToXml:(NSMutableString*)xml;
-(void)appendCloseToXml:(NSMutableString*)xml;
-(void)appendChildrenToXml:(NSMutableString*)xml;
@end
@implementation NSObject (UIAccessibilityViewer)
-(void)appendOpenToXml:(NSMutableString*)xml {
[xml appendFormat: @"<%@", NSStringFromClass([self class])];
if ([self isAccessibilityElement]) {
[self appendTraitsToXml: xml];
if ([[self accessibilityLabel] length] > 0) {
NSString *escaped_label = [self accessibilityLabel];
escaped_label = [escaped_label stringByReplacingOccurrencesOfString: @"&" withString: @"&"];
escaped_label = [escaped_label stringByReplacingOccurrencesOfString: @"\"" withString: @"""];
escaped_label = [escaped_label stringByReplacingOccurrencesOfString: @"'" withString: @"'"];
escaped_label = [escaped_label stringByReplacingOccurrencesOfString: @"\\" withString: @"'"];
escaped_label = [escaped_label stringByReplacingOccurrencesOfString: @">" withString: @">"];
escaped_label = [escaped_label stringByReplacingOccurrencesOfString: @"<" withString: @"<"];
[xml appendFormat: @"label=\"%@\" ", escaped_label];
}
if ([[self accessibilityHint] length] > 0) {
[xml appendFormat: @"hint=\"%@\" ", [self accessibilityHint]];
}
if ([[self accessibilityValue] length] > 0) {
NSString *escaped_value = [self accessibilityValue];
escaped_value = [escaped_value stringByReplacingOccurrencesOfString: @"&" withString: @"&"];
escaped_value = [escaped_value stringByReplacingOccurrencesOfString: @"\"" withString: @"""];
escaped_value = [escaped_value stringByReplacingOccurrencesOfString: @"'" withString: @"'"];
escaped_value = [escaped_value stringByReplacingOccurrencesOfString: @"\\" withString: @"'"];
escaped_value = [escaped_value stringByReplacingOccurrencesOfString: @">" withString: @">"];
escaped_value = [escaped_value stringByReplacingOccurrencesOfString: @"<" withString: @"<"];
[xml appendFormat: @"value=\"%@\" ", escaped_value];
}
}
[xml appendString: @">"];
}
-(void)appendTraitsToXml:(NSMutableString *)xml {
[xml appendString: @" traits=\""];
if ([self accessibilityTraits] & UIAccessibilityTraitButton) {
[xml appendString: @"button "];
}
if ([self accessibilityTraits] & UIAccessibilityTraitLink) {
[xml appendString: @"link "];
}
if ([self accessibilityTraits] & UIAccessibilityTraitSearchField) {
[xml appendString: @"search_field "];
}
if ([self accessibilityTraits] & UIAccessibilityTraitImage) {
[xml appendString: @"image "];
}
if ([self accessibilityTraits] & UIAccessibilityTraitSelected) {
[xml appendString: @"selected "];
}
if ([self accessibilityTraits] & UIAccessibilityTraitPlaysSound) {
[xml appendString: @"plays_sound "];
}
if ([self accessibilityTraits] & UIAccessibilityTraitKeyboardKey) {
[xml appendString: @"keyboard_key "];
}
if ([self accessibilityTraits] & UIAccessibilityTraitStaticText) {
[xml appendString: @"static_text "];
}
if ([self accessibilityTraits] & UIAccessibilityTraitSummaryElement) {
[xml appendString: @"summary_element "];
}
if ([self accessibilityTraits] & UIAccessibilityTraitNotEnabled) {
[xml appendString: @"not_enabled "];
}
if ([self accessibilityTraits] & UIAccessibilityTraitUpdatesFrequently) {
[xml appendString: @"updates_frequently "];
}
[xml appendString: @"\" "];
}
-(void)appendFrameToXml:(NSMutableString *)xml {
CGRect frame = [self accessibilityFrame];
[xml appendFormat: @"<frame x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\"/>",
frame.origin.x, frame.origin.y, frame.size.width, frame.size.height];
}
-(void)appendCloseToXml:(NSMutableString *)xml {
[xml appendFormat: @"</%@>", NSStringFromClass([self class])];
}
-(void)appendChildrenToXml:(NSMutableString *)xml {
// Bug that accessibilityElementCount is returning 2147483647 rather than 0?
if ([self accessibilityElementCount] != 2147483647) {
for (int i = 0; i < [self accessibilityElementCount]; i++) {
id accessibilityElement = [self accessibilityElementAtIndex: i];
[accessibilityElement appendToXml: xml];
}
}
}
-(void)appendToXml:(NSMutableString *)xml {
[self appendOpenToXml: xml];
[self appendFrameToXml: xml];
[self appendChildrenToXml: xml];
[self appendCloseToXml: xml];
}
@end
@implementation UITableView (AccessibilityVisibilityFix)
-(void)appendChildrenToXml:(NSMutableString *)xml {
// Ignore the accessibility interface here because it doesn't allow us a way to tell if a cell is visible.
for (UIView *view in [self visibleCells]) {
[view appendToXml: xml];
}
}
@end
@interface UIView (Viewer)
-(void)appendToXml:(NSMutableString *)xml;
@end
@implementation UIView (Viewer)
-(void)appendToXml:(NSMutableString *)xml {
[self appendOpenToXml: xml];
if ([self isAccessibilityElement]) {
[self appendFrameToXml: xml];
}
for (UIView *view in self.subviews) {
[view appendToXml: xml];
}
[self appendChildrenToXml: xml];
[self appendCloseToXml: xml];
}
@end
@implementation Viewer
+(Viewer *)sharedViewer {
if (sharedViewer == nil) {
sharedViewer = [[super allocWithZone:NULL] init];
}
return sharedViewer;
}
-(NSString *)screen {
NSMutableString *xml = [NSMutableString stringWithString: @"<screen>"];
CGRect frame = [[UIScreen mainScreen] applicationFrame];
[xml appendFormat: @"<frame x=\"%f\" y=\"%f\" width=\"%f\" height=\"%f\"/>",
frame.origin.x, frame.origin.y, frame.size.width, frame.size.height];
for (UIWindow *window in [UIApplication sharedApplication].windows) {
[window appendToXml: xml];
}
[xml appendString: @"</screen>"];
return xml;
}
@end