Пример #1
0
        protected virtual void ApplyStyle(ResolvedControl control)
        {
            //if (SetHtmlAttributes != null)
            //{
            //    foreach (var attr in SetHtmlAttributes)
            //    {
            //        if (!control.HtmlAttributes.ContainsKey(attr.Key) || attr.Value.append)
            //        {
            //            ResolvedHtmlAttributeSetter setter = null;
            //            if (attr.Value.value is ResolvedBinding)
            //            {
            //                setter = new ResolvedHtmlAttributeBinding(attr.Key, (ResolvedBinding)attr.Value.value);
            //            }
            //            else
            //            {
            //                setter = new ResolvedHtmlAttributeValue(attr.Key, (string)attr.Value.value);
            //            }

            //            control.SetHtmlAttribute(setter);
            //        }
            //    }
            //}
            if (SetProperties != null)
            {
                foreach (var prop in SetProperties)
                {
                    if (!control.Properties.ContainsKey(prop.Key))
                    {
                        string error;
                        control.SetProperty(prop.Value.Value, prop.Value.Append, out error);
                    }
                }
            }
        }
        private ResolvedControl CreateRequiredResourceControl(string resource, Parser.Dothtml.Parser.DothtmlNode node, DataContextStack dataContext)
        {
            var control = new ResolvedControl(requiredResourceConrolMetadata, node, dataContext);

            control.SetProperty(new ResolvedPropertyValue(RequiredResource.NameProperty, resource));
            return(control);
        }
Пример #3
0
 public override void VisitControl(ResolvedControl control)
 {
     if (control.DataContextTypeStack != control.Parent?.As <ResolvedControl>()?.DataContextTypeStack)
     {
         control.SetProperty(new ResolvedPropertyValue(Internal.DataContextTypeProperty, control.DataContextTypeStack));
     }
     base.VisitControl(control);
 }
Пример #4
0
        private void ProcessControlContent(IEnumerable <DothtmlNode> nodes, ResolvedControl control)
        {
            var  content    = new List <DothtmlNode>();
            bool properties = true;

            foreach (var node in nodes)
            {
                var element = node as DothtmlElementNode;
                if (element != null && properties)
                {
                    var property = FindProperty(control.Metadata, element.TagName);
                    if (property != null && string.IsNullOrEmpty(element.TagPrefix) && property.MarkupOptions.MappingMode.HasFlag(MappingMode.InnerElement))
                    {
                        content.Clear();
                        control.SetProperty(ProcessElementProperty(control, property, element.Content));
                    }
                    else
                    {
                        content.Add(node);
                        if (node.IsNotEmpty())
                        {
                            properties = false;
                        }
                    }
                }
                else
                {
                    content.Add(node);
                }
            }
            if (content.Any(DothtmlNodeHelper.IsNotEmpty))
            {
                if (control.Metadata.DefaultContentProperty != null)
                {
                    control.SetProperty(ProcessElementProperty(control, control.Metadata.DefaultContentProperty, content));
                }
                else
                {
                    foreach (var node in content)
                    {
                        // TODO: data context from parent
                        control.Content.Add(ProcessNode(node, control.Metadata, control.DataContextTypeStack));
                    }
                }
            }
        }
Пример #5
0
 protected virtual void ApplyStyle(ResolvedControl control, DotvvmConfiguration configuration)
 {
     if (SetProperties != null)
     {
         foreach (var prop in SetProperties)
         {
             if (!control.Properties.ContainsKey(prop.Key) || prop.Value.Append)
             {
                 control.SetProperty(prop.Value.Value, prop.Value.Append, out string error);
             }
         }
     }
 }
Пример #6
0
 protected virtual void ApplyStyle(ResolvedControl control, DotvvmConfiguration configuration)
 {
     if (SetProperties != null)
     {
         foreach (var prop in SetProperties)
         {
             if (!control.Properties.ContainsKey(prop.Key) ||
                 prop.Value.Type == StyleOverrideOptions.Append ||
                 prop.Value.Type == StyleOverrideOptions.Overwrite)
             {
                 control.SetProperty(prop.Value.GetPropertySetter(control, configuration),
                                     prop.Value.Type == StyleOverrideOptions.Overwrite, out string error);
             }
         }
     }
 }
