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:
parent
e8eb3b7b1c
commit
3e22c16930
|
|
@ -12,6 +12,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
{
|
{
|
||||||
private const string ScriptTagName = "script";
|
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 static readonly char[] ValidAfterTypeAttributeNameCharacters = { ' ', '\t', '\r', '\n', '\f', '=' };
|
||||||
private SourceLocation _lastTagStart = SourceLocation.Zero;
|
private SourceLocation _lastTagStart = SourceLocation.Zero;
|
||||||
private HtmlSymbol _bufferedOpenAngle;
|
private HtmlSymbol _bufferedOpenAngle;
|
||||||
|
|
@ -649,16 +651,15 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
internal static bool IsCommentContentDisallowed(IEnumerable<HtmlSymbol> sequence)
|
internal static bool IsCommentContentDisallowed(IEnumerable<HtmlSymbol> sequence)
|
||||||
{
|
{
|
||||||
var reversedSequence = sequence.Reverse();
|
var reversedSequence = sequence.Reverse();
|
||||||
var disallowEnding = new[] { new HtmlSymbol("-", HtmlSymbolType.Text), new HtmlSymbol("!", HtmlSymbolType.Bang), new HtmlSymbol("<", HtmlSymbolType.OpenAngle) };
|
|
||||||
var index = 0;
|
var index = 0;
|
||||||
foreach (var item in reversedSequence)
|
foreach (var item in reversedSequence)
|
||||||
{
|
{
|
||||||
if (!item.Equals(disallowEnding[index++]))
|
if (!item.Equals(nonAllowedHtmlCommentEnding[index++]))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (index == disallowEnding.Length)
|
if (index == nonAllowedHtmlCommentEnding.Length)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -133,6 +133,16 @@ namespace Microsoft.AspNetCore.Razor.Language.Test.Legacy
|
||||||
Assert.False(sut.IsHtmlCommentAhead());
|
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]
|
[Fact]
|
||||||
public void IsCommentContentDisallowed_ReturnsFalseForAllowedContent()
|
public void IsCommentContentDisallowed_ReturnsFalseForAllowedContent()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1111,7 +1111,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Rewrite_AllowsSimpleHtmlCommentsAsChildren()
|
public void Rewrite_AllowsSimpleHtmlCommentsAsChildren()
|
||||||
{
|
{
|
||||||
// Arrangestring documentContent,
|
// Arrange
|
||||||
IEnumerable<string> allowedChildren = new List<string> { "b" };
|
IEnumerable<string> allowedChildren = new List<string> { "b" };
|
||||||
string literal = "asdf";
|
string literal = "asdf";
|
||||||
string commentOutput = "Hello World";
|
string commentOutput = "Hello World";
|
||||||
|
|
@ -1148,10 +1148,61 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
Array.Empty<RazorDiagnostic>());
|
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]
|
[Fact]
|
||||||
public void Rewrite_FailsForContentWithCommentsAsChildren()
|
public void Rewrite_FailsForContentWithCommentsAsChildren()
|
||||||
{
|
{
|
||||||
// Arrangestring documentContent,
|
// Arrange
|
||||||
Func<string, string, string, int, int, RazorDiagnostic> nestedTagError =
|
Func<string, string, string, int, int, RazorDiagnostic> nestedTagError =
|
||||||
(childName, parentName, allowed, location, length) =>
|
(childName, parentName, allowed, location, length) =>
|
||||||
RazorDiagnosticFactory.CreateTagHelper_InvalidNestedTag(
|
RazorDiagnosticFactory.CreateTagHelper_InvalidNestedTag(
|
||||||
|
|
@ -1203,7 +1254,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Rewrite_AllowsRazorCommentsAsChildren()
|
public void Rewrite_AllowsRazorCommentsAsChildren()
|
||||||
{
|
{
|
||||||
// Arrangestring documentContent,
|
// Arrange
|
||||||
IEnumerable<string> allowedChildren = new List<string> { "b" };
|
IEnumerable<string> allowedChildren = new List<string> { "b" };
|
||||||
string literal = "asdf";
|
string literal = "asdf";
|
||||||
string commentOutput = $"@*{literal}*@";
|
string commentOutput = $"@*{literal}*@";
|
||||||
|
|
@ -1248,7 +1299,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Rewrite_AllowsRazorMarkupInHtmlComment()
|
public void Rewrite_AllowsRazorMarkupInHtmlComment()
|
||||||
{
|
{
|
||||||
// Arrangestring documentContent,
|
// Arrange
|
||||||
IEnumerable<string> allowedChildren = new List<string> { "b" };
|
IEnumerable<string> allowedChildren = new List<string> { "b" };
|
||||||
string literal = "asdf";
|
string literal = "asdf";
|
||||||
string part1 = "Hello ";
|
string part1 = "Hello ";
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.True(context.AllowMinimizedBooleanTagHelperAttributes);
|
Assert.True(context.AllowMinimizedBooleanTagHelperAttributes);
|
||||||
|
Assert.True(context.AllowHtmlCommentsInTagHelpers);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -26,6 +27,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(context.AllowMinimizedBooleanTagHelperAttributes);
|
Assert.False(context.AllowMinimizedBooleanTagHelperAttributes);
|
||||||
|
Assert.False(context.AllowHtmlCommentsInTagHelpers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue