Exemplo n.º 1
0
            public override void VisitTagHelperHtmlAttribute(TagHelperHtmlAttributeIntermediateNode node)
            {
                var attribute = new HtmlAttributeIntermediateNode()
                {
                    AttributeName = node.AttributeName,
                    Source        = node.Source,
                };

                _children.Add(attribute);

                for (var i = 0; i < node.Diagnostics.Count; i++)
                {
                    attribute.Diagnostics.Add(node.Diagnostics[i]);
                }

                switch (node.AttributeStructure)
                {
                case AttributeStructure.Minimized:

                    attribute.Prefix = node.AttributeName;
                    attribute.Suffix = string.Empty;
                    break;

                case AttributeStructure.NoQuotes:
                case AttributeStructure.SingleQuotes:
                case AttributeStructure.DoubleQuotes:

                    // We're ignoring attribute structure here for simplicity, it doesn't effect us.
                    attribute.Prefix = node.AttributeName + "=\"";
                    attribute.Suffix = "\"";

                    for (var i = 0; i < node.Children.Count; i++)
                    {
                        attribute.Children.Add(RewriteAttributeContent(node.Children[i]));
                    }

                    break;
                }

                IntermediateNode RewriteAttributeContent(IntermediateNode content)
                {
                    if (content is HtmlContentIntermediateNode html)
                    {
                        var value = new HtmlAttributeValueIntermediateNode()
                        {
                            Source = content.Source,
                        };

                        for (var i = 0; i < html.Children.Count; i++)
                        {
                            value.Children.Add(html.Children[i]);
                        }

                        for (var i = 0; i < html.Diagnostics.Count; i++)
                        {
                            value.Diagnostics.Add(html.Diagnostics[i]);
                        }

                        return(value);
                    }


                    return(content);
                }
            }
 public override void VisitTagHelperHtmlAttribute(TagHelperHtmlAttributeIntermediateNode node)
 {
     // Don't rewrite inside of attributes
 }
 public virtual void VisitTagHelperHtmlAttribute(TagHelperHtmlAttributeIntermediateNode node)
 {
     VisitDefault(node);
 }
Exemplo n.º 4
0
 public override void VisitTagHelperHtmlAttribute(TagHelperHtmlAttributeIntermediateNode node)
 {
     WriteContentNode(node, node.AttributeName, string.Format("HtmlAttributeValueStyle.{0}", node.AttributeStructure));
 }
        private static void RewriteCSharpAttributeContent(List <IntermediateNode> nodes, TagHelperHtmlAttributeIntermediateNode node)
        {
            var attributeNode = new HtmlAttributeIntermediateNode()
            {
                AttributeName = node.AttributeName,
                Prefix        = "=\"",
                Suffix        = "\"",
            };

            nodes.Add(attributeNode);

            var valueNode = new CSharpExpressionAttributeValueIntermediateNode();

            attributeNode.Children.Add(valueNode);

            for (var i = 0; i < node.Children[0].Children.Count; i++)
            {
                valueNode.Children.Add(node.Children[0].Children[i]);
            }
        }
        private static void RewriteHtmlAttributeContent(List <IntermediateNode> nodes, TagHelperHtmlAttributeIntermediateNode node)
        {
            switch (node.AttributeStructure)
            {
            case AttributeStructure.Minimized:
                nodes.Add(new HtmlContentIntermediateNode()
                {
                    Children =
                    {
                        new IntermediateToken()
                        {
                            Content = node.AttributeName + " ",
                            Kind    = TokenKind.Html,
                        }
                    }
                });
                break;

            // Blazor doesn't really care about preserving the fidelity of the attributes.
            case AttributeStructure.NoQuotes:
            case AttributeStructure.SingleQuotes:
            case AttributeStructure.DoubleQuotes:

                var htmlNode = new HtmlContentIntermediateNode();
                nodes.Add(htmlNode);

                htmlNode.Children.Add(new IntermediateToken()
                {
                    Content = node.AttributeName + "=\"",
                    Kind    = TokenKind.Html,
                });

                for (var i = 0; i < node.Children[0].Children.Count; i++)
                {
                    htmlNode.Children.Add(node.Children[0].Children[i]);
                }

                htmlNode.Children.Add(new IntermediateToken()
                {
                    Content = "\" ",
                    Kind    = TokenKind.Html,
                });

                break;
            }
        }
 private static void RewriteEmptyAttributeContent(List <IntermediateNode> nodes, TagHelperHtmlAttributeIntermediateNode node)
 {
     nodes.Add(new HtmlContentIntermediateNode()
     {
         Children =
         {
             new IntermediateToken()
             {
                 Content = node.AttributeName + " ",
                 Kind    = TokenKind.Html,
             }
         }
     });
 }
            private void AddTagHelperAttributes(IList <TagHelperAttributeNode> attributes, TagHelperBinding tagHelperBinding)
            {
                var descriptors = tagHelperBinding.Descriptors;
                var renderedBoundAttributeNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

                foreach (var attribute in attributes)
                {
                    var attributeValueNode    = attribute.Value;
                    var associatedDescriptors = descriptors.Where(descriptor =>
                                                                  descriptor.BoundAttributes.Any(attributeDescriptor => TagHelperMatchingConventions.CanSatisfyBoundAttribute(attribute.Name, attributeDescriptor)));

                    if (associatedDescriptors.Any() && renderedBoundAttributeNames.Add(attribute.Name))
                    {
                        var isMinimizedAttribute = attributeValueNode == null;
                        if (isMinimizedAttribute && !_featureFlags.AllowMinimizedBooleanTagHelperAttributes)
                        {
                            // Minimized attributes are not valid for non-boolean bound attributes. TagHelperBlockRewriter
                            // has already logged an error if it was a non-boolean bound attribute; so we can skip.
                            continue;
                        }

                        foreach (var associatedDescriptor in associatedDescriptors)
                        {
                            var associatedAttributeDescriptor = associatedDescriptor.BoundAttributes.First(a =>
                            {
                                return(TagHelperMatchingConventions.CanSatisfyBoundAttribute(attribute.Name, a));
                            });

                            var expectsBooleanValue = associatedAttributeDescriptor.ExpectsBooleanValue(attribute.Name);

                            if (isMinimizedAttribute && !expectsBooleanValue)
                            {
                                // We do not allow minimized non-boolean bound attributes.
                                continue;
                            }

                            var setTagHelperProperty = new TagHelperPropertyIntermediateNode()
                            {
                                AttributeName      = attribute.Name,
                                BoundAttribute     = associatedAttributeDescriptor,
                                TagHelper          = associatedDescriptor,
                                AttributeStructure = attribute.AttributeStructure,
                                Source             = BuildSourceSpanFromNode(attributeValueNode),
                                IsIndexerNameMatch = TagHelperMatchingConventions.SatisfiesBoundAttributeIndexer(attribute.Name, associatedAttributeDescriptor),
                            };

                            _builder.Push(setTagHelperProperty);
                            attributeValueNode?.Accept(this);
                            _builder.Pop();
                        }
                    }
                    else
                    {
                        var addHtmlAttribute = new TagHelperHtmlAttributeIntermediateNode()
                        {
                            AttributeName      = attribute.Name,
                            AttributeStructure = attribute.AttributeStructure
                        };

                        _builder.Push(addHtmlAttribute);
                        if (attributeValueNode != null)
                        {
                            attributeValueNode.Accept(this);
                        }
                        _builder.Pop();
                    }
                }
            }