Skip to content

Commit

Permalink
Merge pull request #573 from euju-ms/aot
Browse files Browse the repository at this point in the history
Fix AOT warning with HtmlNode.GetAttributeValue
  • Loading branch information
JonathanMagnan authored Nov 11, 2024
2 parents 97f984c + 1c20bd3 commit 2a6af61
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions src/HtmlAgilityPack.Shared/HtmlNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ public string GetAttributeValue(string name, string def)

return att.Value;
#else
return GetAttributeValue<string>(name, def);
return GetAttributeValue<string>(name, def, null);
#endif
}

Expand Down Expand Up @@ -1430,7 +1430,7 @@ public int GetAttributeValue(string name, int def)
return def;
}
#else
return GetAttributeValue<int>(name, def);
return GetAttributeValue<int>(name, def, int.TryParse);
#endif
}

Expand Down Expand Up @@ -1468,7 +1468,7 @@ public bool GetAttributeValue(string name, bool def)
return def;
}
#else
return GetAttributeValue<bool>(name, def);
return GetAttributeValue<bool>(name, def, bool.TryParse);
#endif
}

Expand All @@ -1481,11 +1481,12 @@ public bool GetAttributeValue(string name, bool 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>
[Obsolete("Use GetAttributeValue with an explicit custom parser for AOT compatibility")]
public T GetAttributeValue<T>(string name, T def)
{
if (name == null)
{
throw new ArgumentNullException("name");
throw new ArgumentNullException(nameof(name));
}

if (!HasAttributes)
Expand All @@ -1508,6 +1509,51 @@ public T GetAttributeValue<T>(string name, T def)
return def;
}
}

/// <summary>
/// Parser for attribute value.
/// </summary>
/// <typeparam name="T">The type to parse string value into</typeparam>
public delegate bool AttributeValueParser<T>(string value, out T result);

/// <summary>
/// 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.
/// </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>
/// <param name="parser">The parser used to convert string attribute value to T.</param>
/// <returns>The value of the attribute if found and parsable, the default value otherwise.</returns>
public T GetAttributeValue<T>(string name, T def, AttributeValueParser<T> 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

/// <summary>
Expand Down

0 comments on commit 2a6af61

Please sign in to comment.