HtmlMarkupParser is now aware of HtmlComment blocks. Majority (if not all, will confirm soon) tests have been updated to account for HtmlComment blocks.

This commit is contained in:
Artak Mkrtchyan 2018-02-20 18:24:27 -08:00
parent 696a65c780
commit f42754371e
10 changed files with 144 additions and 56 deletions

View File

@ -207,10 +207,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
if (last != null)
{
Accept(last);
if (At(HtmlSymbolType.OpenAngle) && last.Type == HtmlSymbolType.Text)
{
Output(SpanKindInternal.Markup);
}
}
}
@ -501,31 +497,57 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
using (Context.Builder.StartBlock(BlockKindInternal.HtmlComment))
{
AcceptAndMoveNext();
Output(SpanKindInternal.Markup, AcceptedCharactersInternal.None);
Span.EditHandler.AcceptedCharacters = AcceptedCharactersInternal.Any;
Span.EditHandler.AcceptedCharacters = AcceptedCharactersInternal.WhiteSpace;
while (!EndOfFile)
{
SkipToAndParseCode(HtmlSymbolType.DoubleHyphen);
if (At(HtmlSymbolType.DoubleHyphen))
{
AcceptWhile(HtmlSymbolType.DoubleHyphen);
var lastDoubleHyphen = CurrentSymbol;
AcceptWhile(s =>
{
if (NextIs(HtmlSymbolType.DoubleHyphen))
{
lastDoubleHyphen = s;
return true;
}
NextToken();
EnsureCurrent();
return false;
});
if (At(HtmlSymbolType.Text) &&
string.Equals(CurrentSymbol.Content, "-", StringComparison.Ordinal))
{
// Doing this here to maintain the order of symbols
if (!NextIs(HtmlSymbolType.CloseAngle))
{
Accept(lastDoubleHyphen);
lastDoubleHyphen = null;
}
AcceptAndMoveNext();
}
if (At(HtmlSymbolType.CloseAngle))
{
// This is the end of a comment block
Accept(this.CurrentSymbol);
Output(SpanKindInternal.Markup);
// Output the content in the comment block as a separate markup
Output(SpanKindInternal.Markup, AcceptedCharactersInternal.WhiteSpace);
// This is the end of a comment block
Accept(lastDoubleHyphen);
AcceptAndMoveNext();
Output(SpanKindInternal.Markup, AcceptedCharactersInternal.None);
NextToken();
//AcceptAndMoveNext();
return true;
}
else if (lastDoubleHyphen != null)
{
Accept(lastDoubleHyphen);
}
}
}
}
@ -1486,6 +1508,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
// Checking to see if we meet the conditions of a special '!' tag: <!DOCTYPE, <![CDATA[, <!--.
if (!IsBangEscape(lookahead: 1))
{
if (Lookahead(2)?.Type == HtmlSymbolType.DoubleHyphen)
{
Output(SpanKindInternal.Markup);
}
AcceptAndMoveNext(); // Accept '<'
BangTag();

View File

@ -367,7 +367,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
{
// Act & Assert
ParseDocumentTest(
"@section foo "
"@section foo "
+ Environment.NewLine
+ Environment.NewLine
+ Environment.NewLine
@ -606,7 +606,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
Factory.Span(SpanKindInternal.Markup, " ", CSharpSymbolType.WhiteSpace).Accepts(AcceptedCharactersInternal.AllWhiteSpace),
Factory.MetaCode("{").AutoCompleteWith(null, atEndOfSpan: true).Accepts(AcceptedCharactersInternal.None),
new MarkupBlock(
Factory.Markup("<!-- -->")),
BlockFactory.HtmlCommentBlock(" "),
Factory.EmptyHtml()),
Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None)),
Factory.EmptyHtml()));
}
@ -630,7 +631,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
Factory.Span(SpanKindInternal.Markup, " ", CSharpSymbolType.WhiteSpace).Accepts(AcceptedCharactersInternal.AllWhiteSpace),
Factory.MetaCode("{").AutoCompleteWith(null, atEndOfSpan: true).Accepts(AcceptedCharactersInternal.None),
new MarkupBlock(
Factory.Markup("<!-- > \" '-->")),
BlockFactory.HtmlCommentBlock(" > \" '"),
Factory.EmptyHtml()),
Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None)),
Factory.EmptyHtml()));
}
@ -655,7 +657,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
Factory.Markup(Environment.NewLine),
new MarkupTagBlock(
Factory.Markup("<a" + Environment.NewLine)),
Factory.Markup("<!-- > \" '-->")),
BlockFactory.HtmlCommentBlock(" > \" '"),
Factory.EmptyHtml()),
Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None)),
Factory.EmptyHtml()));
}

