[Fixes #379] Ignoring whitespace at the end of text tag

This commit is contained in:
Ajay Bhargav Baaskaran 2015-05-18 15:42:49 -07:00
parent 58c0a36200
commit 47577fd936
12 changed files with 108 additions and 47 deletions

View File

@ -1039,7 +1039,7 @@ namespace Microsoft.AspNet.Razor.Parser
{ {
tags.Pop(); tags.Pop();
} }
Tuple<HtmlSymbol, SourceLocation> tag = tags.Pop(); var tag = tags.Pop();
Context.OnError(tag.Item2, RazorResources.FormatParseError_MissingEndTag(tag.Item1.Content)); Context.OnError(tag.Item2, RazorResources.FormatParseError_MissingEndTag(tag.Item1.Content));
} }
else if (complete) else if (complete)
@ -1049,7 +1049,21 @@ namespace Microsoft.AspNet.Razor.Parser
tags.Clear(); tags.Clear();
if (!Context.DesignTimeMode) if (!Context.DesignTimeMode)
{ {
AcceptWhile(HtmlSymbolType.WhiteSpace); if (Context.LastSpan.Kind == SpanKind.Transition)
{
// Output current span content as markup.
Output(SpanKind.Markup);
// Accept and mark the whitespace at the end of a <text> tag as code.
AcceptWhile(HtmlSymbolType.WhiteSpace);
Span.CodeGenerator = new StatementCodeGenerator();
Output(SpanKind.Code);
}
else
{
AcceptWhile(HtmlSymbolType.WhiteSpace);
}
if (!EndOfFile && CurrentSymbol.Type == HtmlSymbolType.NewLine) if (!EndOfFile && CurrentSymbol.Type == HtmlSymbolType.NewLine)
{ {
AcceptAndMoveNext(); AcceptAndMoveNext();

View File

@ -381,25 +381,27 @@ namespace Microsoft.AspNet.Razor.Test.Generator
BuildLineMapping(463, 15, 2127, 79, 63, 4), BuildLineMapping(463, 15, 2127, 79, 63, 4),
BuildLineMapping(507, 16, 31, 2334, 86, 6, 30), BuildLineMapping(507, 16, 31, 2334, 86, 6, 30),
BuildLineMapping(574, 17, 30, 2683, 95, 0, 10), BuildLineMapping(574, 17, 30, 2683, 95, 0, 10),
BuildLineMapping(607, 17, 63, 2765, 101, 0, 8), BuildLineMapping(606, 17, 62, 2765, 101, 0, 1),
BuildLineMapping(638, 17, 94, 2845, 107, 0, 1), BuildLineMapping(607, 17, 63, 2838, 107, 0, 8),
BuildLineMapping(645, 18, 3099, 116, 0, 15), BuildLineMapping(637, 17, 93, 2918, 113, 0, 1),
BuildLineMapping(163, 7, 32, 3248, 123, 6, 12), BuildLineMapping(638, 17, 94, 2991, 119, 0, 1),
BuildLineMapping(725, 21, 3331, 128, 0, 12), BuildLineMapping(645, 18, 0, 3245, 128, 0, 15),
BuildLineMapping(739, 21, 3429, 134, 14, 21), BuildLineMapping(163, 7, 32, 3394, 135, 6, 12),
BuildLineMapping(793, 22, 30, 3686, 142, 28, 7), BuildLineMapping(725, 21, 0, 3477, 140, 0, 12),
BuildLineMapping(691, 20, 17, 3842, 148, 19, 23), BuildLineMapping(739, 21, 14, 3575, 146, 14, 21),
BuildLineMapping(714, 20, 40, 3865, 148, 42, 7), BuildLineMapping(793, 22, 30, 3832, 154, 28, 7),
BuildLineMapping(903, 25, 30, 4107, 155, 28, 30), BuildLineMapping(691, 20, 17, 3988, 160, 19, 23),
BuildLineMapping(837, 24, 16, 4286, 161, 19, 8), BuildLineMapping(714, 20, 40, 4011, 160, 42, 7),
BuildLineMapping(846, 24, 25, 4294, 161, 27, 23), BuildLineMapping(903, 25, 30, 4253, 167, 28, 30),
BuildLineMapping(1032, 28, 4552, 168, 28, 30), BuildLineMapping(837, 24, 16, 4432, 173, 19, 8),
BuildLineMapping(970, 27, 16, 4731, 174, 19, 30), BuildLineMapping(846, 24, 25, 4440, 173, 27, 23),
BuildLineMapping(1162, 31, 4996, 181, 28, 3), BuildLineMapping(1032, 28, 28, 4698, 180, 28, 30),
BuildLineMapping(1167, 31, 33, 4999, 181, 31, 27), BuildLineMapping(970, 27, 16, 4877, 186, 19, 30),
BuildLineMapping(1195, 31, 61, 5026, 181, 58, 10), BuildLineMapping(1162, 31, 28, 5142, 193, 28, 3),
BuildLineMapping(1100, 30, 18, 5185, 187, 19, 29), BuildLineMapping(1167, 31, 33, 5145, 193, 31, 27),
BuildLineMapping(1237, 34, 5285, 192, 0, 1), BuildLineMapping(1195, 31, 61, 5172, 193, 58, 10),
BuildLineMapping(1100, 30, 18, 5331, 199, 19, 29),
BuildLineMapping(1237, 34, 0, 5431, 204, 0, 1),
} }
}, },
{ {

View File

@ -190,7 +190,7 @@ namespace Microsoft.AspNet.Razor.Test.Generator
Assert.Equal(expectedOutput, textOutput); Assert.Equal(expectedOutput, textOutput);
#endif #endif
IEnumerable<Span> generatedSpans = results.Document.Flatten(); var generatedSpans = results.Document.Flatten();
foreach (var span in generatedSpans) foreach (var span in generatedSpans)
{ {

View File

@ -3,6 +3,7 @@
using System; using System;
using Microsoft.AspNet.Razor.Editor; using Microsoft.AspNet.Razor.Editor;
using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Parser; using Microsoft.AspNet.Razor.Parser;
using Microsoft.AspNet.Razor.Parser.SyntaxTree; using Microsoft.AspNet.Razor.Parser.SyntaxTree;
using Microsoft.AspNet.Razor.Test.Framework; using Microsoft.AspNet.Razor.Test.Framework;
@ -583,7 +584,7 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp
Factory.Markup(";").Accepts(AcceptedCharacters.None), Factory.Markup(";").Accepts(AcceptedCharacters.None),
new MarkupTagBlock( new MarkupTagBlock(
Factory.MarkupTransition("</text>").Accepts(AcceptedCharacters.None)), Factory.MarkupTransition("</text>").Accepts(AcceptedCharacters.None)),
Factory.Markup(" ").Accepts(AcceptedCharacters.None) Factory.CodeMarkup(" ").With(new StatementCodeGenerator()).Accepts(AcceptedCharacters.None)
), ),
Factory.Code("}").AsStatement())); Factory.Code("}").AsStatement()));
} }
@ -603,7 +604,7 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp
Factory.Markup(";").Accepts(AcceptedCharacters.None), Factory.Markup(";").Accepts(AcceptedCharacters.None),
new MarkupTagBlock( new MarkupTagBlock(
Factory.MarkupTransition("</text>").Accepts(AcceptedCharacters.None)), Factory.MarkupTransition("</text>").Accepts(AcceptedCharacters.None)),
Factory.Markup(" ").Accepts(AcceptedCharacters.None) Factory.CodeMarkup(" ").With(new StatementCodeGenerator()).Accepts(AcceptedCharacters.None)
), ),
Factory.Code("} ").AsStatement(), Factory.Code("} ").AsStatement(),
Factory.MetaCode("}").Accepts(AcceptedCharacters.None))); Factory.MetaCode("}").Accepts(AcceptedCharacters.None)));
@ -646,7 +647,7 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp
), ),
new MarkupTagBlock( new MarkupTagBlock(
Factory.MarkupTransition("</text>").Accepts(AcceptedCharacters.None)), Factory.MarkupTransition("</text>").Accepts(AcceptedCharacters.None)),
Factory.Markup(Environment.NewLine).Accepts(AcceptedCharacters.None) Factory.Markup(Environment.NewLine).With(new StatementCodeGenerator()).Accepts(AcceptedCharacters.None)
), ),
Factory.Code($" }}{Environment.NewLine} if(!false) {{{Environment.NewLine}").AsStatement(), Factory.Code($" }}{Environment.NewLine} if(!false) {{{Environment.NewLine}").AsStatement(),
new MarkupBlock( new MarkupBlock(

View File

@ -495,7 +495,7 @@ namespace Microsoft.AspNet.Razor.Test.Parser.Html
Factory.Markup(" Baz"), Factory.Markup(" Baz"),
new MarkupTagBlock( new MarkupTagBlock(
Factory.MarkupTransition("</text>")), Factory.MarkupTransition("</text>")),
Factory.Markup(" ").Accepts(AcceptedCharacters.None) Factory.CodeMarkup(" ").With(new StatementCodeGenerator()).Accepts(AcceptedCharacters.None)
)); ));
} }
@ -516,7 +516,7 @@ namespace Microsoft.AspNet.Razor.Test.Parser.Html
Factory.Markup("</text>").Accepts(AcceptedCharacters.None)), Factory.Markup("</text>").Accepts(AcceptedCharacters.None)),
new MarkupTagBlock( new MarkupTagBlock(
Factory.MarkupTransition("</text>")), Factory.MarkupTransition("</text>")),
Factory.Markup(" ").Accepts(AcceptedCharacters.None) Factory.CodeMarkup(" ").With(new StatementCodeGenerator()).Accepts(AcceptedCharacters.None)
)); ));
} }

