protected override Expression VisitHelperExpression(HelperExpression hex)
        {
            if (CompilationContext.Configuration.Helpers.ContainsKey(hex.HelperName))
            {
                var helper    = CompilationContext.Configuration.Helpers[hex.HelperName];
                var arguments = new Expression[]
                {
                    Expression.Property(
                        CompilationContext.BindingContext,
#if netstandard
                        typeof(BindingContext).GetRuntimeProperty("TextWriter")),
#else
                        typeof(BindingContext).GetProperty("TextWriter")),
#endif
                    Expression.Property(
                        CompilationContext.BindingContext,
#if netstandard
                        typeof(BindingContext).GetRuntimeProperty("Value")),
#else
                        typeof(BindingContext).GetProperty("Value")),
#endif
                    Expression.NewArrayInit(typeof(object), hex.Arguments.Select(a => Visit(a)))
                };
                if (helper.Target != null)
                {
                    return(Expression.Call(
                               Expression.Constant(helper.Target),
#if netstandard
                               helper.GetMethodInfo(),
#else
                               helper.Method,
#endif
                               arguments));
                }
                else
                {
                    return(Expression.Call(
#if netstandard
                               helper.GetMethodInfo(),
#else
                               helper.Method,
#endif
                               arguments));
                }
            }
            else
            {
                return(Expression.Call(
                           Expression.Constant(this),
#if netstandard
                           new Action <BindingContext, string, IEnumerable <object> >(LateBindHelperExpression).GetMethodInfo(),
#else
                           new Action <BindingContext, string, IEnumerable <object> >(LateBindHelperExpression).Method,
#endif
                           CompilationContext.BindingContext,
                           Expression.Constant(hex.HelperName),
                           Expression.NewArrayInit(typeof(object), hex.Arguments)));
            }
        }
        protected override Expression VisitHelperExpression(HelperExpression hex)
        {
            if (CompilationContext.Configuration.Helpers.ContainsKey(hex.HelperName))
            {
                var helper = CompilationContext.Configuration.Helpers[hex.HelperName];
                var arguments = new Expression[]
                {
                    Expression.Property(
                        CompilationContext.BindingContext,
#if netstandard
                        typeof(BindingContext).GetRuntimeProperty("TextWriter")),
#else
                        typeof(BindingContext).GetProperty("TextWriter")),
#endif
                    Expression.Property(
                        CompilationContext.BindingContext,
#if netstandard
                        typeof(BindingContext).GetRuntimeProperty("Value")),
#else
                        typeof(BindingContext).GetProperty("Value")),
#endif
                    Expression.NewArrayInit(typeof(object), hex.Arguments.Select(a => Visit(a)))
                };
                if (helper.Target != null)
                {
                    return Expression.Call(
                        Expression.Constant(helper.Target),
#if netstandard
                        helper.GetMethodInfo(),
#else
                        helper.Method,
#endif
                        arguments);
                }
                else
                {
                    return Expression.Call(
#if netstandard
                        helper.GetMethodInfo(),
#else
                        helper.Method,
#endif
                        arguments);
                }
            }
            else
            {
                return Expression.Call(
                    Expression.Constant(this),
#if netstandard
                    new Action<BindingContext, string, IEnumerable<object>>(LateBindHelperExpression).GetMethodInfo(),
#else
                    new Action<BindingContext, string, IEnumerable<object>>(LateBindHelperExpression).Method,
#endif
                    CompilationContext.BindingContext,
                    Expression.Constant(hex.HelperName),
                    Expression.NewArrayInit(typeof(object), hex.Arguments));
            }
        }
Esempio n. 3
0
        private static bool IsBlockHelperOrInversion(HelperExpression helperExpression)
        {
            if (helperExpression == null)
            {
                return(false);
            }

            return(helperExpression.HelperName.StartsWith("#") || helperExpression.HelperName.StartsWith("^") || (helperExpression.HelperName == "else"));
        }