View File

@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
Factory.Code(Environment.NewLine).AsStatement().AutoCompleteWith(null),
new MarkupBlock(
Factory.Markup(" "),
Factory.Markup("<!-- Hello, I'm a comment that shouldn't break razor --->").Accepts(AcceptedCharactersInternal.None),
BlockFactory.HtmlCommentBlock(" Hello, I'm a comment that shouldn't break razor -"),
Factory.Markup(Environment.NewLine).Accepts(AcceptedCharactersInternal.None)),
Factory.EmptyCSharp().AsStatement(),
Factory.MetaCode("}").Accepts(AcceptedCharactersInternal.None)),
@ -333,7 +333,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
[Fact]
public void ParseBlockSupportsCommentAsBlock()
{
SingleSpanBlockTest("<!-- foo -->", BlockKindInternal.Markup, SpanKindInternal.Markup, acceptedCharacters: AcceptedCharactersInternal.None);
ParseBlockTest("<!-- foo -->", new MarkupBlock( BlockFactory.HtmlCommentBlock(" foo ")));
}
[Fact]
@ -344,8 +344,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new MarkupTagBlock(
Factory.Markup("<foo>").Accepts(AcceptedCharactersInternal.None)),
Factory.Markup("bar"),
Factory.Markup("<!-- zoop -->").Accepts(AcceptedCharactersInternal.None),
Factory.Markup("baz"),
BlockFactory.HtmlCommentBlock(" zoop "),
Factory.Markup("baz").Accepts(AcceptedCharactersInternal.None),
new MarkupTagBlock(
Factory.Markup("</foo>").Accepts(AcceptedCharactersInternal.None))));
}
@ -355,7 +355,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
get
{
var factory = new SpanFactory();
var blockFactory = new BlockFactory(factory);
return new TheoryData<string, MarkupBlock>
{
{
@ -363,7 +363,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new MarkupBlock(
new MarkupTagBlock(
factory.Markup("<div>").Accepts(AcceptedCharactersInternal.None)),
factory.Markup("<!--- Hello World --->").Accepts(AcceptedCharactersInternal.None),
blockFactory.HtmlCommentBlock("- Hello World -"),
new MarkupTagBlock(
factory.Markup("</div>").Accepts(AcceptedCharactersInternal.None)))
},
@ -372,7 +372,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new MarkupBlock(
new MarkupTagBlock(
factory.Markup("<div>").Accepts(AcceptedCharactersInternal.None)),
factory.Markup("<!---- Hello World ---->").Accepts(AcceptedCharactersInternal.None),
blockFactory.HtmlCommentBlock("-- Hello World --"),
new MarkupTagBlock(
factory.Markup("</div>").Accepts(AcceptedCharactersInternal.None)))
},
@ -381,7 +381,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new MarkupBlock(
new MarkupTagBlock(
factory.Markup("<div>").Accepts(AcceptedCharactersInternal.None)),
factory.Markup("<!----- Hello World ----->").Accepts(AcceptedCharactersInternal.None),
blockFactory.HtmlCommentBlock("--- Hello World ---"),
new MarkupTagBlock(
factory.Markup("</div>").Accepts(AcceptedCharactersInternal.None)))
},
@ -390,7 +390,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new MarkupBlock(
new MarkupTagBlock(
factory.Markup("<div>").Accepts(AcceptedCharactersInternal.None)),
factory.Markup("<!----- Hello < --- > World </div> ----->").Accepts(AcceptedCharactersInternal.None),
blockFactory.HtmlCommentBlock("--- Hello < --- > World </div> ---"),
new MarkupTagBlock(
factory.Markup("</div>").Accepts(AcceptedCharactersInternal.None)))
},
@ -410,19 +410,24 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
[Fact]
public void ParseBlockProperlyBalancesCommentStartAndEndTags()
{
SingleSpanBlockTest("<!--<foo></bar>-->", BlockKindInternal.Markup, SpanKindInternal.Markup, acceptedCharacters: AcceptedCharactersInternal.None);
ParseBlockTest("<!--<foo></bar>-->", new MarkupBlock(BlockFactory.HtmlCommentBlock("<foo></bar>")));
}
[Fact]
public void ParseBlockTerminatesAtEOFWhenParsingComment()
{
SingleSpanBlockTest("<!--<foo>", "<!--<foo>", BlockKindInternal.Markup, SpanKindInternal.Markup);
ParseBlockTest(
"<!--<foo>",
new MarkupBlock(
new HtmlCommentBlock(Factory.Markup("<!--").Accepts(AcceptedCharactersInternal.None)),
Factory.Markup("<foo>").Accepts(AcceptedCharactersInternal.WhiteSpace),
Factory.EmptyHtml()));
}
[Fact]
public void ParseBlockOnlyTerminatesCommentOnFullEndSequence()
{
SingleSpanBlockTest("<!--<foo>--</bar>-->", BlockKindInternal.Markup, SpanKindInternal.Markup, acceptedCharacters: AcceptedCharactersInternal.None);
ParseBlockTest("<!--<foo>--</bar>-->", new MarkupBlock(BlockFactory.HtmlCommentBlock("<foo>--</bar>")));
}
[Fact]
@ -432,8 +437,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new MarkupBlock(
new MarkupTagBlock(
Factory.Markup("<foo>").Accepts(AcceptedCharactersInternal.None)),
Factory.Markup("<!--<foo></bar-->").Accepts(AcceptedCharactersInternal.None),
Factory.Markup("-->"),
BlockFactory.HtmlCommentBlock("<foo></bar"),
Factory.Markup("-->").Accepts(AcceptedCharactersInternal.None),
new MarkupTagBlock(
Factory.Markup("</foo>").Accepts(AcceptedCharactersInternal.None))));
}

