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 9, 2013
1 parent 7aab329 commit 214de53
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 84 deletions.
52 changes: 46 additions & 6 deletions src/classes/GRMustacheBuffer.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,54 @@ - (NSString *)appendString:(NSString *)string contentType:(GRMustacheContentType
case GRMustacheBufferInputTypeStrippableContent:
if (string.length > 0) {
if (self.prefix) {
[self appendSafeString:self.prefix inputType:GRMustacheBufferInputTypeBlank];
self.prefix = nil;
if ([string characterAtIndex:0] == '\n') {
string = [string substringFromIndex:1];
self.prefix = nil;
} else if ([string characterAtIndex:0] == '\r' && string.length >= 2 && [string characterAtIndex:1] == '\n') {
string = [string substringFromIndex:2];
self.prefix = nil;
} else {
[self appendSafeString:self.prefix inputType:GRMustacheBufferInputTypeBlank];
self.prefix = nil;
}
} else if (_atLineStart) {
if ([string characterAtIndex:0] == '\n') {
string = [string substringFromIndex:1];
} else if ([string characterAtIndex:0] == '\r' && string.length >= 2 && [string characterAtIndex:1] == '\n') {
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;
}
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 {
[self appendSafeString:string inputType:inputType];
_atLineStart = NO;
return string;
}
[self appendSafeString:string inputType:inputType];
_atLineStart = NO;
return string;
} else {
if (_atLineStart) {
self.prefix = @"";
_flushablePrefix = YES;
_atLineStart = NO;
} else {
_flushablePrefix = NO;
}
return @"";
}
Expand Down Expand Up @@ -138,9 +176,11 @@ - (NSString *)appendString:(NSString *)string contentType:(GRMustacheContentType
if (_atLineStart) {
self.prefix = string;
_atLineStart = NO;
_flushablePrefix = YES;
return string;
} else if (self.prefix) {
self.prefix = [self.prefix stringByAppendingString:string];
_flushablePrefix = NO;
return string;
} else {
[self appendSafeString:string inputType:inputType];
Expand Down Expand Up @@ -173,7 +213,7 @@ - (NSString *)appendString:(NSString *)string contentType:(GRMustacheContentType

- (void)flush
{
if (self.prefix) {
if (self.prefix && _flushablePrefix) {
[self appendSafeString:self.prefix inputType:GRMustacheBufferInputTypeBlank];
self.prefix = nil;
}
Expand Down
1 change: 1 addition & 0 deletions src/classes/GRMustacheBuffer_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typedef NS_ENUM(NSInteger, GRMustacheBufferInputType) {
GRMustacheContentType _contentType;
NSString *_prefix;
BOOL _atLineStart;
BOOL _flushablePrefix;
}

/**
Expand Down
25 changes: 11 additions & 14 deletions src/classes/GRMustacheCompiler.m
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ - (id)initWithConfiguration:(GRMustacheConfiguration *)configuration
_openingTokenStack = [[NSMutableArray alloc] initWithCapacity:20];
_tagValueStack = [[NSMutableArray alloc] initWithCapacity:20];
_contentType = configuration.contentType;
_stripsBlankLines = configuration.stripsBlankLines;
_contentTypeLocked = NO;
}
return self;
Expand Down Expand Up @@ -214,7 +213,9 @@ - (BOOL)parser:(GRMustacheParser *)parser shouldContinueAfterParsingToken:(GRMus
switch (token.type) {
case GRMustacheTokenTypeSetDelimiter:
case GRMustacheTokenTypeComment:
// Ignore
// ignore
// // insert strippable text component
// [_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:@"" inputType:GRMustacheBufferInputTypeStrippableContent]];
break;

case GRMustacheTokenTypePragma: {
Expand All @@ -237,20 +238,16 @@ - (BOOL)parser:(GRMustacheParser *)parser shouldContinueAfterParsingToken:(GRMus

case GRMustacheTokenTypeContent:
// Parser validation
NSAssert(token.templateSubstring.length > 0, @"WTF parser?");
NSAssert(token.templateSubstring.length > 0, @"WTF empty GRMustacheTokenTypeContent");

// Success: append GRMustacheTextComponent
if (_stripsBlankLines) {
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeStrippableContent]];
} else {
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeContent]];
}
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeContent]];
break;


case GRMustacheTokenTypeContentEndOfLine:
// Parser validation
NSAssert(token.templateSubstring.length > 0, @"WTF parser?");
NSAssert(token.templateSubstring.length > 0, @"WTF empty GRMustacheTokenTypeContentEndOfLine");

// Success: append GRMustacheTextComponent
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeContentEndOfLine]];
Expand All @@ -259,15 +256,15 @@ - (BOOL)parser:(GRMustacheParser *)parser shouldContinueAfterParsingToken:(GRMus

case GRMustacheTokenTypeBlank:
// Parser validation
NSAssert(token.templateSubstring.length > 0, @"WTF parser?");
NSAssert(token.templateSubstring.length > 0, @"WTF empty GRMustacheTokenTypeBlank");

// Success: append GRMustacheTextComponent
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeBlank]];
break;

case GRMustacheTokenTypeBlankEndOfLine:
// Parser validation
NSAssert(token.templateSubstring.length > 0, @"WTF parser?");
NSAssert(token.templateSubstring.length > 0, @"WTF empty GRMustacheTokenTypeBlankEndOfLine");

// Success: append GRMustacheTextComponent
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeBlankEndOfLine]];
Expand Down Expand Up @@ -513,7 +510,7 @@ - (BOOL)parser:(GRMustacheParser *)parser shouldContinueAfterParsingToken:(GRMus
}

if (_currentTagValue == nil) {
NSAssert(_currentTagValue, @"WTF");
NSAssert(_currentTagValue, @"WTF expected _currentTagValue");
}
if (expression && ![expression isEqual:_currentTagValue]) {
[self failWithFatalError:[self parseErrorAtToken:token description:[NSString stringWithFormat:@"Unexpected %@ closing tag", token.templateSubstring]]];
Expand Down Expand Up @@ -570,11 +567,11 @@ - (BOOL)parser:(GRMustacheParser *)parser shouldContinueAfterParsingToken:(GRMus
} break;

default:
NSAssert(NO, @"WTF");
NSAssert(NO, @"WTF unexpected _currentOpeningToken.type");
break;
}

NSAssert(wrapperComponent, @"WTF");
NSAssert(wrapperComponent, @"WTF expected wrapperComponent");
[_tagValueStack removeLastObject];
self.currentTagValue = [_tagValueStack lastObject];

Expand Down
1 change: 0 additions & 1 deletion src/classes/GRMustacheCompiler_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
NSMutableArray *_tagValueStack;

GRMustacheTemplateRepository *_templateRepository;
BOOL _stripsBlankLines;
GRMustacheContentType _contentType;
BOOL _contentTypeLocked;
}
Expand Down
Loading

0 comments on commit 214de53

Please sign in to comment.