[Fixes #50] Removed unnecessary whitespaces before text tag

This commit is contained in:
Ajay Bhargav Baaskaran 2015-05-06 15:45:26 -07:00
parent c680d6b953
commit 4d80c96f9c
11 changed files with 88 additions and 20 deletions

View File

@ -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 '<text>' tag.
if (nextSymbol != null && !nextSymbol.Content.Equals(SyntaxConstants.TextTagName))
{
PutBack(lastWhitespace);
}
}
if (isMarkup)

View File

@ -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);

View File

@ -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

View File

@ -126,7 +126,6 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp
.AsStatement()
.AutoCompleteWith("}"),
new MarkupBlock(
Factory.Markup(" "),
new MarkupTagBlock(
Factory.MarkupTransition("<text").Accepts(AcceptedCharacters.Any)),
Factory.Markup(Environment.NewLine + " ").Accepts(AcceptedCharacters.None),

View File

@ -578,7 +578,6 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp
new StatementBlock(
Factory.Code("if (i > 0) {").AsStatement(),
new MarkupBlock(
Factory.Markup(" "),
new MarkupTagBlock(
Factory.MarkupTransition("<text>").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("<text>").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("<text>").Accepts(AcceptedCharacters.None)),
Factory.Markup("The number is ").Accepts(AcceptedCharacters.None),

View File

@ -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("<text>")),
factory.Markup("Foo").Accepts(AcceptedCharacters.None),

View File

@ -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
}
}

View File

@ -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"
}

View File

@ -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

View File

@ -52,8 +52,8 @@ namespace TestOutput
#line default
#line hidden
Instrumentation.BeginContext(211, 16, true);
WriteLiteral(" \r\n <a");
Instrumentation.BeginContext(221, 12, true);
WriteLiteral("\r\n <a");
Instrumentation.EndContext();
WriteAttribute("href", Tuple.Create(" href=\"", 233), Tuple.Create("\"", 245), Tuple.Create(Tuple.Create("", 240), Tuple.Create<System.Object, System.Int32>(Href("~/Foo"), 240), false));
Instrumentation.BeginContext(246, 20, true);

View File

@ -0,0 +1,4 @@
@{
var a = 1; <text>@a</text>
var b = 1; <text>@b</text>
}