View File

@ -1114,8 +1114,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
// Arrangestring documentContent,
IEnumerable<string> allowedChildren = new List<string> { "b" };
string literal = "asdf";
string commentOutput = "<!--Hello World-->";
string expectedOutput = $"<p><b>{literal}</b>{commentOutput}</p>";
string commentOutput = "Hello World";
string expectedOutput = $"<p><b>{literal}</b><!--{commentOutput}--></p>";
var pTagHelperBuilder = TagHelperDescriptorBuilder
.Create("PTagHelper", "SomeAssembly")
@ -1138,7 +1138,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
blockFactory.MarkupTagBlock("<b>"),
factory.Markup(literal),
blockFactory.MarkupTagBlock("</b>"),
new HtmlCommentBlock(factory.Markup(commentOutput))));
blockFactory.HtmlCommentBlock(commentOutput)));
// Act & Assert
EvaluateData(
@ -1185,9 +1185,9 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
var expectedMarkup = new MarkupBlock(
new MarkupTagHelperBlock("p",
new HtmlCommentBlock(factory.Markup($"<!--{comment1}-->")),
blockFactory.HtmlCommentBlock(comment1),
factory.Markup(literal),
new HtmlCommentBlock(factory.Markup($"<!--{comment2}-->"))));
blockFactory.HtmlCommentBlock(comment2)));
// Act & Assert
EvaluateData(
@ -1251,10 +1251,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
// Arrangestring documentContent,
IEnumerable<string> allowedChildren = new List<string> { "b" };
string literal = "asdf";
string part1 = "<!--Hello ";
string part1 = "Hello ";
string part2 = "World";
string part3 = "-->";
string expectedOutput = $"<p><b>{literal}</b>{part1}@{part2}{part3}</p>";
string commentStart = "<!--";
string commentEnd = "-->";
string expectedOutput = $"<p><b>{literal}</b>{commentStart}{part1}@{part2}{commentEnd}</p>";
var pTagHelperBuilder = TagHelperDescriptorBuilder
.Create("PTagHelper", "SomeAssembly")
@ -1277,13 +1278,13 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
blockFactory.MarkupTagBlock("<b>"),
factory.Markup(literal),
blockFactory.MarkupTagBlock("</b>"),
new HtmlCommentBlock(factory.Markup(part1),
new ExpressionBlock(
factory.CodeTransition(),
factory.Code(part2)
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
.Accepts(AcceptedCharactersInternal.NonWhiteSpace)),
factory.Markup(part3))));
blockFactory.HtmlCommentBlock(
factory.Markup(part1).Accepts(AcceptedCharactersInternal.WhiteSpace),
new ExpressionBlock(
factory.CodeTransition(),
factory.Code(part2)
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
.Accepts(AcceptedCharactersInternal.NonWhiteSpace)))));
// Act & Assert
EvaluateData(
@ -4086,14 +4087,14 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
get
{
var factory = new SpanFactory();
var blockFactory = new BlockFactory(factory);
yield return new object[]
{
"<foo><!-- Hello World --></foo>",
new MarkupBlock(
new MarkupTagBlock(
factory.Markup("<foo>")),
new HtmlCommentBlock( factory.Markup("<!-- Hello World -->")),
blockFactory.HtmlCommentBlock (" Hello World "),
new MarkupTagBlock(
factory.Markup("</foo>")))
};
@ -4103,13 +4104,14 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new MarkupBlock(
new MarkupTagBlock(
factory.Markup("<foo>")),
new HtmlCommentBlock(factory.Markup("<!-- "),
blockFactory.HtmlCommentBlock(
factory.Markup(" ").Accepts(AcceptedCharactersInternal.WhiteSpace),
new ExpressionBlock(
factory.CodeTransition(),
factory.Code("foo")
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
.Accepts(AcceptedCharactersInternal.NonWhiteSpace)),
factory.Markup(" -->")),
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
.Accepts(AcceptedCharactersInternal.NonWhiteSpace)),
factory.Markup(" ").Accepts(AcceptedCharactersInternal.WhiteSpace)),
new MarkupTagBlock(
factory.Markup("</foo>")))
};
@ -4185,8 +4187,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
new ExpressionBlock(
factory.CodeTransition(),
factory.Code("foo")
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
.Accepts(AcceptedCharactersInternal.NonWhiteSpace)),
.AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
.Accepts(AcceptedCharactersInternal.NonWhiteSpace)),
factory.Markup(" ]]>"),
new MarkupTagBlock(
factory.Markup("</foo>")))

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Razor.Language.Legacy;
using Xunit;
namespace Microsoft.AspNetCore.Razor.Language.Test
{
public class TagHelperParseTreeRewriterTests
{
public void IsComment_ReturnsTrueForSpanInHtmlCommentBlock()
{
// Arrange
SpanFactory spanFactory = new SpanFactory();
Span content = spanFactory.Markup("<!-- comment -->");
Block commentBlock = new HtmlCommentBlock(content);
// Act
bool actualResult = TagHelperParseTreeRewriter.IsComment(content);
// Assert
Assert.True(actualResult);
}
}
}

