-
Notifications
You must be signed in to change notification settings - Fork 3
/
External.mm
1202 lines (1012 loc) · 35.4 KB
/
External.mm
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
External code for PortoApp.
All copyrights are inside Bozo.m
*/
/* External {{{ */
/* Keychain {{{ */
@interface KeychainItemWrapper : NSObject {
NSMutableDictionary *keychainItemData; // The actual keychain item data backing store.
NSMutableDictionary *genericPasswordQuery; // A placeholder for the generic keychain item query used to locate the item.
}
@property (nonatomic, retain) NSMutableDictionary *keychainItemData;
@property (nonatomic, retain) NSMutableDictionary *genericPasswordQuery;
// Designated initializer.
- (id)initWithIdentifier: (NSString *)identifier accessGroup:(NSString *) accessGroup;
- (void)setObject:(id)inObject forKey:(id)key;
- (id)objectForKey:(id)key;
// Initializes and resets the default generic keychain item data.
- (void)resetKeychainItem;
@end
@interface KeychainItemWrapper (PrivateMethods)
/*
The decision behind the following two methods (secItemFormatToDictionary and dictionaryToSecItemFormat) was
to encapsulate the transition between what the detail view controller was expecting (NSString *) and what the
Keychain API expects as a validly constructed container class.
*/
- (NSMutableDictionary *)secItemFormatToDictionary:(NSDictionary *)dictionaryToConvert;
- (NSMutableDictionary *)dictionaryToSecItemFormat:(NSDictionary *)dictionaryToConvert;
// Updates the item in the keychain, or adds it if it doesn't exist.
- (void)writeToKeychain;
@end
@implementation KeychainItemWrapper
@synthesize keychainItemData, genericPasswordQuery;
- (id)initWithIdentifier: (NSString *)identifier accessGroup:(NSString *) accessGroup;
{
if (self = [super init])
{
// Begin Keychain search setup. The genericPasswordQuery leverages the special user
// defined attribute kSecAttrGeneric to distinguish itself between other generic Keychain
// items which may be included by the same application.
genericPasswordQuery = [[NSMutableDictionary alloc] init];
[genericPasswordQuery setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[genericPasswordQuery setObject:identifier forKey:(id)kSecAttrGeneric];
// The keychain access group attribute determines if this item can be shared
// amongst multiple apps whose code signing entitlements contain the same keychain access group.
if (accessGroup != nil)
{
#if TARGET_IPHONE_SIMULATOR
// Ignore the access group if running on the iPhone simulator.
//
// Apps that are built for the simulator aren't signed, so there's no keychain access group
// for the simulator to check. This means that all apps can see all keychain items when run
// on the simulator.
//
// If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the
// simulator will return -25243 (errSecNoAccessForItem).
#else
[genericPasswordQuery setObject:accessGroup forKey:(id)kSecAttrAccessGroup];
#endif
}
// Use the proper search constants, return only the attributes of the first match.
[genericPasswordQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
[genericPasswordQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnAttributes];
NSDictionary *tempQuery = [NSDictionary dictionaryWithDictionary:genericPasswordQuery];
NSMutableDictionary *outDictionary = nil;
if (! SecItemCopyMatching((CFDictionaryRef)tempQuery, (CFTypeRef *)&outDictionary) == noErr)
{
// Stick these default values into keychain item if nothing found.
[self resetKeychainItem];
// Add the generic attribute and the keychain access group.
[keychainItemData setObject:identifier forKey:(id)kSecAttrGeneric];
if (accessGroup != nil)
{
#if TARGET_IPHONE_SIMULATOR
// Ignore the access group if running on the iPhone simulator.
//
// Apps that are built for the simulator aren't signed, so there's no keychain access group
// for the simulator to check. This means that all apps can see all keychain items when run
// on the simulator.
//
// If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the
// simulator will return -25243 (errSecNoAccessForItem).
#else
[keychainItemData setObject:accessGroup forKey:(id)kSecAttrAccessGroup];
#endif
}
}
else
{
// load the saved data from Keychain.
self.keychainItemData = [self secItemFormatToDictionary:outDictionary];
}
[outDictionary release];
}
return self;
}
- (void)dealloc
{
[keychainItemData release];
[genericPasswordQuery release];
[super dealloc];
}
- (void)setObject:(id)inObject forKey:(id)key
{
if (inObject == nil) return;
id currentObject = [keychainItemData objectForKey:key];
if (![currentObject isEqual:inObject])
{
[keychainItemData setObject:inObject forKey:key];
[self writeToKeychain];
}
}
- (id)objectForKey:(id)key
{
return [keychainItemData objectForKey:key];
}
- (void)resetKeychainItem
{
OSStatus junk = noErr;
if (!keychainItemData)
{
keychainItemData = [[NSMutableDictionary alloc] init];
}
else if (keychainItemData)
{
NSMutableDictionary *tempDictionary = [self dictionaryToSecItemFormat:keychainItemData];
junk = SecItemDelete((CFDictionaryRef)tempDictionary);
NSAssert( junk == noErr || junk == errSecItemNotFound, @"Problem deleting current dictionary." );
}
// Default attributes for keychain item.
[keychainItemData setObject:@"" forKey:(id)kSecAttrAccount];
[keychainItemData setObject:@"" forKey:(id)kSecAttrLabel];
[keychainItemData setObject:@"" forKey:(id)kSecAttrDescription];
// Default data for keychain item.
[keychainItemData setObject:@"" forKey:(id)kSecValueData];
}
- (NSMutableDictionary *)dictionaryToSecItemFormat:(NSDictionary *)dictionaryToConvert
{
// The assumption is that this method will be called with a properly populated dictionary
// containing all the right key/value pairs for a SecItem.
// Create a dictionary to return populated with the attributes and data.
NSMutableDictionary *returnDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionaryToConvert];
// Add the Generic Password keychain item class attribute.
[returnDictionary setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
// Convert the NSString to NSData to meet the requirements for the value type kSecValueData.
// This is where to store sensitive data that should be encrypted.
NSString *passwordString = [dictionaryToConvert objectForKey:(id)kSecValueData];
[returnDictionary setObject:[passwordString dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecValueData];
return returnDictionary;
}
- (NSMutableDictionary *)secItemFormatToDictionary:(NSDictionary *)dictionaryToConvert
{
// The assumption is that this method will be called with a properly populated dictionary
// containing all the right key/value pairs for the UI element.
// Create a dictionary to return populated with the attributes and data.
NSMutableDictionary *returnDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionaryToConvert];
// Add the proper search key and class attribute.
[returnDictionary setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
[returnDictionary setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
// Acquire the password data from the attributes.
NSData *passwordData = NULL;
if (SecItemCopyMatching((CFDictionaryRef)returnDictionary, (CFTypeRef *)&passwordData) == noErr)
{
// Remove the search, class, and identifier key/value, we don't need them anymore.
[returnDictionary removeObjectForKey:(id)kSecReturnData];
// Add the password to the dictionary, converting from NSData to NSString.
NSString *password = [[[NSString alloc] initWithBytes:[passwordData bytes] length:[passwordData length]
encoding:NSUTF8StringEncoding] autorelease];
[returnDictionary setObject:password forKey:(id)kSecValueData];
}
else
{
// Don't do anything if nothing is found.
NSAssert(NO, @"Serious error, no matching item found in the keychain.\n");
}
[passwordData release];
return returnDictionary;
}
- (void)writeToKeychain
{
NSDictionary *attributes = NULL;
NSMutableDictionary *updateItem = NULL;
OSStatus result;
if (SecItemCopyMatching((CFDictionaryRef)genericPasswordQuery, (CFTypeRef *)&attributes) == noErr)
{
// First we need the attributes from the Keychain.
updateItem = [NSMutableDictionary dictionaryWithDictionary:attributes];
// Second we need to add the appropriate search key/values.
[updateItem setObject:[genericPasswordQuery objectForKey:(id)kSecClass] forKey:(id)kSecClass];
// Lastly, we need to set up the updated attribute list being careful to remove the class.
NSMutableDictionary *tempCheck = [self dictionaryToSecItemFormat:keychainItemData];
[tempCheck removeObjectForKey:(id)kSecClass];
#if TARGET_IPHONE_SIMULATOR
// Remove the access group if running on the iPhone simulator.
//
// Apps that are built for the simulator aren't signed, so there's no keychain access group
// for the simulator to check. This means that all apps can see all keychain items when run
// on the simulator.
//
// If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the
// simulator will return -25243 (errSecNoAccessForItem).
//
// The access group attribute will be included in items returned by SecItemCopyMatching,
// which is why we need to remove it before updating the item.
[tempCheck removeObjectForKey:(id)kSecAttrAccessGroup];
#endif
// An implicit assumption is that you can only update a single item at a time.
result = SecItemUpdate((CFDictionaryRef)updateItem, (CFDictionaryRef)tempCheck);
NSAssert( result == noErr, @"Couldn't update the Keychain Item." );
}
else
{
// No previous item found; add the new one.
result = SecItemAdd((CFDictionaryRef)[self dictionaryToSecItemFormat:keychainItemData], NULL);
NSAssert( result == noErr, @"Couldn't add the Keychain Item." );
}
}
@end
/* }}} */
/* XML {{{ */
#import <libxml/tree.h>
#import <libxml/parser.h>
#import <libxml/HTMLparser.h>
#import <libxml/xpath.h>
#import <libxml/xpathInternals.h>
@class XMLDocument;
@interface XMLElement : NSObject {
xmlNodePtr node;
XMLDocument *document;
NSArray *cachedChildren;
NSDictionary *cachedAttributes;
NSString *cachedContent;
}
- (id)initWithNode:(xmlNodePtr)node_ inDocument:(XMLDocument *)document_;
- (NSString *)content;
- (NSString *)tagName;
- (NSArray *)children;
- (NSDictionary *)attributes;
- (NSString *)attributeWithName:(NSString *)name;
- (BOOL)isTextNode;
- (xmlNodePtr)node;
- (NSArray *)elementsMatchingPath:(NSString *)xpath;
- (XMLElement *)firstElementMatchingPath:(NSString *)xpath;
@end
@interface XMLDocument : NSObject {
xmlDocPtr document;
}
- (id)initWithHTMLData:(NSData *)data_;
- (id)initWithXMLData:(NSData *)data_;
- (NSArray *)elementsMatchingPath:(NSString *)query relativeToElement:(XMLElement *)element;
- (NSArray *)elementsMatchingPath:(NSString *)xpath;
- (XMLElement *)firstElementMatchingPath:(NSString *)xpath;
- (xmlDocPtr)document;
@end
static int XMLElementOutputWriteCallback(void *context, const char *buffer, int len) {
NSMutableData *data = (NSMutableData *)context;
[data appendBytes:buffer length:len];
return len;
}
static int XMLElementOutputCloseCallback(void *context) {
NSMutableData *data = (NSMutableData *)context;
[data release];
return 0;
}
@implementation XMLElement
- (void)dealloc {
[cachedChildren release];
[cachedAttributes release];
[cachedContent release];
[document release];
[super dealloc];
}
- (id)initWithNode:(xmlNodePtr)node_ inDocument:(XMLDocument *)document_ {
if ((self = [super init])) {
node = node_;
document = [document_ retain];
}
return self;
}
- (NSUInteger)hash {
return (NSUInteger) node;
}
- (BOOL)isEqual:(id)object {
if ([object isKindOfClass:[XMLElement class]]) {
XMLElement *other = (XMLElement *)object;
return [other node] == node;
} else {
return NO;
}
}
- (NSString *)content {
if (cachedContent != nil) return cachedContent;
NSMutableString *content = [[NSMutableString string] retain];
if (![self isTextNode]) {
xmlNodePtr children = node->children;
while (children) {
NSMutableData *data = [[NSMutableData alloc] init];
xmlOutputBufferPtr buffer = xmlOutputBufferCreateIO(XMLElementOutputWriteCallback, XMLElementOutputCloseCallback, data, NULL);
xmlNodeDumpOutput(buffer, [document document], children, 0, 0, "utf-8");
xmlOutputBufferFlush(buffer);
[content appendString:[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]];
xmlOutputBufferClose(buffer);
children = children->next;
}
} else {
xmlChar *nodeContent = xmlNodeGetContent(node);
[content appendString:[NSString stringWithUTF8String:(char *) nodeContent]];
xmlFree(nodeContent);
}
cachedContent = content;
return cachedContent;
}
- (NSString *)tagName {
if ([self isTextNode]) return nil;
char *nodeName = (char *) node->name;
if (nodeName == NULL) nodeName = (char *)"";
NSString *name = [NSString stringWithUTF8String:nodeName];
return name;
}
- (NSArray *)children {
if (cachedChildren != nil) return cachedChildren;
xmlNodePtr list = node->children;
NSMutableArray *children = [NSMutableArray array];
while (list) {
XMLElement *element = [[XMLElement alloc] initWithNode:list inDocument:document];
[children addObject:[element autorelease]];
list = list->next;
}
cachedChildren = [children retain];
return cachedChildren;
}
- (NSDictionary *)attributes {
if (cachedAttributes != nil) return cachedAttributes;
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
xmlAttrPtr list = node->properties;
while (list) {
NSString *name = nil, *value = nil;
name = [NSString stringWithCString:(const char *) list->name encoding:NSUTF8StringEncoding];
if (list->children != NULL && list->children->content != NULL) {
value = [NSString stringWithCString:(const char *) list->children->content encoding:NSUTF8StringEncoding];
}
if (name != nil && value != nil) {
[attributes setObject:value forKey:name];
}
list = list->next;
}
cachedAttributes = [attributes retain];
return cachedAttributes;
}
- (NSString *)attributeWithName:(NSString *)name {
return [[self attributes] objectForKey:name];
}
- (BOOL)isTextNode {
return node->type == XML_TEXT_NODE;
}
- (xmlNodePtr)node {
return node;
}
- (NSArray *)elementsMatchingPath:(NSString *)xpath {
return [document elementsMatchingPath:xpath relativeToElement:self];
}
- (XMLElement *)firstElementMatchingPath:(NSString *)xpath {
NSArray *elements = [self elementsMatchingPath:xpath];
if ([elements count] >= 1) {
return [elements objectAtIndex:0];
} else {
return nil;
}
}
@end
@implementation XMLDocument
- (void)dealloc {
xmlFreeDoc(document);
[super dealloc];
}
- (id)initWithData:(NSData *)data isXML:(BOOL)xml {
if ((self = [super init])) {
document = (xml ? xmlReadMemory : htmlReadMemory)((const char *)[data bytes], [data length], "", NULL, xml ? XML_PARSE_RECOVER : HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR);
if (document == NULL) {
[self autorelease];
return nil;
}
}
return self;
}
- (xmlDocPtr)document {
return document;
}
- (id)initWithXMLData:(NSData *)data_ {
return [self initWithData:data_ isXML:YES];
}
- (id)initWithHTMLData:(NSData *)data_ {
return [self initWithData:data_ isXML:NO];
}
- (NSArray *)elementsMatchingPath:(NSString *)query relativeToElement:(XMLElement *)element {
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
xpathCtx = xmlXPathNewContext(document);
if (xpathCtx == NULL) return nil;
xpathCtx->node = [element node];
xpathObj = xmlXPathEvalExpression((xmlChar *) [query cStringUsingEncoding:NSUTF8StringEncoding], xpathCtx);
if (xpathObj == NULL) return nil;
xmlNodeSetPtr nodes = xpathObj->nodesetval;
if (nodes == NULL) return nil;
NSMutableArray *result = [NSMutableArray array];
for (NSInteger i = 0; i < nodes->nodeNr; i++) {
XMLElement *element = [[XMLElement alloc] initWithNode:nodes->nodeTab[i] inDocument:self];
[result addObject:[element autorelease]];
}
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
return result;
}
- (NSArray *)elementsMatchingPath:(NSString *)xpath {
return [self elementsMatchingPath:xpath relativeToElement:nil];
}
- (XMLElement *)firstElementMatchingPath:(NSString *)xpath {
NSArray *elements = [self elementsMatchingPath:xpath];
if ([elements count] >= 1) {
return [elements objectAtIndex:0];
} else {
return nil;
}
}
@end
/* }}} */
/* ABTableViewCell {{{ */
@interface ABTableViewCell : UITableViewCell {
UIView* contentView;
UIView* selectedContentView;
}
- (void)drawContentView:(CGRect)rect highlighted:(BOOL)highlighted; // subclasses should implement
@end
@interface ABTableViewCellView : UIView
@end
@interface ABTableViewSelectedCellView : UIView
@end
@implementation ABTableViewCellView
- (id)initWithFrame:(CGRect)frame {
if((self = [super initWithFrame:frame])) {
self.contentMode = UIViewContentModeRedraw;
}
return self;
}
// Patch for iOS 7 thanks to http://www.blogosfera.co.uk/2013/06/abtableviewcell-issue-in-ios-7-with-drawcontentview-closed/
- (void)drawRect:(CGRect)rect {
UIView *v = self;
while (v && ![v isKindOfClass:[ABTableViewCell class]]) v = v.superview;
[(ABTableViewCell *)v drawContentView:rect highlighted:NO];
}
@end
@implementation ABTableViewSelectedCellView
- (id)initWithFrame:(CGRect)frame {
if((self = [super initWithFrame:frame])) {
self.contentMode = UIViewContentModeRedraw;
}
return self;
}
- (void)drawRect:(CGRect)rect {
UIView *v = self;
while (v && ![v isKindOfClass:[ABTableViewCell class]]) v = v.superview;
[(ABTableViewCell *)v drawContentView:rect highlighted:NO];
}
@end
@implementation ABTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
contentView = [[ABTableViewCellView alloc] initWithFrame:CGRectZero];
contentView.opaque = YES;
self.backgroundView = contentView;
[contentView release];
selectedContentView = [[ABTableViewSelectedCellView alloc] initWithFrame:CGRectZero];
selectedContentView.opaque = YES;
self.selectedBackgroundView = selectedContentView;
[selectedContentView release];
}
return self;
}
- (void)dealloc {
[super dealloc];
}
- (void)setSelected:(BOOL)selected {
[selectedContentView setNeedsDisplay];
if(!selected && self.selected) {
[contentView setNeedsDisplay];
}
[super setSelected:selected];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[selectedContentView setNeedsDisplay];
if(!selected && self.selected) {
[contentView setNeedsDisplay];
}
[super setSelected:selected animated:animated];
}
- (void)setHighlighted:(BOOL)highlighted {
[selectedContentView setNeedsDisplay];
if(!highlighted && self.highlighted) {
[contentView setNeedsDisplay];
}
[super setHighlighted:highlighted];
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[selectedContentView setNeedsDisplay];
if(!highlighted && self.highlighted) {
[contentView setNeedsDisplay];
}
[super setHighlighted:highlighted animated:animated];
}
- (void)setFrame:(CGRect)f {
f.origin.y -= 1;
f.size.height += 1;
[super setFrame:f];
CGRect b = [self bounds];
// b.size.height -= 1; // leave room for the seperator line
[contentView setFrame:b];
[selectedContentView setFrame:b];
}
- (void)setNeedsDisplay {
[super setNeedsDisplay];
[contentView setNeedsDisplay];
if([self isHighlighted] || [self isSelected]) {
[selectedContentView setNeedsDisplay];
}
}
- (void)setNeedsDisplayInRect:(CGRect)rect {
[super setNeedsDisplayInRect:rect];
[contentView setNeedsDisplayInRect:rect];
if([self isHighlighted] || [self isSelected]) {
[selectedContentView setNeedsDisplayInRect:rect];
}
}
- (void)layoutSubviews {
[super layoutSubviews];
self.contentView.hidden = YES;
[self.contentView removeFromSuperview];
}
- (void)drawContentView:(CGRect)rect highlighted:(BOOL)highlighted {
return;
}
@end
/* }}} */
/* HTML NSString {{{ */
@interface NSString (GTMNSStringHTMLAdditions)
- (NSString *)gtm_stringByEscapingForHTML;
- (NSString *)gtm_stringByEscapingForAsciiHTML;
- (NSString *)gtm_stringByUnescapingFromHTML;
@end
typedef struct {
NSString *escapeSequence;
unichar uchar;
} HTMLEscapeMap;
// Taken from http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_Special_characters
// Ordered by uchar lowest to highest for bsearching
static HTMLEscapeMap gAsciiHTMLEscapeMap[] = {
// A.2.2. Special characters
{ @""", 34 },
{ @"&", 38 },
{ @"'", 39 },
{ @"<", 60 },
{ @">", 62 },
// A.2.1. Latin-1 characters
{ @" ", 160 },
{ @"¡", 161 },
{ @"¢", 162 },
{ @"£", 163 },
{ @"¤", 164 },
{ @"¥", 165 },
{ @"¦", 166 },
{ @"§", 167 },
{ @"¨", 168 },
{ @"©", 169 },
{ @"ª", 170 },
{ @"«", 171 },
{ @"¬", 172 },
{ @"­", 173 },
{ @"®", 174 },
{ @"¯", 175 },
{ @"°", 176 },
{ @"±", 177 },
{ @"²", 178 },
{ @"³", 179 },
{ @"´", 180 },
{ @"µ", 181 },
{ @"¶", 182 },
{ @"·", 183 },
{ @"¸", 184 },
{ @"¹", 185 },
{ @"º", 186 },
{ @"»", 187 },
{ @"¼", 188 },
{ @"½", 189 },
{ @"¾", 190 },
{ @"¿", 191 },
{ @"À", 192 },
{ @"Á", 193 },
{ @"Â", 194 },
{ @"Ã", 195 },
{ @"Ä", 196 },
{ @"Å", 197 },
{ @"Æ", 198 },
{ @"Ç", 199 },
{ @"È", 200 },
{ @"É", 201 },
{ @"Ê", 202 },
{ @"Ë", 203 },
{ @"Ì", 204 },
{ @"Í", 205 },
{ @"Î", 206 },
{ @"Ï", 207 },
{ @"Ð", 208 },
{ @"Ñ", 209 },
{ @"Ò", 210 },
{ @"Ó", 211 },
{ @"Ô", 212 },
{ @"Õ", 213 },
{ @"Ö", 214 },
{ @"×", 215 },
{ @"Ø", 216 },
{ @"Ù", 217 },
{ @"Ú", 218 },
{ @"Û", 219 },
{ @"Ü", 220 },
{ @"Ý", 221 },
{ @"Þ", 222 },
{ @"ß", 223 },
{ @"à", 224 },
{ @"á", 225 },
{ @"â", 226 },
{ @"ã", 227 },
{ @"ä", 228 },
{ @"å", 229 },
{ @"æ", 230 },
{ @"ç", 231 },
{ @"è", 232 },
{ @"é", 233 },
{ @"ê", 234 },
{ @"ë", 235 },
{ @"ì", 236 },
{ @"í", 237 },
{ @"î", 238 },
{ @"ï", 239 },
{ @"ð", 240 },
{ @"ñ", 241 },
{ @"ò", 242 },
{ @"ó", 243 },
{ @"ô", 244 },
{ @"õ", 245 },
{ @"ö", 246 },
{ @"÷", 247 },
{ @"ø", 248 },
{ @"ù", 249 },
{ @"ú", 250 },
{ @"û", 251 },
{ @"ü", 252 },
{ @"ý", 253 },
{ @"þ", 254 },
{ @"ÿ", 255 },
// A.2.2. Special characters cont'd
{ @"Œ", 338 },
{ @"œ", 339 },
{ @"Š", 352 },
{ @"š", 353 },
{ @"Ÿ", 376 },
// A.2.3. Symbols
{ @"ƒ", 402 },
// A.2.2. Special characters cont'd
{ @"ˆ", 710 },
{ @"˜", 732 },
// A.2.3. Symbols cont'd
{ @"Α", 913 },
{ @"Β", 914 },
{ @"Γ", 915 },
{ @"Δ", 916 },
{ @"Ε", 917 },
{ @"Ζ", 918 },
{ @"Η", 919 },
{ @"Θ", 920 },
{ @"Ι", 921 },
{ @"Κ", 922 },
{ @"Λ", 923 },
{ @"Μ", 924 },
{ @"Ν", 925 },
{ @"Ξ", 926 },
{ @"Ο", 927 },
{ @"Π", 928 },
{ @"Ρ", 929 },
{ @"Σ", 931 },
{ @"Τ", 932 },
{ @"Υ", 933 },
{ @"Φ", 934 },
{ @"Χ", 935 },
{ @"Ψ", 936 },
{ @"Ω", 937 },
{ @"α", 945 },
{ @"β", 946 },
{ @"γ", 947 },
{ @"δ", 948 },
{ @"ε", 949 },
{ @"ζ", 950 },
{ @"η", 951 },
{ @"θ", 952 },
{ @"ι", 953 },
{ @"κ", 954 },
{ @"λ", 955 },
{ @"μ", 956 },
{ @"ν", 957 },
{ @"ξ", 958 },
{ @"ο", 959 },
{ @"π", 960 },
{ @"ρ", 961 },
{ @"ς", 962 },
{ @"σ", 963 },
{ @"τ", 964 },
{ @"υ", 965 },
{ @"φ", 966 },
{ @"χ", 967 },
{ @"ψ", 968 },
{ @"ω", 969 },
{ @"ϑ", 977 },
{ @"ϒ", 978 },
{ @"ϖ", 982 },
// A.2.2. Special characters cont'd
{ @" ", 8194 },
{ @" ", 8195 },
{ @" ", 8201 },
{ @"‌", 8204 },
{ @"‍", 8205 },
{ @"‎", 8206 },
{ @"‏", 8207 },
{ @"–", 8211 },
{ @"—", 8212 },
{ @"‘", 8216 },
{ @"’", 8217 },
{ @"‚", 8218 },
{ @"“", 8220 },
{ @"”", 8221 },
{ @"„", 8222 },
{ @"†", 8224 },
{ @"‡", 8225 },
// A.2.3. Symbols cont'd
{ @"•", 8226 },
{ @"…", 8230 },
// A.2.2. Special characters cont'd
{ @"‰", 8240 },
// A.2.3. Symbols cont'd
{ @"′", 8242 },
{ @"″", 8243 },
// A.2.2. Special characters cont'd
{ @"‹", 8249 },
{ @"›", 8250 },
// A.2.3. Symbols cont'd
{ @"‾", 8254 },
{ @"⁄", 8260 },
// A.2.2. Special characters cont'd
{ @"€", 8364 },
// A.2.3. Symbols cont'd
{ @"ℑ", 8465 },
{ @"℘", 8472 },
{ @"ℜ", 8476 },
{ @"™", 8482 },
{ @"ℵ", 8501 },
{ @"←", 8592 },
{ @"↑", 8593 },
{ @"→", 8594 },
{ @"↓", 8595 },
{ @"↔", 8596 },
{ @"↵", 8629 },
{ @"⇐", 8656 },
{ @"⇑", 8657 },
{ @"⇒", 8658 },
{ @"⇓", 8659 },
{ @"⇔", 8660 },
{ @"∀", 8704 },
{ @"∂", 8706 },
{ @"∃", 8707 },
{ @"∅", 8709 },
{ @"∇", 8711 },
{ @"∈", 8712 },
{ @"∉", 8713 },
{ @"∋", 8715 },
{ @"∏", 8719 },
{ @"∑", 8721 },
{ @"−", 8722 },
{ @"∗", 8727 },
{ @"√", 8730 },
{ @"∝", 8733 },
{ @"∞", 8734 },
{ @"∠", 8736 },
{ @"∧", 8743 },
{ @"∨", 8744 },
{ @"∩", 8745 },
{ @"∪", 8746 },
{ @"∫", 8747 },
{ @"∴", 8756 },
{ @"∼", 8764 },
{ @"≅", 8773 },
{ @"≈", 8776 },
{ @"≠", 8800 },
{ @"≡", 8801 },
{ @"≤", 8804 },
{ @"≥", 8805 },
{ @"⊂", 8834 },
{ @"⊃", 8835 },
{ @"⊄", 8836 },
{ @"⊆", 8838 },
{ @"⊇", 8839 },
{ @"⊕", 8853 },
{ @"⊗", 8855 },
{ @"⊥", 8869 },
{ @"⋅", 8901 },
{ @"⌈", 8968 },
{ @"⌉", 8969 },
{ @"⌊", 8970 },
{ @"⌋", 8971 },
{ @"⟨", 9001 },
{ @"⟩", 9002 },
{ @"◊", 9674 },
{ @"♠", 9824 },
{ @"♣", 9827 },
{ @"♥", 9829 },
{ @"♦", 9830 }
};
// Taken from http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_Special_characters
// This is table A.2.2 Special Characters
static HTMLEscapeMap gUnicodeHTMLEscapeMap[] = {
// C0 Controls and Basic Latin
{ @""", 34 },
{ @"&", 38 },
{ @"'", 39 },
{ @"<", 60 },