forked from veritech/CSSApply
-
Notifications
You must be signed in to change notification settings - Fork 4
/
CSSStyleSheet.m
82 lines (63 loc) · 2.15 KB
/
CSSStyleSheet.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
//
// CSSStyleSheet.m
// CSSSample
//
// Created by Sam Stewart on 7/16/11.
// Copyright 2011 Float:Right Ltd. All rights reserved.
//
#import "CSSStyleSheet.h"
#import "CSSParser.h"
@interface CSSStyleSheet ()
- (void)buildTree:(NSDictionary*)rules;
@end
@implementation CSSStyleSheet
@synthesize root = _root;
#pragma mark Loader Methods
+ (CSSStyleSheet*)styleSheetFromURL:(NSURL *)url {
CSSStyleSheet *sheet = [[[CSSStyleSheet alloc] init] autorelease];
[sheet loadFromURL:url];
return sheet;
}
+ (CSSStyleSheet*)styleSheetFromString:(NSString *)css_code {
//TODO: load and parse from string
return nil;
}
- (void)loadFromURL:(NSURL*)url {
if (parser)
[parser release], parser = nil;
parser = [[CSSParser alloc] init];
// grab the entire file and load the rule dictionary.
NSDictionary *rules = [parser parseFilename:[url path]];
//build the tree from the parsed rules
[self buildTree:rules];
}
- (void)loadFromString:(NSString *)css_code {
//TODO: load from string (need it in the parser class.)
}
#pragma mark Tree Parsing methods
- (void)buildTree:(NSDictionary *)rules {
if (_root) [_root release], _root = nil;
// create the root node for the selector tree
_root = [[CSSSelectorTree alloc] init];
NSArray *selectors = [rules allKeys];
for (NSString *selector in selectors) {
NSDictionary *ruleset = [rules objectForKey:selector];
//grab subtrees (really horizontal list since they are sublevels.)
// ul, ul, li, sam.red#hello
NSArray *subtrees = [CSSSelectorTree subtreesFromSelector:selector];
if ([subtrees count] < 1)
return;
//ruleset belongs to most specific (or first)
CSSSelectorTree *most_specific = [subtrees objectAtIndex:0];
most_specific.rules = ruleset;
//now we convert into tree
CSSSelectorTree *root_subtree = [CSSSelectorTree chainSubtrees:subtrees];
[_root.nodes addObject:root_subtree];
}
}
- (void)dealloc {
[super dealloc];
[parser release], parser = nil;
[_root release], _root = nil;
}
@end