/// <summary> /// 遍历绑定所有子元素 /// </summary> /// <param name="element">要绑定子元素的元素</param> private void BindChilds(IHtmlElement element) { foreach (var child in element.Elements().ToArray()) { BindElement(child); } }
/// <summary> /// 对元素进行数据绑定 /// </summary> /// <param name="element">要进行数据绑定的元素</param> protected virtual void DataBind(IHtmlElement element, BindingContext context) { var bindings = FindBindings(element).OrderBy(b => b.Priority); context.Enter(element); bindings.ForAll(b => b.DataBind(context)); element.Elements().ForAll(e => DataBind(e, context)); context.Exit(element); }
public HtmlList( IHtmlElement element ) { if ( element == null ) throw new ArgumentNullException( "element" ); if ( !HtmlSpecification.listElements.Contains( element.Name.ToLowerInvariant() ) ) throw new ArgumentException( "只能从列表元素创建 HtmlList 对象" ); if ( element.Elements().Any( e => !e.Name.EqualsIgnoreCase( "li" ) ) ) throw new FormatException( "HTML 文档格式不正确,列表元素只能包含 li 元素" ); if ( element.Nodes().OfType<IHtmlTextNode>().Any( n => !n.IsWhiteSpace() ) ) throw new FormatException( "HTML 文档格式不正确,列表元素只能包含 li 元素,不能包含文本。" ); _element = element; }
/* * /// <summary> * /// 设置元素的InnerHTML * /// </summary> * /// <param name="element">要设置InnerHTML的元素</param> * /// <param name="html">要设置的HTML文本</param> * /// <exception cref="System.InvalidOperationException">如果元素不能被安全的修改内容</exception> * public static void InnerHtml( this IHtmlElement element, string html ) * { * * if ( element == null ) * throw new ArgumentNullException( "element" ); * * lock ( element.SyncRoot ) * { * if ( !IsSafeBindable( element ) ) * throw new InvalidOperationException( "不能对元素设置InnerHTML,因为该元素不能被安全的修改内容。如果确信要改变HTML文档结构,请使用ReplaceChildsWithHtml扩展方法。" ); * * ReplaceChildsWithHtml( element, html ); * } * } * * * /// <summary> * /// 将元素内容替换为指定文本 * /// </summary> * /// <param name="element">要替换的文本</param> * /// <param name="text">要设置的文本</param> * /// <exception cref="System.InvalidOperationException">如果元素不能被安全的修改内容</exception> * public static void InnerText( this IHtmlElement element, string text ) * { * * if ( element == null ) * throw new ArgumentNullException( "element" ); * * lock ( element.SyncRoot ) * { * if ( !IsSafeBindable( element ) ) * throw new InvalidOperationException( "不能对元素设置InnerText,因为该元素不能被安全的修改内容。如果确信要改变HTML文档结构,请使用ReplaceChildsWithText扩展方法。" ); * * ReplaceChildsWithText( element, text ); * } * } */ /// <summary> /// 内部方法,用于确定一个元素的内容是否可以安全数据绑定(不破坏文档结构)。 /// </summary> /// <param name="element">要判断的元素</param> /// <returns></returns> internal static bool IsSafeBindable(this IHtmlElement element) { lock (element.SyncRoot) { var specification = element.Document.HtmlSpecification; if (element.Nodes().All(n => n is IHtmlTextNode || n is IHtmlComment))//只有文本和注释的元素是可以被安全数据绑定的。 { return(true); } if (!element.Nodes().All(n => n is IHtmlTextNode || n is IHtmlComment || n is IHtmlElement))//内容只能由文本、注释和元素三者组成 { return(false); } var childs = element.Elements(); //如果有任何一个子元素有ID属性,那么它是不安全的 if (childs.Any(e => e.Attribute("id").Value() != null)) { return(false); } //如果有任何一个子元素是不安全的,那么它是不安全的 if (childs.Any(e => e.IsSafeBindable() == false)) { return(false); } //如果元素内部只有设置文本样式的子元素,那么它是安全的。 if (childs.All(e => specification.IsStylingElement(e) || specification.IsPhraseElement(e))) { return(true); } return(false); } }
private List <string> ReadLines(IHtmlElement elem) { List <string> list = new List <string>(); var elements = elem.Elements(); if (!elements.Any() || elem.Name == "p" || elem.Name == "div" || elem.Name == "a") { try { var text = elem.InnerText(); if (string.IsNullOrEmpty(text)) { list.Add(""); } else { list.Add(text); } } catch { } } else if (elem.Name == "br" || elem.Name == "hr") { } else { foreach (var item in elements) { if (item.Name != "style" && item.Name != "script") { list.AddRange(ReadLines(item)); } } } return(list); }
bool ICssPseudoClassSelector.IsEligible(IHtmlElement element) { if (element == null) { throw new ArgumentNullException("element"); } switch (_name) { case "only-child": return(element.Siblings().Count() == 1); case "only-of-type": return(element.Siblings(element.Name).Count() == 1); case "empty": return(element.Elements().Count() == 0); default: throw new InvalidOperationException(); } }
/// <summary> /// 合并内容视图与母板视图的文档头 /// </summary> /// <param name="masterHead">母板视图的文档头</param> /// <param name="contentHead">内容视图的文档头</param> /// <returns>合并后的 <head> 元素</returns> protected virtual IHtmlElement MergeHead(IHtmlElement masterHead, IHtmlElement contentHead) { var head = masterHead.Document.CreateFragment().AddElement("head"); head.AddCopy(contentHead.Elements().Where(e => e.Attribute("ignore") == null)); if (!head.Find("title").Any()) { var title = masterHead.Find("title").FirstOrDefault(); head.AddCopy(title); } { var existsStyleSheets = new HashSet <string>(head.Find("link[rel=stylesheet]").Select(e => e.Attribute("href").Value()), StringComparer.OrdinalIgnoreCase); foreach (var element in masterHead.Find("link[rel=stylesheet]")) { if (!existsStyleSheets.Contains(element.Attribute("href").Value())) { head.AddCopy(element); } } } { var existsScripts = new HashSet <string>(head.Find("script[src]").Select(e => e.Attribute("src").Value()), StringComparer.OrdinalIgnoreCase); foreach (var element in masterHead.Find("script[src]")) { if (!existsScripts.Contains(element.Attribute("src").Value())) { head.AddCopy(element); } } } return(head); }
public HtmlList(IHtmlElement element) { if (element == null) { throw new ArgumentNullException("element"); } if (!HtmlSpecification.listElements.Contains(element.Name.ToLowerInvariant())) { throw new ArgumentException("只能从列表元素创建 HtmlList 对象"); } if (element.Elements().Any(e => !e.Name.EqualsIgnoreCase("li"))) { throw new FormatException("HTML 文档格式不正确,列表元素只能包含 li 元素"); } if (element.Nodes().OfType <IHtmlTextNode>().Any(n => !n.IsWhiteSpace())) { throw new FormatException("HTML 文档格式不正确,列表元素只能包含 li 元素,不能包含文本。"); } _element = element; }
/// <summary> /// 合并内容视图与母板视图的文档头 /// </summary> /// <param name="masterHead">母板视图的文档头</param> /// <param name="contentHead">内容视图的文档头</param> /// <returns>合并后的 <head> 元素</returns> protected virtual IHtmlElement MergeHead( IHtmlElement masterHead, IHtmlElement contentHead ) { var head = masterHead.Document.CreateFragment().AddElement( "head" ); head.AddCopy( contentHead.Elements().Where( e => e.Attribute( "ignore" ) == null ) ); if ( !head.Find( "title" ).Any() ) { var title = masterHead.Find( "title" ).FirstOrDefault(); head.AddCopy( title ); } { var existsStyleSheets = new HashSet<string>( head.Find( "link[rel=stylesheet]" ).Select( e => e.Attribute( "href" ).Value() ), StringComparer.OrdinalIgnoreCase ); foreach ( var element in masterHead.Find( "link[rel=stylesheet]" ) ) { if ( !existsStyleSheets.Contains( element.Attribute( "href" ).Value() ) ) head.AddCopy( element ); } } { var existsScripts = new HashSet<string>( head.Find( "script[src]" ).Select( e => e.Attribute( "src" ).Value() ), StringComparer.OrdinalIgnoreCase ); foreach ( var element in masterHead.Find( "script[src]" ) ) { if ( !existsScripts.Contains( element.Attribute( "src" ).Value() ) ) head.AddCopy( element ); } } return head; }
/// <summary> /// 对元素进行数据绑定 /// </summary> /// <param name="element">要进行数据绑定的元素</param> protected virtual void DataBind( IHtmlElement element, BindingContext context ) { var bindings = FindBindings( element ).OrderBy( b => b.Priority ); context.Enter( element ); bindings.ForAll( b => b.DataBind( context ) ); element.Elements().ForAll( e => DataBind( e, context ) ); context.Exit( element ); }
/// <summary> /// 遍历绑定所有子元素 /// </summary> /// <param name="element">要绑定子元素的元素</param> private void BindChilds( IHtmlElement element ) { foreach ( var child in element.Elements().ToArray() ) BindElement( child ); }