Skip to content

Commit

Permalink
fixed breaking variables lists and filters by spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka committed Dec 29, 2017
1 parent 3f144fd commit 140f880
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
29 changes: 28 additions & 1 deletion Sources/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,37 @@ extension String {
components.append(word)
}

return components
return smartJoin(components)
}
}

// joins back components around characters used in variables lists and filters
private func smartJoin(_ components: [String]) -> [String] {
var joinedComponents = components
// convert ["a", "|", "b"] and ["a|", "b"] to ["a|b"]
// do not allow ["a", "|b"]
for char in [",", "|", ":"] {
while let index = joinedComponents.index(of: char) {
if index > 0 {
joinedComponents[index-1] += char

if joinedComponents.count > index + 1 {
joinedComponents[index-1] += joinedComponents[index+1]
joinedComponents.remove(at: index+1)
}
}
joinedComponents.remove(at: index)
}
while let index = joinedComponents.index(where: { $0.hasSuffix(char) }) {
if joinedComponents.count > index {
joinedComponents[index] += joinedComponents[index+1]
joinedComponents.remove(at: index+1)
}
}
}
return joinedComponents
}


public enum Token : Equatable {
/// A token representing a piece of text.
Expand Down
6 changes: 3 additions & 3 deletions Tests/StencilTests/ForNodeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func testForNode() {
try expect(try node.render(context)) == "empty"
}

$0.it("can render a filter") {
let templateString = "{% for article in ars|default:articles %}" +
$0.it("can render a filter with spaces") {
let templateString = "{% for article in ars | default: a, b , articles %}" +
"- {{ article.title }} by {{ article.author }}.\n" +
"{% endfor %}\n"

Expand All @@ -128,7 +128,7 @@ func testForNode() {
}

$0.it("can iterate over dictionary") {
let templateString = "{% for key,value in dict %}" +
let templateString = "{% for key, value in dict %}" +
"{{ key }}: {{ value }}\n" +
"{% endfor %}\n"

Expand Down

0 comments on commit 140f880

Please sign in to comment.