Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds support for background CSS shorthand

Currently just pulls out the color property and sets background-color
  • Loading branch information...
commit 97c3f0fac13dad47646af8c89a80247586b61bc0 1 parent 49c4f3a
Ryan Johnson bismark authored
18 Core/Source/DTCSSStylesheet.m
View
@@ -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
7 Core/Source/NSScanner+HTML.h
View
@@ -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
13 Core/Source/NSScanner+HTML.m
View
@@ -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;
}
12 Test/Source/DTCSSStylesheetTest.m
View
@@ -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.