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 whitespaces after else{...} and elseif{...} is not shown (backport #712) by @yousuketto #713

Merged
merged 5 commits into from
Nov 16, 2023
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: 2 additions & 4 deletions parser/src/main/scala/play/twirl/parser/TwirlParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ import scala.util.parsing.input.OffsetPosition
* complexExpr : parentheses
* safeExpression : '@' parentheses
* ifExpression : '@' "if" parentheses expressionPart (elseIfCall)* elseCall?
* elseCall : whitespaceNoBreak? "else" expressionPart whitespaceNoBreak?
* elseIfCall : whitespaceNoBreak? "else if" parentheses expressionPart whitespaceNoBreak?
* elseCall : whitespaceNoBreak? "else" whitespaceNoBreak? expressionPart
* elseIfCall : whitespaceNoBreak? "else if" parentheses whitespaceNoBreak? expressionPart
* chainedMethods : ('.' methodCall)+
* expressionPart : chainedMethods | block | (whitespaceNoBreak scalaBlockChained) | parentheses
* expression : '@' methodCall expressionPart*
Expand Down Expand Up @@ -853,7 +853,6 @@ class TwirlParser(val shouldParseInclusiveDot: Boolean) {
if (args != null) {
val blk = expressionPart(blockArgsAllowed = true)
if (blk != null) {
whitespaceNoBreak()
Seq(Simple("else if" + args), blk)
} else {
null
Expand All @@ -874,7 +873,6 @@ class TwirlParser(val shouldParseInclusiveDot: Boolean) {
whitespaceNoBreak()
val blk = expressionPart(blockArgsAllowed = true)
if (blk != null) {
whitespaceNoBreak()
Seq(Simple("else"), blk)
} else {
null
Expand Down
36 changes: 36 additions & 0 deletions parser/src/test/scala/play/twirl/parser/test/ParserSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,42 @@ class ParserSpec extends AnyWordSpec with Matchers with Inside {
val secondCaseBlockBody = secondCaseBlock(1).asInstanceOf[Block]
secondCaseBlockBody.content(1).asInstanceOf[Plain].text mustBe "Not a nice string "
}

"whitespaces after 'else {...}' as plain" in {
val template = parseTemplateString(
"""@if(condition) {ifblock body} else {elseblock body} Some plain text with whitespaces"""
)
val ifExpressions = template.content(0).asInstanceOf[Display].exp.parts
ifExpressions.head must be(Simple("if(condition)"))
val ifBlockBody = ifExpressions(1).asInstanceOf[Block].content(0)
ifBlockBody mustBe Plain("ifblock body")
val elsePart = ifExpressions(2)
elsePart mustBe Simple("else")
val elseBlockBody = ifExpressions(3).asInstanceOf[Block].content(0)
elseBlockBody mustBe Plain("elseblock body")
val afterIfExpressionOfWhitespaces = template.content(1)
afterIfExpressionOfWhitespaces mustBe Plain(" ")
val afterWhitespaces = template.content(2)
afterWhitespaces mustBe Plain("Some plain text with whitespaces")
}

"whitespaces after 'else if(condition) {...}' as plain" in {
val template = parseTemplateString(
"""@if(condition) {ifblock body} else if(condition2) {elseifblock body} Some plain text with whitespaces"""
)
val ifExpressions = template.content(0).asInstanceOf[Display].exp.parts
ifExpressions.head must be(Simple("if(condition)"))
val ifBlockBody = ifExpressions(1).asInstanceOf[Block].content(0)
ifBlockBody mustBe Plain("ifblock body")
val elseIfPart = ifExpressions(2)
elseIfPart mustBe Simple("else if(condition2)")
val elseBlockBody = ifExpressions(3).asInstanceOf[Block].content(0)
elseBlockBody mustBe Plain("elseifblock body")
val afterIfExpressionOfWhitespaces = template.content(1)
afterIfExpressionOfWhitespaces mustBe Plain(" ")
val afterWhitespaces = template.content(2)
afterWhitespaces mustBe Plain("Some plain text with whitespaces")
}
}

"handle local definitions" when {
Expand Down
Loading