private void BuildMalformedTagHelpers(int count, ErrorSink errorSink) { for (var i = 0; i < count; i++) { var tracker = _trackerStack.Peek(); // Skip all non-TagHelper entries. Non TagHelper trackers do not need to represent well-formed HTML. if (!tracker.IsTagHelper) { PopTrackerStack(); continue; } var malformedTagHelper = ((TagHelperBlockTracker)tracker).Builder; errorSink.OnError( RazorDiagnosticFactory.CreateParsing_TagHelperFoundMalformedTagHelper( new SourceSpan(SourceLocationTracker.Advance(malformedTagHelper.Start, "<"), malformedTagHelper.TagName.Length), malformedTagHelper.TagName)); BuildCurrentlyTrackedTagHelperBlock(endTag: null); } }
public override SyntaxNode VisitMarkupElement(MarkupElementSyntax node) { if (IsPartOfStartTag(node)) { // If this element is inside a start tag, it is some sort of malformed case like // <p @do { someattribute=\"btn\"></p>, where the end "p" tag is inside the start "p" tag. // We don't want to do tag helper parsing for this tag. return(base.VisitMarkupElement(node)); } MarkupTagHelperStartTagSyntax tagHelperStart = null; MarkupTagHelperEndTagSyntax tagHelperEnd = null; TagHelperInfo tagHelperInfo = null; // Visit the start tag. var startTag = (MarkupStartTagSyntax)Visit(node.StartTag); if (startTag != null) { var tagName = startTag.GetTagNameWithOptionalBang(); if (TryRewriteTagHelperStart(startTag, node.EndTag, out tagHelperStart, out tagHelperInfo)) { // This is a tag helper. if (tagHelperInfo.TagMode == TagMode.SelfClosing || tagHelperInfo.TagMode == TagMode.StartTagOnly) { var tagHelperElement = SyntaxFactory.MarkupTagHelperElement(tagHelperStart, body: new SyntaxList <RazorSyntaxNode>(), endTag: null); var rewrittenTagHelper = tagHelperElement.WithTagHelperInfo(tagHelperInfo); if (node.Body.Count == 0 && node.EndTag == null) { return(rewrittenTagHelper); } // This tag contains a body and/or an end tag which needs to be moved to the parent. var rewrittenNodes = SyntaxListBuilder <RazorSyntaxNode> .Create(); rewrittenNodes.Add(rewrittenTagHelper); var rewrittenBody = VisitList(node.Body); rewrittenNodes.AddRange(rewrittenBody); return(SyntaxFactory.MarkupElement(startTag: null, body: rewrittenNodes.ToList(), endTag: node.EndTag)); } else if (node.EndTag == null) { // Start tag helper with no corresponding end tag. _errorSink.OnError( RazorDiagnosticFactory.CreateParsing_TagHelperFoundMalformedTagHelper( new SourceSpan(SourceLocationTracker.Advance(startTag.GetSourceLocation(_source), "<"), tagName.Length), tagName)); } else { // Tag helper start tag. Keep track. var tracker = new TagHelperTracker(_tagHelperPrefix, tagHelperInfo); _trackerStack.Push(tracker); } } else { // Non-TagHelper tag. ValidateParentAllowsPlainStartTag(startTag); if (node.EndTag != null || (!startTag.IsSelfClosing() && !startTag.IsVoidElement())) { // Ideally we don't want to keep track of self-closing or void tags. // But if a matching end tag exists, keep track of the start tag no matter what. // We will just assume the parser had a good reason to do this. var tracker = new TagTracker(tagName, isTagHelper: false); _trackerStack.Push(tracker); } } } // Visit body between start and end tags. var body = VisitList(node.Body); // Visit end tag. var endTag = (MarkupEndTagSyntax)Visit(node.EndTag); if (endTag != null) { var tagName = endTag.GetTagNameWithOptionalBang(); if (TryRewriteTagHelperEnd(startTag, endTag, out tagHelperEnd)) { // This is a tag helper if (startTag == null) { // The end tag helper has no corresponding start tag, create an error. _errorSink.OnError( RazorDiagnosticFactory.CreateParsing_TagHelperFoundMalformedTagHelper( new SourceSpan(SourceLocationTracker.Advance(endTag.GetSourceLocation(_source), "</"), tagName.Length), tagName)); } } else { // Non tag helper end tag. if (startTag == null) { // Standalone end tag. We may need to error if it is not supposed to be here. // If there was a corresponding start tag, we would have already added this error. ValidateParentAllowsPlainEndTag(endTag); } else { // Since a start tag exists, we must already be tracking it. // Pop the stack as we're done with the end tag. _trackerStack.Pop(); } } } if (tagHelperInfo != null) { // If we get here it means this element was rewritten as a tag helper. var tagHelperElement = SyntaxFactory.MarkupTagHelperElement(tagHelperStart, body, tagHelperEnd); return(tagHelperElement.WithTagHelperInfo(tagHelperInfo)); } // There was no matching tag helper for this element. Return. return(node.Update(startTag, body, endTag)); }
private bool TryRewriteTagHelper(Block tagBlock, ErrorSink errorSink) { // Get tag name of the current block (doesn't matter if it's an end or start tag) var tagName = GetTagName(tagBlock); // Could not determine tag name, it can't be a TagHelper, continue on and track the element. if (tagName == null) { return(false); } TagHelperBinding tagHelperBinding; if (!IsPotentialTagHelper(tagName, tagBlock)) { return(false); } var tracker = _currentTagHelperTracker; var tagNameScope = tracker?.TagName ?? string.Empty; if (!IsEndTag(tagBlock)) { // We're now in a start tag block, we first need to see if the tag block is a tag helper. var elementAttributes = GetAttributeNameValuePairs(tagBlock); tagHelperBinding = _tagHelperBinder.GetBinding( tagName, elementAttributes, CurrentParentTagName, CurrentParentIsTagHelper); // If there aren't any TagHelperDescriptors registered then we aren't a TagHelper if (tagHelperBinding == null) { // If the current tag matches the current TagHelper scope it means the parent TagHelper matched // all the required attributes but the current one did not; therefore, we need to increment the // OpenMatchingTags counter for current the TagHelperBlock so we don't end it too early. // ex: <myth req="..."><myth></myth></myth> We don't want the first myth to close on the inside // tag. if (string.Equals(tagNameScope, tagName, StringComparison.OrdinalIgnoreCase)) { tracker.OpenMatchingTags++; } return(false); } ValidateParentAllowsTagHelper(tagName, tagBlock, errorSink); ValidateBinding(tagHelperBinding, tagName, tagBlock, errorSink); // We're in a start TagHelper block. var validTagStructure = ValidateTagSyntax(tagName, tagBlock, errorSink); var builder = TagHelperBlockRewriter.Rewrite( tagName, validTagStructure, _featureFlags, tagBlock, tagHelperBinding, errorSink); // Track the original start tag so the editor knows where each piece of the TagHelperBlock lies // for formatting. builder.SourceStartTag = tagBlock; // Found a new tag helper block TrackTagHelperBlock(builder); // If it's a non-content expecting block then we don't have to worry about nested children within the // tag. Complete it. if (builder.TagMode == TagMode.SelfClosing || builder.TagMode == TagMode.StartTagOnly) { BuildCurrentlyTrackedTagHelperBlock(endTag: null); } } else { // Validate that our end tag matches the currently scoped tag, if not we may need to error. if (tagNameScope.Equals(tagName, StringComparison.OrdinalIgnoreCase)) { // If there are additional end tags required before we can build our block it means we're in a // situation like this: <myth req="..."><myth></myth></myth> where we're at the inside </myth>. if (tracker.OpenMatchingTags > 0) { tracker.OpenMatchingTags--; return(false); } ValidateTagSyntax(tagName, tagBlock, errorSink); BuildCurrentlyTrackedTagHelperBlock(tagBlock); } else { tagHelperBinding = _tagHelperBinder.GetBinding( tagName, attributes: Array.Empty <KeyValuePair <string, string> >(), parentTagName: CurrentParentTagName, parentIsTagHelper: CurrentParentIsTagHelper); // If there are not TagHelperDescriptors associated with the end tag block that also have no // required attributes then it means we can't be a TagHelper, bail out. if (tagHelperBinding == null) { return(false); } foreach (var descriptor in tagHelperBinding.Descriptors) { var boundRules = tagHelperBinding.GetBoundRules(descriptor); var invalidRule = boundRules.FirstOrDefault(rule => rule.TagStructure == TagStructure.WithoutEndTag); if (invalidRule != null) { // End tag TagHelper that states it shouldn't have an end tag. errorSink.OnError( RazorDiagnosticFactory.CreateParsing_TagHelperMustNotHaveAnEndTag( new SourceSpan(SourceLocationTracker.Advance(tagBlock.Start, "</"), tagName.Length), tagName, descriptor.DisplayName, invalidRule.TagStructure)); return(false); } } // Current tag helper scope does not match the end tag. Attempt to recover the tag // helper by looking up the previous tag helper scopes for a matching tag. If we // can't recover it means there was no corresponding tag helper start tag. if (TryRecoverTagHelper(tagName, tagBlock, errorSink)) { ValidateParentAllowsTagHelper(tagName, tagBlock, errorSink); ValidateTagSyntax(tagName, tagBlock, errorSink); // Successfully recovered, move onto the next element. } else { // Could not recover, the end tag helper has no corresponding start tag, create // an error based on the current childBlock. errorSink.OnError( RazorDiagnosticFactory.CreateParsing_TagHelperFoundMalformedTagHelper( new SourceSpan(SourceLocationTracker.Advance(tagBlock.Start, "</"), tagName.Length), tagName)); return(false); } } } return(true); }