View File

@ -4496,7 +4496,7 @@ namespace Microsoft.AspNet.Razor.Test.TagHelpers
factory.Markup("Foo").Accepts(AcceptedCharacters.None), factory.Markup("Foo").Accepts(AcceptedCharacters.None),
new MarkupTagBlock( new MarkupTagBlock(
factory.MarkupTransition("</text>")), factory.MarkupTransition("</text>")),
factory.Markup(" ").Accepts(AcceptedCharacters.None)), factory.CodeMarkup(" ").With(new StatementCodeGenerator()).Accepts(AcceptedCharacters.None)),
factory.Code("foo++; } while (foo<bar>);").AsStatement().Accepts(AcceptedCharacters.None))); factory.Code("foo++; } while (foo<bar>);").AsStatement().Accepts(AcceptedCharacters.None)));
var currentFormattedString = "<p class=\"{0}\" style='{0}'></p>"; var currentFormattedString = "<p class=\"{0}\" style='{0}'></p>";

View File

@ -1,4 +1,4 @@
#pragma checksum "CodeBlockWithTextElement.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "740b5af27dd6c6ff0e88b39a02d4bf1a38fcdc0b" #pragma checksum "CodeBlockWithTextElement.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "13e48ff59aab8106ceb68dd4a10b0bdf10c322fc"
namespace TestOutput namespace TestOutput
{ {
using System; using System;
@ -21,14 +21,16 @@ namespace TestOutput
#line default #line default
#line hidden #line hidden
Instrumentation.BeginContext(26, 1, false); Instrumentation.BeginContext(25, 3, true);
WriteLiteral("foo");
Instrumentation.EndContext();
#line 2 "CodeBlockWithTextElement.cshtml" #line 2 "CodeBlockWithTextElement.cshtml"
Write(a);
#line default #line default
#line hidden #line hidden
Instrumentation.EndContext();
Instrumentation.BeginContext(34, 2, true); Instrumentation.BeginContext(38, 2, true);
WriteLiteral("\r\n"); WriteLiteral("\r\n");
Instrumentation.EndContext(); Instrumentation.EndContext();
#line 3 "CodeBlockWithTextElement.cshtml" #line 3 "CodeBlockWithTextElement.cshtml"
@ -37,22 +39,28 @@ namespace TestOutput
#line default #line default
#line hidden #line hidden
Instrumentation.BeginContext(60, 1, false); Instrumentation.BeginContext(63, 4, true);
WriteLiteral("bar ");
Instrumentation.EndContext();
Instrumentation.BeginContext(69, 3, false);
#line 3 "CodeBlockWithTextElement.cshtml" #line 3 "CodeBlockWithTextElement.cshtml"
Write(b); Write(a+b);
#line default #line default
#line hidden #line hidden
Instrumentation.EndContext(); Instrumentation.EndContext();
Instrumentation.BeginContext(68, 2, true); #line 3 "CodeBlockWithTextElement.cshtml"
WriteLiteral("\r\n");
Instrumentation.EndContext();
#line default
#line hidden
#line 4 "CodeBlockWithTextElement.cshtml" #line 4 "CodeBlockWithTextElement.cshtml"
#line default #line default
#line hidden #line hidden
Instrumentation.BeginContext(71, 2, true); Instrumentation.BeginContext(83, 2, true);
WriteLiteral("\r\n"); WriteLiteral("\r\n");
Instrumentation.EndContext(); Instrumentation.EndContext();
} }

