コード例 #1
0
        /// <summary>
        /// 将元素重复指定次数
        /// </summary>
        /// <param name="element">要重复的元素</param>
        /// <param name="count">要重复的次数</param>
        /// <returns>所产生的元素序列</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">重复的次数小于 0</exception>
        /// <remarks>
        /// 若提供的 count 等于 0,则会从 DOM中 移除元素,若等于 1 则不做任何事情。
        /// </remarks>
        public static IHtmlElement[] Repeat(this IHtmlElement element, int count)
        {
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            switch (count)
            {
            case 0:
                element.Remove();
                return(new IHtmlElement[0]);

            case 1:
                return(new[] { element });

            default:

                IHtmlElement[] result = new IHtmlElement[count];
                result[0] = element;

                var container = element.Container;
                lock (element.Container)
                {
                    var index = element.NodesIndexOfSelf();
                    for (int i = 1; i < count; i++)
                    {
                        result[i] = container.AddCopy(index + i, element);
                    }
                }

                return(result);
            }
        }
コード例 #2
0
        public void BindValue(object value)
        {
            var parent = _targetElement.Parent();

            if (parent == null)
            {
                throw new NotSupportedException();
            }

            var text = (string)value;

            if (parent.ElementTextMode() == TextMode.Preformated)
            {
                parent.AddTextNode(_targetElement.NodesIndexOfSelf(), HtmlEncoding.HtmlEncode(text));
                _targetElement.Remove();
            }
            else
            {
                var encoded = HtmlEncoding.HtmlEncode(text);

                encoded = encoded.Replace("  ", "&nbsp; ");

                if (encoded.EndsWith("  "))
                {
                    encoded = encoded.Substring(0, encoded.Length - 1) + "&nbsp;";//如果末尾多出一个空格,则替换为&nbsp;
                }
                encoded = encoded.Replace("\r\n", "\n").Replace("\r", "\n");

                encoded = encoded.Replace("\n", "<br />");

                _targetElement.ReplaceWith(encoded);
            }
        }
コード例 #3
0
        protected bool BindExpressionElement(IHtmlElement element)
        {
            var expression = new ElementExpression(element);

            var expressionBinder = GetExpressionBinder(expression) as IElementExpressionBinder;

            if (expressionBinder == null)
            {
                return(false);
            }

            var value = expressionBinder.GetValue(this, expression);

            if (value == null)
            {
                element.Remove();
            }

            else
            {
                element.ReplaceWith(HttpUtility.HtmlEncode(value));
            }

            return(true);
        }
コード例 #4
0
ファイル: StyleBinder.cs プロジェクト: ajayumi/Jumony
    /// <summary>
    /// 对元素进行数据绑定
    /// </summary>
    /// <param name="element">需要绑定数据的元素</param>
    /// <param name="context">绑定上下文</param>
    /// <returns>返回是否对元素进行了不可逆转的操作(例如移除),故而禁止后续的绑定操作</returns>
    public void BindElement( HtmlBindingContext context, IHtmlElement element )
    {

      if ( element.Attribute( "binding-visible" ) != null )
      {
        var visible = element.Attribute( "binding-visible" ).Value();
        if ( visible.EqualsIgnoreCase( "false" ) || visible.EqualsIgnoreCase( "hidden" ) || visible.EqualsIgnoreCase( "invisible" ) )
        {
          element.Remove();

          context.CancelChildsBinding = context.BindCompleted = true;
        }
      }





      {
        //处理样式类
        var classAttribute = element.Attribute( classAttributePrefix );
        if ( classAttribute != null )
        {
          if ( !string.IsNullOrWhiteSpace( classAttribute.AttributeValue ) )
          {

            var classes = Regulars.whiteSpaceSeparatorRegex.Split( classAttribute.AttributeValue ).Where( c => c != "" ).ToArray();
            if ( classes.Any() )
              element.Class( classes );
          }

          element.RemoveAttribute( classAttributePrefix );
        }

      }

      {
        var classAttributes = element.Attributes().Where( a => a.Name.StartsWith( classAttributePrefix ) ).ToArray();
        BindElementClasses( element, classAttributes );
      }

      //处理CSS样式
      var styleAttributes = element.Attributes().Where( a => a.Name.StartsWith( styleAttributePrefix ) ).ToArray();
      BindElementStyles( element, styleAttributes );
    }
