From 8d89711919814924751b84b6716a768546a3076f Mon Sep 17 00:00:00 2001 From: Jonathan Magnan Date: Mon, 17 Feb 2020 12:27:22 -0500 Subject: [PATCH] Update sources --- .../HtmlAttributeCollection.cs | 2 +- src/HtmlAgilityPack.Shared/HtmlNode.cs | 131 ++++++++++++++---- 2 files changed, 106 insertions(+), 27 deletions(-) diff --git a/src/HtmlAgilityPack.Shared/HtmlAttributeCollection.cs b/src/HtmlAgilityPack.Shared/HtmlAttributeCollection.cs index bcc68fec..787a1778 100644 --- a/src/HtmlAgilityPack.Shared/HtmlAttributeCollection.cs +++ b/src/HtmlAgilityPack.Shared/HtmlAttributeCollection.cs @@ -20,7 +20,7 @@ public class HtmlAttributeCollection : IList internal Dictionary Hashitems = new Dictionary(StringComparer.OrdinalIgnoreCase); private HtmlNode _ownernode; - private List items = new List(); + internal List items = new List(); #endregion diff --git a/src/HtmlAgilityPack.Shared/HtmlNode.cs b/src/HtmlAgilityPack.Shared/HtmlNode.cs index 2a4b3bd9..3eb4c8af 100644 --- a/src/HtmlAgilityPack.Shared/HtmlNode.cs +++ b/src/HtmlAgilityPack.Shared/HtmlNode.cs @@ -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; @@ -1253,6 +1254,50 @@ public IEnumerable Elements(string name) yield return node; } + /// Gets data attribute. + /// The key. + /// The data attribute. + public HtmlAttribute GetDataAttribute(string key) + { + return Attributes.Hashitems.SingleOrDefault(x => x.Key.Equals("data-" + key, StringComparison.OrdinalIgnoreCase)).Value; + } + + /// Gets the data attributes in this collection. + /// + /// An enumerator that allows foreach to be used to process the data attributes in this + /// collection. + /// + public IEnumerable GetDataAttributes() + { + return Attributes.Hashitems.Where(x => x.Key.StartsWith("data-", StringComparison.OrdinalIgnoreCase)).Select(x => x.Value).ToList(); + } + + /// Gets the attributes in this collection. + /// + /// An enumerator that allows foreach to be used to process the attributes in this collection. + /// + public IEnumerable GetAttributes() + { + return Attributes.items; + } + + /// Gets the attributes in this collection. + /// A variable-length parameters list containing attribute names. + /// + /// An enumerator that allows foreach to be used to process the attributes in this collection. + /// + public IEnumerable GetAttributes(params string[] attributeNames) + { + List list = new List(); + + foreach(var name in attributeNames) + { + list.Add(Attributes[name]); + } + + return list; + } + /// /// Helper method to get the value of an attribute of this node. If the attribute is not found, the default value will be returned. /// @@ -1261,6 +1306,22 @@ public IEnumerable Elements(string name) /// The value of the attribute if found, the default value if not found. public string GetAttributeValue(string name, string def) { +#if METRO || NETSTANDARD1_3 || NETSTANDARD1_6 + return GetAttributeValue(name, def); +#else + return GetAttributeValue(name, def); +#endif + } + +#if METRO || NETSTANDARD1_3 || NETSTANDARD1_6 + /// + /// Helper method to get the value of an attribute of this node. If the attribute is not found, the default value will be returned. + /// + /// The name of the attribute to get. May not be null. + /// The default value to return if not found. + /// The value of the attribute if found, the default value if not found. + public int GetAttributeValue(string name, int def) + { if (name == null) { throw new ArgumentNullException("name"); @@ -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; + } } /// @@ -1286,7 +1354,7 @@ public string GetAttributeValue(string name, string def) /// The name of the attribute to get. May not be null. /// The default value to return if not found. /// The value of the attribute if found, the default value if not found. - public int GetAttributeValue(string name, int def) + public bool GetAttributeValue(string name, bool def) { if (name == null) { @@ -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; } } - - /// - /// Helper method to get the value of an attribute of this node. If the attribute is not found, the default value will be returned. - /// - /// The name of the attribute to get. May not be null. - /// The default value to return if not found. - /// The value of the attribute if found, the default value if not found. - public bool GetAttributeValue(string name, bool def) +#else + /// + /// Helper method to get the value of an attribute of this node. If the attribute is not found, + /// the default value will be returned. + /// + /// The name of the attribute to get. May not be null. + /// The default value to return if not found. + /// The value of the attribute if found, the default value if not found. + public T GetAttributeValue(string name, T def) { if (name == null) { @@ -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 - /// - /// Inserts the specified node immediately after the specified reference node. - /// - /// The node to insert. May not be null. - /// The node that is the reference node. The newNode is placed after the refNode. - /// The node being inserted. - public HtmlNode InsertAfter(HtmlNode newChild, HtmlNode refChild) + /// + /// Inserts the specified node immediately after the specified reference node. + /// + /// The node to insert. May not be null. + /// The node that is the reference node. The newNode is placed after the refNode. + /// The node being inserted. + public HtmlNode InsertAfter(HtmlNode newChild, HtmlNode refChild) { if (newChild == null) { @@ -1966,9 +2045,9 @@ public void SetParent(HtmlNode parent) } } - #endregion +#endregion - #region Internal Methods +#region Internal Methods internal void SetChanged() { @@ -2212,9 +2291,9 @@ internal void WriteAttributes(TextWriter outText, bool closing) } } - #endregion +#endregion - #region Private Methods +#region Private Methods private string GetRelativeXpath() { @@ -2253,9 +2332,9 @@ private bool IsSingleElementNode() return count <= 1 ? true : false; } - #endregion +#endregion - #region Class Helper +#region Class Helper /// /// Adds one or more classes to this node. @@ -2497,6 +2576,6 @@ private bool IsEmpty(IEnumerable en) return true; } - #endregion +#endregion } } \ No newline at end of file