Beispiel #1
0
        private static string GetDeclaredModelType(ICSharpSource source)
        {
            var directive = source as IRazorDirective;

            if (string.Equals(directive?.Name, "model", StringComparison.Ordinal))
            {
                var modelType = directive.GetValue(RazorDirectiveTokenType.Type);
                return(modelType);
            }

            var csharpBlock = source as CSharpBlock;

            if (csharpBlock != null)
            {
                for (var i = 0; i < csharpBlock.Children.Count; i++)
                {
                    var child     = csharpBlock.Children[i];
                    var modelType = GetDeclaredModelType(child);

                    if (modelType != null)
                    {
                        return(modelType);
                    }
                }
            }

            return(null);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        public void Add(ICSharpSource source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            CurrentScope.Children.Add(source);
        }
        private void AddDiscoveredDirectives(ICSharpSource source, List <IRazorDirective> directives)
        {
            if (source is IRazorDirective)
            {
                directives.Add((IRazorDirective)source);
            }

            if (source is CSharpBlock)
            {
                var block = (CSharpBlock)source;
                for (var i = 0; i < block.Children.Count; i++)
                {
                    AddDiscoveredDirectives(block.Children[i], directives);
                }
            }
        }
        private void Walk(ICSharpSource source)
        {
            var csharpBlock = source as CSharpBlock;

            if (csharpBlock != null)
            {
                for (var i = 0; i < csharpBlock.Children.Count; i++)
                {
                    Walk(csharpBlock.Children[i]);
                }
            }
            else if (source is SetTagHelperProperty)
            {
                var setProperty = (SetTagHelperProperty)source;
                if (!string.Equals(setProperty.AssociatedDescriptor.TypeName, _attributeLiterals.ModelExpressionTypeName, StringComparison.Ordinal))
                {
                    return;
                }

                var csharpPrefix = new StringBuilder();
                csharpPrefix
                .Append(_attributeLiterals.ModelExpressionProviderPropertyName)
                .Append(".")
                .Append(_attributeLiterals.CreateModelExpressionMethodName)
                .Append("(")
                .Append(_attributeLiterals.ViewDataPropertyName)
                .Append(", __model => ");

                if (setProperty.Value.Children.Count == 1 && setProperty.Value.Children.First() is RenderHtml)
                {
                    // Simple attribute value
                    csharpPrefix.Append("__model.");
                }

                // TODO: Code smell, this is mutating the tree. Should we be replacing entirely?
                var csharpPrefixElement = new CSharpSource {
                    Code = csharpPrefix.ToString()
                };
                setProperty.Value.Children.Insert(0, csharpPrefixElement);

                var csharpSuffixElement = new CSharpSource {
                    Code = ")"
                };
                setProperty.Value.Children.Add(csharpSuffixElement);
            }
        }
            private void Render(ICSharpSource source)
            {
                for (var i = 0; i < _renderers.Count; i++)
                {
                    if (_renderers[i].TryRender(source, _generationContext))
                    {
                        // Successfully rendered the source
                        return;
                    }
                }

                var sourceBlock = source as CSharpBlock;

                if (sourceBlock != null)
                {
                    Render(sourceBlock.Children);
                }
            }
 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);
         }
     }
 }
        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 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);
         }
     }
 }