コード例 #5
0
ファイル: StyleBinder.cs プロジェクト: zpzgone/Jumony
        /// <summary>
        /// 对元素进行数据绑定
        /// </summary>
        /// <param name="element">需要绑定数据的元素</param>
        /// <param name="context">绑定上下文</param>
        /// <returns>返回是否对元素进行了不可逆转的操作(例如移除),故而禁止后续的绑定操作</returns>
        public void BindElement(HtmlBindingContext context, IHtmlElement element)
        {
            if (element.Attribute("binding-visible") != null)
            {
                var visible = element.Attribute("binding-visible").Value();
                if (visible.EqualsIgnoreCase("false") || visible.EqualsIgnoreCase("hidden") || visible.EqualsIgnoreCase("invisible"))
                {
                    element.Remove();

                    context.CancelChildsBinding = context.BindCompleted = true;
                }
            }



            {
                //处理样式类
                var classAttribute = element.Attribute(classAttributePrefix);
                if (classAttribute != null)
                {
                    if (!string.IsNullOrWhiteSpace(classAttribute.AttributeValue))
                    {
                        var classes = Regulars.whiteSpaceSeparatorRegex.Split(classAttribute.AttributeValue).Where(c => c != "").ToArray();
                        if (classes.Any())
                        {
                            element.Class(classes);
                        }
                    }

                    element.RemoveAttribute(classAttributePrefix);
                }
            }

            {
                var classAttributes = element.Attributes().Where(a => a.Name.StartsWith(classAttributePrefix)).ToArray();
                BindElementClasses(element, classAttributes);
            }

            //处理CSS样式
            var styleAttributes = element.Attributes().Where(a => a.Name.StartsWith(styleAttributePrefix)).ToArray();

            BindElementStyles(element, styleAttributes);
        }
コード例 #6
0
        /// <summary>
        /// 对元素进行数据绑定
        /// </summary>
        /// <param name="element">需要绑定数据的元素</param>
        /// <param name="context">绑定上下文</param>
        /// <param name="dataContext">数据上下文</param>
        /// <returns>是否进行了绑定</returns>
        public bool BindElement( IHtmlElement element, HtmlBindingContext context, out object dataContext )
        {
            dataContext = null;

              if ( element.Attribute( "binding-visible" ) != null )
              {
            var visible = element.Attribute( "binding-visible" ).Value();
            if ( visible.EqualsIgnoreCase( "false" ) || visible.EqualsIgnoreCase( "hidden" ) || visible.EqualsIgnoreCase( "invisible" ) )
              element.Remove();
            return true;
              }

              //处理样式类
              {
            var classAttribute = element.Attribute( classAttributeName );
            if ( classAttribute != null )
            {
              if ( !string.IsNullOrWhiteSpace( classAttribute.AttributeValue ) )
              {

            var classes = Regulars.whiteSpaceSeparatorRegex.Split( classAttribute.AttributeValue ).Where( c => c != "" ).ToArray();
            if ( classes.Any() )
              element.Class( classes );
              }

              element.RemoveAttribute( classAttributeName );
            }
              }

              //处理CSS样式
              var styleAttributes = element.Attributes().Where( a => a.Name.StartsWith( styleAttributePrefix ) ).ToArray();
              if ( styleAttributes.Any() )
            BindElementStyles( element, styleAttributes );

              if ( !element.Name.EqualsIgnoreCase( "view" ) && !element.Name.EqualsIgnoreCase( "binding" ) )
              {
            var dataContextExpression = AttributeExpression.ParseExpression( element.Attribute( "datacontext" ) );
            if ( dataContextExpression != null )
              dataContext = GetDataObject( dataContextExpression, context );

            return false;
              }

              var expression = new AttributeExpression( element );

              object dataObject = GetDataObject( expression, context );

              if ( dataObject == null )
            return false;

              var format = element.Attribute( "format" ).Value();

              //绑定到客户端脚本对象
              var variableName = element.Attribute( "var" ).Value() ?? element.Attribute( "variable" ).Value();
              if ( variableName != null )
              {

            if ( format != null )
              dataObject = string.Format( format, dataObject );

            var hostName = element.Attribute( "host" ).Value();

            var script = (string) null;

            if ( hostName == null )
              script = string.Format( "(function(){{ this['{0}'] = {1} }})();", variableName, ToJson( dataObject ) );

            else
              script = string.Format( "(function(){{ this['{0}'] = {1} }})();", variableName, ToJson( dataObject ) );

            element.ReplaceWith( string.Format( "<script type=\"text/javascript\">{0}</script>", script ) );
            return true;
              }

              //绑定为 HTML 文本
              var bindValue = string.Format( format ?? "{0}", dataObject );

              var attributeName = element.Attribute( "attribute" ).Value() ?? element.Attribute( "attr" ).Value();
              if ( attributeName != null )
              {
            var nextElement = element.NextElement();
            if ( nextElement == null )
              return false;

            nextElement.SetAttribute( attributeName, bindValue );
            return true;
              }

              element.ReplaceWith( bindValue );
              return true;
        }
