Exemplo n.º 1
0
 internal FreeTextNode( IHtmlFragment fragment, IHtmlNodeFactory factory )
   : base( fragment, factory )
 {
   TextNode = Node as IHtmlTextNode;
   if ( TextNode == null )
     throw new InvalidOperationException();
 }
Exemplo n.º 2
0
 internal FreeElement( IHtmlFragment fragment, IHtmlNodeFactory factory )
   : base( fragment, factory )
 {
   Element = Node as IHtmlElement;
   if ( Element == null )
     throw new InvalidOperationException();
 }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
 internal FreeTextNode(IHtmlFragment fragment, IHtmlNodeFactory factory)
     : base(fragment, factory)
 {
     TextNode = Node as IHtmlTextNode;
     if (TextNode == null)
     {
         throw new InvalidOperationException();
     }
 }
Exemplo n.º 5
0
 internal FreeElement(IHtmlFragment fragment, IHtmlNodeFactory factory)
     : base(fragment, factory)
 {
     Element = Node as IHtmlElement;
     if (Element == null)
     {
         throw new InvalidOperationException();
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// 创建注释的副本
        /// </summary>
        /// <param name="factory">用于创建注释的构建器</param>
        /// <param name="comment">需要被创建副本的注释</param>
        /// <returns>注释的未分配副本</returns>
        public static IFreeComment MakeCopy(this IHtmlNodeFactory factory, IHtmlComment comment)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

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

            return(factory.CreateComment(comment.Comment));
        }
Exemplo n.º 7
0
        /*
         * /// <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);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 创建元素的副本
        /// </summary>
        /// <param name="factory">用于创建元素的构建器</param>
        /// <param name="element">需要被创建副本的元素</param>
        /// <returns>元素的未分配副本</returns>
        public static IFreeElement MakeCopy(this IHtmlNodeFactory factory, IHtmlElement element)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

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


            var free = factory.CreateElement(element.Name);

            foreach (var attribute in element.Attributes())
            {
                free.AddAttribute(attribute.Name, attribute.Value());
            }

            CopyChildNodes(element, free);

            return(free);
        }
Exemplo n.º 9
0
        /*
         * /// <summary>
         * /// 在末尾添加节点的副本,此方法总是创建节点的副本
         * /// </summary>
         * /// <param name="container">要添加节点的容器</param>
         * /// <param name="node">要添加的节点</param>
         * /// <remarks>
         * /// 此方法要求容器所在的文档支持创建节点,即GetNodeFactory方法不能返回null,否则将抛出NotSupportedException。
         * /// 使用此方法可以轻易地将元素或者其他节点从一个文档复制到另一个文档。
         * /// </remarks>
         * /// <exception cref="System.NotSupportedException">容器所在的文档不支持创建节点,或不支持创建此类节点的副本(譬如说IHtmlDocument)</exception>
         * public static TContainer AppendCopy<TContainer>( this TContainer container, IHtmlNode node ) where TContainer : IHtmlContainer
         * {
         * if ( container == null )
         *  throw new ArgumentNullException( "container" );
         *
         * if ( node == null )
         *  throw new ArgumentNullException( "node" );
         *
         *
         * lock ( container.SyncRoot )
         * {
         *  return InsertCopy( container, container.Nodes().Count(), node );
         * }
         * }
         *
         *
         * /// <summary>
         * /// 在指定位置添加节点的副本,此方法总是创建节点的副本
         * /// </summary>
         * /// <param name="container">要添加节点的容器</param>
         * /// <param name="index">要插入节点的位置</param>
         * /// <param name="node">要添加的节点</param>
         * /// <remarks>
         * /// 此方法要求容器所在的文档支持创建节点,即GetNodeFactory方法不能返回null,否则将抛出NotSupportedException。
         * /// 使用此方法可以轻易地将元素或者其他节点从一个文档复制到另一个文档。
         * /// </remarks>
         * /// <exception cref="System.NotSupportedException">容器所在的文档不支持创建节点,或不支持创建此类节点的副本(譬如说IHtmlDocument)</exception>
         * public static TContainer InsertCopy<TContainer>( this TContainer container, int index, IHtmlNode node ) where TContainer : IHtmlContainer
         * {
         * var fragment = container as HtmlFragment;
         *
         * if ( fragment != null )
         * {
         *  fragment.AddCopy( index, node );
         *  return container;
         * }
         *
         *
         * lock ( container.SyncRoot )
         * {
         *
         *  var factory = container.Document.GetNodeFactory();
         *  if ( factory == null )
         *    throw new NotSupportedException();
         *
         *  var nodeCopy = factory.MakeCopy( node );
         *
         *  nodeCopy.Into( container, index );
         *
         *  return container;
         * }
         * }
         */


        /*
         * /// <summary>
         * /// 替换指定节点
         * /// </summary>
         * /// <param name="oldNode">被替换的节点</param>
         * /// <param name="newNode">用于替换的节点</param>
         * /// <remarks>
         * /// 此方法要求容器所在的文档支持创建节点,即GetNodeFactory方法不能返回null,否则将抛出NotSupportedException。
         * /// 使用此方法可以轻易地将元素或者其他节点从一个文档复制到另一个文档。
         * /// </remarks>
         * /// <exception cref="System.NotSupportedException">容器所在的文档不支持创建节点,或不支持创建此类节点的副本(譬如说IHtmlDocument)</exception>
         * public static IHtmlNode ReplaceCopy( this IHtmlNode oldNode, IHtmlNode newNode )
         * {
         * var container = oldNode.Container;
         *
         * if ( container == null )
         *  throw new InvalidOperationException();
         *
         * lock ( container.SyncRoot )
         * {
         *  var factory = container.Document.GetNodeFactory();
         *  if ( factory == null )
         *    throw new NotSupportedException();
         *
         *  var nodeCopy = factory.MakeCopy( newNode );
         *
         *  var result = nodeCopy.Into( container, oldNode.NodesIndexOfSelf() );
         *  oldNode.Remove();
         *
         *  return result;
         * }
         * }
         */


        /// <summary>
        /// 创建节点的副本
        /// </summary>
        /// <param name="factory">用于创建节点的构建器</param>
        /// <param name="node">需要被创建副本的节点</param>
        /// <returns>节点的未分配副本</returns>
        /// <exception cref="System.NotSupportedException">试图创建不被支持的节点的副本</exception>
        public static IFreeNode MakeCopy(this IHtmlNodeFactory factory, IHtmlNode node)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }

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


            var element = node as IHtmlElement;

            if (element != null)
            {
                return(MakeCopy(factory, element));
            }

            var comment = node as IHtmlComment;

            if (comment != null)
            {
                return(MakeCopy(factory, comment));
            }

            var textNode = node as IHtmlTextNode;

            if (textNode != null)
            {
                return(MakeCopy(factory, textNode));
            }

            throw new NotSupportedException();
        }
