diff --git a/src/Microsoft.AspNetCore.Razor.Language/Legacy/HtmlMarkupParser.cs b/src/Microsoft.AspNetCore.Razor.Language/Legacy/HtmlMarkupParser.cs index 39841d4835..48c79c83e1 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Legacy/HtmlMarkupParser.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Legacy/HtmlMarkupParser.cs @@ -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 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; } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/HtmlMarkupParserTests.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/HtmlMarkupParserTests.cs index c3e0cb87e1..a40ec2e422 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/HtmlMarkupParserTests.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/HtmlMarkupParserTests.cs @@ -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() { diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/TagHelperParseTreeRewriterTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/TagHelperParseTreeRewriterTest.cs index 425b728716..3f37c82c4d 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/TagHelperParseTreeRewriterTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/TagHelperParseTreeRewriterTest.cs @@ -1111,7 +1111,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy [Fact] public void Rewrite_AllowsSimpleHtmlCommentsAsChildren() { - // Arrangestring documentContent, + // Arrange IEnumerable allowedChildren = new List { "b" }; string literal = "asdf"; string commentOutput = "Hello World"; @@ -1148,10 +1148,61 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy Array.Empty()); } + [Fact] + public void Rewrite_DoesntAllowSimpleHtmlCommentsAsChildrenWhenFeatureFlagIsOff() + { + // Arrange + Func nestedTagError = + (childName, parentName, allowed, location, length) => + RazorDiagnosticFactory.CreateTagHelper_InvalidNestedTag( + new SourceSpan(absoluteIndex: location, lineIndex: 0, characterIndex: location, length: length), childName, parentName, allowed); + Func nestedContentError = + (parentName, allowed, location, length) => + RazorDiagnosticFactory.CreateTagHelper_CannotHaveNonTagContent( + new SourceSpan(absoluteIndex: location, lineIndex: 0, characterIndex: location, length: length), parentName, allowed); + + IEnumerable allowedChildren = new List { "b" }; + string comment1 = "Hello"; + string expectedOutput = $"

"; + + 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 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 allowedChildren = new List { "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 allowedChildren = new List { "b" }; string literal = "asdf"; string part1 = "Hello "; diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/RazorParserFeatureFlagsTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/RazorParserFeatureFlagsTest.cs index db57a33fdf..cfdb9f023d 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/RazorParserFeatureFlagsTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/RazorParserFeatureFlagsTest.cs @@ -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); } } }