Skip to content

Commit

Permalink
Adds support for background CSS shorthand
Browse files Browse the repository at this point in the history
Currently just pulls out the color property and sets background-color
  • Loading branch information
bismark committed Feb 19, 2014
1 parent 49c4f3a commit 97c3f0f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
18 changes: 18 additions & 0 deletions Core/Source/DTCSSStylesheet.m
Expand Up @@ -410,6 +410,24 @@ - (void)_uncompressShorthands:(NSMutableDictionary *)styles
// remove the shorthand // remove the shorthand
[styles removeObjectForKey:@"padding"]; [styles removeObjectForKey:@"padding"];
} }

shortHand = [styles objectForKey:@"background"];

if (shortHand)
{
[styles removeObjectForKey:@"background"];
NSString *trimmedString = [shortHand stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSScanner *scanner = [NSScanner scannerWithString:trimmedString];
NSCharacterSet *tokenEndSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
while (![scanner isAtEnd]) {
NSString *colorName;
if ([scanner scanHTMLColor:NULL HTMLName:&colorName]) {
[styles setObject:colorName forKey:@"background-color"];
break;
}
[scanner scanUpToCharactersFromSet:tokenEndSet intoString:NULL];
}
}
} }


- (void)_addStyleRule:(NSString *)rule withSelector:(NSString*)selectors - (void)_addStyleRule:(NSString *)rule withSelector:(NSString*)selectors
Expand Down
7 changes: 7 additions & 0 deletions Core/Source/NSScanner+HTML.h
Expand Up @@ -41,6 +41,13 @@
*/ */
- (BOOL)scanHTMLColor:(DTColor **)color; - (BOOL)scanHTMLColor:(DTColor **)color;


/**
Scans for a typical HTML color, typically either #FFFFFF, rgb(255,255,255) or a HTML color name.
@param color An optional output parameter that will contain the scanned color if successful
@param name An optional output parameter that will contain the HTML color string
@returns `YES` if a color could be scanned
*/
- (BOOL)scanHTMLColor:(DTColor **)color HTMLName:(NSString **)name;


@end @end


13 changes: 11 additions & 2 deletions Core/Source/NSScanner+HTML.m
Expand Up @@ -220,6 +220,11 @@ - (BOOL)scanCSSURL:(NSString **)urlString
} }


- (BOOL)scanHTMLColor:(DTColor **)color - (BOOL)scanHTMLColor:(DTColor **)color
{
return [self scanHTMLColor:color HTMLName:NULL];
}

- (BOOL)scanHTMLColor:(DTColor **)color HTMLName:(NSString **)name
{ {
NSUInteger indexBefore = [self scanLocation]; NSUInteger indexBefore = [self scanLocation];


Expand Down Expand Up @@ -262,12 +267,16 @@ - (BOOL)scanHTMLColor:(DTColor **)color
self.scanLocation = indexBefore; self.scanLocation = indexBefore;
return NO; return NO;
} }

if (color) if (color)
{ {
*color = foundColor; *color = foundColor;
} }


if (name) {
*name = colorName;
}

return YES; return YES;
} }


Expand Down
12 changes: 12 additions & 0 deletions Test/Source/DTCSSStylesheetTest.m
Expand Up @@ -495,4 +495,16 @@ - (void)testUncompressPaddingShorthandFour
STAssertTrue([paddingRight isEqualToString:@"20px"], @"margin-right should be 20px"); STAssertTrue([paddingRight isEqualToString:@"20px"], @"margin-right should be 20px");
} }


- (void)testUncompressBackgroundShorthand
{
DTCSSStylesheet *stylesheet = [DTCSSStylesheet defaultStyleSheet];
NSMutableDictionary *styles = [NSMutableDictionary dictionary];

[styles setObject:@"url(\"topbanner.png\") #00D repeat-y fixed" forKey:@"background"];
[stylesheet _uncompressShorthands:styles];

NSString *backgroundColor = [styles objectForKey:@"background-color"];
STAssertTrue([backgroundColor isEqualToString:@"#00D"], @"background-color should be #00D");
}

@end @end

0 comments on commit 97c3f0f

Please sign in to comment.