Skip to content

Commit f5f4776

Browse files
Update source
Update source
1 parent 060dcc8 commit f5f4776

File tree

3 files changed

+127
-13
lines changed

3 files changed

+127
-13
lines changed

src/HtmlAgilityPack.Shared/HtmlNode.cs

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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>

src/HtmlAgilityPack.Shared/HtmlNodeCollection.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public HtmlNodeCollection(HtmlNode parentnode)
3838

3939
#region Properties
4040

41+
/// <summary>Gets the parent node associated to the collection.</summary>
42+
internal HtmlNode ParentNode
43+
{
44+
get
45+
{
46+
return _parentnode;
47+
}
48+
}
49+
4150
/// <summary>
4251
/// Gets a given node from the list.
4352
/// </summary>

src/HtmlAgilityPack.Shared/Utilities.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using System;
99
using System.Collections.Generic;
10+
using System.ComponentModel;
1011

1112
namespace HtmlAgilityPack
1213
{
@@ -19,5 +20,45 @@ internal static class Utilities
1920
return defaultValue;
2021
return value;
2122
}
23+
24+
#if !(METRO || NETSTANDARD1_3 || NETSTANDARD1_6)
25+
internal static object To(this Object @this, Type type)
26+
{
27+
if (@this != null)
28+
{
29+
Type targetType = type;
30+
31+
if (@this.GetType() == targetType)
32+
{
33+
return @this;
34+
}
35+
36+
TypeConverter converter = TypeDescriptor.GetConverter(@this);
37+
if (converter != null)
38+
{
39+
if (converter.CanConvertTo(targetType))
40+
{
41+
return converter.ConvertTo(@this, targetType);
42+
}
43+
}
44+
45+
converter = TypeDescriptor.GetConverter(targetType);
46+
if (converter != null)
47+
{
48+
if (converter.CanConvertFrom(@this.GetType()))
49+
{
50+
return converter.ConvertFrom(@this);
51+
}
52+
}
53+
54+
if (@this == DBNull.Value)
55+
{
56+
return null;
57+
}
58+
}
59+
60+
return @this;
61+
}
62+
#endif
2263
}
2364
}

0 commit comments

Comments
 (0)