/// <summary> /// Called during Razor's code generation process to generate code that instantiates the value of the tag /// helper's property. Last value written should not be or end with a semicolon. /// </summary> /// <param name="attributeDescriptor"> /// The <see cref="TagHelperAttributeDescriptor"/> to generate code for. /// </param> /// <param name="writer">The <see cref="CSharpCodeWriter"/> that's used to write code.</param> /// <param name="context">A <see cref="Chunks.Generators.ChunkGeneratorContext"/> instance that contains /// information about the current code generation process.</param> /// <param name="renderAttributeValue"> /// <see cref="Action"/> that renders the raw value of the HTML attribute. /// </param> /// <param name="complexValue"> /// Indicates whether or not the source attribute value contains more than simple text. <c>false</c> for plain /// C# expressions e.g. <c>"PropertyName"</c>. <c>true</c> if the attribute value contain at least one in-line /// Razor construct e.g. <c>"@(@readonly)"</c>. /// </param> public virtual void RenderAttributeValue( TagHelperAttributeDescriptor attributeDescriptor, CSharpCodeWriter writer, CodeGeneratorContext context, Action<CSharpCodeWriter> renderAttributeValue, bool complexValue) { if (attributeDescriptor == null) { throw new ArgumentNullException(nameof(attributeDescriptor)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } if (renderAttributeValue == null) { throw new ArgumentNullException(nameof(renderAttributeValue)); } renderAttributeValue(writer); }
/// <inheritdoc /> /// <remarks>If the attribute being rendered is of the type /// <see cref="GeneratedTagHelperAttributeContext.ModelExpressionTypeName"/>, then a model expression will be /// created by calling into <see cref="GeneratedTagHelperAttributeContext.CreateModelExpressionMethodName"/>. /// </remarks> public override void RenderAttributeValue( TagHelperAttributeDescriptor attributeDescriptor, CSharpCodeWriter writer, CodeGeneratorContext codeGeneratorContext, Action<CSharpCodeWriter> renderAttributeValue, bool complexValue) { if (attributeDescriptor == null) { throw new ArgumentNullException(nameof(attributeDescriptor)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (codeGeneratorContext == null) { throw new ArgumentNullException(nameof(codeGeneratorContext)); } if (renderAttributeValue == null) { throw new ArgumentNullException(nameof(renderAttributeValue)); } if (attributeDescriptor.TypeName.Equals(_context.ModelExpressionTypeName, StringComparison.Ordinal)) { writer .WriteStartInstanceMethodInvocation(_context.ModelExpressionProviderPropertyName, _context.CreateModelExpressionMethodName) .Write(_context.ViewDataPropertyName) .WriteParameterSeparator() .Write(ModelLambdaVariableName) .Write(" => "); if (!complexValue) { writer .Write(ModelLambdaVariableName) .Write("."); } renderAttributeValue(writer); writer.WriteEndMethodInvocation(endLine: false); } else { base.RenderAttributeValue( attributeDescriptor, writer, codeGeneratorContext, renderAttributeValue, complexValue); } }
public void RenderAttributeValue_RendersModelExpressionsCorrectly( string modelExpressionType, string propertyType, string expectedValue) { // Arrange var renderer = new MvcTagHelperAttributeValueCodeRenderer( new GeneratedTagHelperAttributeContext { ModelExpressionTypeName = modelExpressionType, CreateModelExpressionMethodName = "SomeMethod", ModelExpressionProviderPropertyName = "Provider", ViewDataPropertyName = "ViewData" }); var attributeDescriptor = new TagHelperAttributeDescriptor { Name = "MyAttribute", PropertyName = "SomeProperty", TypeName = propertyType, }; var writer = new CSharpCodeWriter(); var generatorContext = new ChunkGeneratorContext( host: null, className: string.Empty, rootNamespace: string.Empty, sourceFile: string.Empty, shouldGenerateLinePragmas: true); var errorSink = new ErrorSink(); var context = new CodeGeneratorContext(generatorContext, errorSink); // Act renderer.RenderAttributeValue(attributeDescriptor, writer, context, (codeWriter) => { codeWriter.Write("MyValue"); }, complexValue: false); // Assert Assert.Equal(expectedValue, writer.GenerateCode()); }
private void RenderAttributeValue(TagHelperAttributeDescriptor attributeDescriptor, Action<CSharpCodeWriter> valueRenderer, bool complexValue) { AttributeValueCodeRenderer.RenderAttributeValue( attributeDescriptor, _writer, _context, valueRenderer, complexValue); }
private void RenderQuotedAttributeValue(string value, TagHelperAttributeDescriptor attributeDescriptor) { RenderAttributeValue( attributeDescriptor, valueRenderer: (writer) => { writer.WriteStringLiteral(value); }, complexValue: false); }
private void RenderCodeAttributeValue( Chunk attributeValueChunk, TagHelperAttributeDescriptor attributeDescriptor, bool isPlainTextValue) { RenderAttributeValue( attributeDescriptor, valueRenderer: (writer) => { if (attributeDescriptor.IsEnum && isPlainTextValue) { writer .Write("global::") .Write(attributeDescriptor.TypeName) .Write("."); } var visitor = new CSharpTagHelperAttributeValueVisitor(writer, _context, attributeDescriptor.TypeName); visitor.Accept(attributeValueChunk); }, complexValue: !isPlainTextValue); }
private void RenderBufferedAttributeValue(TagHelperAttributeDescriptor attributeDescriptor) { // Pass complexValue: false because variable.ToString() replaces any original complexity in the expression. RenderAttributeValue( attributeDescriptor, valueRenderer: (writer) => { RenderBufferedAttributeValueAccessor(writer); }, complexValue: false); }
// Render assignment of attribute value to the value accessor. private void RenderNewAttributeValueAssignment( TagHelperAttributeDescriptor attributeDescriptor, bool bufferableAttribute, Chunk attributeValueChunk, string valueAccessor) { // Plain text values are non Razor code (@DateTime.Now) values. If an attribute is bufferable it // may be more than just a plain text value, it may also contain Razor code which is why we attempt // to retrieve a plain text value here. string textValue; var isPlainTextValue = TryGetPlainTextValue(attributeValueChunk, out textValue); if (bufferableAttribute) { if (!isPlainTextValue) { // If we haven't recorded a value and we need to buffer an attribute value and the value is not // plain text then we need to prepare the value prior to setting it below. BuildBufferedWritingScope(attributeValueChunk, htmlEncodeValues: false); } _writer.WriteStartAssignment(valueAccessor); if (isPlainTextValue) { // If the attribute is bufferable but has a plain text value that means the value // is a string which needs to be surrounded in quotes. RenderQuotedAttributeValue(textValue, attributeDescriptor); } else { // The value contains more than plain text e.g. stringAttribute ="Time: @DateTime.Now". RenderBufferedAttributeValue(attributeDescriptor); } _writer.WriteLine(";"); } else { // Write out simple assignment for non-string property value. Try to keep the whole // statement together and the #line pragma correct to make debugging possible. using (var lineMapper = new CSharpLineMappingWriter( _writer, attributeValueChunk.Start, _context.SourceFile)) { // Place the assignment LHS to align RHS with original attribute value's indentation. // Unfortunately originalIndent is incorrect if original line contains tabs. Unable to // use a CSharpPaddingBuilder because the Association has no Previous node; lost the // original Span sequence when the parse tree was rewritten. var originalIndent = attributeValueChunk.Start.CharacterIndex; var generatedLength = valueAccessor.Length + " = ".Length; var newIndent = originalIndent - generatedLength; if (newIndent > 0) { _writer.Indent(newIndent); } _writer.WriteStartAssignment(valueAccessor); lineMapper.MarkLineMappingStart(); // Write out code expression for this attribute value. Property is not a string. // So quoting or buffering are not helpful. RenderCodeAttributeValue(attributeValueChunk, attributeDescriptor, isPlainTextValue); // End the assignment to the attribute. lineMapper.MarkLineMappingEnd(); _writer.WriteLine(";"); } } }
private string RenderBoundAttribute( TagHelperAttributeTracker attribute, string tagHelperVariableName, string previousValueAccessor, TagHelperAttributeDescriptor attributeDescriptor) { var currentValueAccessor = string.Format( CultureInfo.InvariantCulture, "{0}.{1}", tagHelperVariableName, attributeDescriptor.PropertyName); if (attributeDescriptor.IsIndexer) { var dictionaryKey = attribute.Name.Substring(attributeDescriptor.Name.Length); currentValueAccessor += $"[\"{dictionaryKey}\"]"; } // If this attribute value has not been seen before, need to record its value. if (previousValueAccessor == null) { var preallocatedAttributeValueChunk = attribute.Value as PreallocatedTagHelperAttributeChunk; if (preallocatedAttributeValueChunk != null) { RenderBoundPreAllocatedAttribute(preallocatedAttributeValueChunk, currentValueAccessor); return currentValueAccessor; } // Bufferable attributes are attributes that can have Razor code inside of them. Such // attributes have string values and may be calculated using a temporary TextWriter or other // buffer. var bufferableAttribute = attributeDescriptor.IsStringProperty; RenderNewAttributeValueAssignment( attributeDescriptor, bufferableAttribute, attribute.Value, currentValueAccessor); if (_designTimeMode) { // Execution contexts are a runtime feature. return currentValueAccessor; } // We need to inform the context of the attribute value. _writer .WriteStartInstanceMethodInvocation( ExecutionContextVariableName, _tagHelperContext.ExecutionContextAddTagHelperAttributeMethodName) .WriteStringLiteral(attribute.Name) .WriteParameterSeparator() .Write(currentValueAccessor) .WriteParameterSeparator() .Write($"global::{typeof(HtmlAttributeValueStyle).FullName}.{attribute.ValueStyle}") .WriteEndMethodInvocation(); return currentValueAccessor; } else { // The attribute value has already been determined and accessor was passed to us as // previousValueAccessor, we don't want to evaluate the value twice so lets just use the // previousValueLocation. _writer .WriteStartAssignment(currentValueAccessor) .Write(previousValueAccessor) .WriteLine(";"); return previousValueAccessor; } }