fix(lexer): Handle incomplete tokens

Fixes #135
This commit is contained in:
Jan Berkel
2017-09-01 11:59:52 +02:00
committed by Kyle Fuller
parent f1fc747897
commit 482d595d01
3 changed files with 14 additions and 1 deletions

View File

@@ -6,6 +6,8 @@
- Fixes a potential crash when using the `{% for %}` template tag with the
incorrect amount of arguments.
- Fixes a potential crash when using incomplete tokens in a template for
example, `{%%}` or `{{}}`.
## 0.9.0

View File

@@ -5,8 +5,9 @@ struct Lexer {
self.templateString = templateString
}
func createToken(string:String) -> Token {
func createToken(string: String) -> Token {
func strip() -> String {
guard string.characters.count > 4 else { return "" }
let start = string.index(string.startIndex, offsetBy: 2)
let end = string.index(string.endIndex, offsetBy: -2)
return string[start..<end].trim(character: " ")

View File

@@ -54,5 +54,15 @@ func testLexer() {
try expect(tokens[0]) == Token.variable(value: "thing")
try expect(tokens[1]) == Token.variable(value: "name")
}
$0.it("can tokenize an unclosed block") {
let lexer = Lexer(templateString: "{%}")
let _ = lexer.tokenize()
}
$0.it("can tokenize an empty variable") {
let lexer = Lexer(templateString: "{{}}")
let _ = lexer.tokenize()
}
}
}