private void Render(SetTagHelperProperty source, CSharpRenderingContext context) { var tagHelperVariableName = GetTagHelperVariableName(source.TagHelperTypeName); var renderTagHelperContext = context.GetRenderTagHelperContext(); var propertyValueAccessor = GetTagHelperPropertyAccessor(tagHelperVariableName, source.AttributeName, source.AssociatedDescriptor); string previousValueAccessor; if (renderTagHelperContext.RenderedBoundAttributes.TryGetValue(source.AttributeName, out previousValueAccessor)) { context.Writer .WriteStartAssignment(propertyValueAccessor) .Write(previousValueAccessor) .WriteLine(";"); return; } else { renderTagHelperContext.RenderedBoundAttributes[source.AttributeName] = propertyValueAccessor; } if (source.AssociatedDescriptor.IsStringProperty) { context.Render(source.Value.Children); } else { var firstMappedChild = source.Value.Children.FirstOrDefault(child => child is ISourceMapped) as ISourceMapped; var valueStart = firstMappedChild?.DocumentLocation; using (context.Writer.BuildLinePragma(source.DocumentLocation)) using (context.Writer.NoIndent()) { var assignmentPrefixLength = propertyValueAccessor.Length + " = ".Length; if (source.AssociatedDescriptor.IsEnum && source.Value.Children.Count == 1 && source.Value.Children.First() is RenderHtml) { assignmentPrefixLength += $"global::{source.AssociatedDescriptor.TypeName}.".Length; if (valueStart != null) { var padding = Math.Max(valueStart.CharacterIndex - assignmentPrefixLength, 0); var paddingString = _paddingBuilder.BuildPaddingString(padding); context.Writer.Write(paddingString); } context.Writer .WriteStartAssignment(propertyValueAccessor) .Write("global::") .Write(source.AssociatedDescriptor.TypeName) .Write("."); } else { if (valueStart != null) { var padding = Math.Max(valueStart.CharacterIndex - assignmentPrefixLength, 0); var paddingString = _paddingBuilder.BuildPaddingString(padding); context.Writer.Write(paddingString); } context.Writer.WriteStartAssignment(propertyValueAccessor); } RenderTagHelperAttributeInline(source.Value, source.DocumentLocation, context); context.Writer.WriteLine(";"); } } }
private static void Render(SetTagHelperProperty source, CSharpRenderingContext context) { var tagHelperVariableName = GetTagHelperVariableName(source.TagHelperTypeName); var renderTagHelperContext = context.GetRenderTagHelperContext(); // Ensure that the property we're trying to set has initialized its dictionary bound properties. if (source.AssociatedDescriptor.IsIndexer && renderTagHelperContext.VerifiedPropertyDictionaries.Add(source.AssociatedDescriptor.PropertyName)) { // Throw a reasonable Exception at runtime if the dictionary property is null. context.Writer .Write("if (") .Write(tagHelperVariableName) .Write(".") .Write(source.AssociatedDescriptor.PropertyName) .WriteLine(" == null)"); using (context.Writer.BuildScope()) { // System is in Host.NamespaceImports for all MVC scenarios. No need to generate FullName // of InvalidOperationException type. context.Writer .Write("throw ") .WriteStartNewObject(nameof(InvalidOperationException)) .WriteStartMethodInvocation(context.CodeLiterals.GeneratedTagHelperContext.FormatInvalidIndexerAssignmentMethodName) .WriteStringLiteral(source.AttributeName) .WriteParameterSeparator() .WriteStringLiteral(source.TagHelperTypeName) .WriteParameterSeparator() .WriteStringLiteral(source.AssociatedDescriptor.PropertyName) .WriteEndMethodInvocation(endLine: false) // End of method call .WriteEndMethodInvocation(); // End of new expression / throw statement } } var propertyValueAccessor = GetTagHelperPropertyAccessor(tagHelperVariableName, source.AttributeName, source.AssociatedDescriptor); string previousValueAccessor; if (renderTagHelperContext.RenderedBoundAttributes.TryGetValue(source.AttributeName, out previousValueAccessor)) { context.Writer .WriteStartAssignment(propertyValueAccessor) .Write(previousValueAccessor) .WriteLine(";"); return; } else { renderTagHelperContext.RenderedBoundAttributes[source.AttributeName] = propertyValueAccessor; } if (source.AssociatedDescriptor.IsStringProperty) { context.Writer.WriteMethodInvocation(context.CodeLiterals.GeneratedTagHelperContext.BeginWriteTagHelperAttributeMethodName); var renderingConventions = new CSharpLiteralCodeConventions(context); using (context.UseRenderingConventions(renderingConventions)) { context.Render(source.Value.Children); } context.Writer .WriteStartAssignment(StringValueBufferVariableName) .WriteMethodInvocation(context.CodeLiterals.GeneratedTagHelperContext.EndWriteTagHelperAttributeMethodName) .WriteStartAssignment(propertyValueAccessor) .Write(StringValueBufferVariableName) .WriteLine(";"); } else { using (context.Writer.BuildLinePragma(source.DocumentLocation)) { context.Writer.WriteStartAssignment(propertyValueAccessor); if (source.AssociatedDescriptor.IsEnum && source.Value.Children.Count == 1 && source.Value.Children.First() is RenderHtml) { context.Writer .Write("global::") .Write(source.AssociatedDescriptor.TypeName) .Write("."); } RenderTagHelperAttributeInline(source.Value, source.DocumentLocation, context); context.Writer.WriteLine(";"); } } // We need to inform the context of the attribute value. context.Writer .WriteStartInstanceMethodInvocation( ExecutionContextVariableName, context.CodeLiterals.GeneratedTagHelperContext.ExecutionContextAddTagHelperAttributeMethodName) .WriteStringLiteral(source.AttributeName) .WriteParameterSeparator() .Write(propertyValueAccessor) .WriteParameterSeparator() .Write($"global::{typeof(HtmlAttributeValueStyle).FullName}.{source.ValueStyle}") .WriteEndMethodInvocation(); }