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 d1507d3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- Fixed rendering `{{ block.super }}` with several levels of inheritance
- Fixed checking dictionary values for nil in `default` filter
- Fixed comparing string variables with string literals, in Swift 4 string literals became `Substring` and thus couldn't be directly compared to strings.
- Fixed using spaces in filter expression
- Fixed using spaces in filter expressions and variables lists


## 0.10.1
Expand Down
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 d1507d3

Please sign in to comment.