Skip to content

Commit c22a82a

Browse files
committed
feat: introduce SwapStyle.Default
1 parent f1f3338 commit c22a82a

File tree

7 files changed

+89
-30
lines changed

7 files changed

+89
-30
lines changed

src/Htmxor/HtmxConfig.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace Htmxor;
88
/// </summary>
99
public partial record class HtmxConfig
1010
{
11+
private SwapStyle? defaultSwapStyle;
12+
1113
/// <summary>
1214
/// Defaults to <see langword="true" /> if this property is null. really only useful for testing
1315
/// </summary>
@@ -31,7 +33,11 @@ public partial record class HtmxConfig
3133
/// Defaults to <see cref="SwapStyle.InnerHTML"/> if this property is null.
3234
/// </summary>
3335
[JsonPropertyName("defaultSwapStyle")]
34-
public SwapStyle? DefaultSwapStyle { get; set; }
36+
public SwapStyle? DefaultSwapStyle
37+
{
38+
get => defaultSwapStyle;
39+
set => defaultSwapStyle = value is SwapStyle.Default ? null : value;
40+
}
3541

3642
/// <summary>
3743
/// Defaults to <see langword="0"/> if this property is null.

src/Htmxor/Http/HtmxResponse.cs

+26-20
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,14 @@ public HtmxResponse ReplaceUrl(string url)
144144
/// <returns>This <see cref="HtmxResponse"/> object instance.</returns>
145145
public HtmxResponse Reswap(string modifier)
146146
{
147-
headers[HtmxResponseHeaderNames.Reswap] = modifier;
147+
if (string.IsNullOrWhiteSpace(modifier))
148+
{
149+
headers.Remove(HtmxResponseHeaderNames.Reswap);
150+
}
151+
else
152+
{
153+
headers[HtmxResponseHeaderNames.Reswap] = modifier;
154+
}
148155

149156
return this;
150157
}
@@ -159,37 +166,35 @@ public HtmxResponse Reswap(SwapStyle swapStyle, string? modifier = null)
159166
{
160167
AssertIsHtmxRequest();
161168

162-
var style = swapStyle switch
169+
if (swapStyle is SwapStyle.Default)
163170
{
164-
SwapStyle.InnerHTML => "innerHTML",
165-
SwapStyle.OuterHTML => "outerHTML",
166-
SwapStyle.BeforeBegin => "beforebegin",
167-
SwapStyle.AfterBegin => "afterbegin",
168-
SwapStyle.BeforeEnd => "beforeend",
169-
SwapStyle.AfterEnd => "afterend",
170-
SwapStyle.Delete => "delete",
171-
SwapStyle.None => "none",
172-
_ => throw new SwitchExpressionException(swapStyle),
173-
};
171+
Reswap(modifier);
172+
return this;
173+
}
174174

175-
var value = modifier != null ? $"{style} {modifier}" : style;
175+
var style = swapStyle.ToHtmxString();
176+
var value = !string.IsNullOrWhiteSpace(modifier)
177+
? $"{style} {modifier}"
178+
: style;
176179

177180
headers[HtmxResponseHeaderNames.Reswap] = value;
178181

179182
return this;
180183
}
181184

182185
/// <summary>
183-
/// Allows you to specify how the response will be swapped.
186+
/// Allows you to specify how the response will be swapped.
184187
/// </summary>
185188
/// <param></param>
186189
/// <param name="swapStyle"></param>
187-
/// <returns></returns>
190+
/// <returns>This <see cref="HtmxResponse"/> object instance.</returns>
188191
public HtmxResponse Reswap(SwapStyleBuilder swapStyle)
189192
{
190-
var (style, modifier) = swapStyle.Build();
193+
var (style, modifier) = swapStyle.Build();
191194

192-
return style is null ? Reswap(modifier) : Reswap((SwapStyle)style, modifier);
195+
return style is SwapStyle.Default
196+
? Reswap(modifier)
197+
: Reswap(style, modifier);
193198
}
194199

195200
/// <summary>
@@ -224,11 +229,12 @@ public HtmxResponse Reselect(string selector)
224229
/// Sets response code to stop polling
225230
/// </summary>
226231
/// <returns></returns>
232+
/// <returns>This <see cref="HtmxResponse"/> object instance.</returns>
227233
public HtmxResponse StopPolling()
228234
{
229-
context.Response.StatusCode = HtmxStatusCodes.StopPolling;
235+
context.Response.StatusCode = HtmxStatusCodes.StopPolling;
230236

231-
return this;
237+
return this;
232238
}
233239

234240
/// <summary>
@@ -314,7 +320,7 @@ public override string ToString()
314320