View File

@ -95,12 +95,24 @@ __o = true ? "checkbox" : "anything";
#line 18 "ComplexTagHelpers.cshtml" #line 18 "ComplexTagHelpers.cshtml"
if(true) { if(true) {
#line default
#line hidden
#line 18 "ComplexTagHelpers.cshtml"
#line default #line default
#line hidden #line hidden
#line 18 "ComplexTagHelpers.cshtml" #line 18 "ComplexTagHelpers.cshtml"
} else { } else {
#line default
#line hidden
#line 18 "ComplexTagHelpers.cshtml"
#line default #line default
#line hidden #line hidden

View File

@ -160,14 +160,26 @@ if(true) {
#line default #line default
#line hidden #line hidden
WriteLiteral("checkbox "); WriteLiteral("checkbox");
#line 18 "ComplexTagHelpers.cshtml"
#line default
#line hidden
#line 18 "ComplexTagHelpers.cshtml" #line 18 "ComplexTagHelpers.cshtml"
} else { } else {
#line default #line default
#line hidden #line hidden
WriteLiteral("anything "); WriteLiteral("anything");
#line 18 "ComplexTagHelpers.cshtml"
#line default
#line hidden
#line 18 "ComplexTagHelpers.cshtml" #line 18 "ComplexTagHelpers.cshtml"
} }

View File

@ -41,9 +41,15 @@ WriteTo(__razor_attribute_value_writer, link);
#line default #line default
#line hidden #line hidden
Instrumentation.BeginContext(84, 2, true); Instrumentation.BeginContext(84, 1, true);
WriteLiteralTo(__razor_attribute_value_writer, "# "); WriteLiteralTo(__razor_attribute_value_writer, "#");
Instrumentation.EndContext(); Instrumentation.EndContext();
#line 2 "InlineBlocks.cshtml"
#line default
#line hidden
#line 2 "InlineBlocks.cshtml" #line 2 "InlineBlocks.cshtml"
} }

