Skip to content

Commit

Permalink
Update sources
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanMagnan committed Feb 17, 2020
1 parent 1aa7334 commit 8d89711
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/HtmlAgilityPack.Shared/HtmlAttributeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class HtmlAttributeCollection : IList<HtmlAttribute>

internal Dictionary<string, HtmlAttribute> Hashitems = new Dictionary<string, HtmlAttribute>(StringComparer.OrdinalIgnoreCase);
private HtmlNode _ownernode;
private List<HtmlAttribute> items = new List<HtmlAttribute>();
internal List<HtmlAttribute> items = new List<HtmlAttribute>();

#endregion

Expand Down
131 changes: 105 additions & 26 deletions src/HtmlAgilityPack.Shared/HtmlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -1253,6 +1254,50 @@ public IEnumerable<HtmlNode> Elements(string name)
yield return node;
}

/// <summary>Gets data attribute.</summary>
/// <param name="key">The key.</param>
/// <returns>The data attribute.</returns>
public HtmlAttribute GetDataAttribute(string key)
{
return Attributes.Hashitems.SingleOrDefault(x => x.Key.Equals("data-" + key, StringComparison.OrdinalIgnoreCase)).Value;
}

/// <summary>Gets the data attributes in this collection.</summary>
/// <returns>
/// An enumerator that allows foreach to be used to process the data attributes in this
/// collection.
/// </returns>
public IEnumerable<HtmlAttribute> GetDataAttributes()
{
return Attributes.Hashitems.Where(x => x.Key.StartsWith("data-", StringComparison.OrdinalIgnoreCase)).Select(x => x.Value).ToList();
}

/// <summary>Gets the attributes in this collection.</summary>
/// <returns>
/// An enumerator that allows foreach to be used to process the attributes in this collection.
/// </returns>
public IEnumerable<HtmlAttribute> GetAttributes()
{
return Attributes.items;
}

/// <summary>Gets the attributes in this collection.</summary>
/// <param name="attributeNames">A variable-length parameters list containing attribute names.</param>
/// <returns>
/// An enumerator that allows foreach to be used to process the attributes in this collection.
/// </returns>
public IEnumerable<HtmlAttribute> GetAttributes(params string[] attributeNames)
{
List<HtmlAttribute> list = new List<HtmlAttribute>();

foreach(var name in attributeNames)
{
list.Add(Attributes[name]);
}

return list;
}

/// <summary>
/// Helper method to get the value of an attribute of this node. If the attribute is not found, the default value will be returned.
/// </summary>
Expand All @@ -1261,6 +1306,22 @@ public IEnumerable<HtmlNode> Elements(string name)
/// <returns>The value of the attribute if found, the default value if not found.</returns>
public string GetAttributeValue(string name, string def)
{
#if METRO || NETSTANDARD1_3 || NETSTANDARD1_6
return GetAttributeValue(name, def);
#else
return GetAttributeValue<string>(name, def);
#endif
}

#if METRO || NETSTANDARD1_3 || NETSTANDARD1_6
/// <summary>
/// Helper method to get the value of an attribute of this node. If the attribute is not found, the default value will be returned.
/// </summary>
/// <param name="name">The name of the attribute to get. May not be <c>null</c>.</param>
/// <param name="def">The default value to return if not found.</param>
/// <returns>The value of the attribute if found, the default value if not found.</returns>
public int GetAttributeValue(string name, int def)
{
if (name == null)
{
throw new ArgumentNullException("name");
Expand All @@ -1277,7 +1338,14 @@ public string GetAttributeValue(string name, string def)
return def;
}

return att.Value;
try
{
return Convert.ToInt32(att.Value);
}
catch
{
return def;
}
}

/// <summary>
Expand All @@ -1286,7 +1354,7 @@ public string GetAttributeValue(string name, string def)
/// <param name="name">The name of the attribute to get. May not be <c>null</c>.</param>
/// <param name="def">The default value to return if not found.</param>
/// <returns>The value of the attribute if found, the default value if not found.</returns>
public int GetAttributeValue(string name, int def)
public bool GetAttributeValue(string name, bool def)
{
if (name == null)
{
Expand All @@ -1306,21 +1374,22 @@ public int GetAttributeValue(string name, int def)

try
{
return Convert.ToInt32(att.Value);
return Convert.ToBoolean(att.Value);
}
catch
{
return def;
}
}

/// <summary>
/// Helper method to get the value of an attribute of this node. If the attribute is not found, the default value will be returned.
/// </summary>
/// <param name="name">The name of the attribute to get. May not be <c>null</c>.</param>
/// <param name="def">The default value to return if not found.</param>
/// <returns>The value of the attribute if found, the default value if not found.</returns>
public bool GetAttributeValue(string name, bool def)
#else
/// <summary>
/// Helper method to get the value of an attribute of this node. If the attribute is not found,
/// the default value will be returned.
/// </summary>
/// <param name="name">The name of the attribute to get. May not be <c>null</c>.</param>
/// <param name="def">The default value to return if not found.</param>
/// <returns>The value of the attribute if found, the default value if not found.</returns>
public T GetAttributeValue<T>(string name, T def)
{
if (name == null)
{
Expand All @@ -1337,24 +1406,34 @@ public bool GetAttributeValue(string name, bool def)
{
return def;
}

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

try
{
return Convert.ToBoolean(att.Value);
if (converter != null && converter.CanConvertTo(att.Value.GetType()))
{
return (T)converter.ConvertTo(att.Value, typeof(T));
}
else
{
return (T) (object) att.Value;
}
}
catch
{
return def;
}
}
#endif

/// <summary>
/// Inserts the specified node immediately after the specified reference node.
/// </summary>
/// <param name="newChild">The node to insert. May not be <c>null</c>.</param>
/// <param name="refChild">The node that is the reference node. The newNode is placed after the refNode.</param>
/// <returns>The node being inserted.</returns>
public HtmlNode InsertAfter(HtmlNode newChild, HtmlNode refChild)
/// <summary>
/// Inserts the specified node immediately after the specified reference node.
/// </summary>
/// <param name="newChild">The node to insert. May not be <c>null</c>.</param>
/// <param name="refChild">The node that is the reference node. The newNode is placed after the refNode.</param>
/// <returns>The node being inserted.</returns>
public HtmlNode InsertAfter(HtmlNode newChild, HtmlNode refChild)
{
if (newChild == null)
{
Expand Down Expand Up @@ -1966,9 +2045,9 @@ public void SetParent(HtmlNode parent)
}
}

#endregion
#endregion

#region Internal Methods
#region Internal Methods

internal void SetChanged()
{
Expand Down Expand Up @@ -2212,9 +2291,9 @@ internal void WriteAttributes(TextWriter outText, bool closing)
}
}

#endregion
#endregion

#region Private Methods
#region Private Methods

private string GetRelativeXpath()
{
Expand Down Expand Up @@ -2253,9 +2332,9 @@ private bool IsSingleElementNode()
return count <= 1 ? true : false;
}

#endregion
#endregion

#region Class Helper
#region Class Helper

/// <summary>
/// Adds one or more classes to this node.
Expand Down Expand Up @@ -2497,6 +2576,6 @@ private bool IsEmpty(IEnumerable en)
return true;
}

#endregion
#endregion
}
}

0 comments on commit 8d89711

Please sign in to comment.