Skip to content

Commit

Permalink
Synced up with latest version of HtmlKit for performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jstedfast committed Aug 25, 2023
1 parent f9ab142 commit cec6382
Show file tree
Hide file tree
Showing 19 changed files with 4,044 additions and 533 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.csproj text
*.nuspec text
*.sln eol=crlf
*.html binary
*.txt text
*.yml text
*.cs text
Expand Down
19 changes: 9 additions & 10 deletions MimeKit/Text/HtmlAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace MimeKit.Text {
/// </example>
public class HtmlAttribute
{
HtmlAttributeId id = (HtmlAttributeId) (-1);

/// <summary>
/// Initialize a new instance of the <see cref="HtmlAttribute"/> class.
/// </summary>
Expand All @@ -56,7 +58,7 @@ public HtmlAttribute (HtmlAttributeId id, string value)

Name = id.ToAttributeName ();
Value = value;
Id = id;
this.id = id;
}

/// <summary>
Expand All @@ -81,20 +83,12 @@ public HtmlAttribute (string name, string value)
if (!HtmlUtils.IsValidTokenName (name))
throw new ArgumentException ("Invalid attribute name.", nameof (name));

Id = name.ToHtmlAttributeId ();
Value = value;
Name = name;
}

internal HtmlAttribute (string name)
{
if (name is null)
throw new ArgumentNullException (nameof (name));

if (name.Length == 0)
throw new ArgumentException ("The attribute name cannot be empty.", nameof (name));

Id = name.ToHtmlAttributeId ();
Name = name;
}

Expand All @@ -109,7 +103,12 @@ internal HtmlAttribute (string name)
/// </example>
/// <value>The attribute identifier.</value>
public HtmlAttributeId Id {
get; private set;
get {
if (id == (HtmlAttributeId) (-1))
id = Name.ToHtmlAttributeId ();

return id;
}
}

/// <summary>
Expand Down
140 changes: 116 additions & 24 deletions MimeKit/Text/HtmlAttributeId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public enum HtmlAttributeId {
/// <summary>
/// The "accept-charset" attribute.
/// </summary>
[HtmlAttributeName ("accept-charset")]
AcceptCharset,

/// <summary>
Expand Down Expand Up @@ -291,7 +290,6 @@ public enum HtmlAttributeId {
/// <summary>
/// The "http-equiv" attribute.
/// </summary>
[HtmlAttributeName ("http-equiv")]
HttpEquiv,

/// <summary>
Expand Down Expand Up @@ -575,18 +573,6 @@ public enum HtmlAttributeId {
XmlNS
}

[AttributeUsage (AttributeTargets.Field)]
class HtmlAttributeNameAttribute : Attribute {
public HtmlAttributeNameAttribute (string name)
{
Name = name;
}

public string Name {
get; protected set;
}
}

/// <summary>
/// <see cref="HtmlAttributeId"/> extension methods.
/// </summary>
Expand All @@ -595,16 +581,124 @@ public HtmlAttributeNameAttribute (string name)
/// </remarks>
public static class HtmlAttributeIdExtensions
{
static readonly Dictionary<string, HtmlAttributeId> AttributeNameToId;
static readonly string[] AttributeNames = new string[] {
"abbr",
"accept",
"accept-charset",
"accesskey",
"action",
"align",
"alink",
"alt",
"archive",
"axis",
"background",
"bgcolor",
"border",
"cellpadding",
"cellspacing",
"char",
"charoff",
"charset",
"checked",
"cite",
"class",
"classid",
"clear",
"code",
"codebase",
"codetype",
"color",
"cols",
"colspan",
"compact",
"content",
"coords",
"data",
"datetime",
"declare",
"defer",
"dir",
"disabled",
"dynsrc",
"enctype",
"face",
"for",
"frame",
"frameborder",
"headers",
"height",
"href",
"hreflang",
"hspace",
"http-equiv",
"id",
"ismap",
"label",
"lang",
"language",
"leftmargin",
"link",
"longdesc",
"lowsrc",
"marginheight",
"marginwidth",
"maxlength",
"media",
"method",
"multiple",
"name",
"nohref",
"noresize",
"noshade",
"nowrap",
"object",
"profile",
"prompt",
"readonly",
"rel",
"rev",
"rows",
"rowspan",
"rules",
"scheme",
"scope",
"scrolling",
"selected",
"shape",
"size",
"span",
"src",
"standby",
"start",
"style",
"summary",
"tabindex",
"target",
"text",
"title",
"topmargin",
"type",
"usemap",
"valign",
"value",
"valuetype",
"version",
"vlink",
"vspace",
"width",
"xmlns",
};
static readonly Dictionary<string, HtmlAttributeId> IdMapping;

static HtmlAttributeIdExtensions ()
{
var values = (HtmlAttributeId[]) Enum.GetValues (typeof (HtmlAttributeId));

AttributeNameToId = new Dictionary<string, HtmlAttributeId> (values.Length - 1, MimeUtils.OrdinalIgnoreCase);
IdMapping = new Dictionary<string, HtmlAttributeId> (values.Length - 1, MimeUtils.OrdinalIgnoreCase);

for (int i = 1; i < values.Length; i++)
AttributeNameToId.Add (values[i].ToAttributeName (), values[i]);
IdMapping.Add (values[i].ToAttributeName (), values[i]);
}

/// <summary>
Expand All @@ -617,14 +711,12 @@ static HtmlAttributeIdExtensions ()
/// <param name="value">The enum value.</param>
public static string ToAttributeName (this HtmlAttributeId value)
{
var name = value.ToString ();
var field = typeof (HtmlAttributeId).GetField (name);
var attrs = field.GetCustomAttributes (typeof (HtmlAttributeNameAttribute), false);
int index = (int) value;

if (attrs != null && attrs.Length == 1)
return ((HtmlAttributeNameAttribute) attrs[0]).Name;
if (index > 0 && index <= AttributeNames.Length)
return AttributeNames[index - 1];

return name.ToLowerInvariant ();
return value.ToString ();
}

/// <summary>
Expand All @@ -637,7 +729,7 @@ public static string ToAttributeName (this HtmlAttributeId value)
/// <param name="name">The attribute name.</param>
internal static HtmlAttributeId ToHtmlAttributeId (this string name)
{
if (!AttributeNameToId.TryGetValue (name, out HtmlAttributeId value))
if (!IdMapping.TryGetValue (name, out HtmlAttributeId value))
return HtmlAttributeId.Unknown;

return value;
Expand Down
Loading

0 comments on commit cec6382

Please sign in to comment.