/// <summary> /// 尝试使用指定的 HTML 碎片替换此节点 /// </summary> /// <param name="node">要被替换的节点</param> /// <param name="fragment">替换节点的 HTML 碎片</param> /// <returns>碎片置入后产生的节点集</returns> /// <exception cref="System.InvalidOperationException">若节点和碎片不在同一文档</exception> public static IEnumerable <IHtmlNode> ReplaceWith(this IHtmlNode node, IHtmlFragment fragment) { if (node == null) { throw new ArgumentNullException("node"); } if (fragment == null) { throw new ArgumentNullException("fragment"); } if (!object.Equals(fragment.Document, node.Document)) { throw new InvalidOperationException(); } var container = node.Container; lock ( container ) { int index = node.NodesIndexOfSelf(); Remove(node); return(fragment.Into(container, index)); } }
/// <summary> /// 添加一个文档碎片 /// </summary> /// <param name="container">要添加碎片的容器</param> /// <param name="index">要添加的位置</param> /// <param name="fragment">要添加的碎片</param> /// <returns></returns> public static IEnumerable <IHtmlNode> AddFragment(this IHtmlContainer container, int index, IHtmlFragment fragment) { if (container == null) { throw new ArgumentNullException("container"); } if (fragment == null) { throw new ArgumentNullException("fragment"); } return(fragment.Into(container, index)); }
public IHtmlNode Into(IHtmlContainer container, int index) { return(_fragment.Into(container, index).First()); }