Skip to content
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ For example, values declared on a parent element can be inherited by its childre
</div>
```

When `HtmxV4Config.MetaCharacter` is configured, that character is used instead of `:` in the generated attribute name.
Alternatively, set `HtmxV4Config.ImplicitInheritance` to `true` to enable inheritance globally.
For HTMX 1.9.x and 2.x, the Tag Helper attributes above do not change the generated attribute names.

Expand Down
10 changes: 0 additions & 10 deletions src/Ramstack.HtmxToolkit/Configuration/HtmxV4Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ public string? Prefix
set => SetField(ref field, value);
}

/// <summary>
/// Gets or sets the character used instead of <c>:</c> in attribute names.
/// The HTMX default is <c>undefined</c>.
/// </summary>
public string? MetaCharacter
{
get;
set => SetField(ref field, value);
}

/// <summary>
/// Gets or sets how HTMX history restoration is handled.
/// The HTMX default is <see cref="HtmxHistoryMode.Enabled" />.
Expand Down
7 changes: 1 addition & 6 deletions src/Ramstack.HtmxToolkit/TagHelpers/HtmxHeaderTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ public override Task ProcessAsync(TagHelperContext context, TagHelperOutput outp
{
var name = "hx-headers";
if (Inherited && options.Value.TargetVersion == HtmxTargetVersion.V4)
{
if (options.Value.HtmxConfig is HtmxV4Config config)
name = string.IsNullOrEmpty(config.MetaCharacter)
? "hx-headers:inherited"
: $"hx-headers{config.MetaCharacter}inherited";
}
name = "hx-headers:inherited";

var info = HtmxDictionaryJsonSerializerContext.Default.IDictionaryStringString;
var json = JsonSerializer.Serialize(Headers, info);
Expand Down
20 changes: 5 additions & 15 deletions src/Ramstack.HtmxToolkit/TagHelpers/HtmxRequestTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,12 @@ public override Task ProcessAsync(TagHelperContext context, TagHelperOutput outp

if (json != "{}")
{
var name = "hx-request";

if (options.Value.TargetVersion == HtmxTargetVersion.V4)
var name = options.Value.TargetVersion switch
{
if (Inherited)
{
if (options.Value.HtmxConfig is HtmxV4Config config)
name = string.IsNullOrEmpty(config.MetaCharacter)
? "hx-config:inherited"
: $"hx-config{config.MetaCharacter}inherited";
}
else
{
name = "hx-config";
}
}
HtmxTargetVersion.V4 when Inherited => "hx-config:inherited",
HtmxTargetVersion.V4 => "hx-config",
_ => "hx-request"
};

output.Attributes.SetAttribute(
new TagHelperAttribute(name, new HtmlString(json), HtmlAttributeValueStyle.SingleQuotes));
Expand Down
5 changes: 1 addition & 4 deletions src/Ramstack.HtmxToolkit/TagHelpers/HtmxValsTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public override Task ProcessAsync(TagHelperContext context, TagHelperOutput outp
{
var name = "hx-vals";
if (Inherited && options.Value.TargetVersion == HtmxTargetVersion.V4)
if (options.Value.HtmxConfig is HtmxV4Config config)
name = string.IsNullOrEmpty(config.MetaCharacter)
? "hx-vals:inherited"
: $"hx-vals{config.MetaCharacter}inherited";
name = "hx-vals:inherited";

var info = HtmxDictionaryJsonSerializerContext.Default.IDictionaryStringString;
var json = JsonSerializer.Serialize(values, info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ public async Task ProcessAsync_SerializesOnlyHtmx4Options()
{
htmx.LogAll = true;
htmx.Prefix = "data-custom-";
htmx.MetaCharacter = "-";
htmx.History = HtmxHistoryMode.Reload;
htmx.DefaultSwap = HtmxSwap.OuterMorph;
htmx.AllowEmptySwapAfterOob = true;
Expand All @@ -273,7 +272,7 @@ public async Task ProcessAsync_SerializesOnlyHtmx4Options()

Assert.That(json.Keys, Is.EquivalentTo(new[]
{
"logAll", "prefix", "metaCharacter", "history", "defaultSwap", "allowEmptySwapAfterOOB",
"logAll", "prefix", "history", "defaultSwap", "allowEmptySwapAfterOOB",
"defaultSettleDelay", "includeIndicatorCSS", "indicatorClass", "requestClass",
"inlineScriptNonce", "extensions", "implicitInheritance", "defaultTimeout", "mode",
"defaultFocusScroll", "transitions", "morphIgnore", "morphSkip", "morphSkipChildren",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,6 @@ public async Task ProcessAsync_Htmx4_Inherited_UsesInheritedAttribute()
Assert.That(output.Attributes["hx-headers"], Is.Null);
}

[Test]
public async Task ProcessAsync_Htmx4_Inherited_UsesConfiguredMetaCharacter()
{
var output = TestHelper.CreateTagHelperOutput();
var helper = CreateHelper(HtmxTargetVersion.V4, "-");

helper.Inherited = true;
helper.Headers["X-Custom"] = "value";

await helper.ProcessAsync(TestHelper.CreateTagHelperContext(), output);
var attribute = output.Attributes["hx-headers-inherited"];

Assert.That(attribute!.Value.ToString(), Is.EqualTo("{\"X-Custom\":\"value\"}"));
Assert.That(output.Attributes["hx-headers:inherited"], Is.Null);
}

[Test]
public async Task ProcessAsync_Htmx4_NotInherited_UsesOrdinaryAttribute()
{
Expand Down Expand Up @@ -140,7 +124,7 @@ public async Task ProcessAsync_Htmx1_Htmx2_Inherited_UsesOrdinaryAttribute(HtmxT
Assert.That(output.Attributes["hx-inherit"], Is.Null);
}

private static HtmxHeaderTagHelper CreateHelper(HtmxTargetVersion version = HtmxTargetVersion.V2, string? metachar = null)
private static HtmxHeaderTagHelper CreateHelper(HtmxTargetVersion version = HtmxTargetVersion.V2)
{
var options = new HtmxToolkitOptions();

Expand All @@ -153,7 +137,7 @@ private static HtmxHeaderTagHelper CreateHelper(HtmxTargetVersion version = Htmx
options.UseHtmxV2();
break;
case HtmxTargetVersion.V4:
options.UseHtmxV4(config => config.MetaCharacter = metachar);
options.UseHtmxV4();
break;
default:
throw new ArgumentOutOfRangeException(nameof(version));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,6 @@ public async Task ProcessAsync_Htmx4_Inherited_UsesInheritedAttribute()
Assert.That(output.Attributes["hx-config"], Is.Null);
}

[Test]
public async Task ProcessAsync_Htmx4_Inherited_UsesConfiguredMetaCharacter()
{
var output = TestHelper.CreateTagHelperOutput();
var helper = CreateHelper(HtmxTargetVersion.V4, "-");

helper.Inherited = true;
helper.Timeout = 500;

await helper.ProcessAsync(TestHelper.CreateTagHelperContext(), output);
var attribute = output.Attributes["hx-config-inherited"];

Assert.That(attribute!.Value.ToString(), Is.EqualTo("{\"timeout\":500}"));
Assert.That(output.Attributes["hx-config:inherited"], Is.Null);
}

[Test]
public async Task ProcessAsync_Htmx4_Inherited_WithImplicitInheritance_StillUsesInheritedAttribute()
{
Expand Down Expand Up @@ -269,7 +253,7 @@ public async Task ProcessAsync_Htmx1_Htmx2_Inherited_UsesOrdinaryAttribute(HtmxT
Assert.That(output.Attributes["hx-inherit"], Is.Null);
}

private static HtmxRequestTagHelper CreateHelper(HtmxTargetVersion version, string? metachar = null, bool? implicitInheritance = null)
private static HtmxRequestTagHelper CreateHelper(HtmxTargetVersion version, bool? implicitInheritance = null)
{
var options = new HtmxToolkitOptions();

Expand All @@ -282,11 +266,7 @@ private static HtmxRequestTagHelper CreateHelper(HtmxTargetVersion version, stri
options.UseHtmxV2();
break;
case HtmxTargetVersion.V4:
options.UseHtmxV4(config =>
{
config.MetaCharacter = metachar;
config.ImplicitInheritance = implicitInheritance;
});
options.UseHtmxV4(config => config.ImplicitInheritance = implicitInheritance);
break;
default:
throw new ArgumentOutOfRangeException(nameof(version));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,6 @@ public async Task ProcessAsync_Htmx4_Inherited_UsesInheritedAttribute()
Assert.That(output.Attributes["hx-vals"], Is.Null);
}

[Test]
public async Task ProcessAsync_Htmx4_Inherited_UsesConfiguredMetaCharacter()
{
var output = TestHelper.CreateTagHelperOutput();
var helper = CreateHelper(HtmxTargetVersion.V4, "-");

helper.Inherited = true;
helper.Values["sort"] = "title";

await helper.ProcessAsync(TestHelper.CreateTagHelperContext(), output);
var attribute = output.Attributes["hx-vals-inherited"];

Assert.That(attribute!.Value.ToString(), Is.EqualTo("{\"sort\":\"title\"}"));
Assert.That(output.Attributes["hx-vals:inherited"], Is.Null);
}

[Test]
public async Task ProcessAsync_Htmx4_NotInherited_UsesOrdinaryAttribute()
{
Expand Down Expand Up @@ -135,7 +119,7 @@ public async Task ProcessAsync_Htmx1_Htmx2_Inherited_UsesOrdinaryAttribute(HtmxT
Assert.That(output.Attributes["hx-inherit"], Is.Null);
}

private static HtmxValsTagHelper CreateHelper(HtmxTargetVersion version = HtmxTargetVersion.V2, string? metachar = null)
private static HtmxValsTagHelper CreateHelper(HtmxTargetVersion version = HtmxTargetVersion.V2)
{
var options = new HtmxToolkitOptions();

Expand All @@ -148,7 +132,7 @@ private static HtmxValsTagHelper CreateHelper(HtmxTargetVersion version = HtmxTa
options.UseHtmxV2();
break;
case HtmxTargetVersion.V4:
options.UseHtmxV4(config => config.MetaCharacter = metachar);
options.UseHtmxV4();
break;
default:
throw new ArgumentOutOfRangeException(nameof(version));
Expand Down