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
171 lines (129 loc) · 4.6 KB
/
Copy pathCSSStyleDeclaration.m
File metadata and controls
171 lines (129 loc) · 4.6 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
165
166
167
168
169
170
171
#import "CSSStyleDeclaration.h"
#import "CSSValue.h"
#import "CSSValueList.h"
#import "CSSPrimitiveValue.h"
#import "CocoaLumberjack/DDFileLogger.h"
@interface CSSStyleDeclaration()
@property(nonatomic,retain) NSMutableDictionary* internalDictionaryOfStylesByCSSClass;
@end
@implementation CSSStyleDeclaration
@synthesize internalDictionaryOfStylesByCSSClass;
@synthesize cssText = _cssText;
@synthesize length;
@synthesize parentRule;
- (void)dealloc {
[_cssText release];
self.parentRule = nil;
self.internalDictionaryOfStylesByCSSClass = nil;
[super dealloc];
}
- (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 release];
_cssText = newCSSText;
[newCSSText retain];
/** 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];
NSCharacterSet* trimChars = [NSCharacterSet whitespaceAndNewlineCharacterSet];
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 == ':') {
strncpy(name, accum, MAX_NAME);
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 stringWithUTF8String:name]
stringByTrimmingCharactersInSet:trimChars];
NSString *cssValueString = [[NSString stringWithUTF8String:accum]
stringByTrimmingCharactersInSet:trimChars];
CSSValue *cssValue;
if( [cssValueString rangeOfString:@" "].length > 0 )
cssValue = [[[CSSValueList alloc] init] autorelease];
else
cssValue = [[[CSSPrimitiveValue alloc] init] autorelease];
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;
if (accumIdx >= MAX_ACCUM) {
SVGKitLogWarn(@"Buffer ovverun while parsing style sheet - skipping");
return [dict autorelease];
}
}
return [dict autorelease];
}
-(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