Skip to content

Commit

Permalink
feat: fixed parsing several filter arguments with quotes (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyapuchka authored and kylef committed Apr 18, 2017
1 parent d1717df commit 5541eae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
10 changes: 7 additions & 3 deletions Sources/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ extension String {
var word = ""
var components: [String] = []
var separate: Character = separator
var singleQuoteCount = 0
var doubleQuoteCount = 0

for character in self.characters {
if character == "'" { singleQuoteCount += 1 }
else if character == "\"" { doubleQuoteCount += 1 }

if character == separate {

if separate != separator {
word.append(separate)
}

if !word.isEmpty {
} else if singleQuoteCount % 2 == 0 && doubleQuoteCount % 2 == 0 && !word.isEmpty {
components.append(word)
word = ""
}
Expand Down
22 changes: 19 additions & 3 deletions Tests/StencilTests/FilterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func testFilter() {
try expect(result) == "Kyle Kyle"
}

$0.it("allows you to register a custom filter which accepts arguments") {
let template = Template(templateString: "{{ name|repeat:'value' }}")
$0.it("allows you to register a custom filter which accepts single argument") {
let template = Template(templateString: "{{ name|repeat:'value1, \"value2\"' }}")

let repeatExtension = Extension()
repeatExtension.registerFilter("repeat") { value, arguments in
Expand All @@ -35,7 +35,23 @@ func testFilter() {
}
let result = try template.render(Context(dictionary: context, environment: Environment(extensions: [repeatExtension])))
try expect(result) == "Kyle Kyle with args value"
try expect(result) == "Kyle Kyle with args value1, \"value2\""
}

$0.it("allows you to register a custom filter which accepts several arguments") {
let template = Template(templateString: "{{ name|repeat:'value\"1\"',\"value'2'\",'(key, value)' }}")

let repeatExtension = Extension()
repeatExtension.registerFilter("repeat") { value, arguments in
if !arguments.isEmpty {
return "\(value!) \(value!) with args 0: \(arguments[0]!), 1: \(arguments[1]!), 2: \(arguments[2]!)"
}

return nil
}

let result = try template.render(Context(dictionary: context, environment: Environment(extensions: [repeatExtension])))
try expect(result) == "Kyle Kyle with args 0: value\"1\", 1: value'2', 2: (key, value)"
}

$0.it("allows you to register a custom which throws") {
Expand Down

0 comments on commit 5541eae

Please sign in to comment.