Пример #7
0
        public static void OnCompilation(ResolvedControl control, BindingCompilationService bindingService)
        {
            // ComboBoxed does not have to have the DataSource property and then they don't use the CurrentIndexBindingProperty
            if (!control.HasProperty(DataSourceProperty))
            {
                return;
            }

            var dcChange = ControlTreeResolverBase.ApplyContextChange(control.DataContextTypeStack,
                                                                      new DataContextChangeAttribute[] { new ControlPropertyBindingDataContextChangeAttribute(nameof(DataSource)), new CollectionElementDataContextChangeAttribute(0) },
                                                                      control, null);
            var dataContext = DataContextStack.Create(ResolvedTypeDescriptor.ToSystemType(dcChange.type), control.DataContextTypeStack, extenstionParameters: dcChange.extensionParameters);

            control.SetProperty(new ResolvedPropertyBinding(Internal.CurrentIndexBindingProperty,
                                                            new ResolvedBinding(bindingService, new Compilation.BindingParserOptions(typeof(ValueBindingExpression)), dataContext,
                                                                                parsedExpression: Expression.Parameter(typeof(int), "_index").AddParameterAnnotation(
                                                                                    new BindingParameterAnnotation(dataContext, new CurrentCollectionIndexExtensionParameter())))));
        }
Пример #8
0
        public static void OnCompilation(ResolvedControl control, BindingCompilationService bindingService)
        {
            // ComboBox does not have to have the DataSource property and then they don't use the CurrentIndexBindingProperty
            if (!control.Properties.TryGetValue(DataSourceProperty, out var dataSourceProperty))
            {
                return;
            }
            if (!(dataSourceProperty is ResolvedPropertyBinding dataSourceBinding))
            {
                return;
            }

            var dataContext = dataSourceBinding.Binding.Binding.GetProperty <CollectionElementDataContextBindingProperty>().DataContext;

            control.SetProperty(new ResolvedPropertyBinding(Internal.CurrentIndexBindingProperty,
                                                            new ResolvedBinding(bindingService, new Compilation.BindingParserOptions(typeof(ValueBindingExpression)), dataContext,
                                                                                parsedExpression: Expression.Parameter(typeof(int), "_index").AddParameterAnnotation(
                                                                                    new BindingParameterAnnotation(dataContext, new CurrentCollectionIndexExtensionParameter())))));
        }
        public override void VisitControl(ResolvedControl control)
        {
            base.VisitControl(control);

            if (typeof(DotvvmControl).IsAssignableFrom(control.Metadata.Type))
            {
                var req      = GetRequirements(control.Metadata.Type);
                var childReq = control.Content
                               .Select(c => c.GetValue(CompileTimeLifecycleRequirementsProperty).As <ResolvedPropertyValue>()?.Value as ControlLifecycleRequirements? ?? ControlLifecycleRequirements.None)
                               .Aggregate(ControlLifecycleRequirements.None, (a, b) => a | b);
                var value = req | childReq;
                // don't have to do the assignment for RawLiteral, as it has None by default
                if (!(value == ControlLifecycleRequirements.None && control.Metadata.Type == typeof(RawLiteral)) &&
                    // don't assign for markup controls, they already contain content when created
                    control.Metadata.VirtualPath == null)
                {
                    control.SetProperty(new ResolvedPropertyValue(CompileTimeLifecycleRequirementsProperty, value));
                }
            }
        }
Пример #10
0
 public static void OnCompilation(ResolvedControl control)
 {
     control.SetProperty(new ResolvedPropertyValue(Internal.IsNamingContainerProperty, true), replace: false, error: out _);
 }
