使用指定元素作为包装的wrap方法

Coordinator
Dec 28, 2010 at 1:44 AM
public static class WrapExtensions
{
  public static IHtmlElement Wrap( this IHtmlNode node, IFreeElement wrapper )
  {
    if ( node == null )
      throw new ArgumentNullException( "node" );

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

    if ( node.Parent == null )
      throw new InvalidOperationException();

    if ( !node.Document.Equals( wrapper.Document ) )
      throw new InvalidOperationException();

    if ( wrapper.Nodes().Any() )
      throw new ArgumentException( "用于包装的元素不能有任何子节点", "wrapper" );

    wrapper.AppendCopy( node );

    return (IHtmlElement) node.Replace( wrapper );
  }
}