Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AOT warning with HtmlNode.GetAttributeValue #573

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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