/// <summary> /// 替换指定节点 /// </summary> /// <param name="oldNode">被替换的节点</param> /// <param name="fragment">用于替换的碎片</param> /// <exception cref="System.InvalidOperationException">被替换的节点与用于替换的节点不属于同一个文档</exception> public static IEnumerable <IHtmlNode> Replace(this IHtmlNode oldNode, HtmlFragment fragment) { if (oldNode == null) { throw new ArgumentNullException("oldNode"); } if (fragment == null) { throw new ArgumentNullException("fragment"); } var container = oldNode.Container; if (container == null) { throw new InvalidOperationException(); } lock (container.SyncRoot) { var result = fragment.InsertTo(container, oldNode.NodesIndexOfSelf()); oldNode.Remove(); return(result); } }
/// <summary> /// 将容器所有内容创建为文档碎片 /// </summary> /// <param name="factory">用于创建节点的创造器</param> /// <param name="container">包含内容的容器</param> /// <returns>文档碎片</returns> public static HtmlFragment MakeFragment(this IHtmlNodeFactory factory, IHtmlContainer container) { var fragment = new HtmlFragment(factory); fragment.AddCopies(container.Nodes()); return(fragment); }
/* * /// <summary> * /// 使用指定文本替换元素内容(警告,此方法会清除元素所有内容) * /// </summary> * /// <param name="element">要替换内容的元素</param> * /// <param name="text">文本内容</param> * public static void ReplaceChildsWithText( this IHtmlElement element, string text ) * { * if ( element == null ) * throw new ArgumentNullException( "element" ); * * var factory = element.Document.GetNodeFactory(); * if ( factory == null ) * throw new NotSupportedException(); * * lock ( element.SyncRoot ) * { * ClearNodes( element ); * * if ( !HtmlSpecification.cdataTags.Contains( element.Name, StringComparer.OrdinalIgnoreCase ) ) * { * var fragment = HtmlEncode( text, factory ); * * fragment.InsertTo( element, 0 ); * } * else * { * var textNode = factory.CreateTextNode( text ); * textNode.Into( element, 0 ); * } * } * * } * * /// <summary> * /// 使用指定的HTML文本替换元素内容(警告,此方法会清除元素所有内容) * /// </summary> * /// <param name="element">要替换内容的元素</param> * /// <param name="html">要替换的HTML代码</param> * public static void ReplaceChildsWithHtml( this IHtmlElement element, string html ) * { * if ( element == null ) * throw new ArgumentNullException( "element" ); * * var factory = element.Document.GetNodeFactory(); * if ( factory == null ) * throw new NotSupportedException(); * * lock ( element.SyncRoot ) * { * ClearNodes( element ); * * if ( HtmlSpecification.cdataTags.Contains( element.Name, StringComparer.OrdinalIgnoreCase ) ) * { * var textNode = factory.CreateTextNode( html ); * textNode.Into( element, 0 ); * } * else * { * var fragment = factory.ParseFragment( html ); * fragment.InsertTo( element, 0 ); * } * } * } */ private static HtmlFragment HtmlEncode(string text, IHtmlNodeFactory factory) { if (text == null) { throw new ArgumentNullException("text"); } if (factory == null) { throw new ArgumentNullException("factory"); } var fragment = new HtmlFragment(factory); var encoded = HtmlEncoding.HtmlEncode(text); encoded = encoded.Replace("\r\n", "\n").Replace("\r", "\n"); int index = 0, brIndex = 0; while (true) { brIndex = encoded.IndexOf('\n', index); if (brIndex == -1) { if (index < encoded.Length) { fragment.AddNode(factory.CreateTextNode(encoded.Substring(index))); } break; } if (index != brIndex) { fragment.AddNode(factory.CreateTextNode(encoded.Substring(index, brIndex - index))); } fragment.AddNode(factory.CreateElement("br")); index = brIndex + 1; } return(fragment); }