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
This commit is contained in:
@@ -8,9 +8,15 @@
|
|||||||
[Yonas Kolb](https://github.com/yonaskolb)
|
[Yonas Kolb](https://github.com/yonaskolb)
|
||||||
[#394](https://github.com/stencilproject/Stencil/pull/214)
|
[#394](https://github.com/stencilproject/Stencil/pull/214)
|
||||||
|
|
||||||
|
- Adds support for using spaces in filter expression
|
||||||
|
[Ilya Puchka](https://github.com/yonaskolb)
|
||||||
|
[#178](https://github.com/stencilproject/Stencil/pull/178)
|
||||||
|
|
||||||
### Bug Fixes
|
### Bug Fixes
|
||||||
|
|
||||||
- Fixed using quote as a filter parameter
|
- Fixed using quote as a filter parameter
|
||||||
|
[Ilya Puchka](https://github.com/yonaskolb)
|
||||||
|
[#210](https://github.com/stencilproject/Stencil/pull/210)
|
||||||
|
|
||||||
|
|
||||||
## 0.11.0 (2018-04-04)
|
## 0.11.0 (2018-04-04)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class ForNode : NodeType {
|
|||||||
let loopVariables = components[1].characters
|
let loopVariables = components[1].characters
|
||||||
.split(separator: ",")
|
.split(separator: ",")
|
||||||
.map(String.init)
|
.map(String.init)
|
||||||
.map { $0.trimmingCharacters(in: CharacterSet.whitespaces) }
|
.map { $0.trim(character: " ") }
|
||||||
|
|
||||||
var emptyNodes = [NodeType]()
|
var emptyNodes = [NodeType]()
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,21 @@ extension String {
|
|||||||
var singleQuoteCount = 0
|
var singleQuoteCount = 0
|
||||||
var doubleQuoteCount = 0
|
var doubleQuoteCount = 0
|
||||||
|
|
||||||
|
let specialCharacters = ",|:"
|
||||||
|
func appendWord(_ word: String) {
|
||||||
|
if components.count > 0 {
|
||||||
|
if let precedingChar = components.last?.characters.last, specialCharacters.characters.contains(precedingChar) {
|
||||||
|
components[components.count-1] += word
|
||||||
|
} else if specialCharacters.contains(word) {
|
||||||
|
components[components.count-1] += word
|
||||||
|
} else {
|
||||||
|
components.append(word)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
components.append(word)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for character in self.characters {
|
for character in self.characters {
|
||||||
if character == "'" { singleQuoteCount += 1 }
|
if character == "'" { singleQuoteCount += 1 }
|
||||||
else if character == "\"" { doubleQuoteCount += 1 }
|
else if character == "\"" { doubleQuoteCount += 1 }
|
||||||
@@ -19,7 +34,7 @@ extension String {
|
|||||||
if separate != separator {
|
if separate != separator {
|
||||||
word.append(separate)
|
word.append(separate)
|
||||||
} else if (singleQuoteCount % 2 == 0 || doubleQuoteCount % 2 == 0) && !word.isEmpty {
|
} else if (singleQuoteCount % 2 == 0 || doubleQuoteCount % 2 == 0) && !word.isEmpty {
|
||||||
components.append(word)
|
appendWord(word)
|
||||||
word = ""
|
word = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +48,7 @@ extension String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !word.isEmpty {
|
if !word.isEmpty {
|
||||||
components.append(word)
|
appendWord(word)
|
||||||
}
|
}
|
||||||
|
|
||||||
return components
|
return components
|
||||||
|
|||||||
@@ -209,11 +209,11 @@ extension Dictionary : Normalizable {
|
|||||||
|
|
||||||
func parseFilterComponents(token: String) -> (String, [Variable]) {
|
func parseFilterComponents(token: String) -> (String, [Variable]) {
|
||||||
var components = token.smartSplit(separator: ":")
|
var components = token.smartSplit(separator: ":")
|
||||||
let name = components.removeFirst()
|
let name = components.removeFirst().trim(character: " ")
|
||||||
let variables = components
|
let variables = components
|
||||||
.joined(separator: ":")
|
.joined(separator: ":")
|
||||||
.smartSplit(separator: ",")
|
.smartSplit(separator: ",")
|
||||||
.map { Variable($0) }
|
.map { Variable($0.trim(character: " ")) }
|
||||||
return (name, variables)
|
return (name, variables)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,9 +78,9 @@ func testFilter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$0.it("allows whitespace in expression") {
|
$0.it("allows whitespace in expression") {
|
||||||
let template = Template(templateString: "{{ name | uppercase }}")
|
let template = Template(templateString: "{{ value | join : \", \" }}")
|
||||||
let result = try template.render(Context(dictionary: ["name": "kyle"]))
|
let result = try template.render(Context(dictionary: ["value": ["One", "Two"]]))
|
||||||
try expect(result) == "KYLE"
|
try expect(result) == "One, Two"
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("throws when you pass arguments to simple filter") {
|
$0.it("throws when you pass arguments to simple filter") {
|
||||||
|
|||||||
@@ -111,8 +111,8 @@ func testForNode() {
|
|||||||
try expect(try node.render(context)) == "empty"
|
try expect(try node.render(context)) == "empty"
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can render a filter") {
|
$0.it("can render a filter with spaces") {
|
||||||
let templateString = "{% for article in ars|default:articles %}" +
|
let templateString = "{% for article in ars | default: a, b , articles %}" +
|
||||||
"- {{ article.title }} by {{ article.author }}.\n" +
|
"- {{ article.title }} by {{ article.author }}.\n" +
|
||||||
"{% endfor %}\n"
|
"{% endfor %}\n"
|
||||||
|
|
||||||
@@ -182,7 +182,7 @@ func testForNode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can iterate over dictionary") {
|
$0.it("can iterate over dictionary") {
|
||||||
let templateString = "{% for key,value in dict %}" +
|
let templateString = "{% for key, value in dict %}" +
|
||||||
"{{ key }}: {{ value }}," +
|
"{{ key }}: {{ value }}," +
|
||||||
"{% endfor %}"
|
"{% endfor %}"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user