Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed using spaces in filter expressions and variables lists #178

Merged
merged 5 commits into from
May 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
[Yonas Kolb](https://github.com/yonaskolb)
[#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

- 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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/ForTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ForNode : NodeType {
let loopVariables = components[1].characters
.split(separator: ",")
.map(String.init)
.map { $0.trimmingCharacters(in: CharacterSet.whitespaces) }
.map { $0.trim(character: " ") }

var emptyNodes = [NodeType]()

Expand Down
19 changes: 17 additions & 2 deletions Sources/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ extension String {
var singleQuoteCount = 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 {
if character == "'" { singleQuoteCount += 1 }
else if character == "\"" { doubleQuoteCount += 1 }
Expand All @@ -19,7 +34,7 @@ extension String {
if separate != separator {
word.append(separate)
} else if (singleQuoteCount % 2 == 0 || doubleQuoteCount % 2 == 0) && !word.isEmpty {
components.append(word)
appendWord(word)
word = ""
}

Expand All @@ -33,7 +48,7 @@ extension String {
}

if !word.isEmpty {
components.append(word)
appendWord(word)
}

return components
Expand Down
4 changes: 2 additions & 2 deletions Sources/Variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ extension Dictionary : Normalizable {

func parseFilterComponents(token: String) -> (String, [Variable]) {
var components = token.smartSplit(separator: ":")
let name = components.removeFirst()
let name = components.removeFirst().trim(character: " ")
let variables = components
.joined(separator: ":")
.smartSplit(separator: ",")
.map { Variable($0) }
.map { Variable($0.trim(character: " ")) }
return (name, variables)
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/StencilTests/FilterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func testFilter() {
}

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

$0.it("throws when you pass arguments to simple filter") {
Expand Down
6 changes: 3 additions & 3 deletions Tests/StencilTests/ForNodeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,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 %}" +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I'm familiar with that default filter accepting 3 arguments?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It supports variadic fallbacks for the default value. If a and b are not defined then articles would be there.

Copy link
Collaborator

@AliSoftware AliSoftware May 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! TIL 👍

"- {{ article.title }} by {{ article.author }}.\n" +
"{% endfor %}\n"

Expand Down Expand Up @@ -182,7 +182,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 }}," +
"{% endfor %}"

Expand Down