Esempio n. 1
0
        /// <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));
            }
        }
Esempio n. 2
0
        public HtmlRange(IHtmlNode node1, IHtmlNode node2, bool inclusiveNode1, bool inclusiveNode2)
        {
            if (node1 == null)
            {
                throw new ArgumentNullException("node1");
            }

            if (node2 == null)
            {
                throw new ArgumentNullException("node2");
            }


            container = node1.Container;

            if (container == null || !container.Equals(node2.Container) || node1.Equals(node2))
            {
                throw new InvalidOperationException();
            }

            if (node1.NodesIndexOfSelf() <= node2.NodesIndexOfSelf())
            {
                beginNode      = node1;
                endNode        = node2;
                inclusiveBegin = inclusiveNode1;
                inclusiveEnd   = inclusiveNode2;
            }
            else
            {
                beginNode      = node2;
                endNode        = node1;
                inclusiveBegin = inclusiveNode2;
                inclusiveEnd   = inclusiveNode1;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 在自己后面添加一个文档碎片
        /// </summary>
        /// <param name="node">要在其后添加碎片的节点</param>
        /// <param name="html">要分析成碎片的 HTML 文本</param>
        /// <returns>添加后的节点集</returns>
        public static IEnumerable <IHtmlNode> AddFragmentAfterSelf(this IHtmlNode node, string html)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (html == null)
            {
                throw new ArgumentNullException("html");
            }

            var container = node.Container;


            if (container == null)
            {
                throw new InvalidOperationException();
            }

            lock (container.SyncRoot)
            {
                return(container.AddFragment(node.NodesIndexOfSelf(), html));
            }
        }
Esempio n. 4
0
        public HtmlRange( IHtmlNode node1, IHtmlNode node2, bool inclusiveNode1, bool inclusiveNode2 )
        {
            if ( node1 == null )
            throw new ArgumentNullException( "node1" );

              if ( node2 == null )
            throw new ArgumentNullException( "node2" );

              container = node1.Container;

              if ( container == null || !container.Equals( node2.Container ) || node1.Equals( node2 ) )
            throw new InvalidOperationException();

              if ( node1.NodesIndexOfSelf() <= node2.NodesIndexOfSelf() )
              {
            beginNode = node1;
            endNode = node2;
            inclusiveBegin = inclusiveNode1;
            inclusiveEnd = inclusiveNode2;
              }
              else
              {
            beginNode = node2;
            endNode = node1;
            inclusiveBegin = inclusiveNode2;
            inclusiveEnd = inclusiveNode1;
              }
        }
Esempio n. 5
0
        /// <summary>
        /// 在自己前面添加一个元素
        /// </summary>
        /// <param name="node">要在其前面添加元素的节点</param>
        /// <param name="elementName">添加的元素名</param>
        /// <returns>添加的元素</returns>
        public static IHtmlElement AddElementBeforeSelf(this IHtmlNode node, string elementName)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (elementName == null)
            {
                throw new ArgumentNullException("elementName");
            }

            var container = node.Container;


            if (container == null)
            {
                throw new InvalidOperationException();
            }

            lock ( container )
            {
                return(container.AddElement(node.NodesIndexOfSelf(), elementName));
            }
        }
Esempio n. 6
0
        /// <summary>
        /// 在自己前面添加一个文档碎片
        /// </summary>
        /// <param name="node">要在其前面添加碎片的节点</param>
        /// <param name="fragment">要添加 HTML 碎片</param>
        /// <returns>添加后的节点集</returns>
        public static IEnumerable <IHtmlNode> AddFragmentBeforeSelf(this IHtmlNode node, IHtmlFragment fragment)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (fragment == null)
            {
                throw new ArgumentNullException("fragment");
            }

            var container = node.Container;


            if (container == null)
            {
                throw new InvalidOperationException();
            }

            lock (container.SyncRoot)
            {
                return(container.AddFragment(node.NodesIndexOfSelf(), fragment));
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 在自己前面添加一个文本节点
        /// </summary>
        /// <param name="node">要在其前面添加文本的节点</param>
        /// <param name="htmlText">要添加 HTML 文本</param>
        /// <returns>添加的文本节点</returns>
        public static IHtmlTextNode AddTextNodeBeforSelf(this IHtmlNode node, string htmlText)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (htmlText == null)
            {
                throw new ArgumentNullException("htmlText");
            }

            var container = node.Container;


            if (container == null)
            {
                throw new InvalidOperationException();
            }

            lock ( container )
            {
                return(container.AddTextNode(node.NodesIndexOfSelf(), htmlText));
            }
        }
Esempio n. 8
0
        /// <summary>
        /// 在自己前面添加一个注释
        /// </summary>
        /// <param name="node">要在其前面添加注释的节点</param>
        /// <param name="comment">要添加 HTML 注释内容</param>
        /// <returns>添加的注释节点</returns>
        public static IHtmlComment AddCommentBeforeSelf(this IHtmlNode node, string comment)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (comment == null)
            {
                throw new ArgumentNullException("comment");
            }

            var container = node.Container;


            if (container == null)
            {
                throw new InvalidOperationException();
            }

            lock (container.SyncRoot)
            {
                return(container.AddComment(node.NodesIndexOfSelf(), comment));
            }
        }
Esempio n. 9
0
        /// <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);
            }
        }
Esempio n. 10
0
        /// <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);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// 在前面添加节点的副本
        /// </summary>
        /// <param name="node">要在其前面添加副本的节点</param>
        /// <param name="newNodes">要创作副本的节点</param>
        /// <returns>添加后的节点</returns>
        public static IEnumerable <IHtmlNode> AddCopyBeforeSelf(this IHtmlNode node, IEnumerable <IHtmlNode> newNodes)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (newNodes == null)
            {
                throw new ArgumentNullException("newNodes");
            }


            var container = node.Container;

            lock (container.SyncRoot)
            {
                return(container.AddCopy(node.NodesIndexOfSelf(), newNodes));
            }
        }
Esempio n. 12
0
        /// <summary>
        /// 在后面添加节点的副本
        /// </summary>
        /// <param name="node">要在其后面添加副本的节点</param>
        /// <param name="newNode">要创作副本的节点</param>
        /// <returns>添加后的节点</returns>
        public static IHtmlNode AddCopyAfterSelf(this IHtmlNode node, IHtmlNode newNode)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (newNode == null)
            {
                throw new ArgumentNullException("newNode");
            }


            var container = node.Container;

            lock (container.SyncRoot)
            {
                return(container.AddCopy(node.NodesIndexOfSelf() + 1, newNode));
            }
        }
Esempio n. 13
0
        /// <summary>
        /// 在前面添加节点的副本
        /// </summary>
        /// <param name="node">要在其前面添加副本的节点</param>
        /// <param name="element">要创作副本的节点</param>
        /// <returns>添加后的节点</returns>
        public static IHtmlElement AddCopyBeforeSelf(this IHtmlNode node, IHtmlElement element)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (element == null)
            {
                throw new ArgumentNullException("element");
            }


            var container = node.Container;

            lock (container.SyncRoot)
            {
                return(container.AddCopy(node.NodesIndexOfSelf(), element));
            }
        }