From 4d80c96f9c1d1e5f50cc8e7d5a26f95322aa60ea Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Wed, 6 May 2015 15:45:26 -0700 Subject: [PATCH] [Fixes #50] Removed unnecessary whitespaces before text tag --- .../Parser/CSharpCodeParser.Statements.cs | 21 ++++--- .../Generator/CSharpRazorCodeGeneratorTest.cs | 1 + .../Generator/RazorCodeGeneratorTest.cs | 2 +- .../Parser/CSharp/CSharpRazorCommentsTest.cs | 1 - .../Parser/CSharp/CSharpToMarkupSwitchTest.cs | 3 - .../TagHelperParseTreeRewriterTest.cs | 1 - .../CS/Output/CodeBlockWithTextElement.cs | 61 +++++++++++++++++++ .../CS/Output/ComplexTagHelpers.cs | 4 +- .../CodeGenerator/CS/Output/InlineBlocks.cs | 6 +- .../CodeGenerator/CS/Output/ResolveUrl.cs | 4 +- .../CS/Source/CodeBlockWithTextElement.cshtml | 4 ++ 11 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/CodeBlockWithTextElement.cs create mode 100644 test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Source/CodeBlockWithTextElement.cshtml diff --git a/src/Microsoft.AspNet.Razor/Parser/CSharpCodeParser.Statements.cs b/src/Microsoft.AspNet.Razor/Parser/CSharpCodeParser.Statements.cs index e70d06b3e6..9ae42b5ac6 100644 --- a/src/Microsoft.AspNet.Razor/Parser/CSharpCodeParser.Statements.cs +++ b/src/Microsoft.AspNet.Razor/Parser/CSharpCodeParser.Statements.cs @@ -388,14 +388,14 @@ namespace Microsoft.AspNet.Razor.Parser Span.EditHandler.AcceptedCharacters = AcceptedCharacters.Any; // Accept whitespace but always keep the last whitespace node so we can put it back if necessary - var lastWs = AcceptWhiteSpaceInLines(); - Debug.Assert(lastWs == null || (lastWs.Start.AbsoluteIndex + lastWs.Content.Length == CurrentLocation.AbsoluteIndex)); + var lastWhitespace = AcceptWhiteSpaceInLines(); + Debug.Assert(lastWhitespace == null || (lastWhitespace.Start.AbsoluteIndex + lastWhitespace.Content.Length == CurrentLocation.AbsoluteIndex)); if (EndOfFile) { - if (lastWs != null) + if (lastWhitespace != null) { - Accept(lastWs); + Accept(lastWhitespace); } return; } @@ -411,16 +411,23 @@ namespace Microsoft.AspNet.Razor.Parser if (Context.DesignTimeMode || !isMarkup) { // CODE owns whitespace, MARKUP owns it ONLY in DesignTimeMode. - if (lastWs != null) + if (lastWhitespace != null) { - Accept(lastWs); + Accept(lastWhitespace); } } else { + var nextSymbol = Lookahead(1); + // MARKUP owns whitespace EXCEPT in DesignTimeMode. PutCurrentBack(); - PutBack(lastWs); + + // Don't putback the whitespace if it precedes a '' tag. + if (nextSymbol != null && !nextSymbol.Content.Equals(SyntaxConstants.TextTagName)) + { + PutBack(lastWhitespace); + } } if (isMarkup) diff --git a/test/Microsoft.AspNet.Razor.Test/Generator/CSharpRazorCodeGeneratorTest.cs b/test/Microsoft.AspNet.Razor.Test/Generator/CSharpRazorCodeGeneratorTest.cs index f6346c987a..f0be9641cd 100644 --- a/test/Microsoft.AspNet.Razor.Test/Generator/CSharpRazorCodeGeneratorTest.cs +++ b/test/Microsoft.AspNet.Razor.Test/Generator/CSharpRazorCodeGeneratorTest.cs @@ -66,6 +66,7 @@ namespace Microsoft.AspNet.Razor.Test.Generator [InlineData("ConditionalAttributes")] [InlineData("ResolveUrl")] [InlineData("Await")] + [InlineData("CodeBlockWithTextElement")] public void CSharpCodeGeneratorCorrectlyGeneratesRunTimeCode(string testType) { RunTest(testType); diff --git a/test/Microsoft.AspNet.Razor.Test/Generator/RazorCodeGeneratorTest.cs b/test/Microsoft.AspNet.Razor.Test/Generator/RazorCodeGeneratorTest.cs index 506c87ec9f..8701f1494e 100644 --- a/test/Microsoft.AspNet.Razor.Test/Generator/RazorCodeGeneratorTest.cs +++ b/test/Microsoft.AspNet.Razor.Test/Generator/RazorCodeGeneratorTest.cs @@ -186,7 +186,7 @@ namespace Microsoft.AspNet.Razor.Test.Generator onResults(results); } - //// Verify code against baseline + // Verify code against baseline Assert.Equal(expectedOutput, textOutput); #endif diff --git a/test/Microsoft.AspNet.Razor.Test/Parser/CSharp/CSharpRazorCommentsTest.cs b/test/Microsoft.AspNet.Razor.Test/Parser/CSharp/CSharpRazorCommentsTest.cs index 4da7ce51e6..b89b61c3dc 100644 --- a/test/Microsoft.AspNet.Razor.Test/Parser/CSharp/CSharpRazorCommentsTest.cs +++ b/test/Microsoft.AspNet.Razor.Test/Parser/CSharp/CSharpRazorCommentsTest.cs @@ -126,7 +126,6 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp .AsStatement() .AutoCompleteWith("}"), new MarkupBlock( - Factory.Markup(" "), new MarkupTagBlock( Factory.MarkupTransition(" 0) {").AsStatement(), new MarkupBlock( - Factory.Markup(" "), new MarkupTagBlock( Factory.MarkupTransition("").Accepts(AcceptedCharacters.None)), Factory.Markup(";").Accepts(AcceptedCharacters.None), @@ -599,7 +598,6 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp .AsStatement() .AutoCompleteWith(autoCompleteString: null), new MarkupBlock( - Factory.Markup(" "), new MarkupTagBlock( Factory.MarkupTransition("").Accepts(AcceptedCharacters.None)), Factory.Markup(";").Accepts(AcceptedCharacters.None), @@ -639,7 +637,6 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp ), Factory.Code($" }}{Environment.NewLine} foreach (var p in Enumerable.Range(1, 10)) {{{Environment.NewLine}").AsStatement(), new MarkupBlock( - Factory.Markup(" "), new MarkupTagBlock( Factory.MarkupTransition("").Accepts(AcceptedCharacters.None)), Factory.Markup("The number is ").Accepts(AcceptedCharacters.None), diff --git a/test/Microsoft.AspNet.Razor.Test/TagHelpers/TagHelperParseTreeRewriterTest.cs b/test/Microsoft.AspNet.Razor.Test/TagHelpers/TagHelperParseTreeRewriterTest.cs index de2254478f..adafe042d6 100644 --- a/test/Microsoft.AspNet.Razor.Test/TagHelpers/TagHelperParseTreeRewriterTest.cs +++ b/test/Microsoft.AspNet.Razor.Test/TagHelpers/TagHelperParseTreeRewriterTest.cs @@ -4386,7 +4386,6 @@ namespace Microsoft.AspNet.Razor.Test.TagHelpers factory.CodeTransition(), factory.Code("do { var foo = bar;").AsStatement(), new MarkupBlock( - factory.Markup(" "), new MarkupTagBlock( factory.MarkupTransition("")), factory.Markup("Foo").Accepts(AcceptedCharacters.None), diff --git a/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/CodeBlockWithTextElement.cs b/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/CodeBlockWithTextElement.cs new file mode 100644 index 0000000000..b52e32211f --- /dev/null +++ b/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/CodeBlockWithTextElement.cs @@ -0,0 +1,61 @@ +#pragma checksum "CodeBlockWithTextElement.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "740b5af27dd6c6ff0e88b39a02d4bf1a38fcdc0b" +namespace TestOutput +{ + using System; + using System.Threading.Tasks; + + public class CodeBlockWithTextElement + { + #line hidden + public CodeBlockWithTextElement() + { + } + + #pragma warning disable 1998 + public override async Task ExecuteAsync() + { +#line 1 "CodeBlockWithTextElement.cshtml" + + var a = 1; + +#line default +#line hidden + + Instrumentation.BeginContext(26, 1, false); +#line 2 "CodeBlockWithTextElement.cshtml" + Write(a); + +#line default +#line hidden + Instrumentation.EndContext(); + Instrumentation.BeginContext(34, 2, true); + WriteLiteral("\r\n"); + Instrumentation.EndContext(); +#line 3 "CodeBlockWithTextElement.cshtml" + var b = 1; + +#line default +#line hidden + + Instrumentation.BeginContext(60, 1, false); +#line 3 "CodeBlockWithTextElement.cshtml" + Write(b); + +#line default +#line hidden + Instrumentation.EndContext(); + Instrumentation.BeginContext(68, 2, true); + WriteLiteral("\r\n"); + Instrumentation.EndContext(); +#line 4 "CodeBlockWithTextElement.cshtml" + +#line default +#line hidden + + Instrumentation.BeginContext(71, 2, true); + WriteLiteral("\r\n"); + Instrumentation.EndContext(); + } + #pragma warning restore 1998 + } +} diff --git a/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/ComplexTagHelpers.cs b/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/ComplexTagHelpers.cs index 461c6fb808..d6aa418c40 100644 --- a/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/ComplexTagHelpers.cs +++ b/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/ComplexTagHelpers.cs @@ -160,14 +160,14 @@ if(true) { #line default #line hidden - WriteLiteral(" checkbox "); + WriteLiteral("checkbox "); #line 18 "ComplexTagHelpers.cshtml" } else { #line default #line hidden - WriteLiteral(" anything "); + WriteLiteral("anything "); #line 18 "ComplexTagHelpers.cshtml" } diff --git a/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/InlineBlocks.cs b/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/InlineBlocks.cs index f56f0a9994..addae5b033 100644 --- a/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/InlineBlocks.cs +++ b/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/InlineBlocks.cs @@ -41,11 +41,11 @@ WriteTo(__razor_attribute_value_writer, link); #line default #line hidden - Instrumentation.BeginContext(77, 3, true); - WriteLiteralTo(__razor_attribute_value_writer, " # "); + Instrumentation.BeginContext(84, 2, true); + WriteLiteralTo(__razor_attribute_value_writer, "# "); Instrumentation.EndContext(); #line 2 "InlineBlocks.cshtml" - } + } #line default #line hidden diff --git a/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/ResolveUrl.cs b/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/ResolveUrl.cs index 320645bfcc..db852f50d0 100644 --- a/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/ResolveUrl.cs +++ b/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Output/ResolveUrl.cs @@ -52,8 +52,8 @@ namespace TestOutput #line default #line hidden - Instrumentation.BeginContext(211, 16, true); - WriteLiteral(" \r\n (Href("~/Foo"), 240), false)); Instrumentation.BeginContext(246, 20, true); diff --git a/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Source/CodeBlockWithTextElement.cshtml b/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Source/CodeBlockWithTextElement.cshtml new file mode 100644 index 0000000000..c85dc419a1 --- /dev/null +++ b/test/Microsoft.AspNet.Razor.Test/TestFiles/CodeGenerator/CS/Source/CodeBlockWithTextElement.cshtml @@ -0,0 +1,4 @@ +@{ + var a = 1; @a + var b = 1; @b +}