Skip to content

Commit

Permalink
Update source
Browse files Browse the repository at this point in the history
Update source
  • Loading branch information
JonathanMagnan committed Apr 22, 2021
1 parent 060dcc8 commit f5f4776
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 13 deletions.
90 changes: 77 additions & 13 deletions src/HtmlAgilityPack.Shared/HtmlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -756,10 +756,28 @@ public static bool CanOverlapElement(string name)
/// <param name="html">The HTML text.</param>
/// <returns>The newly created node instance.</returns>
public static HtmlNode CreateNode(string html)
{
return CreateNode(html, null);
}

/// <summary>
/// Creates an HTML node from a string representing literal HTML.
/// </summary>
/// <param name="html">The HTML text.</param>
/// <param name="htmlDocumentBuilder">The HTML Document builder.</param>
/// <returns>The newly created node instance.</returns>
public static HtmlNode CreateNode(string html, Action<HtmlDocument> htmlDocumentBuilder)
{
// REVIEW: this is *not* optimum...
HtmlDocument doc = new HtmlDocument();

if (htmlDocumentBuilder != null)
{
htmlDocumentBuilder(doc);
}

doc.LoadHtml(html);

if (!doc.DocumentNode.IsSingleElementNode())
{
throw new Exception("Multiple node elments can't be created.");
Expand Down Expand Up @@ -1447,20 +1465,11 @@ public T GetAttributeValue<T>(string name, T def)
{
return def;
}

TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));

try
{
if (converter != null && converter.CanConvertTo(att.Value.GetType()))
{
return (T)converter.ConvertTo(att.Value, typeof(T));
}
else
{
return (T) (object) att.Value;
}
}
try
{
return (T)att.Value.To(typeof(T));
}
catch
{
return def;
Expand Down Expand Up @@ -1659,6 +1668,61 @@ public void RemoveAllIDforNode(HtmlNode node)
}
}

/// <summary>Move a node already associated and append it to this node instead.</summary>
/// <param name="child">The child node to move.</param>
public void MoveChild(HtmlNode child)
{
if (child == null)
{
throw new ArgumentNullException($"Oops! the '{nameof(child)}' parameter cannot be null.");
}

var oldParent = child.ParentNode;

AppendChild(child);

if (oldParent != null)
{
oldParent.RemoveChild(child);
}
}

/// <summary>Move a children collection already associated and append it to this node instead.</summary>
/// <param name="children">The children collection already associated to move to another node.</param>
public void MoveChildren(HtmlNodeCollection children)
{
if (children == null)
{
throw new ArgumentNullException($"Oops! the '{nameof(children)}' parameter cannot be null.");
}

var oldParent = children.ParentNode;

AppendChildren(children);

if (oldParent != null)
{
oldParent.RemoveChildren(children);
}
}

/// <summary>Removes the children collection for this node.</summary>
/// <param name="oldChildren">The old children collection to remove.</param>
public void RemoveChildren(HtmlNodeCollection oldChildren)
{
if (oldChildren == null)
{
throw new ArgumentNullException($"Oops! the '{nameof(oldChildren)}' parameter cannot be null.");
}

var list = oldChildren.ToList();

foreach (HtmlNode newChild in list)
{
RemoveChild(newChild);
}
}

/// <summary>
/// Removes the specified child node.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/HtmlAgilityPack.Shared/HtmlNodeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ public HtmlNodeCollection(HtmlNode parentnode)

#region Properties

/// <summary>Gets the parent node associated to the collection.</summary>
internal HtmlNode ParentNode
{
get
{
return _parentnode;
}
}

/// <summary>
/// Gets a given node from the list.
/// </summary>
Expand Down
41 changes: 41 additions & 0 deletions src/HtmlAgilityPack.Shared/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace HtmlAgilityPack
{
Expand All @@ -19,5 +20,45 @@ internal static class Utilities
return defaultValue;
return value;
}

#if !(METRO || NETSTANDARD1_3 || NETSTANDARD1_6)
internal static object To(this Object @this, Type type)
{
if (@this != null)
{
Type targetType = type;

if (@this.GetType() == targetType)
{
return @this;
}

TypeConverter converter = TypeDescriptor.GetConverter(@this);
if (converter != null)
{
if (converter.CanConvertTo(targetType))
{
return converter.ConvertTo(@this, targetType);
}
}

converter = TypeDescriptor.GetConverter(targetType);
if (converter != null)
{
if (converter.CanConvertFrom(@this.GetType()))
{
return converter.ConvertFrom(@this);
}
}

if (@this == DBNull.Value)
{
return null;
}
}

return @this;
}
#endif
}
}

0 comments on commit f5f4776

Please sign in to comment.