/// <summary> /// 在指定位置添加节点 /// </summary> /// <param name="container">要添加节点的容器</param> /// <param name="index">要插入节点的位置</param> /// <param name="node">要添加的节点</param> /// <exception cref="System.InvalidOperationException">添加的节点与节点容器不属于同一个文档</exception> public static TContainer Insert <TContainer>(this TContainer container, int index, IFreeNode node) where TContainer : IHtmlContainer { if (container == null) { throw new ArgumentNullException("container"); } if (node == null) { throw new ArgumentNullException("node"); } var fragment = container as HtmlFragment; if (fragment != null) { fragment.AddNode(index, node); return(container); } lock (container.SyncRoot) { if (!container.Document.Equals(node.Document)) { throw new InvalidOperationException(); } node.Into(container, index); return(container); } }
/// <summary> /// 替换指定节点 /// </summary> /// <param name="oldNode">被替换的节点</param> /// <param name="newNode">用于替换的节点</param> /// <exception cref="System.InvalidOperationException">被替换的节点与用于替换的节点不属于同一个文档</exception> public static IHtmlNode Replace(this IHtmlNode oldNode, IFreeNode newNode) { if (oldNode == null) { throw new ArgumentNullException("oldNode"); } if (newNode == null) { throw new ArgumentNullException("newNode"); } var container = oldNode.Container; if (container == null) { throw new InvalidOperationException(); } lock (container.SyncRoot) { var result = newNode.Into(container, oldNode.NodesIndexOfSelf()); oldNode.Remove(); return(result); } }