View File

@ -81,9 +81,15 @@ namespace TestOutput
Instrumentation.EndContext(); Instrumentation.EndContext();
WriteAttribute("href", Tuple.Create(" href=\"", 387), Tuple.Create("\"", 442), Tuple.Create(Tuple.Create("", 394), Tuple.Create<System.Object, System.Int32>(Href("~/A+Really(Crazy),Url.Is:This/"), 394), false), WriteAttribute("href", Tuple.Create(" href=\"", 387), Tuple.Create("\"", 442), Tuple.Create(Tuple.Create("", 394), Tuple.Create<System.Object, System.Int32>(Href("~/A+Really(Crazy),Url.Is:This/"), 394), false),
Tuple.Create(Tuple.Create("", 424), Tuple.Create<System.Object, System.Int32>(product.id, 424), false), Tuple.Create(Tuple.Create("", 435), Tuple.Create("/Detail", 435), true)); Tuple.Create(Tuple.Create("", 424), Tuple.Create<System.Object, System.Int32>(product.id, 424), false), Tuple.Create(Tuple.Create("", 435), Tuple.Create("/Detail", 435), true));
Instrumentation.BeginContext(443, 23, true); Instrumentation.BeginContext(443, 21, true);
WriteLiteral(">Crazy Url!</a>\r\n \r\n"); WriteLiteral(">Crazy Url!</a>\r\n ");
Instrumentation.EndContext(); Instrumentation.EndContext();
#line 12 "ResolveUrl.cshtml"
#line default
#line hidden
#line 13 "ResolveUrl.cshtml" #line 13 "ResolveUrl.cshtml"
#line default #line default

View File

@ -1,4 +1,4 @@
@{ @{
var a = 1; <text>@a</text> var a = 1; <text>foo</text>
var b = 1; <text>@b</text> var b = 1; <text>bar @(a+b)</text>
} }