Esempio n. 4
0
 private static bool IsPartialBlock(Expression item)
 {
     item = UnwrapStatement(item);
     return(item switch
     {
         PathExpression expression => expression.Path.StartsWith("#>"),
         HelperExpression helperExpression => helperExpression.HelperName.StartsWith("#>"),
         _ => false,
     });
        protected virtual Expression VisitHelperExpression(HelperExpression hex)
        {
            var arguments = VisitExpressionList(hex.Arguments);

            if (arguments != hex.Arguments)
            {
                return(HandlebarsExpression.Helper(hex.HelperName, hex.IsBlock, arguments, hex.IsRaw));
            }
            return(hex);
        }
 public BlockHelperAccumulatorContext(Expression startingNode)
     : base(startingNode)
 {
     if (startingNode is StatementExpression statementExpression)
     {
         _trimBefore = statementExpression.TrimBefore;
         _trimAfter  = statementExpression.TrimAfter;
     }
     startingNode  = UnwrapStatement(startingNode);
     _startingNode = (HelperExpression)startingNode;
 }
 private static Expression CreateIfBlock(HelperExpression startingNode, IEnumerable<Expression> body)
 {
     var condition = HandlebarsExpression.Boolish(startingNode.Arguments.Single());
     body = UnwrapBlockExpression(body);
     body = body.Concat(new Expression[] { Expression.Empty() });
     if (startingNode.HelperName == "#if")
     {
         return Expression.IfThen(condition, Expression.Block(body));
     }
     else if (startingNode.HelperName == "#unless")
     {
         return Expression.IfThen(Expression.Not(condition), Expression.Block(body));
     }
     else
     {
         throw new HandlebarsCompilerException(string.Format(
                 "Tried to create a conditional expression for '{0}'", startingNode.HelperName));
     }
 }
        private static Expression CreateIfBlock(HelperExpression startingNode, IEnumerable <Expression> body)
        {
            var condition = HandlebarsExpression.Boolish(startingNode.Arguments.Single());

            body = UnwrapBlockExpression(body);
            body = body.Concat(new Expression[] { Expression.Empty() });
            if (startingNode.HelperName == "#if")
            {
                return(Expression.IfThen(condition, Expression.Block(body)));
            }
            else if (startingNode.HelperName == "#unless")
            {
                return(Expression.IfThen(Expression.Not(condition), Expression.Block(body)));
            }
            else
            {
                throw new HandlebarsCompilerException(string.Format(
                                                          "Tried to create a conditional expression for '{0}'", startingNode.HelperName));
            }
        }
Esempio n. 9
0
        protected override Expression VisitHelperExpression(HelperExpression hex)
        {
            var pathInfo = PathInfoStore.Shared.GetOrAdd(hex.HelperName);

            if (!pathInfo.IsValidHelperLiteral && !CompilationContext.Configuration.Compatibility.RelaxedHelperNaming)
            {
                return(Expression.Empty());
            }

            var helperName     = pathInfo.TrimmedPath;
            var bindingContext = CompilationContext.Args.BindingContext;
            var options        = New(() => new HelperOptions(bindingContext));
            var textWriter     = CompilationContext.Args.EncodedWriter;

            var contextValue = New(() => new Context(bindingContext));
            var args         = FunctionBinderHelpers.CreateArguments(hex.Arguments, CompilationContext);

            var configuration = CompilationContext.Configuration;

            if (configuration.Helpers.TryGetValue(pathInfo, out var helper))
            {
                return(Call(() => helper.Value.Invoke(textWriter, options, contextValue, args)));
            }

            for (var index = 0; index < configuration.HelperResolvers.Count; index++)
            {
                var resolver = configuration.HelperResolvers[index];
                if (resolver.TryResolveHelper(helperName, typeof(object), out var resolvedHelper))
                {
                    helper = new Ref <IHelperDescriptor <HelperOptions> >(resolvedHelper);
                    configuration.Helpers.AddOrReplace(pathInfo, helper);
                    return(Call(() => resolvedHelper.Invoke(textWriter, options, contextValue, args)));
                }
            }

            var lateBindDescriptor = new Ref <IHelperDescriptor <HelperOptions> >(new LateBindHelperDescriptor(pathInfo));

            configuration.Helpers.AddOrReplace(pathInfo, lateBindDescriptor);

            return(Call(() => lateBindDescriptor.Value.Invoke(textWriter, options, contextValue, args)));
        }
Esempio n. 10
0
        protected override Expression VisitHelperExpression(HelperExpression hex)
        {
            var pathInfo = CompilationContext.Configuration.PathInfoStore.GetOrAdd(hex.HelperName);

            if (!pathInfo.IsValidHelperLiteral && !CompilationContext.Configuration.Compatibility.RelaxedHelperNaming)
            {
                return(Expression.Empty());
            }

            var helperName     = pathInfo.TrimmedPath;
            var bindingContext = Arg <BindingContext>(CompilationContext.BindingContext);
            var contextValue   = bindingContext.Property(o => o.Value);
            var textWriter     = bindingContext.Property(o => o.TextWriter);
            var arguments      = hex.Arguments
                                 .ApplyOn <Expression, PathExpression>(path => path.Context = PathExpression.ResolutionContext.Parameter)
                                 .Select(o => FunctionBuilder.Reduce(o, CompilationContext));

            var args = Array <object>(arguments);

            var configuration = CompilationContext.Configuration;

            if (configuration.Helpers.TryGetValue(pathInfo, out var helper))
            {
                return(Call(() => helper.Value.WriteInvoke(bindingContext, textWriter, contextValue, args)));
            }

            for (var index = 0; index < configuration.HelperResolvers.Count; index++)
            {
                var resolver = configuration.HelperResolvers[index];
                if (resolver.TryResolveHelper(helperName, typeof(object), out var resolvedHelper))
                {
                    return(Call(() => resolvedHelper.WriteInvoke(bindingContext, textWriter, contextValue, args)));
                }
            }

            var lateBindDescriptor = new StrongBox <HelperDescriptorBase>(new LateBindHelperDescriptor(pathInfo, configuration));

            configuration.Helpers.Add(pathInfo, lateBindDescriptor);

            return(Call(() => lateBindDescriptor.Value.WriteInvoke(bindingContext, textWriter, contextValue, args)));
        }
        protected override Expression VisitHelperExpression(HelperExpression hex)
        {
            var helperName     = hex.HelperName;
            var bindingContext = ExpressionShortcuts.Arg <BindingContext>(CompilationContext.BindingContext);
            var textWriter     = bindingContext.Property(o => o.TextWriter);
            var arguments      = hex.Arguments.Select(o => FunctionBuilder.Reduce(o, CompilationContext));
            var args           = ExpressionShortcuts.Array <object>(arguments);

            var configuration = CompilationContext.Configuration;

            if (configuration.Helpers.TryGetValue(helperName, out var helper))
            {
                return(ExpressionShortcuts.Call(() => helper(textWriter, bindingContext, args)));
            }

            if (configuration.ReturnHelpers.TryGetValue(helperName, out var returnHelper))
            {
                return(ExpressionShortcuts.Call(() =>
                                                CaptureResult(textWriter, ExpressionShortcuts.Call(() => returnHelper(bindingContext, args)))
                                                ));
            }

            var pureHelperName = helperName.Substring(1);

            foreach (var resolver in configuration.HelperResolvers)
            {
                if (resolver.TryResolveReturnHelper(pureHelperName, typeof(object), out var resolvedHelper))
                {
                    return(ExpressionShortcuts.Call(() =>
                                                    CaptureResult(textWriter, ExpressionShortcuts.Call(() => resolvedHelper(bindingContext, args)))
                                                    ));
                }
            }

            return(ExpressionShortcuts.Call(() =>
                                            CaptureResult(textWriter, ExpressionShortcuts.Call(() =>
                                                                                               LateBindHelperExpression(bindingContext, helperName, args)
                                                                                               ))
                                            ));
        }
        protected override Expression VisitHelperExpression(HelperExpression hex)
        {
            var pathInfo = CompilationContext.Configuration.PathInfoStore.GetOrAdd(hex.HelperName);

            if (!pathInfo.IsValidHelperLiteral && !CompilationContext.Configuration.Compatibility.RelaxedHelperNaming)
            {
                return(Expression.Empty());
            }

            var helperName     = pathInfo.TrimmedPath;
            var bindingContext = CompilationContext.Args.BindingContext;
            var textWriter     = CompilationContext.Args.EncodedWriter;

            var contextValue = bindingContext.Property(o => o.Value);
            var args         = FunctionBinderHelpers.CreateArguments(hex.Arguments, CompilationContext);

            var configuration = CompilationContext.Configuration;

            if (configuration.Helpers.TryGetValue(pathInfo, out var helper))
            {
                return(Call(() => helper.Value.WriteInvoke(bindingContext, textWriter, contextValue, args)));
            }

            for (var index = 0; index < configuration.HelperResolvers.Count; index++)
            {
                var resolver = configuration.HelperResolvers[index];
                if (resolver.TryResolveHelper(helperName, typeof(object), out var resolvedHelper))
                {
                    helper = new StrongBox <HelperDescriptorBase>(resolvedHelper);
                    configuration.Helpers.Add(pathInfo, helper);
                    return(Call(() => resolvedHelper.WriteInvoke(bindingContext, textWriter, contextValue, args)));
                }
            }

            var lateBindDescriptor = new StrongBox <HelperDescriptorBase>(new LateBindHelperDescriptor(pathInfo, configuration));

            configuration.Helpers.Add(pathInfo, lateBindDescriptor);

            return(Call(() => lateBindDescriptor.Value.WriteInvoke(bindingContext, textWriter, contextValue, args)));
        }
 protected override Expression VisitHelperExpression(HelperExpression hex)
 {
     if (CompilationContext.Configuration.Helpers.ContainsKey(hex.HelperName))
     {
         var helper    = CompilationContext.Configuration.Helpers[hex.HelperName];
         var arguments = new Expression[]
         {
             Expression.Property(
                 CompilationContext.BindingContext,
                 typeof(BindingContext).GetProperty("TextWriter")),
             Expression.Property(
                 CompilationContext.BindingContext,
                 typeof(BindingContext).GetProperty("Value")),
             Expression.NewArrayInit(typeof(object), hex.Arguments)
         };
         if (helper.Target != null)
         {
             return(Expression.Call(
                        Expression.Constant(helper.Target),
                        helper.Method,
                        arguments));
         }
         else
         {
             return(Expression.Call(
                        helper.Method,
                        arguments));
         }
     }
     else
     {
         return(Expression.Call(
                    Expression.Constant(this),
                    new Action <BindingContext, string, IEnumerable <object> >(LateBindHelperExpression).Method,
                    CompilationContext.BindingContext,
                    Expression.Constant(hex.HelperName),
                    Expression.NewArrayInit(typeof(object), hex.Arguments)));
     }
 }
 protected override Expression VisitHelperExpression(HelperExpression hex)
 {
     if (CompilationContext.Configuration.Helpers.ContainsKey(hex.HelperName))
     {
         var helper = CompilationContext.Configuration.Helpers[hex.HelperName];
         var arguments = new Expression[]
         {
             Expression.Property(
                 CompilationContext.BindingContext,
                 typeof(BindingContext).GetProperty("TextWriter")),
             Expression.Property(
                 CompilationContext.BindingContext,
                 typeof(BindingContext).GetProperty("Value")),
             Expression.NewArrayInit(typeof(object), hex.Arguments)
         };
         if (helper.Target != null)
         {
             return Expression.Call(
                 Expression.Constant(helper.Target),
                 helper.Method,
                 arguments);
         }
         else
         {
             return Expression.Call(
                 helper.Method,
                 arguments);
         }
     }
     else
     {
         return Expression.Call(
             Expression.Constant(this),
             new Action<BindingContext, string, IEnumerable<object>>(LateBindHelperExpression).Method,
             CompilationContext.BindingContext,
             Expression.Constant(hex.HelperName),
             Expression.NewArrayInit(typeof(object), hex.Arguments));
     }
 }
 protected virtual Expression VisitHelperExpression(HelperExpression hex)
 {
     return hex;
 }
Esempio n. 16
0
 protected override Expression VisitHelperExpression(HelperExpression hex)
 {
     return(HandlebarsExpression.Helper(
                hex.HelperName,
                hex.Arguments.Select(Visit)));
 }
Esempio n. 17
0
 protected override Expression VisitHelperExpression(HelperExpression hex)
 {
     return HandlebarsExpression.Helper(
         hex.HelperName,
         hex.Arguments.Select(arg => Visit(arg)));
 }
 public BlockHelperAccumulatorContext(Expression startingNode)
     : base(startingNode)
 {
     startingNode = UnwrapStatement(startingNode);
     _startingNode = (HelperExpression)startingNode;
 }
 public ConditionalBlockAccumulatorContext(Expression startingNode)
     : base(startingNode)
 {
     startingNode  = UnwrapStatement(startingNode);
     _startingNode = (HelperExpression)startingNode;
 }
Esempio n. 20
0
 protected virtual Expression VisitHelperExpression(HelperExpression hex)
 {
     return(hex);
 }
Esempio n. 21
0
        private static bool IsBlockHelperOrInversion(HelperExpression helperExpression)
        {
            if (helperExpression == null) return false;

            return helperExpression.HelperName.StartsWith("#") || (helperExpression.HelperName == "else");
        }