Skip to content

Commit d35f54c

Browse files
authored
Merge pull request #130 from vi4m/fix/eof-comments-parsing
Fix parsing inline comments
2 parents fc6a393 + 560adff commit d35f54c

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

Sources/Leaf/Parser/LeafParser.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,20 @@ extension TemplateByteScanner {
195195
// Extract tag body.
196196
let body: [TemplateSyntax]?
197197
if name == "//" {
198+
let isEntirelyCommentLine: Bool = (peek(by: -4) == .newLine)
199+
198200
let s = makeSourceStart()
199201
let bytes = try extractBytes(untilUnescaped: [.newLine])
200-
// pop the newline
201-
try requirePop()
202+
203+
if isEntirelyCommentLine {
204+
try requirePop()
205+
}
206+
202207
body = [TemplateSyntax(
203208
type: .raw(TemplateRaw(data: bytes)),
204209
source: makeSource(using: s)
205210
)]
211+
206212
} else if name == "/*" {
207213
let s = makeSourceStart()
208214
var i = 0

Tests/LeafTests/LeafTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,46 @@ class LeafTests: XCTestCase {
433433
}
434434
}
435435

436+
// https://github.com/vapor/leaf/issues/127
437+
func testGH127Inline() throws {
438+
do {
439+
let template = """
440+
<html>
441+
<head>
442+
<title></title>#// Translate all copy!!!!!
443+
<style>
444+
"""
445+
let expected = """
446+
<html>
447+
<head>
448+
<title></title>
449+
<style>
450+
"""
451+
let data = try TemplateDataEncoder().testEncode(["a": "a"])
452+
try XCTAssertEqual(renderer.testRender(template, data), expected)
453+
}
454+
}
455+
456+
func testGH127SingleLine() throws {
457+
do {
458+
let template = """
459+
<html>
460+
<head>
461+
<title></title>
462+
#// Translate all copy!!!!!
463+
<style>
464+
"""
465+
let expected = """
466+
<html>
467+
<head>
468+
<title></title>
469+
<style>
470+
"""
471+
let data = try TemplateDataEncoder().testEncode(["a": "a"])
472+
try XCTAssertEqual(renderer.testRender(template, data), expected)
473+
}
474+
}
475+
436476
static var allTests = [
437477
("testPrint", testPrint),
438478
("testConstant", testConstant),
@@ -464,6 +504,8 @@ class LeafTests: XCTestCase {
464504
("testGH99", testGH99),
465505
("testGH101", testGH101),
466506
("testGH105", testGH105),
507+
("testGH127Inline", testGH127Inline),
508+
("testGH127SingleLine", testGH127SingleLine)
467509
]
468510
}
469511

0 commit comments

Comments
 (0)