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

Auto fill boolean html attributes #29

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
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
48 changes: 48 additions & 0 deletions Htmt/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private void Parse()
_docType = GetDoctype(Template);

RemoveDoctype();
FillBooleanAttributes();
CloseVoidElements();
TransformHtmlEntities();

Expand Down Expand Up @@ -190,6 +191,53 @@ private void CloseVoidElements()
Template = Template.Replace(element, newElement);
}
}

[GeneratedRegex(@"(<[^>]*?)(?<attr>\s[\w-]+)(?=\s|>|/>)", RegexOptions.IgnoreCase)]
private static partial Regex BooleanAttributeRegex();

/// <summary>
/// Fills boolean HTML attributes such as "checked" or "defer".
/// </summary>
private void FillBooleanAttributes()
{
var voidAttributes = new Dictionary<string, string>
{
{ "allowfullscreen", "" },
{ "async", "" },
{ "autofocus", "" },
{ "autoplay", "" },
{ "checked", "" },
{ "controls", "" },
{ "default", "" },
{ "defer", "" },
{ "disabled", "" },
{ "formnovalidate", "" },
{ "hidden", "" },
{ "ismap", "" },
{ "itemscope", "" },
{ "loop", "" },
{ "multiple", "" },
{ "muted", "" },
{ "nomodule", "" },
{ "novalidate", "" },
{ "open", "" },
{ "playsinline", "" },
{ "readonly", "" },
{ "required", "" },
{ "reversed", "" },
{ "selected", "" }
};

foreach (Match match in BooleanAttributeRegex().Matches(Template))
{
var attribute = match.Groups["attr"].Value.Trim();

if (!voidAttributes.TryGetValue(attribute, out var val)) continue;

// Replace the attribute with the filled one
Template = Template.Replace(attribute, $"{attribute}=\"{val}\"");
}
}

/// <summary>
/// The regex for HTML entities.
Expand Down
18 changes: 18 additions & 0 deletions HtmtTests/ParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,22 @@ public void TestRemoveXmlnsFromChildren()

Assert.AreEqual("<html xmlns=\"http://www.w3.org/1999/xhtml\"><head></head><body><div><span>One</span></div><div><span>Two</span></div><div><span>Three</span></div></body></html>", parser.ToHtml());
}

[TestMethod]
public void TestFillVoidAttributes()
{
const string template = "<html><head></head><body><input type=\"checkbox\" checked /></body></html>";
var parser = new Htmt.Parser { Template = template };

Assert.AreEqual("<html><head></head><body><input type=\"checkbox\" checked=\"\" /></body></html>", parser.ToHtml());
}

[TestMethod]
public void TestFillVoidAttributes2()
{
const string template = "<html><head><script defer src=\"\"></script></head><body></body></html>";
var parser = new Htmt.Parser { Template = template };

Assert.AreEqual("<html><head><script defer=\"\" src=\"\"></script></head><body></body></html>", parser.ToHtml());
}
}