View File

@ -10,7 +10,8 @@ Document -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - public async - System.Threading.Tasks.Task - ExecuteAsync
HtmlContent - (0:0,0 [45] HtmlCommentWithQuote_Double.cshtml)
IntermediateToken - (0:0,0 [12] HtmlCommentWithQuote_Double.cshtml) - Html - <!-- " -->\n
IntermediateToken - (0:0,0 [10] HtmlCommentWithQuote_Double.cshtml) - Html - <!-- " -->
IntermediateToken - (10:0,10 [2] HtmlCommentWithQuote_Double.cshtml) - Html - \n
IntermediateToken - (12:1,0 [4] HtmlCommentWithQuote_Double.cshtml) - Html - <img
IntermediateToken - (16:1,4 [26] HtmlCommentWithQuote_Double.cshtml) - Html - src="~/images/submit.png"
IntermediateToken - (42:1,30 [3] HtmlCommentWithQuote_Double.cshtml) - Html - />

View File

@ -5,7 +5,8 @@ Document -
ClassDeclaration - - public - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_HtmlCommentWithQuote_Double_Runtime - -
MethodDeclaration - - public async - System.Threading.Tasks.Task - ExecuteAsync
HtmlContent - (0:0,0 [45] HtmlCommentWithQuote_Double.cshtml)
IntermediateToken - (0:0,0 [12] HtmlCommentWithQuote_Double.cshtml) - Html - <!-- " -->\n
IntermediateToken - (0:0,0 [10] HtmlCommentWithQuote_Double.cshtml) - Html - <!-- " -->
IntermediateToken - (10:0,10 [2] HtmlCommentWithQuote_Double.cshtml) - Html - \n
IntermediateToken - (12:1,0 [4] HtmlCommentWithQuote_Double.cshtml) - Html - <img
IntermediateToken - (16:1,4 [26] HtmlCommentWithQuote_Double.cshtml) - Html - src="~/images/submit.png"
IntermediateToken - (42:1,30 [3] HtmlCommentWithQuote_Double.cshtml) - Html - />

