forked from veritech/CSSApply
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CSSSelector and added tree building code
- Loading branch information
Sam Stewart
committed
Jul 17, 2011
1 parent
bc85681
commit 078cfeb
Showing
19 changed files
with
370 additions
and
155 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,9 @@ | |
|
||
|
||
@implementation CSSParser | ||
@synthesize delegate; | ||
|
||
- (void)dealloc { | ||
[super dealloc]; | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ | |
|
||
} | ||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// CSSRuleset.h | ||
// CSSSample | ||
// | ||
// Created by Sam Stewart on 7/16/11. | ||
// Copyright 2011 Float:Right Ltd. All rights reserved. | ||
// | ||
/** This class represents a single CSS ruleset. It allows us to load | ||
resources and provids a storage container for a given ruleset. | ||
For example, this class represents the rule section of a selector group. | ||
{ | ||
background-image: url('test.jpg'); | ||
color: #FFFFFF | ||
} | ||
This class does lazy loading of resources so it only loads when you call getRuleValue. If it doesn't have the resource | ||
cached it will load and cache it. | ||
*/ | ||
#import <Foundation/Foundation.h> | ||
|
||
|
||
@interface CSSRuleset : NSObject { | ||
// string rep of the ruleset | ||
NSDictionary *parsedRuleset; | ||
|
||
// dictionary with actual loaded resources (like UIImage) | ||
// this thing acts as a cache. | ||
NSDictionary *loadedRuleset; | ||
} | ||
- (id)initWithDict:(NSDictionary*)dictionary; | ||
/** Grabs the rule value for the specified key. It'll load the resource if it doesn't | ||
already exist.*/ | ||
- (NSObject*)getRuleValue:(NSString*)key; | ||
/** Sets the resource uri (like background-image for css) for the specified ruleset key.*/ | ||
- (void)setRuleValue:(NSString*)value forKey:(NSString*)key; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// CSSRuleset.m | ||
// CSSSample | ||
// | ||
// Created by Sam Stewart on 7/16/11. | ||
// Copyright 2011 Float:Right Ltd. All rights reserved. | ||
// | ||
|
||
#import "CSSRuleset.h" | ||
@interface CSSRuleset() | ||
- (NSObject*)loadResource:(NSString*)resource_uri; | ||
@end | ||
|
||
@implementation CSSRuleset | ||
|
||
- (id)initWithDict:(NSDictionary *)dictionary { | ||
self = [super init]; | ||
if (self) { | ||
parsedRuleset = [dictionary retain]; | ||
} | ||
return self; | ||
} | ||
- (NSObject*)getRuleValue:(NSString *)key { | ||
NSObject *value = [loadedRuleset objectForKey:key]; | ||
if (!value) { | ||
// since not in cache grab string uri and load | ||
value = [self loadResource:[parsedRuleset objectForKey:key]]; | ||
[loadedRuleset setValue:value forKey:key]; // save for next time. | ||
} | ||
return value; | ||
} | ||
- (void)setRuleValue:(NSObject *)value forKey:(NSString *)key { | ||
[parsedRuleset setValue:value forKey:key]; | ||
} | ||
|
||
|
||
- (void)dealloc { | ||
[super dealloc]; | ||
[parsedRuleset release], parsedRuleset = nil; | ||
[loadedRuleset release], loadedRuleset = nil; | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// CSSSelector.h | ||
// CSSSample | ||
// | ||
// Created by Sam Stewart on 7/16/11. | ||
// Copyright 2011 Float:Right Ltd. All rights reserved. | ||
// | ||
/** | ||
This class represents a css selector. Each selector is a bit like a uri with the ending element called | ||
a "slug". For example, in the css selector "ul.red#hello" "#hello" would be the slug. | ||
You can get the entire selector by calling "description" on this. | ||
This is a simple wrapper class which allows us to manage selectors.*/ | ||
#import <Foundation/Foundation.h> | ||
|
||
|
||
@interface CSSSelector : NSObject { | ||
// the ending component | ||
NSString *slug; | ||
// the main selector (without slug) | ||
NSString *main; | ||
} | ||
- (id)initWithSelector:(NSString*)selector; | ||
/** Returns the components of the the selector (including the slug). | ||
The components which are separated by spaces (mainly descendant selectors). | ||
*/ | ||
- (NSArray*)selectorComponents; | ||
|
||
//not the best idea to update these directly. | ||
@property (nonatomic, retain) NSString *main; | ||
@property (nonatomic, retain) NSString *slug; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// CSSSelector.m | ||
// CSSSample | ||
// | ||
// Created by Sam Stewart on 7/16/11. | ||
// Copyright 2011 Float:Right Ltd. All rights reserved. | ||
// | ||
|
||
#import "CSSSelector.h" | ||
@interface CSSSelector () | ||
- (void)breakIntoSlugAndMain:(NSString*)selector; | ||
@end | ||
|
||
@implementation CSSSelector | ||
@synthesize main, slug; | ||
|
||
- (id)initWithSelector:(NSString *)selector { | ||
self = [super init]; | ||
if (self) { | ||
[self breakIntoSlugAndMain:selector]; | ||
} | ||
return self; | ||
} | ||
|
||
/** Breaks into main url and slug.*/ | ||
- (void)breakIntoSlugAndMain:(NSString*)selector { | ||
// find slug (start at the end of the string and stop when we find control character) | ||
unichar control_chars[5] = {'.', '#', ' ', '>', ':'}; | ||
|
||
for (int i = [selector length]; i>=0; i--) { | ||
unichar cur_char = [selector characterAtIndex:i]; | ||
|
||
for (int j = 0; j<5;i++) { | ||
if (cur_char == control_chars[j]) { | ||
NSRange range = NSMakeRange(i-1, i+j+1); | ||
self.slug = [selector substringWithRange:range]; | ||
self.main = [selector substringToIndex:i]; | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
- (NSArray*)selectorComponents { | ||
// just split on spaces.. | ||
return [[self description] componentsSeparatedByString:@" "]; | ||
} | ||
|
||
- (NSString*)description { | ||
return [NSString stringWithFormat:@"%@%@", main, slug]; | ||
} | ||
- (void)dealloc { | ||
[super dealloc]; | ||
[main release], main = nil; | ||
[slug release], slug = nil; | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// CSSStyleSheet.h | ||
// CSSSample | ||
// | ||
// Created by Sam Stewart on 7/16/11. | ||
// Copyright 2011 Float:Right Ltd. All rights reserved. | ||
// | ||
/** | ||
Represents css style sheet which has been parsed and loaded into our custom format. | ||
Each rule is represented backwards. For example you might have div.red#hello which would turn into | ||
#hello -> .red -> div | ||
Most specific to least specific. This setup allows us to ensure the correct properties take precendence. | ||
Thus, each entry (like #hello) has is a dictionary with a a few built in keys. We always have: | ||
1. score | ||
2. All (is a default ruleset in case we don't have any parent selectors). For example, #hello would have an "All" key. | ||
All is usually an instance of CSSRuleset since it's a toplevel namespace. | ||
3. any other selectors that are up a level. (for example, we'd have .red) | ||
This structure continues all the way up the tree. | ||
*/ | ||
#import <Foundation/Foundation.h> | ||
|
||
|
||
@class CSSParser; | ||
@class CSSSelector; | ||
@interface CSSStyleSheet : NSObject { | ||
// main css style tree. Custom format. | ||
NSMutableDictionary *mainTree; | ||
|
||
CSSParser *parser; | ||
} | ||
- (NSDictionary*)lookup:(CSSSelector*)selector; | ||
|
||
- (void)loadFromURL:(NSURL*)url; | ||
- (void)loadFromString:(NSString*)css_code; | ||
|
||
+ (CSSStyleSheet*)styleSheetFromURL:(NSURL*)url; | ||
+ (CSSStyleSheet*)styleSheetFromString:(NSString*)css_code; | ||
@end |
Oops, something went wrong.