315321
private void AssertIsHtmxRequest()
316322
{
317-
if(!isHtmxRequest)
323+
if (!isHtmxRequest)
318324
{
319325
throw new InvalidOperationException(
320326
"The active request is not an htmx request. " +

src/Htmxor/Http/SwapStyleBuilder.cs

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ namespace Htmxor.Http;
77
/// <summary>
88
/// A builder class for constructing a swap style command string for HTMX responses.
99
/// </summary>
10-
public sealed class SwapStyleBuilder
10+
public sealed class SwapStyleBuilder
1111
{
12-
private readonly SwapStyle? style;
13-
private readonly OrderedDictionary modifiers = new();
12+
private readonly SwapStyle style;
13+
private readonly OrderedDictionary modifiers = new(StringComparer.Ordinal);
1414

1515
/// <summary>
1616
/// Initializes a new instance of the SwapStyleBuilder with a specified swap style.
1717
/// </summary>
1818
/// <param name="style">The initial swap style to be applied.</param>
19-
public SwapStyleBuilder(SwapStyle? style = null)
19+
public SwapStyleBuilder(SwapStyle style = SwapStyle.Default)
2020
{
2121
this.style = style;
2222
}
@@ -314,14 +314,15 @@ public SwapStyleBuilder ShowNone()
314314
/// <summary>
315315
/// Builds the swap style command string with all specified modifiers.
316316
/// </summary>
317-
/// <returns>A tuple containing the SwapStyle and the constructed command string.</returns>
318-
internal (SwapStyle?, string) Build()
317+
/// <returns>A tuple containing the <see cref="SwapStyle"/> and the constructed command string.</returns>
318+
internal (SwapStyle, string) Build()
319319
{
320320
var value = string.Empty;
321321

322322
if (modifiers.Count > 0)
323323
{
324-
value = modifiers.Cast<DictionaryEntry>()
324+
value = modifiers
325+
.Cast<DictionaryEntry>()
325326
.Select(entry => $"{entry.Key}:{entry.Value}")
326327
.Aggregate((current, next) => $"{current} {next}");
327328
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace Htmxor.Http;
4+
5+
public static class SwapStyleExtensions
6+
{
7+
/// <summary>
8+
/// Converts the <paramref name="swapStyle"/> to their string version expected by htmx.
9+
/// </summary>
10+
/// <param name="swapStyle">The style to convert.</param>
11+
public static string ToHtmxString(this SwapStyle swapStyle)
12+
{
13+
var style = swapStyle switch
14+
{
15+
SwapStyle.InnerHTML => "innerHTML",
16+
SwapStyle.OuterHTML => "outerHTML",
17+
SwapStyle.BeforeBegin => "beforebegin",
18+
SwapStyle.AfterBegin => "afterbegin",
19+
SwapStyle.BeforeEnd => "beforeend",
20+
SwapStyle.AfterEnd => "afterend",
21+
SwapStyle.Delete => "delete",
22+
SwapStyle.None => "none",
23+
SwapStyle.Default => "",
24+
_ => throw new SwitchExpressionException(swapStyle),
25+
};
26+
27+
return style;
28+
}
29+
}

src/Htmxor/SwapStyle.cs

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
/// </summary>
66
public enum SwapStyle
77
{
8+
/// <summary>
9+
/// Default style is what is specified in <see cref="HtmxConfig.DefaultSwapStyle"/> for the application
10+
/// or htmx's default, which is <see cref="InnerHTML"/>.
11+
/// </summary>
12+
Default,
13+
814
/// <summary>
915
/// Replace the inner html of the target element.
1016
/// </summary>

test/Htmxor.Tests/Configuration/HtmxHeadOutletTest.cs

+11
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,15 @@ public void HtmxConfig_serializer()
9090
}
9191
""");
9292
}
93+
94+
[Fact]
95+
public void HtmxConfig_default_swap_style_serializer()
96+
{
97+
var config = new HtmxConfig
98+
{
99+
DefaultSwapStyle = SwapStyle.Default,
100+
};
101+
102+
config.DefaultSwapStyle.Should().BeNull();
103+
}
93104
}

test/Htmxor.Tests/Http/SwapStyleBuilderTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ public void SwapStyleBuilder_ShowWindow_BottomDirection_AddsCorrectModifier()
191191
public void SwapStyleBuilder_NullSwapStyle_ReturnsNullStyle()
192192
{
193193
// Arrange & Act
194-
var builder = new SwapStyleBuilder(null);
194+
var builder = new SwapStyleBuilder();
195195
var (style, _) = builder.Build();
196196

197197
// Assert
198-
style.Should().BeNull();
198+
style.Should().Be(SwapStyle.Default);
199199
}
200200

201201
[Fact]

0 commit comments

Comments
 (0)