Beispiel #10
0
 private void WalkSource(ICSharpSource source)
 {
     if (source is RenderTagHelper)
     {
         var renderTagHelper = (RenderTagHelper)source;
         for (var i = 0; i < renderTagHelper.Children.Count; i++)
         {
             var current = renderTagHelper.Children[i];
             if (current is AddTagHelperHtmlAttribute)
             {
                 HandleUnboundAttribute((AddTagHelperHtmlAttribute)current, i, renderTagHelper);
             }
             else if (current is SetTagHelperProperty)
             {
                 HandleBoundAttribute((SetTagHelperProperty)current, i, renderTagHelper);
             }
             else if (current is CSharpBlock)
             {
                 Walk((CSharpBlock)current);
             }
         }
     }
 }
        public bool TryRender(ICSharpSource source, CSharpRenderingContext context)
        {
            // TODO: Need to handle generic directives and render appropriate design time code to color them.

            if (source is CSharpSource)
            {
                Render((CSharpSource)source, context);
            }
            else if (source is RenderExpression)
            {
                Render((RenderExpression)source, context);
            }
            else if (source is RenderTagHelper)
            {
                Render((RenderTagHelper)source, context);
            }
            else if (source is InitializeTagHelperStructure)
            {
                Render((InitializeTagHelperStructure)source, context);
            }
            else if (source is CreateTagHelper)
            {
                Render((CreateTagHelper)source, context);
            }
            else if (source is SetTagHelperProperty)
            {
                Render((SetTagHelperProperty)source, context);
            }
            else if (source is ImportNamespace)
            {
                Render((ImportNamespace)source, context);
            }
            else if (source is RenderConditionalAttribute)
            {
                Render((RenderConditionalAttribute)source, context);
            }
            else if (source is ConditionalAttributePiece)
            {
                Render((ConditionalAttributePiece)source, context);
            }
            else if (source is RenderSection)
            {
                Render((RenderSection)source, context);
            }
            else if (source is RenderStatement)
            {
                Render((RenderStatement)source, context);
            }
            else if (source is ExecuteMethodDeclaration)
            {
                Render((ExecuteMethodDeclaration)source, context);

                // We can't fully render the execute method declaration. Let another CSharpRenderer do it.
                return(false);
            }
            else if (source is DeclareTagHelperFields)
            {
                Render((DeclareTagHelperFields)source, context);
            }
            else if (source is Template)
            {
                Render((Template)source, context);
            }
            else
            {
                return(false);
            }

            return(true);
        }
        public bool TryRender(ICSharpSource source, CSharpRenderingContext context)
        {
            if (source is Checksum)
            {
                Render((Checksum)source, context);
            }
            else if (source is CSharpSource)
            {
                Render((CSharpSource)source, context);
            }
            else if (source is RenderHtml)
            {
                Render((RenderHtml)source, context);
            }
            else if (source is RenderExpression)
            {
                Render((RenderExpression)source, context);
            }
            else if (source is RenderTagHelper)
            {
                Render((RenderTagHelper)source, context);
            }
            else if (source is InitializeTagHelperStructure)
            {
                Render((InitializeTagHelperStructure)source, context);
            }
            else if (source is CreateTagHelper)
            {
                Render((CreateTagHelper)source, context);
            }
            else if (source is AddPreallocatedTagHelperHtmlAttribute)
            {
                Render((AddPreallocatedTagHelperHtmlAttribute)source, context);
            }
            else if (source is AddTagHelperHtmlAttribute)
            {
                Render((AddTagHelperHtmlAttribute)source, context);
            }
            else if (source is SetPreallocatedTagHelperProperty)
            {
                Render((SetPreallocatedTagHelperProperty)source, context);
            }
            else if (source is SetTagHelperProperty)
            {
                Render((SetTagHelperProperty)source, context);
            }
            else if (source is ExecuteTagHelpers)
            {
                Render((ExecuteTagHelpers)source, context);
            }
            else if (source is DeclarePreallocatedTagHelperHtmlAttribute)
            {
                Render((DeclarePreallocatedTagHelperHtmlAttribute)source, context);
            }
            else if (source is DeclarePreallocatedTagHelperAttribute)
            {
                Render((DeclarePreallocatedTagHelperAttribute)source, context);
            }
            else if (source is BeginInstrumentation)
            {
                Render((BeginInstrumentation)source, context);
            }
            else if (source is EndInstrumentation)
            {
                Render((EndInstrumentation)source, context);
            }
            else if (source is ImportNamespace)
            {
                Render((ImportNamespace)source, context);
            }
            else if (source is RenderConditionalAttribute)
            {
                Render((RenderConditionalAttribute)source, context);
            }
            else if (source is LiteralAttributePiece)
            {
                Render((LiteralAttributePiece)source, context);
            }
            else if (source is ConditionalAttributePiece)
            {
                Render((ConditionalAttributePiece)source, context);
            }
            else if (source is RenderSection)
            {
                Render((RenderSection)source, context);
            }
            else if (source is RenderStatement)
            {
                Render((RenderStatement)source, context);
            }
            else if (source is Template)
            {
                Render((Template)source, context);
            }
            else if (source is DeclareTagHelperFields)
            {
                Render((DeclareTagHelperFields)source, context);
            }
            else
            {
                return(false);
            }

            return(true);
        }