Skip to content

Commit

Permalink
Increase #46 compatibility with Mustache spec
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Mar 10, 2013
1 parent 214de53 commit c87fb0c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
54 changes: 35 additions & 19 deletions src/classes/GRMustacheBuffer.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,45 @@ - (NSString *)appendString:(NSString *)string contentType:(GRMustacheContentType
string = [string substringFromIndex:2];
}
}
NSRange blankRange = [string rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] options:NSBackwardsSearch|NSAnchoredSearch];
if (blankRange.location != NSNotFound) {
NSRange whiteSpaceRange = [string rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet] options:NSBackwardsSearch|NSAnchoredSearch];
if (whiteSpaceRange.location == NSNotFound) {
whiteSpaceRange.location = string.length;
whiteSpaceRange.length = 0;
NSUInteger prefixIndex = NSNotFound;
if (string.length > 0) {
prefixIndex = [string length]-1;
for (NSUInteger i = prefixIndex;;--i) {
unichar c = [string characterAtIndex:i];
if (c == ' ' || c == '\t') {
prefixIndex = i;
} else if (c == '\n') {
prefixIndex = i + 1;
break;
} else {
prefixIndex = NSNotFound;
break;
}
if (i == 0) {
break;
}
}
if (whiteSpaceRange.location != blankRange.location) {
self.prefix = [string substringWithRange:whiteSpaceRange];
string = [string substringWithRange:(NSRange){ .location = 0, .length = whiteSpaceRange.location }];
[self appendSafeString:string inputType:inputType];
_atLineStart = NO;
_flushablePrefix = YES;
return string;
} else {
[self appendSafeString:string inputType:inputType];
_atLineStart = NO;
return string;
}
} else {
}
if (prefixIndex != NSNotFound) {
self.prefix = [string substringFromIndex:prefixIndex];
string = [string substringWithRange:(NSRange){ .location = 0, .length = prefixIndex }];
[self appendSafeString:string inputType:inputType];
_atLineStart = NO;
_flushablePrefix = YES;
return string;
} else if (string.length > 0) {
[self appendSafeString:string inputType:inputType];
_atLineStart = NO;
return string;
} else {
if (_atLineStart) {
self.prefix = @"";
_flushablePrefix = YES;
_atLineStart = NO;
} else {
_flushablePrefix = NO;
}
return @"";
}
} else {
if (_atLineStart) {
Expand Down
14 changes: 10 additions & 4 deletions src/classes/GRMustacheCompiler.m
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,8 @@ - (BOOL)parser:(GRMustacheParser *)parser shouldContinueAfterParsingToken:(GRMus
switch (token.type) {
case GRMustacheTokenTypeSetDelimiter:
case GRMustacheTokenTypeComment:
// ignore
// // insert strippable text component
// [_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:@"" inputType:GRMustacheBufferInputTypeStrippableContent]];
// insert empty blank
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:@"" inputType:GRMustacheBufferInputTypeBlank]];
break;

case GRMustacheTokenTypePragma: {
Expand All @@ -234,6 +233,8 @@ - (BOOL)parser:(GRMustacheParser *)parser shouldContinueAfterParsingToken:(GRMus
}
_contentType = GRMustacheContentTypeHTML;
}
// insert empty blank
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:@"" inputType:GRMustacheBufferInputTypeBlank]];
} break;

case GRMustacheTokenTypeContent:
Expand All @@ -259,7 +260,11 @@ - (BOOL)parser:(GRMustacheParser *)parser shouldContinueAfterParsingToken:(GRMus
NSAssert(token.templateSubstring.length > 0, @"WTF empty GRMustacheTokenTypeBlank");

// Success: append GRMustacheTextComponent
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeBlank]];
if (_currentComponents.count == 0 && _componentsStack.count > 1) {
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeContent]];
} else {
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeBlank]];
}
break;

case GRMustacheTokenTypeBlankEndOfLine:
Expand Down Expand Up @@ -582,6 +587,7 @@ - (BOOL)parser:(GRMustacheParser *)parser shouldContinueAfterParsingToken:(GRMus
self.currentComponents = [_componentsStack lastObject];

[_currentComponents addObject:wrapperComponent];
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:@"" inputType:GRMustacheBufferInputTypeStrippableContent]];
} break;


Expand Down
6 changes: 3 additions & 3 deletions src/tests/Public/GRMustachePublicAPITest.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

@interface GRMustachePublicAPISuiteTest: GRMustachePublicAPITest
- (void)runTestsFromResource:(NSString *)name subdirectory:(NSString *)subpath;
- (GRMustacheTemplate *)templateForTemplateString:(NSString *)templateString partialsDictionary:(NSDictionary *)partialsDictionary error:(NSError **)error;
- (GRMustacheTemplate *)templateForTemplateNamed:(NSString *)templateName templatesPath:(NSString *)templatesPath encoding:(NSStringEncoding)encoding error:(NSError **)error;
- (GRMustacheTemplate *)templateForTemplateNamed:(NSString *)templateName templatesURL:(NSURL *)templatesURL encoding:(NSStringEncoding)encoding error:(NSError **)error;
- (GRMustacheTemplate *)templateForTemplateString:(NSString *)templateString partialsDictionary:(NSDictionary *)partialsDictionary configuration:(GRMustacheConfiguration *)configuration error:(NSError **)error;
- (GRMustacheTemplate *)templateForTemplateNamed:(NSString *)templateName templatesPath:(NSString *)templatesPath encoding:(NSStringEncoding)encoding configuration:(GRMustacheConfiguration *)configuration error:(NSError **)error;
- (GRMustacheTemplate *)templateForTemplateNamed:(NSString *)templateName templatesURL:(NSURL *)templatesURL encoding:(NSStringEncoding)encoding configuration:(GRMustacheConfiguration *)configuration error:(NSError **)error;
@end
10 changes: 5 additions & 5 deletions src/tests/Public/v6.0/GRSpecificationSuitesTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ - (void)testSuiteFromContentsOfJSONFile:(NSString *)path
GRMustacheTemplate *template = [repository templateFromString:templateString error:NULL];
NSString *rendering = [template renderObject:data error:NULL];

// GRMustache doesn't care about white space rules of the Mustache specification.
// Compare rendering and expected rendering, but ignoring white space.
NSCharacterSet *w = [NSCharacterSet whitespaceAndNewlineCharacterSet];
rendering = [[rendering componentsSeparatedByCharactersInSet:w] componentsJoinedByString:@""];
expected = [[expected componentsSeparatedByCharactersInSet:w] componentsJoinedByString:@""];
// // GRMustache doesn't care about white space rules of the Mustache specification.
// // Compare rendering and expected rendering, but ignoring white space.
// NSCharacterSet *w = [NSCharacterSet whitespaceAndNewlineCharacterSet];
// rendering = [[rendering componentsSeparatedByCharactersInSet:w] componentsJoinedByString:@""];
// expected = [[expected componentsSeparatedByCharactersInSet:w] componentsJoinedByString:@""];

STAssertEqualObjects(rendering, expected, @"Failed specification test in suite %@: %@", path, test);

Expand Down

0 comments on commit c87fb0c

Please sign in to comment.