Exemplo n.º 10
0
        /// <summary>
        /// 创建 HtmlFragment 实例
        /// </summary>
        /// <param name="factory">用于创建游离节点的节点工厂</param>
        public HtmlFragment(IHtmlNodeFactory factory)
        {
            _factory = factory;

            _nodes = new SynchronizedCollection <IFreeNode>(SyncRoot);
        }
Exemplo n.º 11
0
    /// <summary>
    /// 创建 HtmlFragment 实例
    /// </summary>
    /// <param name="factory">用于创建游离节点的节点工厂</param>
    public HtmlFragment( IHtmlNodeFactory factory )
    {
      _factory = factory;

      _nodes = new SynchronizedCollection<IFreeNode>( SyncRoot );
    }
Exemplo n.º 12
0
 /// <summary>
 /// 创建文本节点的副本
 /// </summary>
 /// <param name="factory">用于创建文本节点的构建器</param>
 /// <param name="textNode">需要被创建副本的文本节点</param>
 /// <returns>文本节点的未分配副本</returns>
 public static IFreeTextNode MakeCopy(this IHtmlNodeFactory factory, IHtmlTextNode textNode)
 {
     return(factory.CreateTextNode(textNode.HtmlText));
 }
Exemplo n.º 13
0
 internal FreeNode( IHtmlFragment fragment, IHtmlNodeFactory factory )
 {
   _fragment = fragment;
   _factory = factory;
   Node = _fragment.Nodes().First();
 }
Exemplo n.º 14
0
 internal FreeNode(IHtmlFragment fragment, IHtmlNodeFactory factory)
 {
     _fragment = fragment;
     _factory  = factory;
     Node      = _fragment.Nodes().First();
 }