View File

@ -10,7 +10,8 @@ Document -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - public async - System.Threading.Tasks.Task - ExecuteAsync
HtmlContent - (0:0,0 [45] HtmlCommentWithQuote_Single.cshtml)
IntermediateToken - (0:0,0 [12] HtmlCommentWithQuote_Single.cshtml) - Html - <!-- ' -->\n
IntermediateToken - (0:0,0 [10] HtmlCommentWithQuote_Single.cshtml) - Html - <!-- ' -->
IntermediateToken - (10:0,10 [2] HtmlCommentWithQuote_Single.cshtml) - Html - \n
IntermediateToken - (12:1,0 [4] HtmlCommentWithQuote_Single.cshtml) - Html - <img
IntermediateToken - (16:1,4 [26] HtmlCommentWithQuote_Single.cshtml) - Html - src="~/images/submit.png"
IntermediateToken - (42:1,30 [3] HtmlCommentWithQuote_Single.cshtml) - Html - />

View File

@ -5,7 +5,8 @@ Document -
ClassDeclaration - - public - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_HtmlCommentWithQuote_Single_Runtime - -
MethodDeclaration - - public async - System.Threading.Tasks.Task - ExecuteAsync
HtmlContent - (0:0,0 [45] HtmlCommentWithQuote_Single.cshtml)
IntermediateToken - (0:0,0 [12] HtmlCommentWithQuote_Single.cshtml) - Html - <!-- ' -->\n
IntermediateToken - (0:0,0 [10] HtmlCommentWithQuote_Single.cshtml) - Html - <!-- ' -->
IntermediateToken - (10:0,10 [2] HtmlCommentWithQuote_Single.cshtml) - Html - \n
IntermediateToken - (12:1,0 [4] HtmlCommentWithQuote_Single.cshtml) - Html - <img
IntermediateToken - (16:1,4 [26] HtmlCommentWithQuote_Single.cshtml) - Html - src="~/images/submit.png"
IntermediateToken - (42:1,30 [3] HtmlCommentWithQuote_Single.cshtml) - Html - />

View File

@ -55,6 +55,27 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
);
}
public HtmlCommentBlock HtmlCommentBlock(string content)
{
return new HtmlCommentBlock(new SyntaxTreeNode[] {
_factory.Markup("<!--").Accepts(AcceptedCharactersInternal.None),
_factory.Markup(content).Accepts(AcceptedCharactersInternal.WhiteSpace),
_factory.Markup("-->").Accepts(AcceptedCharactersInternal.None) });
}
public HtmlCommentBlock HtmlCommentBlock(params SyntaxTreeNode[] syntaxTreeNodes)
{
var nodes = new List<SyntaxTreeNode>();
nodes.Add(_factory.Markup("<!--").Accepts(AcceptedCharactersInternal.None));
if (syntaxTreeNodes != null)
{
nodes.AddRange(syntaxTreeNodes);
}
nodes.Add(_factory.Markup("-->").Accepts(AcceptedCharactersInternal.None));
return new HtmlCommentBlock(nodes.ToArray());
}
public Block TagHelperBlock(
string tagName,
TagMode tagMode,