本文整理汇总了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);
}
}
示例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;
}
示例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;
}
}
}
}
}