Skip to content

Commit

Permalink
Allow escaped quotes in tag parameters (#124)
Browse files Browse the repository at this point in the history
* add escaping with "$"

* Add new constant

* Change escaping to double back slash

---------

Co-authored-by: Sam Bishop <sambobjr@gmail.com>
  • Loading branch information
roya1v and dannflor committed May 13, 2024
1 parent f6a08a1 commit 547e48c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Sources/LeafKit/LeafLexer/LeafLexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ internal struct LeafLexer {
case .comma:
return .parameterDelimiter
case .quote:
let read = src.readWhile { $0 != .quote && $0 != .newLine }
let read = readWithEscapingQuotes(src: &src)
guard src.peek() == .quote else {
throw LexerError(.unterminatedStringLiteral, src: src, lexed: lexed)
}
Expand Down Expand Up @@ -269,4 +269,14 @@ internal struct LeafLexer {
return .parameter(.variable(name: name))
}
}

private func readWithEscapingQuotes(src: inout LeafRawTemplate) -> String {
let read = src.readWhile { $0 != .quote && $0 != .newLine }
if read.last == .backSlash && src.peek() == .quote {
src.pop()
return read.dropLast() + "\"" + readWithEscapingQuotes(src: &src)
} else {
return read
}
}
}
10 changes: 10 additions & 0 deletions Tests/LeafKitTests/LeafTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ final class LeafTests: XCTestCase {
try XCTAssertEqual(render(template, [:]), expected)
}

func testEscapingQuote() throws {
let template = """
#("foo \\"bar\\"")
"""
let expected = """
foo "bar"
"""
try XCTAssertEqual(render(template), expected)
}

func testCount() throws {
let template = """
count: #count(array)
Expand Down

0 comments on commit 547e48c

Please sign in to comment.