Skip to content

Commit

Permalink
Merge pull request #29 from askonomm/27-auto-fill-void-html-attributes
Browse files Browse the repository at this point in the history
Auto fill boolean html attributes
  • Loading branch information
askonomm authored Nov 7, 2024
2 parents 8e486c7 + eceac80 commit 0c1f58f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
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());
}
}

0 comments on commit 0c1f58f

Please sign in to comment.