Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #22 (extra lines) #32

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 27 additions & 5 deletions StencilTests/NodeTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation
import XCTest
import Stencil
@testable import Stencil


class ErrorNode : NodeType {
Expand Down Expand Up @@ -54,6 +54,28 @@ class RenderNodeTests: NodeTests {
}

class ForNodeTests: NodeTests {
func testParseFor() {
let tokens = [
Token.Block(value: "for item in items"),
.Text(value: "\nan item\n"),
.Block(value: "endfor"),
.Text(value: "\nthe end\n")
]

let parser = TokenParser(tokens: tokens)
assertSuccess(try parser.parse()) { nodes in
XCTAssertEqual(nodes.count, 2)
let forNode = nodes[0] as! ForNode
XCTAssertEqual(forNode.variable, Variable("items"))
XCTAssertEqual(forNode.loopVariable, "item")
XCTAssertEqual(forNode.nodes.count, 1)
let loopNode = forNode.nodes[0] as? TextNode
XCTAssertEqual(loopNode?.text, "an item\n")
let textNode = nodes[1] as! TextNode
XCTAssertEqual(textNode.text, "the end\n")
}
}

func testForNodeRender() {
let node = ForNode(variable: "items", loopVariable: "item", nodes: [VariableNode(variable: "item")], emptyNodes:[])
XCTAssertEqual(try? node.render(context), "123")
Expand All @@ -67,9 +89,9 @@ class IfNodeTests: NodeTests {
func testParseIf() {
let tokens = [
Token.Block(value: "if value"),
Token.Text(value: "true"),
Token.Text(value: "\ntrue"),
Token.Block(value: "else"),
Token.Text(value: "false"),
Token.Text(value: "\nfalse"),
Token.Block(value: "endif")
]

Expand All @@ -91,9 +113,9 @@ class IfNodeTests: NodeTests {
func testParseIfNot() {
let tokens = [
Token.Block(value: "ifnot value"),
Token.Text(value: "false"),
Token.Text(value: "\nfalse"),
Token.Block(value: "else"),
Token.Text(value: "true"),
Token.Text(value: "\ntrue"),
Token.Block(value: "endif")
]

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