コード例 #1
0
        public override void VisitMarkupTagHelperStartTag(MarkupTagHelperStartTagSyntax node)
        {
            AddSemanticRange(node.OpenAngle, RazorSemanticTokensLegend.MarkupTagDelimiter);
            if (node.Bang != null)
            {
                AddSemanticRange(node.Bang, RazorSemanticTokensLegend.MarkupElement);
            }

            if (ClassifyTagName((MarkupTagHelperElementSyntax)node.Parent))
            {
                AddSemanticRange(node.Name, RazorSemanticTokensLegend.RazorTagHelperElement);
            }
            else
            {
                AddSemanticRange(node.Name, RazorSemanticTokensLegend.MarkupElement);
            }

            Visit(node.Attributes);

            if (node.ForwardSlash != null)
            {
                AddSemanticRange(node.ForwardSlash, RazorSemanticTokensLegend.MarkupTagDelimiter);
            }
            AddSemanticRange(node.CloseAngle, RazorSemanticTokensLegend.MarkupTagDelimiter);
        }
コード例 #2
0
 private static SyntaxNode?GetStartOrEndTagName(SyntaxNode node)
 {
     return(node switch
     {
         MarkupTagHelperStartTagSyntax tagHelperStartTag => tagHelperStartTag.Name,
         MarkupTagHelperEndTagSyntax tagHelperEndTag => tagHelperEndTag.Name,
         _ => null
     });
コード例 #3
0
 public override void VisitMarkupTagHelperStartTag(MarkupTagHelperStartTagSyntax node)
 {
     if (ClassifyTagName((MarkupTagHelperElementSyntax)node.Parent))
     {
         var result = CreateSemanticRange(node.Name, SyntaxKind.MarkupTagHelperStartTag);
         AddNode(result);
     }
     base.VisitMarkupTagHelperStartTag(node);
 }
 public override void VisitMarkupTagHelperStartTag(MarkupTagHelperStartTagSyntax node)
 {
     if (ClassifyTagName((MarkupTagHelperElementSyntax)node.Parent))
     {
         var result = new SyntaxResult(node.Name, SyntaxKind.MarkupTagHelperStartTag, _razorCodeDocument);
         _syntaxNodes.Add(result);
     }
     base.VisitMarkupTagHelperStartTag(node);
 }
コード例 #5
0
 public override void VisitMarkupTagHelperStartTag(MarkupTagHelperStartTagSyntax node)
 {
     WriteBlock(node, FormattingBlockKind.Tag, n =>
     {
         foreach (var child in n.Children)
         {
             Visit(child);
         }
     });
 }
コード例 #6
0
 public override void VisitMarkupTagHelperStartTag(MarkupTagHelperStartTagSyntax node)
 {
     foreach (var child in node.Children)
     {
         if (child is MarkupTagHelperAttributeSyntax attribute)
         {
             Visit(attribute);
         }
     }
 }
コード例 #7
0
 public override void VisitMarkupTagHelperStartTag(MarkupTagHelperStartTagSyntax node)
 {
     foreach (var child in node.Attributes)
     {
         if (child is MarkupTagHelperAttributeSyntax ||
             child is MarkupTagHelperDirectiveAttributeSyntax ||
             child is MarkupMinimizedTagHelperDirectiveAttributeSyntax)
         {
             Visit(child);
         }
     }
 }
コード例 #8
0
        public override void VisitMarkupTagHelperStartTag(MarkupTagHelperStartTagSyntax node)
        {
            AddSemanticRange(node.OpenAngle);
            if (ClassifyTagName((MarkupTagHelperElementSyntax)node.Parent))
            {
                AddSemanticRange(node.Name, SyntaxKind.MarkupTagHelperStartTag);
            }
            else
            {
                AddSemanticRange(node.Name, SyntaxKind.MarkupElement);
            }

            base.VisitMarkupTagHelperStartTag(node);

            if (node.ForwardSlash != null)
            {
                AddSemanticRange(node.ForwardSlash);
            }
            AddSemanticRange(node.CloseAngle);
        }
コード例 #9
0
            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));
            }
コード例 #10
0
            private bool TryRewriteTagHelperStart(
                MarkupStartTagSyntax startTag,
                MarkupEndTagSyntax endTag,
                out MarkupTagHelperStartTagSyntax rewritten,
                out TagHelperInfo tagHelperInfo)
            {
                rewritten     = null;
                tagHelperInfo = null;

                // Get tag name of the current block
                var tagName = startTag.GetTagNameWithOptionalBang();

                // Could not determine tag name, it can't be a TagHelper, continue on and track the element.
                if (string.IsNullOrEmpty(tagName) || tagName.StartsWith("!", StringComparison.Ordinal))
                {
                    return(false);
                }

                TagHelperBinding tagHelperBinding;

                if (!IsPotentialTagHelperStart(tagName, startTag))
                {
                    return(false);
                }

                var tracker      = CurrentTagHelperTracker;
                var tagNameScope = tracker?.TagName ?? string.Empty;

                // We're now in a start tag block, we first need to see if the tag block is a tag helper.
                var elementAttributes = GetAttributeNameValuePairs(startTag);

                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, startTag);
                ValidateBinding(tagHelperBinding, tagName, startTag);

                // We're in a start TagHelper block.
                ValidateStartTagSyntax(tagName, startTag);

                var rewrittenStartTag = TagHelperBlockRewriter.Rewrite(
                    tagName,
                    _featureFlags,
                    startTag,
                    tagHelperBinding,
                    _errorSink,
                    _source);

                var tagMode = TagHelperBlockRewriter.GetTagMode(startTag, endTag, tagHelperBinding);

                tagHelperInfo = new TagHelperInfo(tagName, tagMode, tagHelperBinding);
                rewritten     = rewrittenStartTag;

                return(true);
            }
コード例 #11
0
ファイル: RazorHtmlWriter.cs プロジェクト: zzl1010/aspnetcore
 public override void VisitMarkupTagHelperStartTag(MarkupTagHelperStartTagSyntax node)
 {
     WriteNode(node, isHtml: true, base.VisitMarkupTagHelperStartTag);
 }