Skip to content

Commit

Permalink
[ExtraEmptyLines] Fix #22, making the tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
AliSoftware committed Oct 18, 2015
1 parent ac742b0 commit fa0df6d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Stencil/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,16 @@ public class ForNode : NodeType {

var emptyNodes = [NodeType]()

parser.eatNextCRLF() // If CRLF after {% for %}, eat it
let forNodes = try parser.parse(until(["endfor", "empty"]))

if let token = parser.nextToken() {
if token.contents == "empty" {
parser.eatNextCRLF() // if CRLF after {% empty %}, eat it
emptyNodes = try parser.parse(until(["endfor"]))
parser.nextToken()
}
parser.eatNextCRLF() // if CRLF after {% endfor %}, eat it
} else {
throw TemplateSyntaxError("`endfor` was not found.")
}
Expand Down Expand Up @@ -172,13 +175,16 @@ public class IfNode : NodeType {
var trueNodes = [NodeType]()
var falseNodes = [NodeType]()

parser.eatNextCRLF() // If CRLF after {% if %}, eat it
trueNodes = try parser.parse(until(["endif", "else"]))

if let token = parser.nextToken() {
if token.contents == "else" {
parser.eatNextCRLF() // If CRLF after {% else %}, eat it
falseNodes = try parser.parse(until(["endif"]))
parser.nextToken()
}
parser.eatNextCRLF() // If CRLF after {% endif %}, eat it
} else {
throw TemplateSyntaxError("`endif` was not found.")
}
Expand All @@ -191,13 +197,16 @@ public class IfNode : NodeType {
var trueNodes = [NodeType]()
var falseNodes = [NodeType]()

parser.eatNextCRLF() // If CRLF after {% ifnot %}, eat it
falseNodes = try parser.parse(until(["endif", "else"]))

if let token = parser.nextToken() {
if token.contents == "else" {
parser.eatNextCRLF() // If CRLF after {% else %}, eat it
trueNodes = try parser.parse(until(["endif"]))
parser.nextToken()
}
parser.eatNextCRLF() // If CRLF after {% endif %}, eat it
} else {
throw TemplateSyntaxError("`endif` was not found.")
}
Expand Down
7 changes: 7 additions & 0 deletions Stencil/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public class TokenParser {
return nodes
}

public func eatNextCRLF() {
if let token = tokens.first, case Token.Text(let text) = token where text.characters.first == "\n" {
let strippedText: String = String(text.characters.dropFirst())
tokens[0] = Token.Text(value: strippedText)
}
}

public func nextToken() -> Token? {
if tokens.count > 0 {
return tokens.removeAtIndex(0)
Expand Down
3 changes: 1 addition & 2 deletions StencilTests/StencilTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class StencilTests: XCTestCase {
let fixture = "There are 2 articles.\n" +
"\n" +
" - Migrating from OCUnit to XCTest by Kyle Fuller.\n" +
" - Memory Management with ARC by Kyle Fuller.\n" +
"\n"
" - Memory Management with ARC by Kyle Fuller.\n"

XCTAssertEqual(result, fixture)
}
Expand Down

0 comments on commit fa0df6d

Please sign in to comment.