public void WriteTagHelperHtmlAttribute_RendersCorrectly()
    {
        // Arrange
        var extension = new PreallocatedAttributeTargetExtension();
        var context   = TestCodeRenderingContext.CreateRuntime();

        var tagHelperNode = new TagHelperIntermediateNode();
        var node          = new PreallocatedTagHelperHtmlAttributeIntermediateNode()
        {
            VariableName = "_tagHelper1"
        };

        tagHelperNode.Children.Add(node);
        Push(context, tagHelperNode);

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

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

        Assert.Equal(
            @"__tagHelperExecutionContext.AddHtmlAttribute(_tagHelper1);
",
            csharp,
            ignoreLineEndingDifferences: true);
    }
    public void WriteTagHelperHtmlAttribute(CodeRenderingContext context, PreallocatedTagHelperHtmlAttributeIntermediateNode node)
    {
        if (context.Parent as TagHelperIntermediateNode == null)
        {
            var message = Resources.FormatIntermediateNodes_InvalidParentNode(node.GetType(), typeof(TagHelperIntermediateNode));
            throw new InvalidOperationException(message);
        }

        context.CodeWriter
        .WriteStartInstanceMethodInvocation(ExecutionContextVariableName, ExecutionContextAddHtmlAttributeMethodName)
        .Write(node.VariableName)
        .WriteEndMethodInvocation();
    }
Ejemplo n.º 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;
        }