Add more safeguards 🚓🛂

This commit is contained in:
Olivier Halligon
2015-10-18 07:17:03 +02:00
parent f4ed872a45
commit 05dc420808
2 changed files with 17 additions and 11 deletions

View File

@@ -73,10 +73,13 @@ public class VariableNode : NodeType {
public class NowNode : NodeType { public class NowNode : NodeType {
public let format:Variable public let format:Variable
public class func parse(parser:TokenParser, token:Token) -> NodeType { public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
var format:Variable? var format:Variable?
let components = token.components() let components = token.components()
guard components.count <= 2 else {
throw TemplateSyntaxError("'now' tags may only have one argument: the format string `\(token.contents)`.")
}
if components.count == 2 { if components.count == 2 {
format = Variable(components[1]) format = Variable(components[1])
} }
@@ -85,11 +88,7 @@ public class NowNode : NodeType {
} }
public init(format:Variable?) { public init(format:Variable?) {
if let format = format { self.format = format ?? Variable("\"yyyy-MM-dd 'at' HH:mm\"")
self.format = format
} else {
self.format = Variable("\"yyyy-MM-dd 'at' HH:mm\"")
}
} }
public func render(context: Context) throws -> String { public func render(context: Context) throws -> String {
@@ -145,6 +144,7 @@ public class ForNode : NodeType {
self.variable = Variable(variable) self.variable = Variable(variable)
self.loopVariable = loopVariable self.loopVariable = loopVariable
self.nodes = nodes self.nodes = nodes
// TODO: Handle emptyNodes
} }
public func render(context: Context) throws -> String { public func render(context: Context) throws -> String {
@@ -168,7 +168,11 @@ public class IfNode : NodeType {
public let falseNodes:[NodeType] public let falseNodes:[NodeType]
public class func parse(parser:TokenParser, token:Token) throws -> NodeType { public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
let variable = token.components()[1] let components = token.components()
guard components.count == 2 else {
throw TemplateSyntaxError("'if' statements should use the following 'if condition' `\(token.contents)`.")
}
let variable = components[1]
var trueNodes = [NodeType]() var trueNodes = [NodeType]()
var falseNodes = [NodeType]() var falseNodes = [NodeType]()
@@ -187,7 +191,11 @@ public class IfNode : NodeType {
} }
public class func parse_ifnot(parser:TokenParser, token:Token) throws -> NodeType { public class func parse_ifnot(parser:TokenParser, token:Token) throws -> NodeType {
let variable = token.components()[1] let components = token.components()
guard components.count == 2 else {
throw TemplateSyntaxError("'ifnot' statements should use the following 'if condition' `\(token.contents)`.")
}
let variable = components[1]
var trueNodes = [NodeType]() var trueNodes = [NodeType]()
var falseNodes = [NodeType]() var falseNodes = [NodeType]()

View File

@@ -59,11 +59,9 @@ public class TokenParser {
case .Block: case .Block:
let tag = token.components().first let tag = token.components().first
if let parse_until = parse_until { if let parse_until = parse_until where parse_until(parser: self, token: token) {
if parse_until(parser: self, token: token) {
prependToken(token) prependToken(token)
return nodes return nodes
}
} }
if let tag = tag, let parser = self.tags[tag] { if let tag = tag, let parser = self.tags[tag] {