Skip to content

Commit 39ed9aa

Browse files
authored
Fixed using spaces in filter expressions and variables lists (#178)
* fixed using spaces in filter expression * fixed breaking variables lists and filters by spaces * simplified smartJoin * avoid force unwrap
1 parent d935f65 commit 39ed9aa

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
[Yonas Kolb](https://github.com/yonaskolb)
99
[#394](https://github.com/stencilproject/Stencil/pull/214)
1010

11+
- Adds support for using spaces in filter expression
12+
[Ilya Puchka](https://github.com/yonaskolb)
13+
[#178](https://github.com/stencilproject/Stencil/pull/178)
14+
1115
### Bug Fixes
1216

1317
- Fixed using quote as a filter parameter
18+
[Ilya Puchka](https://github.com/yonaskolb)
19+
[#210](https://github.com/stencilproject/Stencil/pull/210)
1420

1521

1622
## 0.11.0 (2018-04-04)

Sources/ForTag.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ForNode : NodeType {
2424
let loopVariables = components[1].characters
2525
.split(separator: ",")
2626
.map(String.init)
27-
.map { $0.trimmingCharacters(in: CharacterSet.whitespaces) }
27+
.map { $0.trim(character: " ") }
2828

2929
var emptyNodes = [NodeType]()
3030

Sources/Tokenizer.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ extension String {
1010
var singleQuoteCount = 0
1111
var doubleQuoteCount = 0
1212

13+
let specialCharacters = ",|:"
14+
func appendWord(_ word: String) {
15+
if components.count > 0 {
16+
if let precedingChar = components.last?.characters.last, specialCharacters.characters.contains(precedingChar) {
17+
components[components.count-1] += word
18+
} else if specialCharacters.contains(word) {
19+
components[components.count-1] += word
20+
} else {
21+
components.append(word)
22+
}
23+
} else {
24+
components.append(word)
25+
}
26+
}
27+
1328
for character in self.characters {
1429
if character == "'" { singleQuoteCount += 1 }
1530
else if character == "\"" { doubleQuoteCount += 1 }
@@ -19,7 +34,7 @@ extension String {
1934
if separate != separator {
2035
word.append(separate)
2136
} else if (singleQuoteCount % 2 == 0 || doubleQuoteCount % 2 == 0) && !word.isEmpty {
22-
components.append(word)
37+
appendWord(word)
2338
word = ""
2439
}
2540

@@ -33,7 +48,7 @@ extension String {
3348
}
3449

3550
if !word.isEmpty {
36-
components.append(word)
51+
appendWord(word)
3752
}
3853

3954
return components

Sources/Variable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ extension Dictionary : Normalizable {
209209

210210
func parseFilterComponents(token: String) -> (String, [Variable]) {
211211
var components = token.smartSplit(separator: ":")
212-
let name = components.removeFirst()
212+
let name = components.removeFirst().trim(character: " ")
213213
let variables = components
214214
.joined(separator: ":")
215215
.smartSplit(separator: ",")
216-
.map { Variable($0) }
216+
.map { Variable($0.trim(character: " ")) }
217217
return (name, variables)
218218
}
219219

Tests/StencilTests/FilterSpec.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ func testFilter() {
7878
}
7979

8080
$0.it("allows whitespace in expression") {
81-
let template = Template(templateString: "{{ name | uppercase }}")
82-
let result = try template.render(Context(dictionary: ["name": "kyle"]))
83-
try expect(result) == "KYLE"
81+
let template = Template(templateString: "{{ value | join : \", \" }}")
82+
let result = try template.render(Context(dictionary: ["value": ["One", "Two"]]))
83+
try expect(result) == "One, Two"
8484
}
8585

8686
$0.it("throws when you pass arguments to simple filter") {

Tests/StencilTests/ForNodeSpec.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ func testForNode() {
111111
try expect(try node.render(context)) == "empty"
112112
}
113113

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

@@ -182,7 +182,7 @@ func testForNode() {
182182
}
183183

184184
$0.it("can iterate over dictionary") {
185-
let templateString = "{% for key,value in dict %}" +
185+
let templateString = "{% for key, value in dict %}" +
186186
"{{ key }}: {{ value }}," +
187187
"{% endfor %}"
188188

0 commit comments

Comments
 (0)