Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit 743df7a

Browse files
committed
Using a flag to consume whitespace and newline at the end of a verbatim block
1 parent 69780da commit 743df7a

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

src/Microsoft.AspNet.Razor/Parser/CSharpCodeParser.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,17 @@ private void VerbatimBlock()
316316
Span.ChunkGenerator = SpanChunkGenerator.Null;
317317
}
318318

319-
if (!At(CSharpSymbolType.WhiteSpace) && !At(CSharpSymbolType.NewLine))
319+
Output(SpanKind.MetaCode);
320+
321+
if (IsNested)
320322
{
321-
PutCurrentBack();
323+
CompleteBlock(insertMarkerIfNecessary: false, captureWhitespaceToEndOfLine: true);
324+
Output(SpanKind.Code);
325+
}
326+
else if (At(CSharpSymbolType.WhiteSpace) || At(CSharpSymbolType.NewLine))
327+
{
328+
Context.NullGenerateWhitespaceAndNewLine = true;
322329
}
323-
324-
CompleteBlock(insertMarkerIfNecessary: false, captureWhitespaceToEndOfLine: true);
325-
Output(SpanKind.MetaCode);
326330
}
327331

328332
private void ImplicitExpression()

src/Microsoft.AspNet.Razor/Parser/HtmlMarkupParser.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,19 @@ protected void SkipToAndParseCode(Func<HtmlSymbol, bool> condition)
7171
var startOfLine = false;
7272
while (!EndOfFile && !condition(CurrentSymbol))
7373
{
74-
if (At(HtmlSymbolType.NewLine))
74+
if (Context.NullGenerateWhitespaceAndNewLine)
75+
{
76+
Context.NullGenerateWhitespaceAndNewLine = false;
77+
Span.ChunkGenerator = SpanChunkGenerator.Null;
78+
AcceptWhile(symbol => symbol.Type == HtmlSymbolType.WhiteSpace);
79+
if (At(HtmlSymbolType.NewLine))
80+
{
81+
AcceptAndMoveNext();
82+
}
83+
84+
Output(SpanKind.Markup);
85+
}
86+
else if (At(HtmlSymbolType.NewLine))
7587
{
7688
if (last != null)
7789
{

src/Microsoft.AspNet.Razor/Parser/ParserContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public BlockBuilder CurrentBlock
6767
public Span LastSpan { get; private set; }
6868
public bool WhiteSpaceIsSignificantToAncestorBlock { get; set; }
6969

70+
public bool NullGenerateWhitespaceAndNewLine { get; set; }
71+
7072
public AcceptedCharacters LastAcceptedCharacters
7173
{
7274
get

test/Microsoft.AspNet.Razor.Test/Parser/Html/HtmlDocumentTest.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public void ParseDocumentIgnoresTagsInContentsOfScriptTag()
291291
}
292292

293293
[Fact]
294-
public void ParseDocumentDoesNotRenderExtraNewLine()
294+
public void ParseDocumentDoesNotRenderExtraNewLineAtTheEndOfVerbatimBlock()
295295
{
296296
ParseDocumentTest("@{\r\n}\r\n<html>",
297297
new MarkupBlock(
@@ -300,10 +300,27 @@ public void ParseDocumentDoesNotRenderExtraNewLine()
300300
Factory.CodeTransition(),
301301
Factory.MetaCode("{").Accepts(AcceptedCharacters.None),
302302
Factory.Code("\r\n").AsStatement().AutoCompleteWith(null, false),
303-
Factory.MetaCode("}\r\n").Accepts(AcceptedCharacters.None)),
303+
Factory.MetaCode("}").Accepts(AcceptedCharacters.None)),
304+
Factory.Markup("\r\n").With(SpanChunkGenerator.Null),
304305
BlockFactory.MarkupTagBlock("<html>")));
305306
}
306307

308+
[Fact]
309+
public void ParseDocumentDoesNotIgnoreNewLineAtTheEndOfMarkupBlock()
310+
{
311+
ParseDocumentTest("@{\r\n}\r\n<html>\r\n",
312+
new MarkupBlock(
313+
Factory.EmptyHtml(),
314+
new StatementBlock(
315+
Factory.CodeTransition(),
316+
Factory.MetaCode("{").Accepts(AcceptedCharacters.None),
317+
Factory.Code("\r\n").AsStatement().AutoCompleteWith(null, false),
318+
Factory.MetaCode("}").Accepts(AcceptedCharacters.None)),
319+
Factory.Markup("\r\n").With(SpanChunkGenerator.Null),
320+
BlockFactory.MarkupTagBlock("<html>"),
321+
Factory.Markup("\r\n")));
322+
}
323+
307324
[Fact]
308325
public void ParseSectionIgnoresTagsInContentsOfScriptTag()
309326
{

0 commit comments

Comments
 (0)