public string CompileBinding(string expression, Type[] contexts, Type expectedType) { var configuration = DotvvmTestHelper.CreateConfiguration(); configuration.RegisterApiClient(typeof(TestApiClient), "http://server/api", "./apiscript.js", "_api"); configuration.Markup.ImportedNamespaces.Add(new NamespaceImport("DotVVM.Framework.Tests.Binding")); var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extensionParameters: new BindingExtensionParameter[] { new BindingPageInfoExtensionParameter(), }.Concat(configuration.Markup.DefaultExtensionParameters).ToArray()); for (int i = 1; i < contexts.Length; i++) { context = DataContextStack.Create(contexts[i], context); } var parser = new BindingExpressionBuilder(); var expressionTree = parser.ParseWithLambdaConversion(expression, context, BindingParserOptions.Create <ValueBindingExpression>(), expectedType); var jsExpression = configuration.ServiceProvider.GetRequiredService <StaticCommandBindingCompiler>().CompileToJavascript(context, expressionTree); return(KnockoutHelper.GenerateClientPostBackScript( "", new FakeCommandBinding(BindingPropertyResolvers.FormatJavascript(jsExpression, nullChecks: false), null), new Literal(), new PostbackScriptOptions( allowPostbackHandlers: false, returnValue: null, commandArgs: CodeParameterAssignment.FromIdentifier("commandArguments") ))); }
protected void ParseParameter() { XPathNavigator nav = this.Navigator; string nameValue = nav.GetAttribute("name", ""); if (this.Parameters.Contains(nameValue)) { return; } PageParameterInfo paramInfo = null; if (nav.MoveToAttribute("bind", WebModule.Namespace)) { var exprBuilderContext = new BindingExpressionContext(this, nav.Clone()); BindingExpressionInfo exprInfo = null; try { exprInfo = BindingExpressionBuilder.ParseExpr(nav.Value, exprBuilderContext); } catch (Exception ex) { throw CreateParseException(ex.Message); } if (exprInfo != null) { exprInfo.LineNumber = ((IXmlLineInfo)nav).LineNumber; } nav.MoveToParent(); IDictionary<string, string> namespacesInScope = nav.GetNamespacesInScope(XmlNamespaceScope.All); bool hasDefaultValue = !String.IsNullOrEmpty(nav.GetAttribute("select", "")); string asValue = nav.GetAttribute("as", ""); bool required = nav.GetAttribute("required", "") == "yes"; paramInfo = PageParameterInfo.FromSequenceType(nameValue, asValue, namespacesInScope); if (hasDefaultValue) { if (paramInfo.MinLength > 0) { paramInfo.MinLength = 0; } } else if (required) { if (paramInfo.MinLength == 0) { paramInfo.MinLength = 1; } } paramInfo.Binding = exprInfo; } if (paramInfo != null) { this.Parameters.Add(paramInfo); } }
public string CompileBinding(string expression, Type[] contexts, Type expectedType) { var context = new DataContextStack(contexts.FirstOrDefault() ?? typeof(object), rootControlType: typeof(DotvvmControl)); for (int i = 1; i < contexts.Length; i++) { context = new DataContextStack(contexts[i], context); } var parser = new BindingExpressionBuilder(); var expressionTree = TypeConversion.ImplicitConversion(parser.Parse(expression, context, BindingParserOptions.Create <ValueBindingExpression>()), expectedType, true, true); return(JavascriptTranslator.CompileToJavascript(expressionTree, context)); }
public object ExecuteBinding(string expression, object[] contexts, DotvvmControl control, NamespaceImport[] imports = null, Type expectedType = null) { var context = new DataContextStack(contexts.FirstOrDefault()?.GetType() ?? typeof(object), rootControlType: control?.GetType() ?? typeof(DotvvmControl)); for (int i = 1; i < contexts.Length; i++) { context = new DataContextStack(contexts[i].GetType(), context); } var parser = new BindingExpressionBuilder(); var expressionTree = parser.Parse(expression, context, BindingParserOptions.Create <ValueBindingExpression>(importNs: new[] { new NamespaceImport("DotVVM.Framework.Tests.Binding") }.Concat(imports ?? Enumerable.Empty <NamespaceImport>()).ToArray())); Array.Reverse(contexts); return(new BindingCompilationAttribute().CompileToDelegate(expressionTree, context, expectedType ?? typeof(object)).Compile()(contexts, control)); }
public string CompileBinding(Func<Dictionary<string, Expression>, Expression> expr, Type[] contexts) { var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extensionParameters: new BindingExtensionParameter[]{ new BindingPageInfoExtensionParameter() }); for (int i = 1; i < contexts.Length; i++) { context = DataContextStack.Create(contexts[i], context); } var expressionTree = expr(BindingExpressionBuilder.GetParameters(context).ToDictionary(e => e.Name, e => (Expression)e)); var configuration = DotvvmTestHelper.CreateConfiguration(); var jsExpression = new JsParenthesizedExpression(configuration.ServiceProvider.GetRequiredService<JavascriptTranslator>().CompileToJavascript(expressionTree, context)); jsExpression.AcceptVisitor(new KnockoutObservableHandlingVisitor(true)); JsTemporaryVariableResolver.ResolveVariables(jsExpression); return JavascriptTranslator.FormatKnockoutScript(jsExpression.Expression); }
public string CompileBinding(string expression, Type[] contexts, Type expectedType) { var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extensionParameters: new BindingExtensionParameter[] { new BindingPageInfoExtensionParameter(), }.Concat(configuration.Markup.DefaultExtensionParameters).ToArray()); for (int i = 1; i < contexts.Length; i++) { context = DataContextStack.Create(contexts[i], context); } var parser = new BindingExpressionBuilder(); var expressionTree = TypeConversion.ImplicitConversion(parser.Parse(expression, context, BindingParserOptions.Create <ValueBindingExpression>()), expectedType, true, true); var jsExpression = new JsParenthesizedExpression(configuration.ServiceProvider.GetRequiredService <JavascriptTranslator>().CompileToJavascript(expressionTree, context)); jsExpression.AcceptVisitor(new KnockoutObservableHandlingVisitor(true)); JsTemporaryVariableResolver.ResolveVariables(jsExpression); return(JavascriptTranslator.FormatKnockoutScript(jsExpression.Expression)); }
protected void ParsePagePI() { XPathNavigator nav = this.Navigator; IDictionary<string, string> attribs = GetAttributes(nav.Value); // language string language = GetNonEmptyNoWhitespaceAttribute(attribs, page.language); if (language != null) { this.Language = language; } // class-name string className = GetFullClassNameAttribute(attribs, page.class_name); if (!String.IsNullOrEmpty(className)) { this.GeneratedTypeFullName = className; } // content-type string contentType = GetNonEmptyAttribute(attribs, page.content_type); if (contentType != null) { this.ContentType = contentType; } // enable-session-state object enableSs = GetEnumAttribute(attribs, page.enable_session_state, typeof(PagesEnableSessionState)); if (enableSs != null) { this.EnableSessionState = (PagesEnableSessionState)enableSs; } // validate-request bool valReq = default(bool); if (GetBooleanAttribute(attribs, page.validate_request, ref valReq)) { this.ValidateRequest = valReq; } // accept-verbs string acceptVerbs = GetNonEmptyAttribute(attribs, page.accept_verbs); if (acceptVerbs != null) { string[] verbs = acceptVerbs.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < verbs.Length; i++) { this.AcceptVerbs.Add(verbs[i].Trim()); } } // XSLT related attributes XPathNavigator nav2 = nav.CreateNavigator(); nav2.MoveToRoot(); nav2.MoveToChild(XPathNodeType.Element); IDictionary<string, string> namespacesInScope = nav2.GetNamespacesInScope(XmlNamespaceScope.All); // initial-template string initialTempl = GetNonEmptyAttribute(attribs, page.initial_template); if (initialTempl != null) { if (this.PageType != XsltPageType.StandardStylesheet) { throw CreateParseException("The '{0}' attribute can only be used on standard XSLT pages.", page.initial_template); } string itLocal = initialTempl; string itNamespace = ""; if (initialTempl.Contains(":")) { string[] itParts = initialTempl.Split(':'); itLocal = itParts[1]; string itPrefix = itParts[0]; if (namespacesInScope.ContainsKey(itPrefix)) { itNamespace = namespacesInScope[itPrefix]; } } this.InitialTemplate = new XmlQualifiedName(itLocal, itNamespace); } // initial-template-binding string initialTemplBind = GetNonEmptyAttribute(attribs, page.bind_initial_template); if (initialTemplBind != null) { if (this.PageType != XsltPageType.StandardStylesheet) { throw CreateParseException("The '{0}' attribute can only be used on standard XSLT pages.", page.bind_initial_template); } var exprBuilderContext = new BindingExpressionContext(this, nav.Clone(), namespacesInScope) { NodeName = page.bind_initial_template, AffectsXsltInitiation = true }; try { this.InitialTemplateBinding = BindingExpressionBuilder.ParseExpr(initialTemplBind, exprBuilderContext); } catch (Exception ex) { throw CreateParseException(ex.Message); } if (this.InitialTemplateBinding != null) { this.InitialTemplateBinding.LineNumber = ((IXmlLineInfo)nav).LineNumber; } } // processor string processor = GetNonEmptyAttribute(attribs, page.processor); if (processor != null) { if (!Processors.Xslt.Exists(processor)) { throw CreateParseException("The processor '{0}' is not registered.", processor); } this.ProcessorName = processor; } }