public bool TryRender(ICSharpSource source, CSharpRenderingContext context) { if (source is ExecuteMethodDeclaration) { var directives = context.GetDirectives(); foreach (var directive in directives) { if (!string.Equals(directive.Name, "inject", StringComparison.Ordinal)) { continue; } var typeName = directive.GetValue(RazorDirectiveTokenType.Type); var memberName = directive.GetValue(RazorDirectiveTokenType.Member); context.Writer .WriteLine(_injectAttribute) .Write("public global::") .Write(typeName) .Write(" ") .Write(memberName) .WriteLine(" { get; private set; }"); } } return(false); }
private void Render(InitializeTagHelperStructure source, CSharpRenderingContext context) { // Call into the tag helper scope manager to start a new tag helper scope. // Also capture the value as the current execution context. context.Writer .WriteStartAssignment(ExecutionContextVariableName) .WriteStartInstanceMethodInvocation( ScopeManagerVariableName, context.CodeLiterals.GeneratedTagHelperContext.ScopeManagerBeginMethodName); // Assign a unique ID for this instance of the source HTML tag. This must be unique // per call site, e.g. if the tag is on the view twice, there should be two IDs. context.Writer.WriteStringLiteral(source.TagName) .WriteParameterSeparator() .Write("global::") .Write(typeof(TagMode).FullName) .Write(".") .Write(source.TagMode.ToString()) .WriteParameterSeparator() .WriteStringLiteral(GenerateUniqueTagHelperId()) .WriteParameterSeparator(); // We remove and redirect writers so TagHelper authors can retrieve content. var nonRedirectedConventions = new CSharpRenderingConventions(context); using (context.UseRenderingConventions(nonRedirectedConventions)) using (context.Writer.BuildAsyncLambda(endLine: false)) { context.Render(source.Children); } context.Writer.WriteEndMethodInvocation(); }
private void Render(ExecuteMethodDeclaration source, CSharpRenderingContext context) { context.Writer .WriteLine("#pragma warning disable 1998") .Write(source.Accessor) .Write(" "); for (var i = 0; i < source.Modifiers.Count; i++) { context.Writer.Write(source.Modifiers[i]); if (i + 1 < source.Modifiers.Count) { context.Writer.Write(" "); } } context.Writer .Write(" ") .Write(source.ReturnTypeName) .Write(" ") .Write(source.Name) .WriteLine("()"); using (context.Writer.BuildScope()) { context.Render(source.Children); } context.Writer.WriteLine("#pragma warning restore 1998"); }
private static void Render(DeclarePreallocatedTagHelperHtmlAttribute source, CSharpRenderingContext context) { context.Writer .Write("private static readonly global::") .Write(context.CodeLiterals.GeneratedTagHelperContext.TagHelperAttributeTypeName) .Write(" ") .Write(source.VariableName) .Write(" = ") .WriteStartNewObject("global::" + context.CodeLiterals.GeneratedTagHelperContext.TagHelperAttributeTypeName) .WriteStringLiteral(source.Name); if (source.ValueStyle == HtmlAttributeValueStyle.Minimized) { context.Writer.WriteEndMethodInvocation(); } else { context.Writer .WriteParameterSeparator() .WriteStartNewObject("global::" + context.CodeLiterals.GeneratedTagHelperContext.EncodedHtmlStringTypeName) .WriteStringLiteral(source.Value) .WriteEndMethodInvocation(endLine: false) .WriteParameterSeparator() .Write($"global::{typeof(HtmlAttributeValueStyle).FullName}.{source.ValueStyle}") .WriteEndMethodInvocation(); } }
private static void Render(RenderConditionalAttribute source, CSharpRenderingContext context) { var valuePieceCount = source.ValuePieces.Count(piece => piece is LiteralAttributePiece || piece is ConditionalAttributePiece); var prefixLocation = source.DocumentLocation.AbsoluteIndex; var suffixLocation = source.DocumentLocation.AbsoluteIndex + source.DocumentLocation.ContentLength - source.Suffix.Length; var renderingConventions = context.GetRenderingConventions(); renderingConventions .StartBeginWriteAttributeMethod() .WriteStringLiteral(source.Name) .WriteParameterSeparator() .WriteStringLiteral(source.Prefix) .WriteParameterSeparator() .Write(prefixLocation.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator() .WriteStringLiteral(source.Suffix) .WriteParameterSeparator() .Write(suffixLocation.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator() .Write(valuePieceCount.ToString(CultureInfo.InvariantCulture)) .WriteEndMethodInvocation(); context.Render(source.ValuePieces); renderingConventions .StartEndWriteAttributeMethod() .WriteEndMethodInvocation(); }
private static void Render(RenderHtml source, CSharpRenderingContext context) { const int MaxStringLiteralLength = 1024; var charactersConsumed = 0; var renderingConventions = context.GetRenderingConventions(); // Render the string in pieces to avoid Roslyn OOM exceptions at compile time: https://github.com/aspnet/External/issues/54 while (charactersConsumed < source.Html.Length) { string textToRender; if (source.Html.Length <= MaxStringLiteralLength) { textToRender = source.Html; } else { var charactersToSubstring = Math.Min(MaxStringLiteralLength, source.Html.Length - charactersConsumed); textToRender = source.Html.Substring(charactersConsumed, charactersToSubstring); } renderingConventions .StartWriteLiteralMethod() .WriteStringLiteral(textToRender) .WriteEndMethodInvocation(); charactersConsumed += textToRender.Length; } }
private void Render(RenderExpression source, CSharpRenderingContext context) { if (source.Expression.Children.Count == 0) { return; } if (source.DocumentLocation != null) { using (context.Writer.BuildLinePragma(source.DocumentLocation)) using (context.Writer.NoIndent()) { var paddingString = BuildAssignmentOffsetPadding(source.Padding); context.Writer .Write(paddingString) .WriteStartAssignment(DesignTimeVariable); using (context.Writer.BuildCodeMapping(source.DocumentLocation)) { context.Render(source.Expression.Children); } context.Writer.WriteLine(";"); } } else { context.Writer.WriteStartAssignment(DesignTimeVariable); context.Render(source.Expression.Children); context.Writer.WriteLine(";"); } }
private void Render(RenderTagHelper source, CSharpRenderingContext context) { var renderTagHelperContext = new RenderTagHelperContext(); using (context.UseRenderTagHelperContext(renderTagHelperContext)) { context.Render(source.Children); } }
private static void Render(AddPreallocatedTagHelperHtmlAttribute source, CSharpRenderingContext context) { context.Writer .WriteStartInstanceMethodInvocation( ExecutionContextVariableName, context.CodeLiterals.GeneratedTagHelperContext.ExecutionContextAddHtmlAttributeMethodName) .Write(source.AttributeVariableName) .WriteEndMethodInvocation(); }
private static void Render(BeginInstrumentation source, CSharpRenderingContext context) { context.Writer .WriteStartMethodInvocation(context.CodeLiterals.BeginContextMethodName) .Write(source.DocumentLocation.AbsoluteIndex.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator() .Write(source.DocumentLocation.ContentLength.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator() .Write(source.Literal ? "true" : "false") .WriteEndMethodInvocation(); }
private void Render(CreateTagHelper source, CSharpRenderingContext context) { var tagHelperVariableName = GetTagHelperVariableName(source.TagHelperTypeName); // Create the tag helper context.Writer .WriteStartAssignment(tagHelperVariableName) .WriteStartMethodInvocation( context.CodeLiterals.GeneratedTagHelperContext.CreateTagHelperMethodName, "global::" + source.TagHelperTypeName) .WriteEndMethodInvocation(); }
private void Render(NamespaceDeclaration source, CSharpRenderingContext context) { context.Writer .Write("namespace ") .WriteLine(source.Namespace); using (context.Writer.BuildScope()) { context.Writer.WriteLineHiddenDirective(); context.Render(source.Children); } }
public static CSharpRenderingConventions GetRenderingConventions(this CSharpRenderingContext context) { var conventions = context.Items[typeof(CSharpRenderingConventions)] as CSharpRenderingConventions; if (conventions == null) { conventions = new CSharpRenderingConventions(context); SetRenderingConventions(context, conventions); } return(conventions); }
private void Render(DeclareTagHelperFields source, CSharpRenderingContext context) { foreach (var tagHelperTypeName in source.UsedTagHelperTypeNames) { var tagHelperVariableName = GetTagHelperVariableName(tagHelperTypeName); context.Writer .Write("private global::") .WriteVariableDeclaration( tagHelperTypeName, tagHelperVariableName, value: null); } }
private static void Render(ConditionalAttributePiece source, CSharpRenderingContext context) { const string ValueWriterName = "__razor_attribute_value_writer"; var expressionValue = source.Value.Children.First() as RenderExpression; var linePragma = expressionValue != null?context.Writer.BuildLinePragma(source.DocumentLocation) : null; var prefixLocation = source.DocumentLocation.AbsoluteIndex; var valueLocation = source.DocumentLocation.AbsoluteIndex + source.Prefix.Length; var valueLength = source.DocumentLocation.ContentLength - source.Prefix.Length; var renderingConventions = context.GetRenderingConventions(); renderingConventions .StartWriteAttributeValueMethod() .WriteStringLiteral(source.Prefix) .WriteParameterSeparator() .Write(prefixLocation.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator(); if (expressionValue != null) { Debug.Assert(source.Value.Children.Count == 1); RenderExpressionInline(expressionValue.Expression, context); } else { // Not an expression; need to buffer the result. context.Writer.WriteStartNewObject(context.CodeLiterals.TemplateTypeName); var redirectConventions = new CSharpRedirectRenderingConventions(ValueWriterName, context); using (context.UseRenderingConventions(redirectConventions)) using (context.Writer.BuildAsyncLambda(endLine: false, parameterNames: ValueWriterName)) { context.Render(source.Value.Children); } context.Writer.WriteEndMethodInvocation(false); } context.Writer .WriteParameterSeparator() .Write(valueLocation.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator() .Write(valueLength.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator() .WriteBooleanLiteral(false) .WriteEndMethodInvocation(); linePragma?.Dispose(); }
public static IDisposable UseRenderTagHelperContext( this CSharpRenderingContext context, RenderTagHelperContext renderTagHelperContext) { var initialContext = context.GetRenderTagHelperContext(); var scope = new ActionScope(() => { context.SetRenderTagHelperContext(initialContext); }); context.SetRenderTagHelperContext(renderTagHelperContext); return(scope); }
private static void Render(Checksum source, CSharpRenderingContext context) { if (!string.IsNullOrEmpty(source.Bytes)) { context.Writer .Write("#pragma checksum \"") .Write(source.FileName) .Write("\" \"") .Write(source.Guid) .Write("\" \"") .Write(source.Bytes) .WriteLine("\""); } }
public static IDisposable UseRenderingConventions( this CSharpRenderingContext context, CSharpRenderingConventions conventions) { var initialConventions = context.GetRenderingConventions(); var scope = new ActionScope(() => { context.SetRenderingConventions(initialConventions); }); context.SetRenderingConventions(conventions); return(scope); }
private void Render(ViewClassDeclaration source, CSharpRenderingContext context) { context.Writer .Write(source.Accessor) .Write(" class ") .Write(source.Name); if (source.BaseTypeName != null || source.ImplementedInterfaceNames != null) { context.Writer.Write(" : "); } if (source.BaseTypeName != null) { context.Writer.Write(source.BaseTypeName); if (source.ImplementedInterfaceNames != null) { context.Writer.WriteParameterSeparator(); } } if (source.ImplementedInterfaceNames != null) { for (var i = 0; i < source.ImplementedInterfaceNames.Count; i++) { context.Writer.Write(source.ImplementedInterfaceNames[i]); if (i + 1 < source.ImplementedInterfaceNames.Count) { context.Writer.WriteParameterSeparator(); } } } context.Writer.WriteLine(); using (context.Writer.BuildScope()) { context.Writer .Write("private static object @") .Write(DesignTimeVariable) .WriteLine(";"); context.Render(source.Children); } }
private static void Render(RenderExpression source, CSharpRenderingContext context) { IDisposable linePragmaScope = null; if (source.DocumentLocation != null) { linePragmaScope = context.Writer.BuildLinePragma(source.DocumentLocation); } var renderingConventions = context.GetRenderingConventions(); renderingConventions.StartWriteMethod(); context.Render(source.Expression.Children); context.Writer.WriteEndMethodInvocation(); linePragmaScope?.Dispose(); }
private static void Render(SetPreallocatedTagHelperProperty source, CSharpRenderingContext context) { var tagHelperVariableName = GetTagHelperVariableName(source.TagHelperTypeName); var propertyValueAccessor = GetTagHelperPropertyAccessor(tagHelperVariableName, source.AttributeName, source.AssociatedDescriptor); var attributeValueAccessor = $"{source.AttributeVariableName}.{context.CodeLiterals.GeneratedTagHelperContext.TagHelperAttributeValuePropertyName}"; context.Writer .WriteStartAssignment(propertyValueAccessor) .Write("(string)") .Write(attributeValueAccessor) .WriteLine(";") .WriteStartInstanceMethodInvocation( ExecutionContextVariableName, context.CodeLiterals.GeneratedTagHelperContext.ExecutionContextAddTagHelperAttributeMethodName) .Write(source.AttributeVariableName) .WriteEndMethodInvocation(); }
private static void Render(RenderStatement source, CSharpRenderingContext context) { if (string.IsNullOrWhiteSpace(source.Code)) { return; } if (source.DocumentLocation != null) { using (context.Writer.BuildLinePragma(source.DocumentLocation)) { context.Writer.WriteLine(source.Code); } } else { context.Writer.WriteLine(source.Code); } }
private static void RenderExpressionInline(ICSharpSource expression, CSharpRenderingContext context) { if (expression is CSharpSource) { context.Writer.Write(((CSharpSource)expression).Code); } else if (expression is RenderExpression) { RenderExpressionInline(((RenderExpression)expression).Expression, context); } else if (expression is CSharpBlock) { var expressionBlock = (CSharpBlock)expression; for (var i = 0; i < expressionBlock.Children.Count; i++) { RenderExpressionInline(expressionBlock.Children[i], context); } } }
private void Render(Template source, CSharpRenderingContext context) { const string ItemParameterName = "item"; const string TemplateWriterName = "__razor_template_writer"; context.Writer .Write(ItemParameterName).Write(" => ") .WriteStartNewObject(context.CodeLiterals.TemplateTypeName); var redirectConventions = new CSharpRedirectRenderingConventions(TemplateWriterName, context); using (context.UseRenderingConventions(redirectConventions)) using (context.Writer.BuildAsyncLambda(endLine: false, parameterNames: TemplateWriterName)) { context.Render(source.Children); } context.Writer.WriteEndMethodInvocation(); }
private void Render(RenderSection source, CSharpRenderingContext context) { const string SectionWriterName = "__razor_section_writer"; context.Writer .WriteStartMethodInvocation(context.CodeLiterals.DefineSectionMethodName) .WriteStringLiteral(source.Name) .WriteParameterSeparator(); var redirectConventions = new CSharpRedirectRenderingConventions(SectionWriterName, context); using (context.UseRenderingConventions(redirectConventions)) using (context.Writer.BuildAsyncLambda(endLine: false, parameterNames: SectionWriterName)) { context.Render(source.Children); } context.Writer.WriteEndMethodInvocation(); }
public bool TryRender(ICSharpSource source, CSharpRenderingContext context) { if (source is NamespaceDeclaration) { Render((NamespaceDeclaration)source, context); } else if (source is ExecuteMethodDeclaration) { Render((ExecuteMethodDeclaration)source, context); } else if (source is ViewClassDeclaration) { Render((ViewClassDeclaration)source, context); } else { return(false); } return(true); }
private static void Render(LiteralAttributePiece source, CSharpRenderingContext context) { var prefixLocation = source.DocumentLocation.AbsoluteIndex; var valueLocation = source.DocumentLocation.AbsoluteIndex + source.Prefix.Length; var valueLength = source.DocumentLocation.ContentLength - source.Prefix.Length; var renderingConventions = context.GetRenderingConventions(); renderingConventions .StartWriteAttributeValueMethod() .WriteStringLiteral(source.Prefix) .WriteParameterSeparator() .Write(prefixLocation.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator() .WriteStringLiteral(source.Value) .WriteParameterSeparator() .Write(valueLocation.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator() .Write(valueLength.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator() .WriteBooleanLiteral(true) .WriteEndMethodInvocation(); }
private static void Render(ExecuteTagHelpers source, CSharpRenderingContext context) { context.Writer .Write("await ") .WriteStartInstanceMethodInvocation(RunnerVariableName, context.CodeLiterals.GeneratedTagHelperContext.RunnerRunAsyncMethodName) .Write(ExecutionContextVariableName) .WriteEndMethodInvocation(); var tagHelperOutputAccessor = $"{ExecutionContextVariableName}.{context.CodeLiterals.GeneratedTagHelperContext.ExecutionContextOutputPropertyName}"; context.Writer .Write("if (!") .Write(tagHelperOutputAccessor) .Write(".") .Write(context.CodeLiterals.GeneratedTagHelperContext.TagHelperOutputIsContentModifiedPropertyName) .WriteLine(")"); using (context.Writer.BuildScope()) { context.Writer .Write("await ") .WriteInstanceMethodInvocation( ExecutionContextVariableName, context.CodeLiterals.GeneratedTagHelperContext.ExecutionContextSetOutputContentAsyncMethodName); } var renderingConventions = context.GetRenderingConventions(); renderingConventions .StartWriteMethod() .Write(tagHelperOutputAccessor) .WriteEndMethodInvocation() .WriteStartAssignment(ExecutionContextVariableName) .WriteInstanceMethodInvocation( ScopeManagerVariableName, context.CodeLiterals.GeneratedTagHelperContext.ScopeManagerEndMethodName); }
private static void RenderTagHelperAttributeInline( ICSharpSource attributeValue, MappingLocation documentLocation, CSharpRenderingContext context) { if (attributeValue is CSharpSource) { context.Writer.Write(((CSharpSource)attributeValue).Code); } else if (attributeValue is RenderHtml) { context.Writer.Write(((RenderHtml)attributeValue).Html); } else if (attributeValue is RenderExpression) { RenderTagHelperAttributeInline(((RenderExpression)attributeValue).Expression, documentLocation, context); } else if (attributeValue is RenderStatement) { context.ErrorSink.OnError( documentLocation, "TODO: RazorResources.TagHelpers_CodeBlocks_NotSupported_InAttributes"); } else if (attributeValue is Template) { context.ErrorSink.OnError( documentLocation, "TODO: RazorResources.FormatTagHelpers_InlineMarkupBlocks_NotSupported_InAttributes(_attributeTypeName)"); } else if (attributeValue is CSharpBlock) { var expressionBlock = (CSharpBlock)attributeValue; for (var i = 0; i < expressionBlock.Children.Count; i++) { RenderTagHelperAttributeInline(expressionBlock.Children[i], documentLocation, context); } } }
private void Render(ExecuteMethodDeclaration source, CSharpRenderingContext context) { const string DesignTimeHelperMethodName = "__RazorDesignTimeHelpers__"; const int DisableVariableNamingWarnings = 219; context.Writer .Write("private static object @") .Write(DesignTimeVariable) .WriteLine(";"); using (context.Writer.BuildMethodDeclaration("private", "void", "@" + DesignTimeHelperMethodName)) { using (context.Writer.BuildDisableWarningScope(DisableVariableNamingWarnings)) { context.Writer.WriteVariableDeclaration(typeof(Action).FullName, ActionHelper, value: null); var directives = context.GetDirectives(); foreach (var directive in directives) { Render(directive, context); } } } }