/// <summary> /// 获取所有父代元素 /// </summary> /// <param name="node">要获取父代元素集合的节点</param> /// <returns>节点的所有父代元素集合</returns> public static IEnumerable <IHtmlElement> Ancestors(this IHtmlNode node) { if (node == null) { throw new ArgumentNullException("node"); } EnsureAvaliable(node); while (true) { var element = node.Parent(); if (element == null) { yield break; } yield return(element); node = element; } }
/// <summary> /// 获取指定节点或元素最近的数据上下文 /// </summary> /// <returns>最近的数据上下文,将递归查找父级和父级上下文,如果都没找到则返回null</returns> internal object GetDataContext(IHtmlNode node) { VerifyAccess(); object data = GetDataContextCore(node); if (data != null) { return(data); } IHtmlElement container; while ((container = node.Parent()) != null) { data = GetDataContextCore(container); if (data != null) { return(data); } node = container; } return(null); }
/// <summary> /// 获取当前节点的文本模式 /// </summary> /// <param name="node">要获取文本模式的节点</param> /// <returns>节点当前适用的文本模式</returns> public static TextMode ElementTextMode(this IHtmlNode node) { if (node == null) { throw new ArgumentNullException("node"); } var specification = node.Document.HtmlSpecification; var element = node as IHtmlElement ?? node.Parent(); if (element == null) { return(TextMode.Normal); } return(specification.ElementTextMode(element)); }
/// <summary> /// 获取所有父代元素 /// </summary> /// <param name="node">要获取父代元素集合的节点</param> /// <returns>节点的所有父代元素集合</returns> public static IEnumerable <IHtmlElement> Ancestors(this IHtmlNode node) { EnsureAvaliable(node); while (true) { var element = node.Parent(); if (element == null) { yield break; } yield return(element); node = element; } }