public void WriteTagHelperHtmlAttributeValue(CodeRenderingContext context, PreallocatedTagHelperHtmlAttributeValueIntermediateNode node)
        {
            context.CodeWriter
            .Write("private static readonly global::")
            .Write(TagHelperAttributeTypeName)
            .Write(" ")
            .Write(node.VariableName)
            .Write(" = ")
            .WriteStartNewObject("global::" + TagHelperAttributeTypeName)
            .WriteStringLiteral(node.AttributeName);

            if (node.AttributeStructure == AttributeStructure.Minimized)
            {
                context.CodeWriter.WriteEndMethodInvocation();
            }
            else
            {
                context.CodeWriter
                .WriteParameterSeparator()
                .WriteStartNewObject("global::" + EncodedHtmlStringTypeName)
                .WriteStringLiteral(node.Value)
                .WriteEndMethodInvocation(endLine: false)
                .WriteParameterSeparator()
                .Write($"global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.{node.AttributeStructure}")
                .WriteEndMethodInvocation();
            }
        }
예제 #2
0
        public void WriteTagHelperHtmlAttributeValue_Minimized_RendersCorrectly()
        {
            // Arrange
            var extension = new PreallocatedAttributeTargetExtension();
            var context   = TestCodeRenderingContext.CreateRuntime();

            var node = new PreallocatedTagHelperHtmlAttributeValueIntermediateNode()
            {
                AttributeName      = "Foo",
                Value              = "Bar",
                AttributeStructure = AttributeStructure.Minimized,
                VariableName       = "_tagHelper1"
            };

            // Act
            extension.WriteTagHelperHtmlAttributeValue(context, node);

            // Assert
            var csharp = context.CodeWriter.GenerateCode();

            Assert.Equal(
                @"private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute _tagHelper1 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute(""Foo"");
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
예제 #3
0
            public void VisitExtension(DefaultTagHelperHtmlAttributeIntermediateNode node)
            {
                if (node.Children.Count != 1 || !(node.Children.First() is HtmlContentIntermediateNode))
                {
                    return;
                }

                var htmlContentNode = node.Children.First() as HtmlContentIntermediateNode;
                var plainTextValue  = GetContent(htmlContentNode);

                PreallocatedTagHelperHtmlAttributeValueIntermediateNode declaration = null;

                for (var i = 0; i < _classDeclaration.Children.Count; i++)
                {
                    var current = _classDeclaration.Children[i];

                    if (current is PreallocatedTagHelperHtmlAttributeValueIntermediateNode existingDeclaration)
                    {
                        if (string.Equals(existingDeclaration.AttributeName, node.AttributeName, StringComparison.Ordinal) &&
                            string.Equals(existingDeclaration.Value, plainTextValue, StringComparison.Ordinal) &&
                            existingDeclaration.AttributeStructure == node.AttributeStructure)
                        {
                            declaration = existingDeclaration;
                            break;
                        }
                    }
                }

                if (declaration == null)
                {
                    var variableCount = _classDeclaration.Children.Count - _variableCountOffset;
                    var preAllocatedAttributeVariableName = PreAllocatedAttributeVariablePrefix + variableCount;
                    declaration = new PreallocatedTagHelperHtmlAttributeValueIntermediateNode
                    {
                        VariableName       = preAllocatedAttributeVariableName,
                        AttributeName      = node.AttributeName,
                        Value              = plainTextValue,
                        AttributeStructure = node.AttributeStructure,
                    };
                    _classDeclaration.Children.Insert(_preallocatedDeclarationCount++, declaration);
                }

                var addPreAllocatedAttribute = new PreallocatedTagHelperHtmlAttributeIntermediateNode
                {
                    VariableName = declaration.VariableName,
                };

                var nodeIndex = Parent.Children.IndexOf(node);

                Parent.Children[nodeIndex] = addPreAllocatedAttribute;
            }