Skip to content

Commit

Permalink
Merge pull request #28 from askonomm/26-add-data-htmt-as-an-alternati…
Browse files Browse the repository at this point in the history
…ve-to-x

Implement alternative attribute syntax.
  • Loading branch information
askonomm authored Nov 7, 2024
2 parents f294b78 + f00291d commit 8e486c7
Show file tree
Hide file tree
Showing 14 changed files with 507 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET
name: Tests

on:
push:
Expand Down
13 changes: 10 additions & 3 deletions Htmt/AttributeParsers/ForAttributeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Htmt.AttributeParsers;
/// </summary>
public class ForAttributeParser : BaseAttributeParser
{
public override string XTag => "//*[@x:for]";
public override string XTag => "//*[@x:for or @data-htmt-for]";

public override void Parse(XmlNodeList? nodes)
{
Expand All @@ -21,11 +21,18 @@ public override void Parse(XmlNodeList? nodes)
{
if (node is not XmlElement n) return;

var collection = n.GetAttribute("x:for");
var asVar = n.GetAttribute("x:as");
var collection = string.IsNullOrEmpty(n.GetAttribute("x:for")) ?
n.GetAttribute("data-htmt-for") :
n.GetAttribute("x:for");

var asVar = string.IsNullOrEmpty(n.GetAttribute("x:as")) ?
n.GetAttribute("data-htmt-as") :
n.GetAttribute("x:as");

n.RemoveAttribute("x:for");
n.RemoveAttribute("data-htmt-for");
n.RemoveAttribute("x:as");
n.RemoveAttribute("data-htmt-as");

var value = Utils.FindValueByKeys(Data, collection.Split('.'));
if (value is not IEnumerable<object> enumerable) return;
Expand Down
7 changes: 4 additions & 3 deletions Htmt/AttributeParsers/GenericValueAttributeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Htmt.AttributeParsers;
/// </summary>
public class GenericValueAttributeParser : BaseAttributeParser
{
public override string XTag => "//*[@*[starts-with(name(), 'x:attr-')]]";
public override string XTag => "//*[@*[starts-with(name(), 'x:attr-') or starts-with(name(), 'data-htmt-attr-')]]";

public override void Parse(XmlNodeList? nodes)
{
Expand All @@ -22,14 +22,15 @@ public override void Parse(XmlNodeList? nodes)
if (node is not XmlElement n) continue;

var attributes = n.Attributes.Cast<XmlAttribute>()
.Where(a => a.Name.StartsWith("x:attr-"))
.Where(a => a.Name.StartsWith("x:attr-") || a.Name.StartsWith("data-htmt-attr-"))
.ToList();

foreach (var attr in attributes)
{
var val = n.GetAttribute(attr.Name);
var newVal = ParseExpression(val);
n.SetAttribute(attr.Name[7..], newVal);

n.SetAttribute(attr.Name.StartsWith("x:attr-") ? attr.Name[7..] : attr.Name[15..], newVal);
n.RemoveAttribute(attr.Name);
}
}
Expand Down
8 changes: 6 additions & 2 deletions Htmt/AttributeParsers/IfAttributeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Htmt.AttributeParsers;
/// </summary>
public class IfAttributeParser : BaseAttributeParser
{
public override string XTag => "//*[@x:if]";
public override string XTag => "//*[@x:if or @data-htmt-if]";

public override void Parse(XmlNodeList? nodes)
{
Expand All @@ -22,7 +22,11 @@ public override void Parse(XmlNodeList? nodes)
{
if (node is not XmlElement n) continue;

var key = n.GetAttribute("x:if");
var key = string.IsNullOrEmpty(n.GetAttribute("x:if")) ?
n.GetAttribute("data-htmt-if") :
n.GetAttribute("x:if");

n.RemoveAttribute("data-htmt-if");
n.RemoveAttribute("x:if");

// if key is a single word, we just check for a truthy value
Expand Down
8 changes: 6 additions & 2 deletions Htmt/AttributeParsers/InnerHtmlAttributeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Htmt.AttributeParsers;
/// </summary>
public class InnerHtmlAttributeParser : BaseAttributeParser
{
public override string XTag => "//*[@x:inner-html]";
public override string XTag => "//*[@x:inner-html or @data-htmt-inner-html]";

public override void Parse(XmlNodeList? nodes)
{
Expand All @@ -22,7 +22,11 @@ public override void Parse(XmlNodeList? nodes)
{
if (node is not XmlElement n) continue;

var innerHtmlVal = n.GetAttribute("x:inner-html");
var innerHtmlVal = string.IsNullOrEmpty(n.GetAttribute("x:inner-html")) ?
n.GetAttribute("data-htmt-inner-html") :
n.GetAttribute("x:inner-html");

n.RemoveAttribute("data-htmt-inner-html");
n.RemoveAttribute("x:inner-html");

if (string.IsNullOrEmpty(innerHtmlVal)) continue;
Expand Down
8 changes: 6 additions & 2 deletions Htmt/AttributeParsers/InnerPartialAttributeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Htmt.AttributeParsers;
/// </summary>
public class InnerPartialAttributeParser : BaseAttributeParser
{
public override string XTag => "//*[@x:inner-partial]";
public override string XTag => "//*[@x:inner-partial or @data-htmt-inner-partial]";

public override void Parse(XmlNodeList? nodes)
{
Expand All @@ -21,7 +21,11 @@ public override void Parse(XmlNodeList? nodes)
{
if (node is not XmlElement n) continue;

var innerPartial = n.GetAttribute("x:inner-partial");
var innerPartial = string.IsNullOrEmpty(n.GetAttribute("x:inner-partial")) ?
n.GetAttribute("data-htmt-inner-partial") :
n.GetAttribute("x:inner-partial");

n.RemoveAttribute("data-htmt-inner-partial");
n.RemoveAttribute("x:inner-partial");

if (string.IsNullOrEmpty(innerPartial)) continue;
Expand Down
8 changes: 6 additions & 2 deletions Htmt/AttributeParsers/InnerTextAttributeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Htmt.AttributeParsers;
/// </summary>
public class InnerTextAttributeParser : BaseAttributeParser
{
public override string XTag => "//*[@x:inner-text]";
public override string XTag => "//*[@x:inner-text or @data-htmt-inner-text]";

public override void Parse(XmlNodeList? nodes)
{
Expand All @@ -21,7 +21,11 @@ public override void Parse(XmlNodeList? nodes)
{
if (node is not XmlElement n) continue;

var innerVal = n.GetAttribute("x:inner-text");
var innerVal = string.IsNullOrEmpty(n.GetAttribute("x:inner-text")) ?
n.GetAttribute("data-htmt-inner-text") :
n.GetAttribute("x:inner-text");

n.RemoveAttribute("data-htmt-inner-text");
n.RemoveAttribute("x:inner-text");

if (string.IsNullOrEmpty(innerVal)) continue;
Expand Down
9 changes: 6 additions & 3 deletions Htmt/AttributeParsers/OuterHtmlAttributeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Htmt.AttributeParsers;
/// </summary>
public class OuterHtmlAttributeParser : BaseAttributeParser
{
public override string XTag => "//*[@x:outer-html]";
public override string XTag => "//*[@x:outer-html or @data-htmt-outer-html]";

public override void Parse(XmlNodeList? nodes)
{
Expand All @@ -21,15 +21,18 @@ public override void Parse(XmlNodeList? nodes)
{
if (node is not XmlElement n) continue;

var outerHtmlVal = n.GetAttribute("x:outer-html");
var outerHtmlVal = string.IsNullOrEmpty(n.GetAttribute("x:outer-html")) ?
n.GetAttribute("data-htmt-outer-html") :
n.GetAttribute("x:outer-html");

n.RemoveAttribute("data-htmt-outer-html");
n.RemoveAttribute("x:outer-html");

if (string.IsNullOrEmpty(outerHtmlVal)) continue;

var outerXml = new XmlDocument();
outerXml.LoadXml($"<root>{ParseExpression(outerHtmlVal)}</root>");


if (outerXml.DocumentElement?.FirstChild == null) continue;

var importedNode = Xml.ImportNode(outerXml.DocumentElement.FirstChild, true);
Expand Down
8 changes: 6 additions & 2 deletions Htmt/AttributeParsers/OuterPartialAttributeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Htmt.AttributeParsers;
/// </summary>
public class OuterPartialAttributeParser : BaseAttributeParser
{
public override string XTag => "//*[@x:outer-partial]";
public override string XTag => "//*[@x:outer-partial or @data-htmt-outer-partial]";

public override void Parse(XmlNodeList? nodes)
{
Expand All @@ -21,7 +21,11 @@ public override void Parse(XmlNodeList? nodes)
{
if (node is not XmlElement n) continue;

var outerPartial = n.GetAttribute("x:outer-partial");
var outerPartial = string.IsNullOrEmpty(n.GetAttribute("x:outer-partial")) ?
n.GetAttribute("data-htmt-outer-partial") :
n.GetAttribute("x:outer-partial");

n.RemoveAttribute("data-htmt-outer-partial");
n.RemoveAttribute("x:outer-partial");

if (string.IsNullOrEmpty(outerPartial)) continue;
Expand Down
8 changes: 6 additions & 2 deletions Htmt/AttributeParsers/OuterTextAttributeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Htmt.AttributeParsers;
/// </summary>
public class OuterTextAttributeParser : BaseAttributeParser
{
public override string XTag => "//*[@x:outer-text]";
public override string XTag => "//*[@x:outer-text or @data-htmt-outer-text]";

public override void Parse(XmlNodeList? nodes)
{
Expand All @@ -22,7 +22,11 @@ public override void Parse(XmlNodeList? nodes)
{
if (node is not XmlElement n) continue;

var outerVal = n.GetAttribute("x:outer-text");
var outerVal = string.IsNullOrEmpty(n.GetAttribute("x:outer-text")) ?
n.GetAttribute("data-htmt-outer-text") :
n.GetAttribute("x:outer-text");

n.RemoveAttribute("data-htmt-outer-text");
n.RemoveAttribute("x:outer-text");

if (string.IsNullOrEmpty(outerVal)) continue;
Expand Down
8 changes: 6 additions & 2 deletions Htmt/AttributeParsers/UnlessAttributeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Htmt.AttributeParsers;
/// </summary>
public class UnlessAttributeParser : BaseAttributeParser
{
public override string XTag => "//*[@x:unless]";
public override string XTag => "//*[@x:unless or @data-htmt-unless]";

public override void Parse(XmlNodeList? nodes)
{
Expand All @@ -22,7 +22,11 @@ public override void Parse(XmlNodeList? nodes)
{
if (node is not XmlElement n) continue;

var key = n.GetAttribute("x:unless");
var key = string.IsNullOrEmpty(n.GetAttribute("x:unless")) ?
n.GetAttribute("data-htmt-unless") :
n.GetAttribute("x:unless");

n.RemoveAttribute("data-htmt-unless");
n.RemoveAttribute("x:unless");

// if key is a single word, we just check for a truthy value
Expand Down
12 changes: 5 additions & 7 deletions Htmt/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,7 @@ public string ToHtml()
{
Parse();

if (Xml.DocumentElement == null) return string.Empty;

return $"{_docType}{Xml.DocumentElement.InnerXml}";
return Xml.DocumentElement == null ? string.Empty : $"{_docType}{Xml.DocumentElement.InnerXml}";
}

/// <summary>
Expand Down Expand Up @@ -256,8 +254,8 @@ private void RunAttributeParsers()
parser.Parse(nodes);
}

// Remove all leftover attributes that start with x:
var leftOverNodes = Xml.DocumentElement?.SelectNodes("//*[@*[starts-with(name(), 'x:')]]", _nsManager);
// Remove all leftover attributes that start with x: or data-htmt-:
var leftOverNodes = Xml.DocumentElement?.SelectNodes("//*[@*[starts-with(name(), 'x:') or starts-with(name(), 'data-htmt-')]]", _nsManager);

if (leftOverNodes == null) return;

Expand All @@ -266,14 +264,14 @@ private void RunAttributeParsers()
if (node is not XmlElement element) continue;

var attributes = element.Attributes.Cast<XmlAttribute>()
.Where(a => a.Name.StartsWith("x:"))
.Where(a => a.Name.StartsWith("x:") || a.Name.StartsWith("data-htmt-"))
.ToList();

foreach (var attr in attributes)
{
element.RemoveAttribute(attr.Name);
}
}
}
}

/// <summary>
Expand Down
Loading

0 comments on commit 8e486c7

Please sign in to comment.