Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #726 from thusfresh/backgroundshorthand

Adds support for background CSS shorthand
  • Loading branch information...
commit aca5b1a44b5995c1cc4785d6887cb896eb8a4814 2 parents 49c4f3a + 97c3f0f
@Cocoanetics authored
View
18 Core/Source/DTCSSStylesheet.m
@@ -410,6 +410,24 @@ - (void)_uncompressShorthands:(NSMutableDictionary *)styles
// remove the shorthand
[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
View
7 Core/Source/NSScanner+HTML.h
@@ -41,6 +41,13 @@
*/
- (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
View
13 Core/Source/NSScanner+HTML.m
@@ -221,6 +221,11 @@ - (BOOL)scanCSSURL:(NSString **)urlString
- (BOOL)scanHTMLColor:(DTColor **)color
{
+ return [self scanHTMLColor:color HTMLName:NULL];
+}
+
+- (BOOL)scanHTMLColor:(DTColor **)color HTMLName:(NSString **)name
+{
NSUInteger indexBefore = [self scanLocation];
NSString *colorName = nil;
@@ -262,12 +267,16 @@ - (BOOL)scanHTMLColor:(DTColor **)color
self.scanLocation = indexBefore;
return NO;
}
-
+
if (color)
{
*color = foundColor;
}
-
+
+ if (name) {
+ *name = colorName;
+ }
+
return YES;
}
View
12 Test/Source/DTCSSStylesheetTest.m
@@ -495,4 +495,16 @@ - (void)testUncompressPaddingShorthandFour
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
Please sign in to comment.
Something went wrong with that request. Please try again.