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

Fix parsing inline comments #130

Merged
merged 3 commits into from
Oct 30, 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
10 changes: 8 additions & 2 deletions Sources/Leaf/Parser/LeafParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,20 @@ extension TemplateByteScanner {
// Extract tag body.
let body: [TemplateSyntax]?
if name == "//" {
let isEntirelyCommentLine: Bool = (peek(by: -4) == .newLine)

let s = makeSourceStart()
let bytes = try extractBytes(untilUnescaped: [.newLine])
// pop the newline
try requirePop()

if isEntirelyCommentLine {
try requirePop()
}

body = [TemplateSyntax(
type: .raw(TemplateRaw(data: bytes)),
source: makeSource(using: s)
)]

} else if name == "/*" {
let s = makeSourceStart()
var i = 0
Expand Down
42 changes: 42 additions & 0 deletions Tests/LeafTests/LeafTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,46 @@ class LeafTests: XCTestCase {
}
}

// https://github.com/vapor/leaf/issues/127
func testGH127Inline() throws {
do {
let template = """
<html>
<head>
<title></title>#// Translate all copy!!!!!
<style>
"""
let expected = """
<html>
<head>
<title></title>
<style>
"""
let data = try TemplateDataEncoder().testEncode(["a": "a"])
try XCTAssertEqual(renderer.testRender(template, data), expected)
}
}

func testGH127SingleLine() throws {
do {
let template = """
<html>
<head>
<title></title>
#// Translate all copy!!!!!
<style>
"""
let expected = """
<html>
<head>
<title></title>
<style>
"""
let data = try TemplateDataEncoder().testEncode(["a": "a"])
try XCTAssertEqual(renderer.testRender(template, data), expected)
}
}

static var allTests = [
("testPrint", testPrint),
("testConstant", testConstant),
Expand Down Expand Up @@ -464,6 +504,8 @@ class LeafTests: XCTestCase {
("testGH99", testGH99),
("testGH101", testGH101),
("testGH105", testGH105),
("testGH127Inline", testGH127Inline),
("testGH127SingleLine", testGH127SingleLine)
]
}

Expand Down