From 7189e53284dcb9be27fa73d0f8d02c34e14674fa Mon Sep 17 00:00:00 2001 From: Eui Jung Date: Wed, 30 Oct 2024 14:21:22 -0700 Subject: [PATCH 1/2] New API for GetAttributeValue that takes in an explicit parser --- src/HtmlAgilityPack.Shared/HtmlNode.cs | 54 ++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/src/HtmlAgilityPack.Shared/HtmlNode.cs b/src/HtmlAgilityPack.Shared/HtmlNode.cs index 25462314..d7318f4e 100644 --- a/src/HtmlAgilityPack.Shared/HtmlNode.cs +++ b/src/HtmlAgilityPack.Shared/HtmlNode.cs @@ -1392,7 +1392,7 @@ public string GetAttributeValue(string name, string def) return att.Value; #else - return GetAttributeValue(name, def); + return GetAttributeValue(name, def, null); #endif } @@ -1430,7 +1430,7 @@ public int GetAttributeValue(string name, int def) return def; } #else - return GetAttributeValue(name, def); + return GetAttributeValue(name, def, int.TryParse); #endif } @@ -1468,7 +1468,7 @@ public bool GetAttributeValue(string name, bool def) return def; } #else - return GetAttributeValue(name, def); + return GetAttributeValue(name, def, bool.TryParse); #endif } @@ -1481,11 +1481,12 @@ public bool GetAttributeValue(string name, bool 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. + [Obsolete("Use GetAttributeValue with an explicit custom parser for AOT compatibility")] public T GetAttributeValue(string name, T def) { if (name == null) { - throw new ArgumentNullException("name"); + throw new ArgumentNullException(nameof(name)); } if (!HasAttributes) @@ -1508,6 +1509,51 @@ public T GetAttributeValue(string name, T def) return def; } } + + /// + /// Parser for attribute value. + /// + /// The type to parse string value into + public delegate bool AttributeValueParser(string value, out T result); + + /// + /// Helper method to get the value of an attribute of this node as type T. If the attribute is not found + /// or if the attribute is not parsable to type T, 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 parser used to convert string attribute value to T. + /// The value of the attribute if found and parsable, the default value otherwise. + public T GetAttributeValue(string name, T def, AttributeValueParser parser) + { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + if (!HasAttributes) + { + return def; + } + + HtmlAttribute att = Attributes[name]; + if (att?.Value == null) + { + return def; + } + + if (att.Value is T value) + { + return value; + } + + if (parser != null && parser(att.Value, out T parsedValue)) + { + return parsedValue; + } + + return def; + } #endif /// From 1c20bd3e8a0b68025a40266002de4ea8cc14b368 Mon Sep 17 00:00:00 2001 From: Eui Jung Date: Wed, 30 Oct 2024 16:03:10 -0700 Subject: [PATCH 2/2] Fix indentation --- src/HtmlAgilityPack.Shared/HtmlNode.cs | 60 +++++++++++++------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/HtmlAgilityPack.Shared/HtmlNode.cs b/src/HtmlAgilityPack.Shared/HtmlNode.cs index d7318f4e..5eeede9f 100644 --- a/src/HtmlAgilityPack.Shared/HtmlNode.cs +++ b/src/HtmlAgilityPack.Shared/HtmlNode.cs @@ -1524,36 +1524,36 @@ public T GetAttributeValue(string name, T def) /// The default value to return if not found. /// The parser used to convert string attribute value to T. /// The value of the attribute if found and parsable, the default value otherwise. - public T GetAttributeValue(string name, T def, AttributeValueParser parser) - { - if (name == null) - { - throw new ArgumentNullException(nameof(name)); - } - - if (!HasAttributes) - { - return def; - } - - HtmlAttribute att = Attributes[name]; - if (att?.Value == null) - { - return def; - } - - if (att.Value is T value) - { - return value; - } - - if (parser != null && parser(att.Value, out T parsedValue)) - { - return parsedValue; - } - - return def; - } + public T GetAttributeValue(string name, T def, AttributeValueParser parser) + { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + if (!HasAttributes) + { + return def; + } + + HtmlAttribute att = Attributes[name]; + if (att?.Value == null) + { + return def; + } + + if (att.Value is T value) + { + return value; + } + + if (parser != null && parser(att.Value, out T parsedValue)) + { + return parsedValue; + } + + return def; + } #endif ///