diff --git a/src/Microsoft.AspNetCore.Razor.Language/Legacy/HtmlMarkupParser.cs b/src/Microsoft.AspNetCore.Razor.Language/Legacy/HtmlMarkupParser.cs index fb5237866e..dcd0254c29 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Legacy/HtmlMarkupParser.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Legacy/HtmlMarkupParser.cs @@ -496,6 +496,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy { using (Context.Builder.StartBlock(BlockKindInternal.HtmlComment)) { + // Accept the double-hyphen symbol at the beginning of the comment block. AcceptAndMoveNext(); Output(SpanKindInternal.Markup, AcceptedCharactersInternal.None); @@ -503,25 +504,22 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy while (!EndOfFile) { SkipToAndParseCode(HtmlSymbolType.DoubleHyphen); - if (At(HtmlSymbolType.DoubleHyphen)) + var lastDoubleHyphen = AcceptAllButLastDoubleHypens(); + + if (At(HtmlSymbolType.CloseAngle)) { - var lastDoubleHyphen = AcceptAllButLastDoubleHypens(); + // Output the content in the comment block as a separate markup + Output(SpanKindInternal.Markup, AcceptedCharactersInternal.WhiteSpace); - if (At(HtmlSymbolType.CloseAngle)) - { - // 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); - return true; - } - else if (lastDoubleHyphen != null) - { - Accept(lastDoubleHyphen); - } + // This is the end of a comment block + Accept(lastDoubleHyphen); + AcceptAndMoveNext(); + Output(SpanKindInternal.Markup, AcceptedCharactersInternal.None); + return true; + } + else if (lastDoubleHyphen != null) + { + Accept(lastDoubleHyphen); } } } @@ -606,28 +604,27 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy } // Check condition 2.2 - bool isValidComment = false; + var isValidComment = false; LookaheadUntil((s, p) => { - bool breakLookahead = false; if (s.Type == HtmlSymbolType.DoubleHyphen) { if (NextIs(HtmlSymbolType.CloseAngle)) { // Check condition 2.3: We're at the end of a comment. Check to make sure the text ending is allowed. isValidComment = !SymbolSequenceEndsWithItems(p, HtmlSymbolType.OpenAngle, HtmlSymbolType.Bang, HtmlSymbolType.DoubleHyphen); - breakLookahead = true; + return true; } else if (NextIs(ns => IsDashSymbol(ns) && NextIs(HtmlSymbolType.CloseAngle))) { // This is also a valid closing comment case, as the dashes lookup is treated with DoubleHyphen symbols first. isValidComment = true; - breakLookahead = true; + return true; } else if (NextIs(ns => ns.Type == HtmlSymbolType.Bang && NextIs(HtmlSymbolType.CloseAngle))) { isValidComment = false; - breakLookahead = true; + return true; } } else if (s.Type == HtmlSymbolType.OpenAngle) @@ -635,11 +632,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy if (NextIs(ns => ns.Type == HtmlSymbolType.Bang && NextIs(HtmlSymbolType.DoubleHyphen))) { isValidComment = false; - breakLookahead = true; + return true; } } - return breakLookahead; + return false; }); return isValidComment; diff --git a/src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperParseTreeRewriter.cs b/src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperParseTreeRewriter.cs index a4c9906b1a..d5d65c2985 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperParseTreeRewriter.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperParseTreeRewriter.cs @@ -488,28 +488,31 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy private void ValidateParentAllowsContent(Span child, ErrorSink errorSink) { - var isDisallowedContent = true; - if (_featureFlags.AllowHtmlCommentsInTagHelpers) + if (HasAllowedChildren()) { - isDisallowedContent = !IsComment(child) && child.Kind != SpanKindInternal.Transition && child.Kind != SpanKindInternal.Code; - } - - if (HasAllowedChildren() && isDisallowedContent) - { - var content = child.Content; - if (!string.IsNullOrWhiteSpace(content)) + var isDisallowedContent = true; + if (_featureFlags.AllowHtmlCommentsInTagHelpers) { - var trimmedStart = content.TrimStart(); - var whitespace = content.Substring(0, content.Length - trimmedStart.Length); - var errorStart = SourceLocationTracker.Advance(child.Start, whitespace); - var length = trimmedStart.TrimEnd().Length; - var allowedChildren = _currentTagHelperTracker.AllowedChildren; - var allowedChildrenString = string.Join(", ", allowedChildren); - errorSink.OnError( - RazorDiagnosticFactory.CreateTagHelper_CannotHaveNonTagContent( - new SourceSpan(errorStart, length), - _currentTagHelperTracker.TagName, - allowedChildrenString)); + isDisallowedContent = !IsComment(child) && child.Kind != SpanKindInternal.Transition && child.Kind != SpanKindInternal.Code; + } + + if (isDisallowedContent) + { + var content = child.Content; + if (!string.IsNullOrWhiteSpace(content)) + { + var trimmedStart = content.TrimStart(); + var whitespace = content.Substring(0, content.Length - trimmedStart.Length); + var errorStart = SourceLocationTracker.Advance(child.Start, whitespace); + var length = trimmedStart.TrimEnd().Length; + var allowedChildren = _currentTagHelperTracker.AllowedChildren; + var allowedChildrenString = string.Join(", ", allowedChildren); + errorSink.OnError( + RazorDiagnosticFactory.CreateTagHelper_CannotHaveNonTagContent( + new SourceSpan(errorStart, length), + _currentTagHelperTracker.TagName, + allowedChildrenString)); + } } } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Legacy/TokenizerBackedParser.cs b/src/Microsoft.AspNetCore.Razor.Language/Legacy/TokenizerBackedParser.cs index 1819407076..98763ad0a3 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Legacy/TokenizerBackedParser.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Legacy/TokenizerBackedParser.cs @@ -122,9 +122,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy throw new ArgumentNullException(nameof(condition)); } - bool matchFound = false; + var matchFound = false; - // We add 1 in order to store the current symbol. var symbols = new List(); symbols.Add(CurrentSymbol);