forked from SVGKit/SVGKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSSStyleDeclaration.m
More file actions
164 lines (120 loc) · 4.54 KB
/
Copy pathCSSStyleDeclaration.m
File metadata and controls
164 lines (120 loc) · 4.54 KB
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
#import "CSSStyleDeclaration.h"
#import "CSSValue.h"
#import "CSSValueList.h"
#import "CSSPrimitiveValue.h"
@interface CSSStyleDeclaration()
@property(nonatomic,retain) NSMutableDictionary* internalDictionaryOfStylesByCSSClass;
@end
@implementation CSSStyleDeclaration
@synthesize internalDictionaryOfStylesByCSSClass;
@synthesize cssText = _cssText;
@synthesize length;
@synthesize parentRule;
- (id)init
{
self = [super init];
if (self) {
self.internalDictionaryOfStylesByCSSClass = [NSMutableDictionary dictionary];
}
return self;
}
#define MAX_ACCUM 256
#define MAX_NAME 256
/** From spec:
"The parsable textual representation of the declaration block (excluding the surrounding curly braces). Setting this attribute will result in the parsing of the new value and resetting of all the properties in the declaration block including the removal or addition of properties."
*/
-(void)setCssText:(NSString *)newCSSText
{
_cssText = newCSSText;
/** and now post-process it, *as required by* the CSS/DOM spec... */
NSMutableDictionary* processedStyles = [self NSDictionaryFromCSSAttributes:_cssText];
self.internalDictionaryOfStylesByCSSClass = processedStyles;
}
-(NSMutableDictionary *) NSDictionaryFromCSSAttributes: (NSString *)css {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
const char *cstr = [css UTF8String];
size_t len = strlen(cstr);
char name[MAX_NAME];
bzero(name, MAX_NAME);
char accum[MAX_ACCUM];
bzero(accum, MAX_ACCUM);
size_t accumIdx = 0;
for (size_t n = 0; n <= len; n++) {
char c = cstr[n];
if (c == '\n' || c == '\t' || c == ' ') {
continue;
}
if (c == ':') {
strcpy(name, accum);
name[accumIdx] = '\0';
bzero(accum, MAX_ACCUM);
accumIdx = 0;
continue;
}
else if (c == ';' || c == '\0') {
if( accumIdx > 0 ) //if there is a ';' and '\0' to end the style, avoid adding an empty key-value pair
{
accum[accumIdx] = '\0';
NSString *keyString = [[NSString alloc] initWithUTF8String:name]; //key is copied anyways, autoreleased object creates clutter
NSString *cssValueString = [NSString stringWithUTF8String:accum];
NSMutableCharacterSet* trimmingSetForKey = [[NSMutableCharacterSet alloc] init];
/* add any extra characters to the trim-set if needed here; seems we're OK with the Apple provided whitespace set right now */
[trimmingSetForKey formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
keyString = [keyString stringByTrimmingCharactersInSet:trimmingSetForKey];
CSSValue *cssValue;
if( [cssValueString rangeOfString:@" "].length > 0 )
cssValue = [[CSSValueList alloc] init];
else
cssValue = [[CSSPrimitiveValue alloc] init];
cssValue.cssText = cssValueString; // has the side-effect of parsing, if required
[dict setObject:cssValue
forKey:keyString];
bzero(name, MAX_NAME);
bzero(accum, MAX_ACCUM);
accumIdx = 0;
}
continue;
}
accum[accumIdx++] = c;
}
return dict;
}
-(NSString*) getPropertyValue:(NSString*) propertyName
{
CSSValue* v = [self getPropertyCSSValue:propertyName];
if( v == nil )
return nil;
else
return v.cssText;
}
-(CSSValue*) getPropertyCSSValue:(NSString*) propertyName
{
return [self.internalDictionaryOfStylesByCSSClass objectForKey:propertyName];
}
-(NSString*) removeProperty:(NSString*) propertyName
{
NSString* oldValue = [self getPropertyValue:propertyName];
[self.internalDictionaryOfStylesByCSSClass removeObjectForKey:propertyName];
return oldValue;
}
-(NSString*) getPropertyPriority:(NSString*) propertyName
{
NSAssert(FALSE, @"CSS 'property priorities' - Not supported");
return nil;
}
-(void) setProperty:(NSString*) propertyName value:(NSString*) value priority:(NSString*) priority
{
NSAssert(FALSE, @"CSS 'property priorities' - Not supported");
}
-(NSString*) item:(long) index
{
/** this is stupid slow, but until Apple *can be bothered* to add a "stable-order" dictionary to their libraries, this is the only sensibly easy way of implementing this method */
NSArray* sortedKeys = [[self.internalDictionaryOfStylesByCSSClass allKeys] sortedArrayUsingSelector:@selector(compare:)];
CSSValue* v = [sortedKeys objectAtIndex:index];
return v.cssText;
}
-(NSString *)description
{
return [NSString stringWithFormat:@"CSSStyleDeclaration: dictionary(%@)", self.internalDictionaryOfStylesByCSSClass];
}
@end