コード例 #7
0
        /// <summary>
        /// 对元素进行数据绑定
        /// </summary>
        /// <param name="element">需要绑定数据的元素</param>
        /// <param name="context">绑定上下文</param>
        /// <param name="dataContext">数据上下文</param>
        /// <returns>是否进行了绑定</returns>
        public bool BindElement(IHtmlElement element, HtmlBindingContext context, out object dataContext)
        {
            dataContext = null;

            if (element.Attribute("binding-visible") != null)
            {
                var visible = element.Attribute("binding-visible").Value();
                if (visible.EqualsIgnoreCase("false") || visible.EqualsIgnoreCase("hidden") || visible.EqualsIgnoreCase("invisible"))
                {
                    element.Remove();
                }
                return(true);
            }



            //处理样式类
            {
                var classAttribute = element.Attribute(classAttributeName);
                if (classAttribute != null)
                {
                    if (!string.IsNullOrWhiteSpace(classAttribute.AttributeValue))
                    {
                        var classes = Regulars.whiteSpaceSeparatorRegex.Split(classAttribute.AttributeValue).Where(c => c != "").ToArray();
                        if (classes.Any())
                        {
                            element.Class(classes);
                        }
                    }

                    element.RemoveAttribute(classAttributeName);
                }
            }


            //处理CSS样式
            var styleAttributes = element.Attributes().Where(a => a.Name.StartsWith(styleAttributePrefix)).ToArray();

            if (styleAttributes.Any())
            {
                BindElementStyles(element, styleAttributes);
            }



            if (!element.Name.EqualsIgnoreCase("view") && !element.Name.EqualsIgnoreCase("binding"))
            {
                var dataContextExpression = AttributeExpression.ParseExpression(element.Attribute("datacontext"));
                if (dataContextExpression != null)
                {
                    dataContext = GetDataObject(dataContextExpression, context);
                }

                return(false);
            }

            var expression = new AttributeExpression(element);

            object dataObject = GetDataObject(expression, context);

            if (dataObject == null)
            {
                return(false);
            }



            var format = element.Attribute("format").Value();


            //绑定到客户端脚本对象
            var variableName = element.Attribute("var").Value() ?? element.Attribute("variable").Value();

            if (variableName != null)
            {
                if (format != null)
                {
                    dataObject = string.Format(format, dataObject);
                }


                var hostName = element.Attribute("host").Value();

                var script = (string)null;

                if (hostName == null)
                {
                    script = string.Format("(function(){{ this['{0}'] = {1} }})();", variableName, ToJson(dataObject));
                }

                else
                {
                    script = string.Format("(function(){{ this['{0}'] = {1} }})();", variableName, ToJson(dataObject));
                }


                element.ReplaceWith(string.Format("<script type=\"text/javascript\">{0}</script>", script));
                return(true);
            }



            //绑定为 HTML 文本
            var bindValue = string.Format(format ?? "{0}", dataObject);

            var attributeName = element.Attribute("attribute").Value() ?? element.Attribute("attr").Value();

            if (attributeName != null)
            {
                var nextElement = element.NextElement();
                if (nextElement == null)
                {
                    return(false);
                }

                nextElement.SetAttribute(attributeName, bindValue);
                return(true);
            }

            element.ReplaceWith(bindValue);
            return(true);
        }