Added extra tests for non-covered scenarios:

- For older version of Razor the HTML comments will be complained about by TahHelperRewriter
- RazorParserFeatureFlags tests now ensure that AllowHtmlCommentsInTagHelpers is true in 2.1 version  and false in older versions
- Added extra test for IsHtmlCommentAhead to make sure Razor code transition is allowed in comment tag
- Moved the unallowed html comment ending to a static array.
This commit is contained in:
Artak Mkrtchyan 2018-03-09 17:18:57 -08:00
parent e8eb3b7b1c
commit 3e22c16930
No known key found for this signature in database
GPG Key ID: 64D580ACBA8CA645
4 changed files with 71 additions and 7 deletions

View File

@ -12,6 +12,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
{
private const string ScriptTagName = "script";
private static readonly HtmlSymbol[] nonAllowedHtmlCommentEnding = new[] { new HtmlSymbol("-", HtmlSymbolType.Text), new HtmlSymbol("!", HtmlSymbolType.Bang), new HtmlSymbol("<", HtmlSymbolType.OpenAngle) };
private static readonly char[] ValidAfterTypeAttributeNameCharacters = { ' ', '\t', '\r', '\n', '\f', '=' };
private SourceLocation _lastTagStart = SourceLocation.Zero;
private HtmlSymbol _bufferedOpenAngle;
@ -649,16 +651,15 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
internal static bool IsCommentContentDisallowed(IEnumerable<HtmlSymbol> sequence)
{
var reversedSequence = sequence.Reverse();
var disallowEnding = new[] { new HtmlSymbol("-", HtmlSymbolType.Text), new HtmlSymbol("!", HtmlSymbolType.Bang), new HtmlSymbol("<", HtmlSymbolType.OpenAngle) };
var index = 0;
foreach (var item in reversedSequence)
{
if (!item.Equals(disallowEnding[index++]))
if (!item.Equals(nonAllowedHtmlCommentEnding[index++]))
{
return false;
}
if (index == disallowEnding.Length)
if (index == nonAllowedHtmlCommentEnding.Length)
{
return true;
}

View File

@ -133,6 +133,16 @@ namespace Microsoft.AspNetCore.Razor.Language.Test.Legacy
Assert.False(sut.IsHtmlCommentAhead());
}
[Fact]
public void IsHtmlCommentAhead_ReturnsTrueForCommentWithCodeInside()
{
// Arrange
var sut = CreateTestParserForContent("-- not closed @DateTime.Now comment-->");
// Act & Assert
Assert.True(sut.IsHtmlCommentAhead());
}
[Fact]
public void IsCommentContentDisallowed_ReturnsFalseForAllowedContent()
{

View File

@ -1111,7 +1111,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
[Fact]
public void Rewrite_AllowsSimpleHtmlCommentsAsChildren()
{
// Arrangestring documentContent,
// Arrange
IEnumerable<string> allowedChildren = new List<string> { "b" };
string literal = "asdf";
string commentOutput = "Hello World";
@ -1148,10 +1148,61 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
Array.Empty<RazorDiagnostic>());
}
[Fact]
public void Rewrite_DoesntAllowSimpleHtmlCommentsAsChildrenWhenFeatureFlagIsOff()
{
// Arrange
Func<string, string, string, int, int, RazorDiagnostic> nestedTagError =
(childName, parentName, allowed, location, length) =>
RazorDiagnosticFactory.CreateTagHelper_InvalidNestedTag(
new SourceSpan(absoluteIndex: location, lineIndex: 0, characterIndex: location, length: length), childName, parentName, allowed);
Func<string, string, int, int, RazorDiagnostic> nestedContentError =
(parentName, allowed, location, length) =>
RazorDiagnosticFactory.CreateTagHelper_CannotHaveNonTagContent(
new SourceSpan(absoluteIndex: location, lineIndex: 0, characterIndex: location, length: length), parentName, allowed);
IEnumerable<string> allowedChildren = new List<string> { "b" };
string comment1 = "Hello";
string expectedOutput = $"<p><!--{comment1}--></p>";
var pTagHelperBuilder = TagHelperDescriptorBuilder
.Create("PTagHelper", "SomeAssembly")
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("p"));
foreach (var childTag in allowedChildren)
{
pTagHelperBuilder.AllowChildTag(childTag);
}
var descriptors = new TagHelperDescriptor[]
{
pTagHelperBuilder.Build()
};
var factory = new SpanFactory();
var blockFactory = new BlockFactory(factory);
var expectedMarkup = new MarkupBlock(
new MarkupTagHelperBlock("p",
blockFactory.HtmlCommentBlock(comment1)));
// Act & Assert
EvaluateData(
descriptors,
expectedOutput,
expectedMarkup,
new[]
{
nestedContentError("p", "b", 3, 4),
nestedContentError("p", "b", 7, 5),
nestedContentError("p", "b", 12, 3),
},
featureFlags: RazorParserFeatureFlags.Create(RazorLanguageVersion.Version_2_0));
}
[Fact]
public void Rewrite_FailsForContentWithCommentsAsChildren()
{
// Arrangestring documentContent,
// Arrange
Func<string, string, string, int, int, RazorDiagnostic> nestedTagError =
(childName, parentName, allowed, location, length) =>
RazorDiagnosticFactory.CreateTagHelper_InvalidNestedTag(
@ -1203,7 +1254,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
[Fact]
public void Rewrite_AllowsRazorCommentsAsChildren()
{
// Arrangestring documentContent,
// Arrange
IEnumerable<string> allowedChildren = new List<string> { "b" };
string literal = "asdf";
string commentOutput = $"@*{literal}*@";
@ -1248,7 +1299,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
[Fact]
public void Rewrite_AllowsRazorMarkupInHtmlComment()
{
// Arrangestring documentContent,
// Arrange
IEnumerable<string> allowedChildren = new List<string> { "b" };
string literal = "asdf";
string part1 = "Hello ";

View File

@ -16,6 +16,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Assert
Assert.True(context.AllowMinimizedBooleanTagHelperAttributes);
Assert.True(context.AllowHtmlCommentsInTagHelpers);
}
[Fact]
@ -26,6 +27,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Assert
Assert.False(context.AllowMinimizedBooleanTagHelperAttributes);
Assert.False(context.AllowHtmlCommentsInTagHelpers);
}
}
}