当前位置: 首页>>代码示例>>C#>>正文


C# IHtmlElement.SetAttribute方法代码示例

本文整理汇总了C#中IHtmlElement.SetAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# IHtmlElement.SetAttribute方法的具体用法?C# IHtmlElement.SetAttribute怎么用?C# IHtmlElement.SetAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IHtmlElement的用法示例。


在下文中一共展示了IHtmlElement.SetAttribute方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SanitizeStyle

        /// <summary>
        /// Sanitizes the style.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="baseUrl">The base URL.</param>
        protected void SanitizeStyle(IHtmlElement element, string baseUrl)
        {
            // filter out invalid CSS declarations
            // see https://github.com/FlorianRappl/AngleSharp/issues/101
            if (element.GetAttribute("style") == null) return;
            element.SetAttribute("style", element.Style.ToCss());

            var styles = element.Style;
            if (styles == null || styles.Length == 0) return;

            var removeStyles = new List<Tuple<ICssProperty, RemoveReason>>();
            var setStyles = new Dictionary<string, string>();

            foreach (var style in styles)
            {
                var key = DecodeCss(style.Name);
                var val = DecodeCss(style.Value);

                if (!AllowedCssProperties.Contains(key))
                {
                    removeStyles.Add(new Tuple<ICssProperty, RemoveReason>(style, RemoveReason.NotAllowedStyle));
                    continue;
                }

                if(CssExpression.IsMatch(val) || DisallowCssPropertyValue.IsMatch(val))
                {
                    removeStyles.Add(new Tuple<ICssProperty, RemoveReason>(style, RemoveReason.NotAllowedValue));
                    continue;
                }

                var urls = CssUrl.Matches(val);

                if (urls.Count > 0)
                {
                    if (urls.Cast<Match>().Any(m => GetSafeUri(m.Groups[2].Value) == null || SanitizeUrl(m.Groups[2].Value, baseUrl) == null))
                        removeStyles.Add(new Tuple<ICssProperty, RemoveReason>(style, RemoveReason.NotAllowedUrlValue));
                    else
                    {
                        var s = CssUrl.Replace(val, m => "url(" + m.Groups[1].Value + SanitizeUrl(m.Groups[2].Value, baseUrl) + m.Groups[3].Value);
                        if (s != val)
                        {
                            if (key != style.Name)
                            {
                                removeStyles.Add(new Tuple<ICssProperty, RemoveReason>(style, RemoveReason.NotAllowedUrlValue));
                            }
                            setStyles[key] = s;
                        }
                    }
                }
            }

            foreach (var style in removeStyles)
            {
                RemoveStyle(element, styles, style.Item1, style.Item2);
            }

            foreach (var style in setStyles)
            {
                styles.SetProperty(style.Key, style.Value);
            }
        }
开发者ID:304NotModified,项目名称:HtmlSanitizer,代码行数:66,代码来源:HtmlSanitizer.cs

示例2: ProcessActionLink

    /// <summary>
    /// 派生类重写此方法对拥有 Action URL 的元素进行处理
    /// </summary>
    /// <param name="element">要处理的元素</param>
    /// <returns>元素是否包含 Action URL 并已经进行处理。</returns>
    protected virtual bool ProcessActionLink( IHtmlElement element )
    {
      if ( element.Attribute( "action" ) == null )
        return false;

      string attributeName;

      if ( element.Name.EqualsIgnoreCase( "a" ) )
        attributeName = "href";

      else if ( element.Name.EqualsIgnoreCase( "img" ) || element.Name.EqualsIgnoreCase( "script" ) )
        attributeName = "src";

      else if ( element.Name.EqualsIgnoreCase( "form" ) && element.Attribute( "controller" ) != null )
        attributeName = "action";

      else
        return false;



      var action = element.Attribute( "action" ).Value() ?? RouteData.Values["action"].CastTo<string>();
      var controller = element.Attribute( "controller" ).Value() ?? RouteData.Values["controller"].CastTo<string>();


      var routeValues = UrlHelper.GetRouteValues( element );


      element.RemoveAttribute( "action" );
      element.RemoveAttribute( "controller" );
      element.RemoveAttribute( "inherits" );


      var url = UrlHelper.Action( action, controller, routeValues );

      if ( url == null )
        element.Attribute( attributeName ).Remove();

      else
        element.SetAttribute( attributeName, url );


      return true;
    }
开发者ID:ajayumi,项目名称:Jumony,代码行数:49,代码来源:ActionUrlBinder.cs

示例3: ParseProperty


//.........这里部分代码省略.........
                         return;
                     }
                 case "Orientation":
                     {
                         element.Style.FlexDirection = property.ValueOnInstance.ToString() == "Vertical" ? "Row" : "Column";
                         return;
                     }
                 case "Text":
                     {
                         //When ctrl is a TextBox / TranslateTextBlock
                         element.TextContent = (property.ValueOnInstance ?? "").ToString();
                         return;
                     }
                 case "Stroke":
                     {
                         //When ctrl is a Rectangle
                         element.Style.BorderColor = ParseXamlColor(property.ValueOnInstance);
                         element.Style.BorderStyle = "solid";
                         return;
                     }
                 case "StrokeThickness":
                     {
                         //When ctrl is a Rectangle
                         element.Style.BorderWidth = property.ValueOnInstance.ToString() + "px";
                         element.Style.BorderStyle = "solid";
                         return;
                     }
                 case "Width":
                     {
                         element.Style.Width = property.ValueOnInstance.ToString() + "px";
                         return;
                     }
                 case "Height":
                     {
                         element.Style.Height = property.ValueOnInstance.ToString() + "px";
                         return;
                     }
                 case "Background":
                 case "Fill":
                     {
                         element.Style.Background = ParseXamlColor(property.ValueOnInstance);
                         return;
                     }
                 case "Margin":
                     {
                         element.Style.Left = ((Thickness)property.ValueOnInstance).Left.ToString() + "px";
                         element.Style.Top = ((Thickness)property.ValueOnInstance).Top.ToString() + "px";
                         return;
                     }
                 case "Left":
                     {
                         //if (property.ParentObject.ParentObject.Instance is Canvas)
                         //element.Style.Position = "absolute";
                         element.Style.Left = property.ValueOnInstance.ToString() + "px";
                         return;
                     }
                 case "Top":
                     {
                         //element.Style.Position = "absolute";
                         element.Style.Top = property.ValueOnInstance.ToString() + "px";
                         return;
                     }
                 case "FontSize":
                     {
                         element.Style.FontSize = property.ValueOnInstance.ToString() + "pt";
                         return;
                     }
                 case "FontWeight":
                     {
                         element.Style.FontWeight = property.ValueOnInstance.ToString();
                         return;
                     }
                 case "RenderTransform":
                     {
                         ParseTransform(element, property.ValueOnInstance as Transform);
                         return;
                     }
                 case "Opacity":
                     {
                         element.Style.Opacity = property.ValueOnInstance.ToString();
                         return;
                     }
                 case "Ignorable":
                     {
                         return;
                     }
                 default:
                     {
                         if (property.ValueOnInstance != null)
                         {
                             var nm = property.PropertyName[0].ToString().ToLower() + property.PropertyName.Substring(1);
                             nm = Regex.Replace(nm, @"(?<!_)([A-Z])", "-$1");
                             element.SetAttribute(nm, property.ValueOnInstance.ToString());
                         }
                         return;
                     }
             }
         }
     }
 }
开发者ID:jogibear9988,项目名称:Xaml2HtmlParser,代码行数:101,代码来源:Parser.cs


注:本文中的IHtmlElement.SetAttribute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。