Skip to content

Commit e41fccd

Browse files
committed
fix: adjust IsFullPageRequest rule to !IsHtmxRequest || (IsBoosted && Target is null)
fixes #26
1 parent 3829d5c commit e41fccd

File tree

1 file changed

+39
-44
lines changed

1 file changed

+39
-44
lines changed

src/Htmxor/Http/HtmxRequest.cs

+39-44
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ namespace Htmxor.Http;
44

55
public sealed class HtmxRequest
66
{
7-
private readonly HttpContext context;
8-
97
/// <summary>
10-
/// Gets whether or not the current request should be treated as a full page request.
8+
/// Gets whether or not the current request will be treated as a full page request.
119
/// </summary>
12-
internal bool IsFullPageRequest => !IsHtmxRequest || IsBoosted;
10+
/// <remarks>
11+
/// The rules are as follows: <c>!IsHtmxRequest || (IsBoosted &amp;&amp; Target is null)</c>
12+
/// </remarks>
13+
public bool IsFullPageRequest => !IsHtmxRequest || (IsBoosted && Target is null);
1314

1415
/// <summary>
1516
/// Gets the HTTP method of the current request.
@@ -34,72 +35,66 @@ public sealed class HtmxRequest
3435
/// <summary>
3536
/// Gets the current URL of the browser.
3637
/// </summary>
37-
public Uri? CurrentURL
38-
=> IsHtmxRequest
39-
&& context.Request.Headers.TryGetValue(HtmxRequestHeaderNames.CurrentURL, out var values)
40-
&& values.Count > 0
41-
&& Uri.TryCreate(values[0], UriKind.RelativeOrAbsolute, out var uri)
42-
? uri
43-
: null;
38+
public Uri? CurrentURL { get; }
4439

4540
/// <summary>
4641
/// Gets the `id` of the target element if it exists.
4742
/// </summary>
48-
public string? Target
49-
=> IsHtmxRequest
50-
&& context.Request.Headers.TryGetValue(HtmxRequestHeaderNames.Target, out var values)
51-
&& values.Count > 0
52-
? values[0]
53-
: null;
43+
public string? Target { get; }
5444

5545
/// <summary>
5646
/// Gets the `name` of the triggered element if it exists.
5747
/// </summary>
58-
public string? TriggerName
59-
=> IsHtmxRequest
60-
&& context.Request.Headers.TryGetValue(HtmxRequestHeaderNames.TriggerName, out var values)
61-
&& values.Count > 0
62-
? values[0]
63-
: null;
48+
public string? TriggerName { get; }
6449

6550
/// <summary>
6651
/// Gets the `id` of the triggered element if it exists.
6752
/// </summary>
68-
public string? Trigger
69-
=> IsHtmxRequest
70-
&& context.Request.Headers.TryGetValue(HtmxRequestHeaderNames.Trigger, out var values)
71-
&& values.Count > 0
72-
? values[0]
73-
: null;
53+
public string? Trigger { get; }
7454

7555
/// <summary>
7656
/// Gets the user response to an hx-prompt, if any.
7757
/// </summary>
78-
public string? Prompt => IsHtmxRequest
79-
&& context.Request.Headers.TryGetValue(HtmxRequestHeaderNames.Prompt, out var values)
80-
&& values.Count > 0
81-
? values[0]
82-
: null;
58+
public string? Prompt { get; }
8359

8460
/// <summary>
8561
/// The `id` of the event handler to trigger on request.
8662
/// </summary>
87-
internal string? EventHandlerId => IsHtmxRequest
88-
&& context.Request.Headers.TryGetValue(HtmxRequestHeaderNames.EventHandlerId, out var values)
89-
&& values.Count > 0
90-
&& !string.IsNullOrWhiteSpace(values[0])
91-
? values[0]
92-
: null;
63+
internal string? EventHandlerId { get; }
9364

9465
/// <summary>
9566
/// Creates a new instance of <see cref="HtmxRequest"/>.
9667
/// </summary>
9768
public HtmxRequest(HttpContext context)
9869
{
99-
this.context = context;
100-
var ishtmx = IsHtmxRequest = context.Request.Headers.ContainsKey(HtmxRequestHeaderNames.HtmxRequest);
101-
IsBoosted = ishtmx && context.Request.Headers.ContainsKey(HtmxRequestHeaderNames.Boosted);
102-
IsHistoryRestoreRequest = ishtmx && context.Request.Headers.ContainsKey(HtmxRequestHeaderNames.HistoryRestoreRequest);
10370
Method = context.Request.Method;
71+
var ishtmx = IsHtmxRequest = context.Request.Headers.ContainsKey(HtmxRequestHeaderNames.HtmxRequest);
72+
73+
if (!ishtmx)
74+
{
75+
return;
76+
}
77+
78+
IsBoosted = context.Request.Headers.ContainsKey(HtmxRequestHeaderNames.Boosted);
79+
IsHistoryRestoreRequest = context.Request.Headers.ContainsKey(HtmxRequestHeaderNames.HistoryRestoreRequest);
80+
CurrentURL = GetHxValueOrDefault(context.Request.Headers, HtmxRequestHeaderNames.CurrentURL, static value => Uri.TryCreate(value, UriKind.RelativeOrAbsolute, out var uri) ? uri : null);
81+
Target = GetHxValueOrDefault(context.Request.Headers, HtmxRequestHeaderNames.Target);
82+
TriggerName = GetHxValueOrDefault(context.Request.Headers, HtmxRequestHeaderNames.TriggerName);
83+
Trigger = GetHxValueOrDefault(context.Request.Headers, HtmxRequestHeaderNames.Trigger);
84+
Prompt = GetHxValueOrDefault(context.Request.Headers, HtmxRequestHeaderNames.Prompt);
85+
EventHandlerId = GetHxValueOrDefault(context.Request.Headers, HtmxRequestHeaderNames.EventHandlerId);
10486
}
87+
88+
private static string? GetHxValueOrDefault(IHeaderDictionary headers, string key)
89+
=> headers.TryGetValue(key, out var values)
90+
&& values.Count > 0
91+
&& values[0] is var value
92+
&& !string.IsNullOrWhiteSpace(value)
93+
? value.Trim()
94+
: null;
95+
96+
private static T? GetHxValueOrDefault<T>(IHeaderDictionary headers, string key, Func<string, T?> factory)
97+
=> GetHxValueOrDefault(headers, key) is string value
98+
? factory.Invoke(value)
99+
: default(T);
105100
}

0 commit comments

Comments
 (0)