예제 #1
0
        public void WriteTagHelperPropertyValue_RendersCorrectly()
        {
            // Arrange
            var extension = new PreallocatedAttributeTargetExtension();
            var context   = TestCodeRenderingContext.CreateRuntime();

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

            // Act
            extension.WriteTagHelperPropertyValue(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"", ""Bar"", global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
",
                csharp,
                ignoreLineEndingDifferences: true);
        }
예제 #2
0
            public void VisitExtension(DefaultTagHelperPropertyIntermediateNode node)
            {
                if (!(node.BoundAttribute.IsStringProperty || (node.IsIndexerNameMatch && node.BoundAttribute.IsIndexerStringProperty)) ||
                    node.Children.Count != 1 ||
                    !(node.Children.First() is HtmlContentIntermediateNode))
                {
                    return;
                }

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

                PreallocatedTagHelperPropertyValueIntermediateNode declaration = null;

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

                    if (current is PreallocatedTagHelperPropertyValueIntermediateNode 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 PreallocatedTagHelperPropertyValueIntermediateNode()
                    {
                        VariableName       = preAllocatedAttributeVariableName,
                        AttributeName      = node.AttributeName,
                        Value              = plainTextValue,
                        AttributeStructure = node.AttributeStructure,
                    };
                    _classDeclaration.Children.Insert(_preallocatedDeclarationCount++, declaration);
                }

                var setPreallocatedProperty = new PreallocatedTagHelperPropertyIntermediateNode(node)
                {
                    VariableName = declaration.VariableName,
                };

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

                Parent.Children[nodeIndex] = setPreallocatedProperty;
            }
 public void WriteTagHelperPropertyValue(CodeRenderingContext context, PreallocatedTagHelperPropertyValueIntermediateNode node)
 {
     context.CodeWriter
     .Write("private static readonly global::")
     .Write(TagHelperAttributeTypeName)
     .Write(" ")
     .Write(node.VariableName)
     .Write(" = ")
     .WriteStartNewObject("global::" + TagHelperAttributeTypeName)
     .WriteStringLiteral(node.AttributeName)
     .WriteParameterSeparator()
     .WriteStringLiteral(node.Value)
     .WriteParameterSeparator()
     .Write($"global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.{node.AttributeStructure}")
     .WriteEndMethodInvocation();
 }