Applied PR feedback (unit testst will be added as part of a separate commit)

This commit is contained in:
Artak Mkrtchyan 2018-03-08 13:29:18 -08:00
parent 32e2c533c7
commit 7357fa04ea
No known key found for this signature in database
GPG Key ID: 64D580ACBA8CA645
3 changed files with 45 additions and 46 deletions

View File

@ -496,6 +496,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
{ {
using (Context.Builder.StartBlock(BlockKindInternal.HtmlComment)) using (Context.Builder.StartBlock(BlockKindInternal.HtmlComment))
{ {
// Accept the double-hyphen symbol at the beginning of the comment block.
AcceptAndMoveNext(); AcceptAndMoveNext();
Output(SpanKindInternal.Markup, AcceptedCharactersInternal.None); Output(SpanKindInternal.Markup, AcceptedCharactersInternal.None);
@ -503,25 +504,22 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
while (!EndOfFile) while (!EndOfFile)
{ {
SkipToAndParseCode(HtmlSymbolType.DoubleHyphen); 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)) // This is the end of a comment block
{ Accept(lastDoubleHyphen);
// Output the content in the comment block as a separate markup AcceptAndMoveNext();
Output(SpanKindInternal.Markup, AcceptedCharactersInternal.WhiteSpace); Output(SpanKindInternal.Markup, AcceptedCharactersInternal.None);
return true;
// This is the end of a comment block }
Accept(lastDoubleHyphen); else if (lastDoubleHyphen != null)
AcceptAndMoveNext(); {
Output(SpanKindInternal.Markup, AcceptedCharactersInternal.None); Accept(lastDoubleHyphen);
return true;
}
else if (lastDoubleHyphen != null)
{
Accept(lastDoubleHyphen);
}
} }
} }
} }
@ -606,28 +604,27 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
} }
// Check condition 2.2 // Check condition 2.2
bool isValidComment = false; var isValidComment = false;
LookaheadUntil((s, p) => LookaheadUntil((s, p) =>
{ {
bool breakLookahead = false;
if (s.Type == HtmlSymbolType.DoubleHyphen) if (s.Type == HtmlSymbolType.DoubleHyphen)
{ {
if (NextIs(HtmlSymbolType.CloseAngle)) 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. // 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); isValidComment = !SymbolSequenceEndsWithItems(p, HtmlSymbolType.OpenAngle, HtmlSymbolType.Bang, HtmlSymbolType.DoubleHyphen);
breakLookahead = true; return true;
} }
else if (NextIs(ns => IsDashSymbol(ns) && NextIs(HtmlSymbolType.CloseAngle))) 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. // This is also a valid closing comment case, as the dashes lookup is treated with DoubleHyphen symbols first.
isValidComment = true; isValidComment = true;
breakLookahead = true; return true;
} }
else if (NextIs(ns => ns.Type == HtmlSymbolType.Bang && NextIs(HtmlSymbolType.CloseAngle))) else if (NextIs(ns => ns.Type == HtmlSymbolType.Bang && NextIs(HtmlSymbolType.CloseAngle)))
{ {
isValidComment = false; isValidComment = false;
breakLookahead = true; return true;
} }
} }
else if (s.Type == HtmlSymbolType.OpenAngle) 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))) if (NextIs(ns => ns.Type == HtmlSymbolType.Bang && NextIs(HtmlSymbolType.DoubleHyphen)))
{ {
isValidComment = false; isValidComment = false;
breakLookahead = true; return true;
} }
} }
return breakLookahead; return false;
}); });
return isValidComment; return isValidComment;

View File

@ -488,28 +488,31 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
private void ValidateParentAllowsContent(Span child, ErrorSink errorSink) private void ValidateParentAllowsContent(Span child, ErrorSink errorSink)
{ {
var isDisallowedContent = true; if (HasAllowedChildren())
if (_featureFlags.AllowHtmlCommentsInTagHelpers)
{ {
isDisallowedContent = !IsComment(child) && child.Kind != SpanKindInternal.Transition && child.Kind != SpanKindInternal.Code; var isDisallowedContent = true;
} if (_featureFlags.AllowHtmlCommentsInTagHelpers)
if (HasAllowedChildren() && isDisallowedContent)
{
var content = child.Content;
if (!string.IsNullOrWhiteSpace(content))
{ {
var trimmedStart = content.TrimStart(); isDisallowedContent = !IsComment(child) && child.Kind != SpanKindInternal.Transition && child.Kind != SpanKindInternal.Code;
var whitespace = content.Substring(0, content.Length - trimmedStart.Length); }
var errorStart = SourceLocationTracker.Advance(child.Start, whitespace);
var length = trimmedStart.TrimEnd().Length; if (isDisallowedContent)
var allowedChildren = _currentTagHelperTracker.AllowedChildren; {
var allowedChildrenString = string.Join(", ", allowedChildren); var content = child.Content;
errorSink.OnError( if (!string.IsNullOrWhiteSpace(content))
RazorDiagnosticFactory.CreateTagHelper_CannotHaveNonTagContent( {
new SourceSpan(errorStart, length), var trimmedStart = content.TrimStart();
_currentTagHelperTracker.TagName, var whitespace = content.Substring(0, content.Length - trimmedStart.Length);
allowedChildrenString)); 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));
}
} }
} }
} }

View File

@ -122,9 +122,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
throw new ArgumentNullException(nameof(condition)); 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<TSymbol>(); var symbols = new List<TSymbol>();
symbols.Add(CurrentSymbol); symbols.Add(CurrentSymbol);