@@ -756,10 +756,28 @@ public static bool CanOverlapElement(string name)
756756 /// <param name="html">The HTML text.</param>
757757 /// <returns>The newly created node instance.</returns>
758758 public static HtmlNode CreateNode ( string html )
759+ {
760+ return CreateNode ( html , null ) ;
761+ }
762+
763+ /// <summary>
764+ /// Creates an HTML node from a string representing literal HTML.
765+ /// </summary>
766+ /// <param name="html">The HTML text.</param>
767+ /// <param name="htmlDocumentBuilder">The HTML Document builder.</param>
768+ /// <returns>The newly created node instance.</returns>
769+ public static HtmlNode CreateNode ( string html , Action < HtmlDocument > htmlDocumentBuilder )
759770 {
760771 // REVIEW: this is *not* optimum...
761772 HtmlDocument doc = new HtmlDocument ( ) ;
773+
774+ if ( htmlDocumentBuilder != null )
775+ {
776+ htmlDocumentBuilder ( doc ) ;
777+ }
778+
762779 doc . LoadHtml ( html ) ;
780+
763781 if ( ! doc . DocumentNode . IsSingleElementNode ( ) )
764782 {
765783 throw new Exception ( "Multiple node elments can't be created." ) ;
@@ -1447,20 +1465,11 @@ public T GetAttributeValue<T>(string name, T def)
14471465 {
14481466 return def ;
14491467 }
1450-
1451- TypeConverter converter = TypeDescriptor . GetConverter ( typeof ( T ) ) ;
14521468
1453- try
1454- {
1455- if ( converter != null && converter . CanConvertTo ( att . Value . GetType ( ) ) )
1456- {
1457- return ( T ) converter . ConvertTo ( att . Value , typeof ( T ) ) ;
1458- }
1459- else
1460- {
1461- return ( T ) ( object ) att . Value ;
1462- }
1463- }
1469+ try
1470+ {
1471+ return ( T ) att . Value . To ( typeof ( T ) ) ;
1472+ }
14641473 catch
14651474 {
14661475 return def ;
@@ -1659,6 +1668,61 @@ public void RemoveAllIDforNode(HtmlNode node)
16591668 }
16601669 }
16611670
1671+ /// <summary>Move a node already associated and append it to this node instead.</summary>
1672+ /// <param name="child">The child node to move.</param>
1673+ public void MoveChild ( HtmlNode child )
1674+ {
1675+ if ( child == null )
1676+ {
1677+ throw new ArgumentNullException ( $ "Oops! the '{ nameof ( child ) } ' parameter cannot be null.") ;
1678+ }
1679+
1680+ var oldParent = child . ParentNode ;
1681+
1682+ AppendChild ( child ) ;
1683+
1684+ if ( oldParent != null )
1685+ {
1686+ oldParent . RemoveChild ( child ) ;
1687+ }
1688+ }
1689+
1690+ /// <summary>Move a children collection already associated and append it to this node instead.</summary>
1691+ /// <param name="children">The children collection already associated to move to another node.</param>
1692+ public void MoveChildren ( HtmlNodeCollection children )
1693+ {
1694+ if ( children == null )
1695+ {
1696+ throw new ArgumentNullException ( $ "Oops! the '{ nameof ( children ) } ' parameter cannot be null.") ;
1697+ }
1698+
1699+ var oldParent = children . ParentNode ;
1700+
1701+ AppendChildren ( children ) ;
1702+
1703+ if ( oldParent != null )
1704+ {
1705+ oldParent . RemoveChildren ( children ) ;
1706+ }
1707+ }
1708+
1709+ /// <summary>Removes the children collection for this node.</summary>
1710+ /// <param name="oldChildren">The old children collection to remove.</param>
1711+ public void RemoveChildren ( HtmlNodeCollection oldChildren )
1712+ {
1713+ if ( oldChildren == null )
1714+ {
1715+ throw new ArgumentNullException ( $ "Oops! the '{ nameof ( oldChildren ) } ' parameter cannot be null.") ;
1716+ }
1717+
1718+ var list = oldChildren . ToList ( ) ;
1719+
1720+ foreach ( HtmlNode newChild in list )
1721+ {
1722+ RemoveChild ( newChild ) ;
1723+ }
1724+ }
1725+
16621726 /// <summary>
16631727 /// Removes the specified child node.
16641728 /// </summary>
0 commit comments