diff --git a/parser/src/main/scala/play/twirl/parser/TwirlParser.scala b/parser/src/main/scala/play/twirl/parser/TwirlParser.scala index e873e0de..1139f6ab 100644 --- a/parser/src/main/scala/play/twirl/parser/TwirlParser.scala +++ b/parser/src/main/scala/play/twirl/parser/TwirlParser.scala @@ -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* @@ -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 @@ -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 diff --git a/parser/src/test/scala/play/twirl/parser/test/ParserSpec.scala b/parser/src/test/scala/play/twirl/parser/test/ParserSpec.scala index 7cac16d1..e6523dde 100644 --- a/parser/src/test/scala/play/twirl/parser/test/ParserSpec.scala +++ b/parser/src/test/scala/play/twirl/parser/test/ParserSpec.scala @@ -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 {