Пример #11
0
        private ResolvedControl ProcessNode(DothtmlNode node, ControlResolverMetadata parentMetadata, DataContextStack dataContext)
        {
            try
            {
                if (node is DothtmlBindingNode)
                {
                    EnsureContentAllowed(parentMetadata);

                    // binding in text
                    var binding = (DothtmlBindingNode)node;
                    var literal = new ResolvedControl(controlResolver.ResolveControl(typeof(Literal)), node, dataContext);
                    literal.SetProperty(new ResolvedPropertyBinding(Literal.TextProperty, ProcessBinding(binding, dataContext)));
                    literal.SetProperty(new ResolvedPropertyValue(Literal.RenderSpanElementProperty, false));
                    return(literal);
                }
                else if (node is DotHtmlCommentNode)
                {
                    var commentNode = node as DotHtmlCommentNode;

                    string text    = commentNode.IsServerSide ?  "" : "<!--" + commentNode.Value + "-->";
                    var    literal = new ResolvedControl(controlResolver.ResolveControl(typeof(RawLiteral)), node, dataContext);
                    literal.ContructorParameters = new object[] { text, commentNode.Value, true };
                    return(literal);
                }
                else if (node is DothtmlLiteralNode)
                {
                    var literalNode = ((DothtmlLiteralNode)node);
                    // text content
                    var whitespace = string.IsNullOrWhiteSpace(literalNode.Value);
                    if (!whitespace)
                    {
                        EnsureContentAllowed(parentMetadata);
                    }

                    string text;

                    if (literalNode.Escape)
                    {
                        text = WebUtility.HtmlEncode(literalNode.Value);
                    }
                    else
                    {
                        text = literalNode.Value;
                    }

                    var literal = new ResolvedControl(controlResolver.ResolveControl(typeof(RawLiteral)), node, dataContext);
                    literal.ContructorParameters = new object[] { text, literalNode.Value, whitespace };
                    return(literal);
                }
                else if (node is DothtmlElementNode)
                {
                    // HTML element
                    var element = (DothtmlElementNode)node;
                    EnsureContentAllowed(parentMetadata);

                    // the element is the content
                    return(ProcessObjectElement(element, dataContext));
                }
                else
                {
                    throw new NotSupportedException($"The node of type '{node.GetType()}' is not supported!");
                }
            }
            catch (DotvvmCompilationException ex)
            {
                if (ex.Tokens == null)
                {
                    ex.Tokens       = node.Tokens;
                    ex.ColumnNumber = node.Tokens.First().ColumnNumber;
                    ex.LineNumber   = node.Tokens.First().LineNumber;
                }
                throw;
            }
            catch (Exception ex)
            {
                throw new DotvvmCompilationException("", ex, node.Tokens);
            }
        }
Пример #12
0
        /// <summary>
        /// Processes the HTML attribute.
        /// </summary>
        private void ProcessAttribute(DothtmlAttributeNode attribute, ResolvedControl control, DataContextStack dataContext)
        {
            if (attribute.AttributePrefix == "html")
            {
                if (!control.Metadata.HasHtmlAttributesCollection)
                {
                    throw new DotvvmCompilationException($"control { control.Metadata.Name } does not have html attribute collection", attribute.Tokens);
                }
                control.SetHtmlAttribute(attribute.AttributeName, ProcessAttributeValue(attribute.ValueNode, dataContext));
                return;
            }

            if (!string.IsNullOrEmpty(attribute.AttributePrefix))
            {
                throw new DotvvmCompilationException("Attributes with XML namespaces are not supported!", attribute.Tokens);
            }

            // find the property
            var property = FindProperty(control.Metadata, attribute.AttributeName);

            if (property != null)
            {
                if (property.IsBindingProperty)
                {
                    var typeChange = DataContextChangeAttribute.GetDataContextExpression(dataContext, control, property);
                    if (typeChange != null)
                    {
                        dataContext = new DataContextStack(typeChange, dataContext);
                    }
                }

                if (!property.MarkupOptions.MappingMode.HasFlag(MappingMode.Attribute))
                {
                    throw new DotvvmCompilationException($"The property '{ property.FullName }' cannot be used as attribute", attribute.Tokens);
                }

                // set the property
                if (attribute.ValueNode == null)
                {
                    throw new DotvvmCompilationException($"The attribute '{property.Name}' on the control '{control.Metadata.Name}' must have a value!", attribute.Tokens);
                }
                else if (attribute.ValueNode is DothtmlValueBindingNode)
                {
                    // binding
                    var bindingNode = (attribute.ValueNode as DothtmlValueBindingNode).BindingNode;
                    if (!property.MarkupOptions.AllowBinding)
                    {
                        throw new DotvvmCompilationException($"The property '{ property.FullName }' cannot contain binding.", bindingNode.Tokens);
                    }
                    var resolvedBinding = ProcessBinding(bindingNode, dataContext);
                    control.SetProperty(new ResolvedPropertyBinding(property, resolvedBinding));
                }
                else
                {
                    // hard-coded value in markup
                    if (!property.MarkupOptions.AllowHardCodedValue)
                    {
                        throw new DotvvmCompilationException($"The property '{ property.FullName }' cannot contain hard coded value.", attribute.ValueNode.Tokens);
                    }

                    var textValue = attribute.ValueNode as DothtmlValueTextNode;

                    var value = ReflectionUtils.ConvertValue(textValue.Text, property.PropertyType);
                    control.SetPropertyValue(property, value);
                }
            }
            else if (control.Metadata.HasHtmlAttributesCollection)
            {
                // if the property is not found, add it as an HTML attribute
                control.SetHtmlAttribute(attribute.AttributeName, ProcessAttributeValue(attribute.ValueNode, dataContext));
            }
            else
            {
                throw new DotvvmCompilationException($"The control '{control.Metadata.Type}' does not have a property '{attribute.AttributeName}' and does not allow HTML attributes!");
            }
        }