Compare commits
52 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cfd8d7eb68 | |||
| 8f93aa4d35 | |||
| 121eb95d87 | |||
| 6b7458070b | |||
| 10c31d77b5 | |||
| f4cd184a15 | |||
| 5626031593 | |||
| f0de18845f | |||
| 961307fd99 | |||
| a2960a79c6 | |||
| 541384a92f | |||
| 8cb5d6bfa7 | |||
| 1a11e3511a | |||
| 32e962416d | |||
| 29839fa795 | |||
| 7f9b3a6234 | |||
| a8d2b819dc | |||
| 1ea05b79ed | |||
| 4f6bb8367a | |||
| b3665a4b72 | |||
| bdb7255a1c | |||
| 9276db7ffe | |||
| 94b68f76c9 | |||
| 90de16529c | |||
| 452f15b2d4 | |||
| 59246ae7f5 | |||
| 16355e8f58 | |||
| 71286b9064 | |||
| 85ac22877c | |||
| dea2323499 | |||
| fcaaa693bc | |||
| 8fd6af8c26 | |||
| 69a4034661 | |||
| 5aa9408e60 | |||
| 98f53a5e53 | |||
| 7798314fb8 | |||
| 50c66435e8 | |||
| 5b3a1be87e | |||
| 168f434030 | |||
| e3db0bced0 | |||
| fb5c4f1bcb | |||
| c3229d51d1 | |||
| 951edef8ad | |||
| 3908451d6e | |||
| 277d93dd99 | |||
| 061f6e5a4e | |||
| bb2df73175 | |||
| e0c567098d | |||
| 4be5bad8ef | |||
| a169000ce2 | |||
| c587a28770 | |||
| b5292bffc4 |
@@ -0,0 +1,57 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "main", "htmx-version-2" ]
|
||||
pull_request:
|
||||
branches: [ "main" ]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build-and-test:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
dotnet-version: [ "6.0", "7.0", "8.0" ]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Setup .NET ${{ matrix.dotnet-version }}.x
|
||||
uses: actions/setup-dotnet@v4
|
||||
with:
|
||||
dotnet-version: ${{ matrix.dotnet-version }}.x
|
||||
- name: Restore dependencies
|
||||
run: dotnet restore src/Giraffe.Htmx.sln
|
||||
- name: Build
|
||||
run: dotnet build src/Giraffe.Htmx.sln --no-restore
|
||||
- name: Test (.NET ${{ matrix.dotnet-version }})
|
||||
run: dotnet run --project src/Tests/Tests.fsproj -f net${{ matrix.dotnet-version }}
|
||||
publish:
|
||||
runs-on: ubuntu-latest
|
||||
needs: build-and-test
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Setup .NET
|
||||
uses: actions/setup-dotnet@v4
|
||||
with:
|
||||
dotnet-version: "8.0"
|
||||
- name: Package Common library
|
||||
run: dotnet pack src/Common/Giraffe.Htmx.Common.fsproj -c Release
|
||||
- name: Move Common package
|
||||
run: cp src/Common/bin/Release/Giraffe.Htmx.Common.*.nupkg .
|
||||
- name: Package Server library
|
||||
run: dotnet pack src/Htmx/Giraffe.Htmx.fsproj -c Release
|
||||
- name: Move Server package
|
||||
run: cp src/Htmx/bin/Release/Giraffe.Htmx.*.nupkg .
|
||||
- name: Package View Engine library
|
||||
run: dotnet pack src/ViewEngine.Htmx/Giraffe.ViewEngine.Htmx.fsproj -c Release
|
||||
- name: Move View Engine package
|
||||
run: cp src/ViewEngine.Htmx/bin/Release/Giraffe.ViewEngine.Htmx.*.nupkg .
|
||||
- name: Save Packages
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: packages
|
||||
path: |
|
||||
*.nupkg
|
||||
@@ -5,3 +5,7 @@
|
||||
.ionide
|
||||
.idea
|
||||
*.user
|
||||
.vscode
|
||||
src/*.nupkg
|
||||
src/tests*.txt
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
htmx uses attributes and HTTP headers to attain its interactivity; the libraries here contain extensions to both Giraffe and Giraffe View Engine to enable strongly-typed development of htmx applications.
|
||||
|
||||
> **NOTE** htmx v4 is curently in beta; this library is supporting it on the [`htmx-v4` branch](https://git.bitbadger.solutions/bit-badger/Giraffe.Htmx/src/branch/htmx-v4/). Beta versions of the v4 library include warnings about obsolete constructs. The htmx project intends to continue supporting the 2.x line (just as they still do the 1.x line, which we track on the [`htmx-v1` branch](https://git.bitbadger.solutions/bit-badger/Giraffe.Htmx/src/branch/htmx-v1/)); the upgrade is available, not forced. See the [htmx v4 migration docs](https://four.htmx.org/docs#migration) for more information.
|
||||
|
||||
## Installation
|
||||
|
||||
`Giraffe.Htmx` provides extensions that facilitate using htmx on the server side, primarily reading and setting headers. `Giraffe.ViewEngine.Htmx` provides attributes and helpers to produce views that utilize htmx. Both can be installed from NuGet via standard methods.
|
||||
@@ -14,6 +16,8 @@ htmx uses attributes and HTTP headers to attain its interactivity; the libraries
|
||||
|---|---|
|
||||
|[](https://www.nuget.org/packages/Giraffe.Htmx/)|[](https://www.nuget.org/packages/Giraffe.ViewEngine.Htmx/)|
|
||||
|
||||
Both of these packages will also install `Giraffe.Htmx.Common`, which has some common definitions and provides a local-to-your-project version of the htmx JavaScript _(as of v2.0.8)_.
|
||||
|
||||
## Server Side (`Giraffe.Htmx`)
|
||||
|
||||
In addition to the regular HTTP request payloads, htmx sets [one or more headers](https://htmx.org/docs/#request_headers) along with the request. Once `Giraffe.Htmx` is opened, these are available as properties on `HttpContext.Request.Headers`. These consist of the header name, translated to a .NET name (ex. `HX-Current-URL` becomes `HxCurrentUrl`), and a strongly-typed property based on the expected value of that header. Additionally, they are all exposed as `Option`s, as they may or may not be present for any given request.
|
||||
@@ -21,19 +25,20 @@ In addition to the regular HTTP request payloads, htmx sets [one or more headers
|
||||
A server may want to respond to a request that originated from htmx differently than a regular request. One way htmx can provide the same feel as a Single Page Application (SPA) is by swapping out the `body` content (or an element within it) instead of reloading the entire page. In this case, the developer can provide a partial layout to be used for these responses, while returning the full page for regular requests. The `IsHtmx` property makes this easy...
|
||||
|
||||
```fsharp
|
||||
// "partial" and "full" are handlers that return the contents;
|
||||
// "view" can be whatever your view engine needs for the body of the page
|
||||
let result view : HttpHandler =
|
||||
// "partial" and "full" are handlers that return the contents;
|
||||
// "view" can be whatever your view engine needs for the body of the page
|
||||
let result view : HttpHandler =
|
||||
fun next ctx ->
|
||||
match ctx.Request.IsHtmx && not ctx.Request.IsHtmxRefresh with
|
||||
| true -> partial view
|
||||
| false -> full view
|
||||
if ctx.Request.IsHtmx && not ctx.Request.IsHtmxRefresh then
|
||||
partial view
|
||||
else
|
||||
full view
|
||||
```
|
||||
|
||||
htmx also utilizes [response headers](https://htmx.org/docs/#response_headers) to affect client-side behavior. For each of these, this library provides `HttpHandler`s that can be chained along with the response. As an example, if the server returns a redirect response (301, 302, 303, 307), the `XMLHttpRequest` handler on the client will follow the redirection before htmx can do anything with it. To redirect to a new page, you would return an OK (200) response with an `HX-Redirect` header set in the response.
|
||||
|
||||
```fsharp
|
||||
let theHandler : HttpHandler =
|
||||
let theHandler : HttpHandler =
|
||||
fun next ctx ->
|
||||
// some interesting stuff
|
||||
withHxRedirect "/the-new-url" >=> Successful.OK
|
||||
@@ -41,6 +46,8 @@ htmx also utilizes [response headers](https://htmx.org/docs/#response_headers) t
|
||||
|
||||
Of note is that the `HX-Trigger` headers can take either one or more events. For a single event with no parameters, use `withHxTrigger`; for a single event with parameters, or multiple events, use `withHxTriggerMany`. Both these have `AfterSettle` and `AfterSwap` versions as well.
|
||||
|
||||
`HtmxScript.local` provides an `HtmlString` with a script tag to load the package-provided htmx library. This can be used in code, Razor templates, etc. (If you're using Giraffe.ViewEngine, see below.)
|
||||
|
||||
## View Engine (`Giraffe.ViewEngine.Htmx`)
|
||||
|
||||
As htmx uses [attributes](https://htmx.org/docs/#attributes) to extend HTML, the primary part of this library defines attributes that can be used within Giraffe views. Simply open `Giraffe.ViewEngine.Htmx`, and these attributes, along with support modules, will be visible.
|
||||
@@ -48,8 +55,10 @@ As htmx uses [attributes](https://htmx.org/docs/#attributes) to extend HTML, the
|
||||
As an example, creating a `div` that loads data once the HTML is rendered:
|
||||
|
||||
```fsharp
|
||||
let autoload =
|
||||
div [ _hxGet "/lazy-load-data"; _hxTrigger "load" ] [ str "Loading..." ]
|
||||
let autoload =
|
||||
div [ _hxGet "/lazy-load-data"; _hxTrigger HxTrigger.Load ] [
|
||||
str "Loading..."
|
||||
]
|
||||
```
|
||||
|
||||
_(As `hx-boost="true"` is the usual desire for boosting, `_hxBoost` implies true. To disable it for an element, use `_hxNoBoost` instead.)_
|
||||
@@ -57,20 +66,20 @@ _(As `hx-boost="true"` is the usual desire for boosting, `_hxBoost` implies true
|
||||
Some attributes have known values, such as `hx-trigger` and `hx-swap`; for these, there are modules with those values. For example, `HxTrigger.Load` could be used in the example above, to ensure that the known values are spelled correctly. `hx-trigger` can also take modifiers, such as an action that only responds to `Ctrl`+click. The `HxTrigger` module has a `Filter` submodule to assist with defining these actions.
|
||||
|
||||
```fsharp
|
||||
let shiftClick =
|
||||
let shiftClick =
|
||||
p [ _hxGet = "/something"; _hxTrigger (HxTrigger.Filter.Shift HxTrigger.Click) ] [
|
||||
str "hold down Shift and click me"
|
||||
]
|
||||
```
|
||||
|
||||
If you want to load htmx from unpkg, `Htmx.Script.minified` or `Htmx.Script.unminified` can be used to load the script in your HTML trees.
|
||||
If you want to use the package-provided htmx library, `Htmx.Script.local` will create the `script` tag for you. To load htmx from jsDelivr, `Htmx.Script.cdnMinified` or `Htmx.Script.cdnUnminified` can be used to load the script in your HTML trees. In this case, if you are using a Content Security Policy (CSP) header, `cdn.jsdelivr.net` will need to be added to the `script-src` list.
|
||||
|
||||
## Feedback / Help
|
||||
|
||||
The author hangs out in the #htmx-general channel of the [htmx Discord server](https://htmx.org/discord) and the #web channel of the [F# Software Foundation's Slack server](https://fsharp.org/guides/slack/).
|
||||
The author hangs out in the #dotnet-htmx channel (and most others) of the [htmx Discord server](https://htmx.org/discord) and the #web channel of the [F# Software Foundation's Slack server](https://fsharp.org/guides/slack/).
|
||||
|
||||
## Thanks
|
||||
|[<img src="https://giraffe.wiki/giraffe.png" alt="Giraffe logo" width="200">](https://giraffe.wiki)|[<img src="https://bitbadger.solutions/htmx-black-transparent.svg" alt="htmx logo" width="200">](https://htmx.org)|[<img src="https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.png" alt="JetBrains Logo (Main)" width="200">](https://jb.gg/OpenSource)|
|
||||
| :---: | :---: | :---: |
|
||||
|for making ASP.NET Core functional|for making HTML cool again|for licensing their tools to this project|
|
||||
|[<img src="https://giraffe.wiki/giraffe.png" alt="Giraffe logo" width="200">](https://giraffe.wiki)| [<img src="https://bitbadger.solutions/upload/bit-badger/2024/01/htmx-black-transparent.svg" alt="htmx logo" width="200">](https://htmx.org) |[<img src="https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.png" alt="JetBrains Logo (Main)" width="200">](https://jb.gg/OpenSource)|
|
||||
| :---: |:------------------------------------------------------------------------------------------------------------------------------------:| :---: |
|
||||
|for making ASP.NET Core functional| for making HTML cool again |for licensing their tools to this project|
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/// <summary>Common definitions shared between attribute values and response headers</summary>
|
||||
[<AutoOpen>]
|
||||
module Giraffe.Htmx.Common
|
||||
|
||||
/// <summary>The version of htmx embedded in the package</summary>
|
||||
let HtmxVersion = "2.0.10"
|
||||
|
||||
/// <summary>The path for the provided htmx script</summary>
|
||||
let internal htmxLocalScript = $"/_content/Giraffe.Htmx.Common/htmx.min.js?ver={HtmxVersion}"
|
||||
|
||||
/// <summary>Serialize a list of key/value pairs to JSON (very rudimentary)</summary>
|
||||
/// <param name="pairs">The key/value pairs to be serialized to JSON</param>
|
||||
/// <returns>A string with the key/value pairs serialized to JSON</returns>
|
||||
let internal toJson (pairs: (string * string) list) =
|
||||
pairs
|
||||
|> List.map (fun pair -> sprintf "\"%s\": \"%s\"" (fst pair) ((snd pair).Replace ("\"", "\\\"")))
|
||||
|> String.concat ", "
|
||||
|> sprintf "{ %s }"
|
||||
|
||||
/// <summary>Convert a boolean to lowercase "true" or "false"</summary>
|
||||
/// <param name="boolValue">The boolean value to convert</param>
|
||||
/// <returns>"true" for <c>true</c>, "false" for <c>false</c></returns>
|
||||
let internal toLowerBool (boolValue: bool) =
|
||||
(string boolValue).ToLowerInvariant()
|
||||
|
||||
|
||||
/// <summary>Valid values for the <c>hx-swap</c> attribute / <c>HX-Reswap</c> header</summary>
|
||||
/// <remarks>May be combined with <c>swap</c> / <c>settle</c> / <c>scroll</c> / <c>show</c> config)</remarks>
|
||||
/// <seealso href="https://htmx.org/attributes/hx-swap/">Documentation</seealso>
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxSwap =
|
||||
|
||||
/// <summary>The default, replace the inner HTML of the target element</summary>
|
||||
[<Literal>]
|
||||
let InnerHtml = "innerHTML"
|
||||
|
||||
/// <summary>Replace the entire target element with the response</summary>
|
||||
[<Literal>]
|
||||
let OuterHtml = "outerHTML"
|
||||
|
||||
/// <summary>Insert the response before the target element</summary>
|
||||
[<Literal>]
|
||||
let BeforeBegin = "beforebegin"
|
||||
|
||||
/// <summary>Insert the response before the first child of the target element</summary>
|
||||
[<Literal>]
|
||||
let AfterBegin = "afterbegin"
|
||||
|
||||
/// <summary>Insert the response after the last child of the target element</summary>
|
||||
[<Literal>]
|
||||
let BeforeEnd = "beforeend"
|
||||
|
||||
/// <summary>Insert the response after the target element</summary>
|
||||
[<Literal>]
|
||||
let AfterEnd = "afterend"
|
||||
|
||||
/// <summary>Does not append content from response (out of band items will still be processed).</summary>
|
||||
[<Literal>]
|
||||
let None = "none"
|
||||
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Razor">
|
||||
|
||||
<PropertyGroup>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<Description>Common definitions for Giraffe.Htmx</Description>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Common.fs" />
|
||||
<None Include="README.md" Pack="true" PackagePath="\" />
|
||||
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Update="FSharp.Core" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="Giraffe.Htmx" />
|
||||
<InternalsVisibleTo Include="Giraffe.ViewEngine.Htmx" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,5 @@
|
||||
## Giraffe.Htmx.Common
|
||||
|
||||
This package contains common code shared between [`Giraffe.Htmx`](https://www.nuget.org/packages/Giraffe.Htmx) and [`Giraffe.ViewEngine.Htmx`](https://www.nuget.org/packages/Giraffe.ViewEngine.Htmx), and will be automatically installed when you install either one. It also contains htmx as a static web asset, allowing it to be loaded from your local (or published) project.
|
||||
|
||||
**htmx version: 2.0.10**
|
||||
Vendored
+1
File diff suppressed because one or more lines are too long
@@ -1,13 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>1.6.1</VersionPrefix>
|
||||
<PackageReleaseNotes>Initial production-ready release</PackageReleaseNotes>
|
||||
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
|
||||
<VersionPrefix>2.0.10</VersionPrefix>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<PackageReleaseNotes>- Updates provided script and CDN links to serve htmx 2.0.10 (no header or attribute changes)
|
||||
|
||||
NOTE: As of 2.0.6, the CDN for htmx changed from unpkg.com to cdn.jsdelivr.net; sites with Content-Security-Policy headers will want to update their allowed script-src domains accordingly
|
||||
</PackageReleaseNotes>
|
||||
<Authors>danieljsummers</Authors>
|
||||
<Company>Bit Badger Solutions</Company>
|
||||
<PackageProjectUrl>https://github.com/bit-badger/Giraffe.Htmx</PackageProjectUrl>
|
||||
<PackageProjectUrl>https://git.bitbadger.solutions/bit-badger/Giraffe.Htmx</PackageProjectUrl>
|
||||
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
|
||||
<RepositoryUrl>https://github.com/bit-badger/Giraffe.Htmx</RepositoryUrl>
|
||||
<RepositoryUrl>https://git.bitbadger.solutions/bit-badger/Giraffe.Htmx</RepositoryUrl>
|
||||
<RepositoryType>Git</RepositoryType>
|
||||
<Copyright>MIT License</Copyright>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<Solution>
|
||||
<Project Path="Common/Giraffe.Htmx.Common.fsproj" />
|
||||
<Project Path="Htmx/Giraffe.Htmx.fsproj" />
|
||||
<Project Path="Tests/Tests.fsproj" />
|
||||
<Project Path="ViewEngine.Htmx/Giraffe.ViewEngine.Htmx.fsproj" />
|
||||
</Solution>
|
||||
@@ -1,34 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<GenerateProgramFile>false</GenerateProgramFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Tests.fs" />
|
||||
<Compile Include="Program.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Giraffe" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
|
||||
<PackageReference Include="NSubstitute" Version="4.2.2" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Htmx\Giraffe.Htmx.fsproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1 +0,0 @@
|
||||
module Program = let [<EntryPoint>] main _ = 0
|
||||
@@ -1,329 +0,0 @@
|
||||
module Giraffe.Htmx.Tests
|
||||
|
||||
open System
|
||||
open Giraffe.Htmx
|
||||
open Microsoft.AspNetCore.Http
|
||||
open NSubstitute
|
||||
open Xunit
|
||||
|
||||
/// Tests for the IHeaderDictionary extension properties
|
||||
module IHeaderDictionaryExtensions =
|
||||
|
||||
[<Fact>]
|
||||
let ``HxBoosted succeeds when the header is not present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore
|
||||
Option.isNone ctx.Request.Headers.HxBoosted |> Assert.True
|
||||
|
||||
[<Fact>]
|
||||
let ``HxBoosted succeeds when the header is present and true`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-Boosted", "true")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Option.isSome ctx.Request.Headers.HxBoosted |> Assert.True
|
||||
Option.get ctx.Request.Headers.HxBoosted |> Assert.True
|
||||
|
||||
[<Fact>]
|
||||
let ``HxBoosted succeeds when the header is present and false`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-Boosted", "false")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Option.isSome ctx.Request.Headers.HxBoosted |> Assert.True
|
||||
Option.get ctx.Request.Headers.HxBoosted |> Assert.False
|
||||
|
||||
[<Fact>]
|
||||
let ``HxCurrentUrl succeeds when the header is not present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore
|
||||
Option.isNone ctx.Request.Headers.HxCurrentUrl |> Assert.True
|
||||
|
||||
[<Fact>]
|
||||
let ``HxCurrentUrl succeeds when the header is present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-Current-URL", "http://localhost/test.htm")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Option.isSome ctx.Request.Headers.HxCurrentUrl |> Assert.True
|
||||
Assert.Equal (Uri "http://localhost/test.htm", Option.get ctx.Request.Headers.HxCurrentUrl)
|
||||
|
||||
[<Fact>]
|
||||
let ``HxHistoryRestoreRequest succeeds when the header is not present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore
|
||||
Option.isNone ctx.Request.Headers.HxHistoryRestoreRequest |> Assert.True
|
||||
|
||||
[<Fact>]
|
||||
let ``HxHistoryRestoreRequest succeeds when the header is present and true`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-History-Restore-Request", "true")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Option.isSome ctx.Request.Headers.HxHistoryRestoreRequest |> Assert.True
|
||||
Option.get ctx.Request.Headers.HxHistoryRestoreRequest |> Assert.True
|
||||
|
||||
[<Fact>]
|
||||
let ``HxHistoryRestoreRequest succeeds when the header is present and false`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-History-Restore-Request", "false")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Option.isSome ctx.Request.Headers.HxHistoryRestoreRequest |> Assert.True
|
||||
Option.get ctx.Request.Headers.HxHistoryRestoreRequest |> Assert.False
|
||||
|
||||
[<Fact>]
|
||||
let ``HxPrompt succeeds when the header is not present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore
|
||||
Option.isNone ctx.Request.Headers.HxPrompt |> Assert.True
|
||||
|
||||
[<Fact>]
|
||||
let ``HxPrompt succeeds when the header is present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-Prompt", "of course")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Option.isSome ctx.Request.Headers.HxPrompt |> Assert.True
|
||||
Assert.Equal("of course", Option.get ctx.Request.Headers.HxPrompt)
|
||||
|
||||
[<Fact>]
|
||||
let ``HxRequest succeeds when the header is not present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore
|
||||
Option.isNone ctx.Request.Headers.HxRequest |> Assert.True
|
||||
|
||||
[<Fact>]
|
||||
let ``HxRequest succeeds when the header is present and true`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-Request", "true")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Option.isSome ctx.Request.Headers.HxRequest |> Assert.True
|
||||
Option.get ctx.Request.Headers.HxRequest |> Assert.True
|
||||
|
||||
[<Fact>]
|
||||
let ``HxRequest succeeds when the header is present and false`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-Request", "false")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Option.isSome ctx.Request.Headers.HxRequest |> Assert.True
|
||||
Option.get ctx.Request.Headers.HxRequest |> Assert.False
|
||||
|
||||
[<Fact>]
|
||||
let ``HxTarget succeeds when the header is not present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore
|
||||
Option.isNone ctx.Request.Headers.HxTarget |> Assert.True
|
||||
|
||||
[<Fact>]
|
||||
let ``HxTarget succeeds when the header is present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-Target", "#leItem")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Option.isSome ctx.Request.Headers.HxTarget |> Assert.True
|
||||
Assert.Equal("#leItem", Option.get ctx.Request.Headers.HxTarget)
|
||||
|
||||
[<Fact>]
|
||||
let ``HxTrigger succeeds when the header is not present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore
|
||||
Option.isNone ctx.Request.Headers.HxTrigger |> Assert.True
|
||||
|
||||
[<Fact>]
|
||||
let ``HxTrigger succeeds when the header is present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-Trigger", "#trig")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Option.isSome ctx.Request.Headers.HxTrigger |> Assert.True
|
||||
Assert.Equal("#trig", Option.get ctx.Request.Headers.HxTrigger)
|
||||
|
||||
[<Fact>]
|
||||
let ``HxTriggerName succeeds when the header is not present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore
|
||||
Option.isNone ctx.Request.Headers.HxTriggerName |> Assert.True
|
||||
|
||||
[<Fact>]
|
||||
let ``HxTriggerName succeeds when the header is present`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-Trigger-Name", "click")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Option.isSome ctx.Request.Headers.HxTriggerName |> Assert.True
|
||||
Assert.Equal("click", Option.get ctx.Request.Headers.HxTriggerName)
|
||||
|
||||
|
||||
/// Tests for the HttpRequest extension properties
|
||||
module HttpRequestExtensions =
|
||||
|
||||
[<Fact>]
|
||||
let ``IsHtmx succeeds when request is not from htmx`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore
|
||||
Assert.False ctx.Request.IsHtmx
|
||||
|
||||
[<Fact>]
|
||||
let ``IsHtmx succeeds when request is from htmx`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-Request", "true")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Assert.True ctx.Request.IsHtmx
|
||||
|
||||
[<Fact>]
|
||||
let ``IsHtmxRefresh succeeds when request is not from htmx`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore
|
||||
Assert.False ctx.Request.IsHtmxRefresh
|
||||
|
||||
[<Fact>]
|
||||
let ``IsHtmxRefresh succeeds when request is from htmx, but not a refresh`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-Request", "true")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Assert.False ctx.Request.IsHtmxRefresh
|
||||
|
||||
[<Fact>]
|
||||
let ``IsHtmxRefresh succeeds when request is from htmx and is a refresh`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
dic.Add ("HX-Request", "true")
|
||||
dic.Add ("HX-History-Restore-Request", "true")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Assert.True ctx.Request.IsHtmxRefresh
|
||||
|
||||
|
||||
/// Tests for the HttpHandler functions provided in the Handlers module
|
||||
module HandlerTests =
|
||||
|
||||
open System.Threading.Tasks
|
||||
|
||||
/// Dummy "next" parameter to get the pipeline to execute/terminate
|
||||
let next (ctx : HttpContext) = Task.FromResult (Some ctx)
|
||||
|
||||
[<Fact>]
|
||||
let ``withHxPush succeeds`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
task {
|
||||
let! _ = withHxPush "/a-new-url" next ctx
|
||||
Assert.True (dic.ContainsKey "HX-Push")
|
||||
Assert.Equal ("/a-new-url", dic.["HX-Push"].[0])
|
||||
}
|
||||
|
||||
[<Fact>]
|
||||
let ``withHxRedirect succeeds`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
task {
|
||||
let! _ = withHxRedirect "/somewhere-else" next ctx
|
||||
Assert.True (dic.ContainsKey "HX-Redirect")
|
||||
Assert.Equal ("/somewhere-else", dic.["HX-Redirect"].[0])
|
||||
}
|
||||
|
||||
[<Fact>]
|
||||
let ``withHxRefresh succeeds when set to true`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
task {
|
||||
let! _ = withHxRefresh true next ctx
|
||||
Assert.True (dic.ContainsKey "HX-Refresh")
|
||||
Assert.Equal ("true", dic.["HX-Refresh"].[0])
|
||||
}
|
||||
|
||||
[<Fact>]
|
||||
let ``withHxRefresh succeeds when set to false`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
task {
|
||||
let! _ = withHxRefresh false next ctx
|
||||
Assert.True (dic.ContainsKey "HX-Refresh")
|
||||
Assert.Equal ("false", dic.["HX-Refresh"].[0])
|
||||
}
|
||||
|
||||
[<Fact>]
|
||||
let ``withHxRetarget succeeds`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
task {
|
||||
let! _ = withHxRetarget "#somewhereElse" next ctx
|
||||
Assert.True (dic.ContainsKey "HX-Retarget")
|
||||
Assert.Equal ("#somewhereElse", dic.["HX-Retarget"].[0])
|
||||
}
|
||||
|
||||
[<Fact>]
|
||||
let ``withHxTrigger succeeds`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
task {
|
||||
let! _ = withHxTrigger "doSomething" next ctx
|
||||
Assert.True (dic.ContainsKey "HX-Trigger")
|
||||
Assert.Equal ("doSomething", dic.["HX-Trigger"].[0])
|
||||
}
|
||||
|
||||
[<Fact>]
|
||||
let ``withHxTriggerMany succeeds`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
task {
|
||||
let! _ = withHxTriggerMany [ "blah", "foo"; "bleh", "bar" ] next ctx
|
||||
Assert.True (dic.ContainsKey "HX-Trigger")
|
||||
Assert.Equal ("""{ "blah": "foo", "bleh": "bar" }""", dic.["HX-Trigger"].[0])
|
||||
}
|
||||
|
||||
[<Fact>]
|
||||
let ``withHxTriggerAfterSettle succeeds`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
task {
|
||||
let! _ = withHxTriggerAfterSettle "byTheWay" next ctx
|
||||
Assert.True (dic.ContainsKey "HX-Trigger-After-Settle")
|
||||
Assert.Equal ("byTheWay", dic.["HX-Trigger-After-Settle"].[0])
|
||||
}
|
||||
|
||||
[<Fact>]
|
||||
let ``withHxTriggerManyAfterSettle succeeds`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
task {
|
||||
let! _ = withHxTriggerManyAfterSettle [ "oof", "ouch"; "hmm", "uh" ] next ctx
|
||||
Assert.True (dic.ContainsKey "HX-Trigger-After-Settle")
|
||||
Assert.Equal ("""{ "oof": "ouch", "hmm": "uh" }""", dic.["HX-Trigger-After-Settle"].[0])
|
||||
}
|
||||
|
||||
[<Fact>]
|
||||
let ``withHxTriggerAfterSwap succeeds`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
task {
|
||||
let! _ = withHxTriggerAfterSwap "justASec" next ctx
|
||||
Assert.True (dic.ContainsKey "HX-Trigger-After-Swap")
|
||||
Assert.Equal ("justASec", dic.["HX-Trigger-After-Swap"].[0])
|
||||
}
|
||||
|
||||
[<Fact>]
|
||||
let ``withHxTriggerManyAfterSwap succeeds`` () =
|
||||
let ctx = Substitute.For<HttpContext> ()
|
||||
let dic = HeaderDictionary ()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
task {
|
||||
let! _ = withHxTriggerManyAfterSwap [ "this", "1"; "that", "2" ] next ctx
|
||||
Assert.True (dic.ContainsKey "HX-Trigger-After-Swap")
|
||||
Assert.Equal ("""{ "this": "1", "that": "2" }""", dic.["HX-Trigger-After-Swap"].[0])
|
||||
}
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<Description>htmx header extensions and helpers for Giraffe</Description>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Htmx.fs" />
|
||||
<None Include="README.md" Pack="true" PackagePath="\" />
|
||||
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Giraffe" Version="5.0.0" />
|
||||
<PackageReference Include="Giraffe" Version="6.4.0" />
|
||||
<PackageReference Update="FSharp.Core" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Common\Giraffe.Htmx.Common.fsproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
+138
-57
@@ -1,4 +1,4 @@
|
||||
module Giraffe.Htmx
|
||||
module Giraffe.Htmx
|
||||
|
||||
open Microsoft.AspNetCore.Http
|
||||
open Microsoft.Extensions.Primitives
|
||||
@@ -6,98 +6,179 @@ open System
|
||||
|
||||
/// Determine if the given header is present
|
||||
let private hdr (headers : IHeaderDictionary) hdr =
|
||||
match headers.[hdr] with it when it = StringValues.Empty -> None | it -> Some it.[0]
|
||||
match headers[hdr] with it when it = StringValues.Empty -> None | it -> Some it[0]
|
||||
|
||||
/// Extensions to the header dictionary
|
||||
type IHeaderDictionary with
|
||||
|
||||
/// Indicates that the request is via an element using `hx-boost`
|
||||
member this.HxBoosted with get () = hdr this "HX-Boosted" |> Option.map bool.Parse
|
||||
/// <summary>Indicates that the request is via an element using <c>hx-boost</c></summary>
|
||||
member this.HxBoosted
|
||||
with get () = hdr this "HX-Boosted" |> Option.map bool.Parse
|
||||
|
||||
/// The current URL of the browser _(note that this does not update until after settle)_
|
||||
member this.HxCurrentUrl with get () = hdr this "HX-Current-URL" |> Option.map Uri
|
||||
/// <summary>The current URL of the browser <em>(note that this does not update until after settle)</em></summary>
|
||||
member this.HxCurrentUrl
|
||||
with get () = hdr this "HX-Current-URL" |> Option.map Uri
|
||||
|
||||
/// `true` if the request is for history restoration after a miss in the local history cache
|
||||
member this.HxHistoryRestoreRequest with get () = hdr this "HX-History-Restore-Request" |> Option.map bool.Parse
|
||||
/// <summary><c>true</c> if the request is for history restoration after a miss in the local history cache</summary>
|
||||
member this.HxHistoryRestoreRequest
|
||||
with get () = hdr this "HX-History-Restore-Request" |> Option.map bool.Parse
|
||||
|
||||
/// The user response to an `hx-prompt`
|
||||
member this.HxPrompt with get () = hdr this "HX-Prompt"
|
||||
/// <summary>The user response to an <c>hx-prompt</c></summary>
|
||||
member this.HxPrompt
|
||||
with get () = hdr this "HX-Prompt"
|
||||
|
||||
/// `true` if the request came from HTMX
|
||||
member this.HxRequest with get () = hdr this "HX-Request" |> Option.map bool.Parse
|
||||
/// <summary><c>true</c> if the request came from htmx</summary>
|
||||
member this.HxRequest
|
||||
with get () = hdr this "HX-Request" |> Option.map bool.Parse
|
||||
|
||||
/// The `id` of the target element if it exists
|
||||
member this.HxTarget with get () = hdr this "HX-Target"
|
||||
/// <summary>The <c>id</c> attribute of the target element if it exists</summary>
|
||||
member this.HxTarget
|
||||
with get () = hdr this "HX-Target"
|
||||
|
||||
/// The `id` of the triggered element if it exists
|
||||
member this.HxTrigger with get () = hdr this "HX-Trigger"
|
||||
/// <summary>The <c>id</c> attribute of the triggered element if it exists</summary>
|
||||
member this.HxTrigger
|
||||
with get () = hdr this "HX-Trigger"
|
||||
|
||||
/// The `name` of the triggered element if it exists
|
||||
member this.HxTriggerName with get () = hdr this "HX-Trigger-Name"
|
||||
/// <summary>The <c>name</c> attribute of the triggered element if it exists</summary>
|
||||
member this.HxTriggerName
|
||||
with get () = hdr this "HX-Trigger-Name"
|
||||
|
||||
|
||||
/// Extensions for the request object
|
||||
type HttpRequest with
|
||||
|
||||
/// Whether this request was initiated from HTMX
|
||||
member this.IsHtmx with get () = this.Headers.HxRequest |> Option.defaultValue false
|
||||
/// <summary>Whether this request was initiated from htmx</summary>
|
||||
member this.IsHtmx
|
||||
with get () = this.Headers.HxRequest |> Option.defaultValue false
|
||||
|
||||
/// Whether this request is an HTMX history-miss refresh request
|
||||
member this.IsHtmxRefresh with get () =
|
||||
this.IsHtmx && (this.Headers.HxHistoryRestoreRequest |> Option.defaultValue false)
|
||||
/// <summary>Whether this request is an htmx history-miss refresh request</summary>
|
||||
member this.IsHtmxRefresh
|
||||
with get () = this.IsHtmx && (this.Headers.HxHistoryRestoreRequest |> Option.defaultValue false)
|
||||
|
||||
|
||||
/// HTTP handlers for setting output headers
|
||||
/// <summary>HTTP handlers for setting output headers</summary>
|
||||
[<AutoOpen>]
|
||||
module Handlers =
|
||||
|
||||
/// Convert a boolean to lowercase `true` or `false`
|
||||
let private toLowerBool (trueOrFalse : bool) =
|
||||
(string trueOrFalse).ToLowerInvariant ()
|
||||
open Giraffe.Htmx.Common
|
||||
|
||||
/// Serialize a list of key/value pairs to JSON (very rudimentary)
|
||||
let private toJson (evts : (string * string) list) =
|
||||
evts
|
||||
|> List.map (fun evt -> sprintf "\"%s\": \"%s\"" (fst evt) ((snd evt).Replace ("\"", "\\\"")))
|
||||
|> String.concat ", "
|
||||
|> sprintf "{ %s }"
|
||||
/// <summary>Instruct htmx to perform a client-side redirect for content</summary>
|
||||
/// <param name="path">The path where the content should be found</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Location</c> header set</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-location/">Documentation</seealso>
|
||||
let withHxLocation (path: string) : HttpHandler =
|
||||
setHttpHeader "HX-Location" path
|
||||
|
||||
// Pushes a new url into the history stack
|
||||
let withHxPush : string -> HttpHandler =
|
||||
setHttpHeader "HX-Push"
|
||||
/// <summary>Pushes a new url into the history stack</summary>
|
||||
/// <param name="url">The URL to be pushed</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Push-Url</c> header set</returns>
|
||||
/// <remarks>Use <see cref="withHxNoPushUrl" /> to explicitly not push a new URL</remarks>
|
||||
/// <seealso href="https://htmx.org/headers/hx-push-url/">Documentation</seealso>
|
||||
let withHxPushUrl (url: string) : HttpHandler =
|
||||
setHttpHeader "HX-Push-Url" url
|
||||
|
||||
/// Can be used to do a client-side redirect to a new location
|
||||
let withHxRedirect : string -> HttpHandler =
|
||||
setHttpHeader "HX-Redirect"
|
||||
/// <summary>Explicitly do not push a new URL into the history stack</summary>
|
||||
/// <returns>An HTTP handler with the <c>HX-Push-Url</c> header set to <c>false</c></returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-push-url/">Documentation</seealso>
|
||||
let withHxNoPushUrl : HttpHandler =
|
||||
toLowerBool false |> withHxPushUrl
|
||||
|
||||
/// If set to `true` the client side will do a a full refresh of the page
|
||||
let withHxRefresh : bool -> HttpHandler =
|
||||
toLowerBool >> setHttpHeader "HX-Refresh"
|
||||
/// <summary>Can be used to do a client-side redirect to a new location</summary>
|
||||
/// <param name="url">The URL to which the client should be redirected</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Redirect</c> header set</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-redirect/">Documentation</seealso>
|
||||
let withHxRedirect (url: string) : HttpHandler =
|
||||
setHttpHeader "HX-Redirect" url
|
||||
|
||||
/// Allows you to override the `hx-target` attribute
|
||||
let withHxRetarget : string -> HttpHandler =
|
||||
setHttpHeader "HX-Retarget"
|
||||
/// <summary>If set to <c>true</c> the client side will do a full refresh of the page</summary>
|
||||
/// <param name="shouldRefresh">Whether the client should refresh their page</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Refresh</c> header set</returns>
|
||||
let withHxRefresh shouldRefresh : HttpHandler =
|
||||
(toLowerBool >> setHttpHeader "HX-Refresh") shouldRefresh
|
||||
|
||||
/// Allows you to trigger a single client side event
|
||||
let withHxTrigger : string -> HttpHandler =
|
||||
setHttpHeader "HX-Trigger"
|
||||
/// <summary>Replaces the current URL in the history stack</summary>
|
||||
/// <param name="url">The URL to place in the history stack in place of the current one</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Replace-URL</c> header set</returns>
|
||||
/// <remarks>Use <see cref="withHxNoRelaceUrl" /> to explicitly not replace the current URL</remarks>
|
||||
/// <seealso href="https://htmx.org/headers/hx-replace-url/">Documentation</seealso>
|
||||
let withHxReplaceUrl url : HttpHandler =
|
||||
setHttpHeader "HX-Replace-Url" url
|
||||
|
||||
/// Allows you to trigger multiple client side events
|
||||
/// <summary>Explicitly do not replace the current URL in the history stack</summary>
|
||||
/// <returns>An HTTP handler with the <c>HX-Replace-URL</c> header set to <c>false</c></returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-replace-url/">Documentation</seealso>
|
||||
let withHxNoReplaceUrl : HttpHandler =
|
||||
toLowerBool false |> withHxReplaceUrl
|
||||
|
||||
/// <summary>Override which portion of the response will be swapped into the target document</summary>
|
||||
/// <param name="target">The selector for the new response target</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Reselect</c> header set</returns>
|
||||
let withHxReselect (target: string) : HttpHandler =
|
||||
setHttpHeader "HX-Reselect" target
|
||||
|
||||
/// <summary>Override the <c>hx-swap</c> attribute from the initiating element</summary>
|
||||
/// <param name="swap">The swap value to override</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Reswap</c> header set</returns>
|
||||
/// <remarks>Use <see cref="T:Giraffe.Htmx.Common.HxSwap">HxSwap</see> constants for best results</remarks>
|
||||
let withHxReswap (swap: string) : HttpHandler =
|
||||
setHttpHeader "HX-Reswap" swap
|
||||
|
||||
/// <summary>Allows you to override the <c>hx-target</c> attribute</summary>
|
||||
/// <param name="target">The new target for the response</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Retarget</c> header set</returns>
|
||||
let withHxRetarget (target: string) : HttpHandler =
|
||||
setHttpHeader "HX-Retarget" target
|
||||
|
||||
/// <summary>Allows you to trigger a single client side event</summary>
|
||||
/// <param name="evt">The call to the event that should be triggered</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Trigger</c> header set</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-trigger/">Documentation</seealso>
|
||||
let withHxTrigger (evt: string) : HttpHandler =
|
||||
setHttpHeader "HX-Trigger" evt
|
||||
|
||||
/// <summary>Allows you to trigger multiple client side events</summary>
|
||||
/// <param name="evts">The calls to events that should be triggered</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Trigger</c> header set for all given events</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-trigger/">Documentation</seealso>
|
||||
let withHxTriggerMany evts : HttpHandler =
|
||||
toJson evts |> setHttpHeader "HX-Trigger"
|
||||
|
||||
/// Allows you to trigger a single client side event after changes have settled
|
||||
let withHxTriggerAfterSettle : string -> HttpHandler =
|
||||
setHttpHeader "HX-Trigger-After-Settle"
|
||||
/// <summary>Allows you to trigger a single client side event after changes have settled</summary>
|
||||
/// <param name="evt">The call to the event that should be triggered</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Trigger-After-Settle</c> header set</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-trigger/">Documentation</seealso>
|
||||
let withHxTriggerAfterSettle (evt: string) : HttpHandler =
|
||||
setHttpHeader "HX-Trigger-After-Settle" evt
|
||||
|
||||
/// Allows you to trigger multiple client side events after changes have settled
|
||||
/// <summary>Allows you to trigger multiple client side events after changes have settled</summary>
|
||||
/// <param name="evts">The calls to events that should be triggered</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Trigger-After-Settle</c> header set for all given events</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-trigger/">Documentation</seealso>
|
||||
let withHxTriggerManyAfterSettle evts : HttpHandler =
|
||||
toJson evts |> setHttpHeader "HX-Trigger-After-Settle"
|
||||
|
||||
/// Allows you to trigger a single client side event after DOM swapping occurs
|
||||
let withHxTriggerAfterSwap : string -> HttpHandler =
|
||||
setHttpHeader "HX-Trigger-After-Swap"
|
||||
/// <summary>Allows you to trigger a single client side event after DOM swapping occurs</summary>
|
||||
/// <param name="evt">The call to the event that should be triggered</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Trigger-After-Swap</c> header set</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-trigger/">Documentation</seealso>
|
||||
let withHxTriggerAfterSwap (evt: string) : HttpHandler =
|
||||
setHttpHeader "HX-Trigger-After-Swap" evt
|
||||
|
||||
/// Allows you to trigger multiple client side events after DOM swapping occurs
|
||||
/// <summary>Allows you to trigger multiple client side events after DOM swapping occurs</summary>
|
||||
/// <param name="evts">The calls to events that should be triggered</param>
|
||||
/// <returns>An HTTP handler with the <c>HX-Trigger-After-Swap</c> header set for all given events</returns>
|
||||
/// <seealso href="https://htmx.org/headers/hx-trigger/">Documentation</seealso>
|
||||
let withHxTriggerManyAfterSwap evts : HttpHandler =
|
||||
toJson evts |> setHttpHeader "HX-Trigger-After-Swap"
|
||||
|
||||
|
||||
/// <summary>Load the package-provided version of the htmx script</summary>
|
||||
[<RequireQualifiedAccess>]
|
||||
module HtmxScript =
|
||||
|
||||
open Giraffe.Htmx.Common
|
||||
open Microsoft.AspNetCore.Html
|
||||
|
||||
/// <summary><c>script</c> tag to load the package-provided version of the htmx script</summary>
|
||||
let local = HtmlString $"""<script src="{htmxLocalScript}"></script>"""
|
||||
|
||||
+10
-4
@@ -2,7 +2,9 @@
|
||||
|
||||
This package enables server-side support for [htmx](https://htmx.org) within [Giraffe](https://giraffe.wiki) and ASP.NET's `HttpContext`.
|
||||
|
||||
**htmx version: 1.6.1**
|
||||
**htmx version: 2.0.10**
|
||||
|
||||
_Upgrading from v1.x: the [migration guide](https://htmx.org/migration-guide-htmx-1/) does not currently specify any request or response header changes. This means that there are no required code changes in moving from v1.* to v2.*._
|
||||
|
||||
### Setup
|
||||
|
||||
@@ -14,7 +16,7 @@ This package enables server-side support for [htmx](https://htmx.org) within [Gi
|
||||
To obtain a request header, using the `IHeaderDictionary` extension properties:
|
||||
|
||||
```fsharp
|
||||
let myHandler : HttpHander =
|
||||
let myHandler : HttpHander =
|
||||
fun next ctx ->
|
||||
match ctx.HxPrompt with
|
||||
| Some prompt -> ... // do something with the text the user provided
|
||||
@@ -24,12 +26,16 @@ To obtain a request header, using the `IHeaderDictionary` extension properties:
|
||||
To set a response header:
|
||||
|
||||
```fsharp
|
||||
let myHandler : HttpHander =
|
||||
let myHandler : HttpHander =
|
||||
fun next ctx ->
|
||||
// some meaningful work
|
||||
withHxPush "/some/new/url" >=> [other handlers]
|
||||
withHxPushUrl "/some/new/url" >=> [other handlers]
|
||||
```
|
||||
|
||||
The `HxSwap` module has constants to use for the `HX-Reswap` header. These may be extended with settle, show, and other qualifiers; see the htmx documentation for the `hx-swap` attribute for more information.
|
||||
|
||||
To load the package-provided htmx library without using Giraffe.ViewEngine, use `HtmxScript.local`.
|
||||
|
||||
### Learn
|
||||
|
||||
The naming conventions of this library were selected to mirror those provided by htmx. The header properties become `Hx*` on the `ctx.Request.Headers` object, and the response handlers are `withHx*` based on the header being set. The only part that does not line up is `withHxTrigger*` and `withHxTriggerMany`; the former set work with a single string (to trigger a single event with no arguments), while the latter set supports both arguments and multiple events.
|
||||
@@ -0,0 +1,39 @@
|
||||
module Common
|
||||
|
||||
open Expecto
|
||||
open Giraffe.Htmx
|
||||
|
||||
/// Test to ensure the version was updated
|
||||
let version =
|
||||
test "HtmxVersion is correct" {
|
||||
Expect.equal HtmxVersion "2.0.10" "htmx version incorrect"
|
||||
}
|
||||
|
||||
/// Tests for the HxSwap module
|
||||
let swap =
|
||||
testList "HxSwap" [
|
||||
test "InnerHtml is correct" {
|
||||
Expect.equal HxSwap.InnerHtml "innerHTML" "Inner HTML swap value incorrect"
|
||||
}
|
||||
test "OuterHtml is correct" {
|
||||
Expect.equal HxSwap.OuterHtml "outerHTML" "Outer HTML swap value incorrect"
|
||||
}
|
||||
test "BeforeBegin is correct" {
|
||||
Expect.equal HxSwap.BeforeBegin "beforebegin" "Before Begin swap value incorrect"
|
||||
}
|
||||
test "BeforeEnd is correct" {
|
||||
Expect.equal HxSwap.BeforeEnd "beforeend" "Before End swap value incorrect"
|
||||
}
|
||||
test "AfterBegin is correct" {
|
||||
Expect.equal HxSwap.AfterBegin "afterbegin" "After Begin swap value incorrect"
|
||||
}
|
||||
test "AfterEnd is correct" {
|
||||
Expect.equal HxSwap.AfterEnd "afterend" "After End swap value incorrect"
|
||||
}
|
||||
test "None is correct" {
|
||||
Expect.equal HxSwap.None "none" "None swap value incorrect"
|
||||
}
|
||||
]
|
||||
|
||||
/// All tests for this module
|
||||
let allTests = testList "Htmx.Common" [ version; swap ]
|
||||
@@ -0,0 +1,370 @@
|
||||
module Htmx
|
||||
|
||||
open System
|
||||
open Expecto
|
||||
open Giraffe.Htmx
|
||||
open Microsoft.AspNetCore.Html
|
||||
open Microsoft.AspNetCore.Http
|
||||
open NSubstitute
|
||||
|
||||
/// Tests for the IHeaderDictionary extension properties
|
||||
let dictExtensions =
|
||||
testList "IHeaderDictionaryExtensions" [
|
||||
testList "HxBoosted" [
|
||||
test "succeeds when the header is not present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs(HeaderDictionary()) |> ignore
|
||||
Expect.isNone ctx.Request.Headers.HxBoosted "There should not have been a header returned"
|
||||
}
|
||||
test "succeeds when the header is present and true" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-Boosted", "true")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isSome ctx.Request.Headers.HxBoosted "There should be a header present"
|
||||
Expect.isTrue ctx.Request.Headers.HxBoosted.Value "The header value should have been true"
|
||||
}
|
||||
test "succeeds when the header is present and false" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-Boosted", "false")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isSome ctx.Request.Headers.HxBoosted "There should be a header present"
|
||||
Expect.isFalse ctx.Request.Headers.HxBoosted.Value "The header value should have been false"
|
||||
}
|
||||
]
|
||||
testList "HxCurrentUrl" [
|
||||
test "succeeds when the header is not present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs(HeaderDictionary()) |> ignore
|
||||
Expect.isNone ctx.Request.Headers.HxCurrentUrl "There should not have been a header returned"
|
||||
}
|
||||
test "succeeds when the header is present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-Current-URL", "http://localhost/test.htm")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isSome ctx.Request.Headers.HxCurrentUrl "There should be a header present"
|
||||
Expect.equal
|
||||
ctx.Request.Headers.HxCurrentUrl.Value (Uri "http://localhost/test.htm")
|
||||
"The header value was not correct"
|
||||
}
|
||||
]
|
||||
testList "HxHistoryRestoreRequest" [
|
||||
test "succeeds when the header is not present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs(HeaderDictionary()) |> ignore
|
||||
Expect.isNone ctx.Request.Headers.HxHistoryRestoreRequest "There should not have been a header returned"
|
||||
}
|
||||
test "succeeds when the header is present and true" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-History-Restore-Request", "true")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isSome ctx.Request.Headers.HxHistoryRestoreRequest "There should be a header present"
|
||||
Expect.isTrue ctx.Request.Headers.HxHistoryRestoreRequest.Value "The header value should have been true"
|
||||
}
|
||||
test "succeeds when the header is present and false" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-History-Restore-Request", "false")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isSome ctx.Request.Headers.HxHistoryRestoreRequest "There should be a header present"
|
||||
Expect.isFalse
|
||||
ctx.Request.Headers.HxHistoryRestoreRequest.Value "The header should have been false"
|
||||
}
|
||||
]
|
||||
testList "HxPrompt" [
|
||||
test "succeeds when the header is not present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs(HeaderDictionary()) |> ignore
|
||||
Expect.isNone ctx.Request.Headers.HxPrompt "There should not have been a header returned"
|
||||
}
|
||||
test "succeeds when the header is present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-Prompt", "of course")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isSome ctx.Request.Headers.HxPrompt "There should be a header present"
|
||||
Expect.equal ctx.Request.Headers.HxPrompt.Value "of course" "The header value was incorrect"
|
||||
}
|
||||
]
|
||||
testList "HxRequest" [
|
||||
test "succeeds when the header is not present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs(HeaderDictionary()) |> ignore
|
||||
Expect.isNone ctx.Request.Headers.HxRequest "There should not have been a header returned"
|
||||
}
|
||||
test "succeeds when the header is present and true" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-Request", "true")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isSome ctx.Request.Headers.HxRequest "There should be a header present"
|
||||
Expect.isTrue ctx.Request.Headers.HxRequest.Value "The header should have been true"
|
||||
}
|
||||
test "succeeds when the header is present and false" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-Request", "false")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isSome ctx.Request.Headers.HxRequest "There should be a header present"
|
||||
Expect.isFalse ctx.Request.Headers.HxRequest.Value "The header should have been false"
|
||||
}
|
||||
]
|
||||
testList "HxTarget" [
|
||||
test "succeeds when the header is not present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs(HeaderDictionary()) |> ignore
|
||||
Expect.isNone ctx.Request.Headers.HxTarget "There should not have been a header returned"
|
||||
}
|
||||
test "succeeds when the header is present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-Target", "#leItem")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isSome ctx.Request.Headers.HxTarget "There should be a header present"
|
||||
Expect.equal ctx.Request.Headers.HxTarget.Value "#leItem" "The header value was incorrect"
|
||||
}
|
||||
]
|
||||
testList "HxTrigger" [
|
||||
test "succeeds when the header is not present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore
|
||||
Expect.isNone ctx.Request.Headers.HxTrigger "There should not have been a header returned"
|
||||
}
|
||||
test "succeeds when the header is present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-Trigger", "#trig")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isSome ctx.Request.Headers.HxTrigger "There should be a header present"
|
||||
Expect.equal ctx.Request.Headers.HxTrigger.Value "#trig" "The header value was incorrect"
|
||||
}
|
||||
]
|
||||
testList "HxTriggerName" [
|
||||
test "succeeds when the header is not present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs(HeaderDictionary()) |> ignore
|
||||
Expect.isNone ctx.Request.Headers.HxTriggerName "There should not have been a header returned"
|
||||
}
|
||||
test "HxTriggerName succeeds when the header is present" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-Trigger-Name", "click")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isSome ctx.Request.Headers.HxTriggerName "There should be a header present"
|
||||
Expect.equal ctx.Request.Headers.HxTriggerName.Value "click" "The header value was incorrect"
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
/// Tests for the HttpRequest extension properties
|
||||
let reqExtensions =
|
||||
testList "HttpRequestExtensions" [
|
||||
testList "IsHtmx" [
|
||||
test "succeeds when request is not from htmx" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs(HeaderDictionary()) |> ignore
|
||||
Expect.isFalse ctx.Request.IsHtmx "The request should not be an htmx request"
|
||||
}
|
||||
test "succeeds when request is from htmx" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-Request", "true")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isTrue ctx.Request.IsHtmx "The request should have been an htmx request"
|
||||
}
|
||||
]
|
||||
testList "IsHtmxRefresh" [
|
||||
test "succeeds when request is not from htmx" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
ctx.Request.Headers.ReturnsForAnyArgs(HeaderDictionary()) |> ignore
|
||||
Expect.isFalse ctx.Request.IsHtmxRefresh "The request should not have been an htmx refresh"
|
||||
}
|
||||
test "succeeds when request is from htmx, but not a refresh" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-Request", "true")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isFalse ctx.Request.IsHtmxRefresh "The request should not have been an htmx refresh"
|
||||
}
|
||||
test "IsHtmxRefresh succeeds when request is from htmx and is a refresh" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
dic.Add("HX-Request", "true")
|
||||
dic.Add("HX-History-Restore-Request", "true")
|
||||
ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
Expect.isTrue ctx.Request.IsHtmxRefresh "The request should have been an htmx refresh"
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
open System.Threading.Tasks
|
||||
|
||||
/// Dummy "next" parameter to get the pipeline to execute/terminate
|
||||
let next (ctx: HttpContext) = Task.FromResult(Some ctx)
|
||||
|
||||
/// Tests for the HttpHandler functions provided in the Handlers module
|
||||
let handlers =
|
||||
testList "HandlerTests" [
|
||||
testTask "withHxLocation succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxLocation "/pagina-otro.html" next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Location") "The HX-Location header should be present"
|
||||
Expect.equal dic["HX-Location"].[0] "/pagina-otro.html" "The HX-Location value was incorrect"
|
||||
}
|
||||
testTask "withHxPushUrl succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxPushUrl "/a-new-url" next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Push-Url") "The HX-Push-Url header should be present"
|
||||
Expect.equal dic["HX-Push-Url"].[0] "/a-new-url" "The HX-Push-Url value was incorrect"
|
||||
}
|
||||
testTask "withHxNoPushUrl succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxNoPushUrl next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Push-Url") "The HX-Push-Url header should be present"
|
||||
Expect.equal dic["HX-Push-Url"].[0] "false" "The HX-Push-Url value was incorrect"
|
||||
}
|
||||
testTask "withHxRedirect succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxRedirect "/somewhere-else" next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Redirect") "The HX-Redirect header should be present"
|
||||
Expect.equal dic["HX-Redirect"].[0] "/somewhere-else" "The HX-Redirect value was incorrect"
|
||||
}
|
||||
testList "withHxRefresh" [
|
||||
testTask "succeeds when set to true" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxRefresh true next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Refresh") "The HX-Refresh header should be present"
|
||||
Expect.equal dic["HX-Refresh"].[0] "true" "The HX-Refresh value was incorrect"
|
||||
}
|
||||
testTask "succeeds when set to false" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxRefresh false next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Refresh") "The HX-Refresh header should be present"
|
||||
Expect.equal dic["HX-Refresh"].[0] "false" "The HX-Refresh value was incorrect"
|
||||
}
|
||||
]
|
||||
testTask "withHxReplaceUrl succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxReplaceUrl "/a-substitute-url" next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Replace-Url") "The HX-Replace-Url header should be present"
|
||||
Expect.equal dic["HX-Replace-Url"].[0] "/a-substitute-url" "The HX-Replace-Url value was incorrect"
|
||||
}
|
||||
testTask "withHxNoReplaceUrl succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxNoReplaceUrl next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Replace-Url") "The HX-Replace-Url header should be present"
|
||||
Expect.equal dic["HX-Replace-Url"].[0] "false" "The HX-Replace-Url value was incorrect"
|
||||
}
|
||||
testTask "withHxReselect succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxReselect "#test" next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Reselect") "The HX-Reselect header should be present"
|
||||
Expect.equal dic["HX-Reselect"].[0] "#test" "The HX-Reselect value was incorrect"
|
||||
}
|
||||
testTask "withHxReswap succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxReswap HxSwap.BeforeEnd next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Reswap") "The HX-Reswap header should be present"
|
||||
Expect.equal dic["HX-Reswap"].[0] HxSwap.BeforeEnd "The HX-Reswap value was incorrect"
|
||||
}
|
||||
testTask "withHxRetarget succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxRetarget "#somewhereElse" next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Retarget") "The HX-Retarget header should be present"
|
||||
Expect.equal dic["HX-Retarget"].[0] "#somewhereElse" "The HX-Retarget value was incorrect"
|
||||
}
|
||||
testTask "withHxTrigger succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxTrigger "doSomething" next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Trigger") "The HX-Trigger header should be present"
|
||||
Expect.equal dic["HX-Trigger"].[0] "doSomething" "The HX-Trigger value was incorrect"
|
||||
}
|
||||
testTask "withHxTriggerMany succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxTriggerMany [ "blah", "foo"; "bleh", "bar" ] next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Trigger") "The HX-Trigger header should be present"
|
||||
Expect.equal
|
||||
dic["HX-Trigger"].[0] """{ "blah": "foo", "bleh": "bar" }""" "The HX-Trigger value was incorrect"
|
||||
}
|
||||
testTask "withHxTriggerAfterSettle succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxTriggerAfterSettle "byTheWay" next ctx
|
||||
Expect.isTrue
|
||||
(dic.ContainsKey "HX-Trigger-After-Settle") "The HX-Trigger-After-Settle header should be present"
|
||||
Expect.equal dic["HX-Trigger-After-Settle"].[0] "byTheWay" "The HX-Trigger-After-Settle value was incorrect"
|
||||
}
|
||||
testTask "withHxTriggerManyAfterSettle succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxTriggerManyAfterSettle [ "oof", "ouch"; "hmm", "uh" ] next ctx
|
||||
Expect.isTrue
|
||||
(dic.ContainsKey "HX-Trigger-After-Settle") "The HX-Trigger-After-Settle header should be present"
|
||||
Expect.equal
|
||||
dic["HX-Trigger-After-Settle"].[0] """{ "oof": "ouch", "hmm": "uh" }"""
|
||||
"The HX-Trigger-After-Settle value was incorrect"
|
||||
}
|
||||
testTask "withHxTriggerAfterSwap succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxTriggerAfterSwap "justASec" next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Trigger-After-Swap") "The HX-Trigger-After-Swap header should be present"
|
||||
Expect.equal dic["HX-Trigger-After-Swap"].[0] "justASec" "The HX-Trigger-After-Swap value was incorrect"
|
||||
}
|
||||
testTask "withHxTriggerManyAfterSwap succeeds" {
|
||||
let ctx = Substitute.For<HttpContext>()
|
||||
let dic = HeaderDictionary()
|
||||
ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore
|
||||
let! _ = withHxTriggerManyAfterSwap [ "this", "1"; "that", "2" ] next ctx
|
||||
Expect.isTrue (dic.ContainsKey "HX-Trigger-After-Swap") "The HX-Trigger-After-Swap header should be present"
|
||||
Expect.equal
|
||||
dic["HX-Trigger-After-Swap"].[0] """{ "this": "1", "that": "2" }"""
|
||||
"The HX-Trigger-After-Swap value was incorrect"
|
||||
}
|
||||
]
|
||||
|
||||
/// Tests for the HtmxScript module
|
||||
let script =
|
||||
testList "HtmxScript" [
|
||||
test "local generates correct link" {
|
||||
Expect.equal
|
||||
(string HtmxScript.local)
|
||||
$"""<script src="/_content/Giraffe.Htmx.Common/htmx.min.js?ver={HtmxVersion}"></script>"""
|
||||
"htmx script link is incorrect"
|
||||
}
|
||||
]
|
||||
|
||||
/// All tests for this module
|
||||
let allTests = testList "Htmx" [ dictExtensions; reqExtensions; handlers; script ]
|
||||
@@ -0,0 +1,6 @@
|
||||
open Expecto
|
||||
|
||||
let allTests = testList "Giraffe" [ Common.allTests; Htmx.allTests; ViewEngine.allTests ]
|
||||
|
||||
[<EntryPoint>]
|
||||
let main args = runTestsWithCLIArgs [] args allTests
|
||||
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Common.fs" />
|
||||
<Compile Include="Htmx.fs" />
|
||||
<Compile Include="ViewEngine.fs" />
|
||||
<Compile Include="Program.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Common\Giraffe.Htmx.Common.fsproj" />
|
||||
<ProjectReference Include="..\Htmx\Giraffe.Htmx.fsproj" />
|
||||
<ProjectReference Include="..\ViewEngine.Htmx\Giraffe.ViewEngine.Htmx.fsproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Expecto" Version="10.2.1" />
|
||||
<PackageReference Include="NSubstitute" Version="5.1.0" />
|
||||
<PackageReference Update="FSharp.Core" Version="8.0.300" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,33 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<GenerateProgramFile>false</GenerateProgramFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Tests.fs" />
|
||||
<Compile Include="Program.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Giraffe.ViewEngine" Version="1.4.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ViewEngine.Htmx\Giraffe.ViewEngine.Htmx.fsproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1 +0,0 @@
|
||||
module Program = let [<EntryPoint>] main _ = 0
|
||||
@@ -1,444 +0,0 @@
|
||||
module Giraffe.ViewEngine.Htmx.Tests
|
||||
|
||||
open Giraffe.ViewEngine
|
||||
open Xunit
|
||||
|
||||
/// Tests for the HxEncoding module
|
||||
module Encoding =
|
||||
|
||||
[<Fact>]
|
||||
let ``Form is correct`` () =
|
||||
Assert.Equal ("application/x-www-form-urlencoded", HxEncoding.Form)
|
||||
|
||||
[<Fact>]
|
||||
let ``MultipartForm is correct`` () =
|
||||
Assert.Equal ("multipart/form-data", HxEncoding.MultipartForm)
|
||||
|
||||
|
||||
/// Tests for the HxHeaders module
|
||||
module Headers =
|
||||
|
||||
[<Fact>]
|
||||
let ``From succeeds with an empty list`` () =
|
||||
Assert.Equal ("{ }", HxHeaders.From [])
|
||||
|
||||
[<Fact>]
|
||||
let ``From succeeds and escapes quotes`` () =
|
||||
Assert.Equal ("{ \"test\": \"one two three\", \"again\": \"four \\\"five\\\" six\" }",
|
||||
HxHeaders.From [ "test", "one two three"; "again", "four \"five\" six" ])
|
||||
|
||||
|
||||
/// Tests for the HxParams module
|
||||
module Params =
|
||||
|
||||
[<Fact>]
|
||||
let ``All is correct`` () =
|
||||
Assert.Equal ("*", HxParams.All)
|
||||
|
||||
[<Fact>]
|
||||
let ``None is correct`` () =
|
||||
Assert.Equal ("none", HxParams.None)
|
||||
|
||||
[<Fact>]
|
||||
let ``With succeeds with empty list`` () =
|
||||
Assert.Equal ("", HxParams.With [])
|
||||
|
||||
[<Fact>]
|
||||
let ``With succeeds with one list item`` () =
|
||||
Assert.Equal ("boo", HxParams.With [ "boo" ])
|
||||
|
||||
[<Fact>]
|
||||
let ``With succeeds with multiple list items`` () =
|
||||
Assert.Equal ("foo,bar,baz", HxParams.With [ "foo"; "bar"; "baz" ])
|
||||
|
||||
[<Fact>]
|
||||
let ``Except succeeds with empty list`` () =
|
||||
Assert.Equal ("not ", HxParams.Except [])
|
||||
|
||||
[<Fact>]
|
||||
let ``Except succeeds with one list item`` () =
|
||||
Assert.Equal ("not that", HxParams.Except [ "that" ])
|
||||
|
||||
[<Fact>]
|
||||
let ``Except succeeds with multiple list items`` () =
|
||||
Assert.Equal ("not blue,green", HxParams.Except [ "blue"; "green" ])
|
||||
|
||||
|
||||
/// Tests for the HxRequest module
|
||||
module Request =
|
||||
|
||||
[<Fact>]
|
||||
let ``Configure succeeds with an empty list`` () =
|
||||
Assert.Equal ("{ }", HxRequest.Configure [])
|
||||
|
||||
[<Fact>]
|
||||
let ``Configure succeeds with a non-empty list`` () =
|
||||
Assert.Equal ("{ \"a\": \"b\", \"c\": \"d\" }", HxRequest.Configure [ "\"a\": \"b\""; "\"c\": \"d\"" ])
|
||||
|
||||
[<Fact>]
|
||||
let ``Configure succeeds with all known params configured`` () =
|
||||
Assert.Equal ("{ \"timeout\": 1000, \"credentials\": false, \"noHeaders\": true }",
|
||||
HxRequest.Configure [ HxRequest.Timeout 1000; HxRequest.Credentials false; HxRequest.NoHeaders true ])
|
||||
|
||||
[<Fact>]
|
||||
let ``Timeout succeeds`` () =
|
||||
Assert.Equal ("\"timeout\": 50", HxRequest.Timeout 50)
|
||||
|
||||
[<Fact>]
|
||||
let ``Credentials succeeds when set to true`` () =
|
||||
Assert.Equal ("\"credentials\": true", HxRequest.Credentials true)
|
||||
|
||||
[<Fact>]
|
||||
let ``Credentials succeeds when set to false`` () =
|
||||
Assert.Equal ("\"credentials\": false", HxRequest.Credentials false)
|
||||
|
||||
[<Fact>]
|
||||
let ``NoHeaders succeeds when set to true`` () =
|
||||
Assert.Equal ("\"noHeaders\": true", HxRequest.NoHeaders true)
|
||||
|
||||
[<Fact>]
|
||||
let ``NoHeaders succeeds when set to false`` () =
|
||||
Assert.Equal ("\"noHeaders\": false", HxRequest.NoHeaders false)
|
||||
|
||||
|
||||
/// Tests for the HxSwap module
|
||||
module Swap =
|
||||
|
||||
[<Fact>]
|
||||
let ``InnerHtml is correct`` () =
|
||||
Assert.Equal ("innerHTML", HxSwap.InnerHtml)
|
||||
|
||||
[<Fact>]
|
||||
let ``OuterHtml is correct`` () =
|
||||
Assert.Equal ("outerHTML", HxSwap.OuterHtml)
|
||||
|
||||
[<Fact>]
|
||||
let ``BeforeBegin is correct`` () =
|
||||
Assert.Equal ("beforebegin", HxSwap.BeforeBegin)
|
||||
|
||||
[<Fact>]
|
||||
let ``BeforeEnd is correct`` () =
|
||||
Assert.Equal ("beforeend", HxSwap.BeforeEnd)
|
||||
|
||||
[<Fact>]
|
||||
let ``AfterBegin is correct`` () =
|
||||
Assert.Equal ("afterbegin", HxSwap.AfterBegin)
|
||||
|
||||
[<Fact>]
|
||||
let ``AfterEnd is correct`` () =
|
||||
Assert.Equal ("afterend", HxSwap.AfterEnd)
|
||||
|
||||
[<Fact>]
|
||||
let ``None is correct`` () =
|
||||
Assert.Equal ("none", HxSwap.None)
|
||||
|
||||
|
||||
/// Tests for the HxTrigger module
|
||||
module Trigger =
|
||||
|
||||
[<Fact>]
|
||||
let ``Click is correct`` () =
|
||||
Assert.Equal ("click", HxTrigger.Click)
|
||||
|
||||
[<Fact>]
|
||||
let ``Load is correct`` () =
|
||||
Assert.Equal ("load", HxTrigger.Load)
|
||||
|
||||
[<Fact>]
|
||||
let ``Revealed is correct`` () =
|
||||
Assert.Equal ("revealed", HxTrigger.Revealed)
|
||||
|
||||
[<Fact>]
|
||||
let ``Every succeeds`` () =
|
||||
Assert.Equal ("every 3s", HxTrigger.Every "3s")
|
||||
|
||||
[<Fact>]
|
||||
let ``Filter.Alt succeeds`` () =
|
||||
Assert.Equal ("click[altKey]", HxTrigger.Filter.Alt HxTrigger.Click)
|
||||
|
||||
[<Fact>]
|
||||
let ``Filter.Ctrl succeeds`` () =
|
||||
Assert.Equal ("click[ctrlKey]", HxTrigger.Filter.Ctrl HxTrigger.Click)
|
||||
|
||||
[<Fact>]
|
||||
let ``Filter.Shift succeeds`` () =
|
||||
Assert.Equal ("click[shiftKey]", HxTrigger.Filter.Shift HxTrigger.Click)
|
||||
|
||||
[<Fact>]
|
||||
let ``Filter.CtrlAlt succeeds`` () =
|
||||
Assert.Equal ("click[ctrlKey&&altKey]", HxTrigger.Filter.CtrlAlt HxTrigger.Click)
|
||||
|
||||
[<Fact>]
|
||||
let ``Filter.CtrlShift succeeds`` () =
|
||||
Assert.Equal ("click[ctrlKey&&shiftKey]", HxTrigger.Filter.CtrlShift HxTrigger.Click)
|
||||
|
||||
[<Fact>]
|
||||
let ``Filter.CtrlAltShift succeeds`` () =
|
||||
Assert.Equal ("click[ctrlKey&&altKey&&shiftKey]", HxTrigger.Filter.CtrlAltShift HxTrigger.Click)
|
||||
|
||||
[<Fact>]
|
||||
let ``Filter.AltShift succeeds`` () =
|
||||
Assert.Equal ("click[altKey&&shiftKey]", HxTrigger.Filter.AltShift HxTrigger.Click)
|
||||
|
||||
[<Fact>]
|
||||
let ``Once succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("once", HxTrigger.Once "")
|
||||
|
||||
[<Fact>]
|
||||
let ``Once succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click once", HxTrigger.Once "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``Changed succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("changed", HxTrigger.Changed "")
|
||||
|
||||
[<Fact>]
|
||||
let ``Changed succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click changed", HxTrigger.Changed "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``Delay succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("delay:1s", HxTrigger.Delay "1s" "")
|
||||
|
||||
[<Fact>]
|
||||
let ``Delay succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click delay:2s", HxTrigger.Delay "2s" "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``Throttle succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("throttle:4s", HxTrigger.Throttle "4s" "")
|
||||
|
||||
[<Fact>]
|
||||
let ``Throttle succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click throttle:7s", HxTrigger.Throttle "7s" "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``From succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("from:.nav", HxTrigger.From ".nav" "")
|
||||
|
||||
[<Fact>]
|
||||
let ``From succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click from:#somewhere", HxTrigger.From "#somewhere" "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``FromDocument succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("from:document", HxTrigger.FromDocument "")
|
||||
|
||||
[<Fact>]
|
||||
let ``FromDocument succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click from:document", HxTrigger.FromDocument "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``FromWindow succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("from:window", HxTrigger.FromWindow "")
|
||||
|
||||
[<Fact>]
|
||||
let ``FromWindow succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click from:window", HxTrigger.FromWindow "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``FromClosest succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("from:closest div", HxTrigger.FromClosest "div" "")
|
||||
|
||||
[<Fact>]
|
||||
let ``FromClosest succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click from:closest p", HxTrigger.FromClosest "p" "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``FromFind succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("from:find li", HxTrigger.FromFind "li" "")
|
||||
|
||||
[<Fact>]
|
||||
let ``FromFind succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click from:find .spot", HxTrigger.FromFind ".spot" "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``Target succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("target:main", HxTrigger.Target "main" "")
|
||||
|
||||
[<Fact>]
|
||||
let ``Target succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click target:footer", HxTrigger.Target "footer" "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``Consume succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("consume", HxTrigger.Consume "")
|
||||
|
||||
[<Fact>]
|
||||
let ``Consume succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click consume", HxTrigger.Consume "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``Queue succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("queue:abc", HxTrigger.Queue "abc" "")
|
||||
|
||||
[<Fact>]
|
||||
let ``Queue succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click queue:def", HxTrigger.Queue "def" "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``QueueFirst succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("queue:first", HxTrigger.QueueFirst "")
|
||||
|
||||
[<Fact>]
|
||||
let ``QueueFirst succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click queue:first", HxTrigger.QueueFirst "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``QueueLast succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("queue:last", HxTrigger.QueueLast "")
|
||||
|
||||
[<Fact>]
|
||||
let ``QueueLast succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click queue:last", HxTrigger.QueueLast "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``QueueAll succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("queue:all", HxTrigger.QueueAll "")
|
||||
|
||||
[<Fact>]
|
||||
let ``QueueAll succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click queue:all", HxTrigger.QueueAll "click")
|
||||
|
||||
[<Fact>]
|
||||
let ``QueueNone succeeds when it is the first modifier`` () =
|
||||
Assert.Equal ("queue:none", HxTrigger.QueueNone "")
|
||||
|
||||
[<Fact>]
|
||||
let ``QueueNone succeeds when it is not the first modifier`` () =
|
||||
Assert.Equal ("click queue:none", HxTrigger.QueueNone "click")
|
||||
|
||||
|
||||
/// Tests for the HxVals module
|
||||
module Vals =
|
||||
|
||||
[<Fact>]
|
||||
let ``From succeeds with an empty list`` () =
|
||||
Assert.Equal ("{ }", HxVals.From [])
|
||||
|
||||
[<Fact>]
|
||||
let ``From succeeds and escapes quotes`` () =
|
||||
Assert.Equal ("{ \"test\": \"a \\\"b\\\" c\", \"2\": \"d e f\" }",
|
||||
HxVals.From [ "test", "a \"b\" c"; "2", "d e f" ])
|
||||
|
||||
|
||||
/// Tests for the HtmxAttrs module
|
||||
module Attributes =
|
||||
|
||||
/// Pipe-able assertion for a rendered node
|
||||
let shouldRender expected node = Assert.Equal (expected, RenderView.AsString.htmlNode node)
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxBoost succeeds`` () =
|
||||
div [ _hxBoost ] [] |> shouldRender """<div hx-boost="true"></div>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxConfirm succeeds`` () =
|
||||
button [ _hxConfirm "REALLY?!?" ] [] |> shouldRender """<button hx-confirm="REALLY?!?"></button>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxDelete succeeds`` () =
|
||||
span [ _hxDelete "/this-endpoint" ] [] |> shouldRender """<span hx-delete="/this-endpoint"></span>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxDisable succeeds`` () =
|
||||
p [ _hxDisable ] [] |> shouldRender """<p hx-disable></p>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxEncoding succeeds`` () =
|
||||
form [ _hxEncoding "utf-7" ] [] |> shouldRender """<form hx-encoding="utf-7"></form>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxExt succeeds`` () =
|
||||
section [ _hxExt "extendme" ] [] |> shouldRender """<section hx-ext="extendme"></section>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxGet succeeds`` () =
|
||||
article [ _hxGet "/the-text" ] [] |> shouldRender """<article hx-get="/the-text"></article>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxHeaders succeeds`` () =
|
||||
figure [ _hxHeaders """{ "X-Special-Header": "some-header" }""" ] []
|
||||
|> shouldRender """<figure hx-headers="{ "X-Special-Header": "some-header" }"></figure>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxHistoryElt succeeds`` () =
|
||||
table [ _hxHistoryElt ] [] |> shouldRender """<table hx-history-elt></table>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxInclude succeeds`` () =
|
||||
a [ _hxInclude ".extra-stuff" ] [] |> shouldRender """<a hx-include=".extra-stuff"></a>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxIndicator succeeds`` () =
|
||||
aside [ _hxIndicator "#spinner" ] [] |> shouldRender """<aside hx-indicator="#spinner"></aside>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxNoBoost succeeds`` () =
|
||||
td [ _hxNoBoost ] [] |> shouldRender """<td hx-boost="false"></td>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxParams succeeds`` () =
|
||||
br [ _hxParams "[p1,p2]" ] |> shouldRender """<br hx-params="[p1,p2]">"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxPatch succeeds`` () =
|
||||
div [ _hxPatch "/arrrgh" ] [] |> shouldRender """<div hx-patch="/arrrgh"></div>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxPost succeeds`` () =
|
||||
hr [ _hxPost "/hear-ye-hear-ye" ] |> shouldRender """<hr hx-post="/hear-ye-hear-ye">"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxPreserve succeeds`` () =
|
||||
img [ _hxPreserve ] |> shouldRender """<img hx-preserve="true">"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxPrompt succeeds`` () =
|
||||
strong [ _hxPrompt "Who goes there?" ] [] |> shouldRender """<strong hx-prompt="Who goes there?"></strong>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxPushUrl succeeds`` () =
|
||||
dl [ _hxPushUrl ] [] |> shouldRender """<dl hx-push-url></dl>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxPut succeeds`` () =
|
||||
s [ _hxPut "/take-this" ] [] |> shouldRender """<s hx-put="/take-this"></s>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxRequest succeeds`` () =
|
||||
u [ _hxRequest "noHeaders" ] [] |> shouldRender """<u hx-request="noHeaders"></u>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxSelect succeeds`` () =
|
||||
nav [ _hxSelect "#navbar" ] [] |> shouldRender """<nav hx-select="#navbar"></nav>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxSse succeeds`` () =
|
||||
footer [ _hxSse "connect:/my-events" ] [] |> shouldRender """<footer hx-sse="connect:/my-events"></footer>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxSwap succeeds`` () =
|
||||
del [ _hxSwap "innerHTML" ] [] |> shouldRender """<del hx-swap="innerHTML"></del>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxSwapOob succeeds`` () =
|
||||
li [ _hxSwapOob "true" ] [] |> shouldRender """<li hx-swap-oob="true"></li>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxTarget succeeds`` () =
|
||||
header [ _hxTarget "#somewhereElse" ] [] |> shouldRender """<header hx-target="#somewhereElse"></header>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxTrigger succeeds`` () =
|
||||
figcaption [ _hxTrigger "load" ] [] |> shouldRender """<figcaption hx-trigger="load"></figcaption>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxVals succeeds`` () =
|
||||
dt [ _hxVals """{ "extra": "values" }""" ] []
|
||||
|> shouldRender """<dt hx-vals="{ "extra": "values" }"></dt>"""
|
||||
|
||||
[<Fact>]
|
||||
let ``_hxWs succeeds`` () =
|
||||
ul [ _hxWs "connect:/web-socket" ] [] |> shouldRender """<ul hx-ws="connect:/web-socket"></ul>"""
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<Description>Extensions to Giraffe View Engine to support htmx attributes and their values</Description>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Htmx.fs" />
|
||||
<None Include="README.md" Pack="true" PackagePath="\" />
|
||||
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Giraffe.ViewEngine" Version="1.4.0" />
|
||||
<PackageReference Update="FSharp.Core" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Common\Giraffe.Htmx.Common.fsproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
+848
-170
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,9 @@
|
||||
|
||||
This package enables [htmx](https://htmx.org) support within the [Giraffe](https://giraffe.wiki) view engine.
|
||||
|
||||
**htmx version: 1.6.1**
|
||||
**htmx version: 2.0.10**
|
||||
|
||||
_Upgrading from v1.x: see [the migration guide](https://htmx.org/migration-guide-htmx-1/) for changes_
|
||||
|
||||
### Setup
|
||||
|
||||
@@ -14,7 +16,7 @@ This package enables [htmx](https://htmx.org) support within the [Giraffe](https
|
||||
Following Giraffe View Engine's lead, there are a set of attribute functions for htmx; for many of the attributes, there are also helper modules to assist with typing the values. The example below utilizes both:
|
||||
|
||||
```fsharp
|
||||
let autoload =
|
||||
let autoload =
|
||||
div [ _hxGet "/this/data"; _hxTrigger HxTrigger.Load ] [ str "Loading..." ]
|
||||
```
|
||||
|
||||
@@ -23,31 +25,39 @@ Support modules include:
|
||||
- `HxHeaders`
|
||||
- `HxParams`
|
||||
- `HxRequest`
|
||||
- `HxSwap`
|
||||
- `HxSwap` (requires `open Giraffe.Htmx`)
|
||||
- `HxTrigger`
|
||||
- `HxVals`
|
||||
|
||||
There are two `XmlNode`s that will load the htmx script from unpkg; `Htmx.Script.minified` loads the minified version, and `Htmx.Script.unminified` loads the unminified version (useful for debugging).
|
||||
`Htmx.Script.local` creates an `XmlNode` to load the package-provided htmx library. There are also two `XmlNode`s that will load the htmx script from jsdelivr; `Htmx.Script.cdnMinified` loads the minified version, and `Htmx.Script.cdnUnminified` loads the unminified version (useful for debugging). When using the CDN nodes and a Content Security Policy (CSP) header, `cdn.jsdelivr.net` needs to be listed as an allowable `script-src`.
|
||||
|
||||
This also supports [fragment rendering](https://bitbadger.solutions/blog/2022/fragment-rendering-in-giraffe-view-engine.html), providing the flexibility to render an entire template, or only a portion of it (based on the element's `id` attribute).
|
||||
|
||||
### Learn
|
||||
|
||||
htmx's attributes and these attribute functions map one-to-one. The lone exception is `_hxBoost`, which implies `true`; use `_hxNoBoost` to set it to `false`. The support modules contain named properties for known values (as illustrated with `HxTrigger.Load` above). A few of the modules are more than collections of names, though:
|
||||
htmx's attributes and these attribute functions map one-to-one. There are two exceptions:
|
||||
- `_hxBoost` implies `true`; use `_hxNoBoost` to set it to `false`.
|
||||
- `_hxSwapWithTransition` renders the standard `hx-swap` attribute and appends `transition:true` to the specified swap value.
|
||||
|
||||
The htmx `hx-on` attribute supports multiple events if they are separated with a newline (`\n`) character. The value provided to this attribute will be attribute-escaped, but in testing, it was interpreted correctly.
|
||||
|
||||
The support modules contain named properties for known values (as illustrated with `HxTrigger.Load` above). A few of the modules are more than collections of names, though:
|
||||
- `HxRequest` has a `Configure` function, which takes a list of strings; the other functions in the module allow for configuring the request.
|
||||
|
||||
```fsharp
|
||||
HxRequest.Configure [ HxRequest.Timeout 500 ] |> _hxRequest
|
||||
HxRequest.Configure [ HxRequest.Timeout 500 ] |> _hxRequest
|
||||
```
|
||||
- `HxTrigger` is _(by far)_ the most complex of these modules. Most uses won't need that complexity; however, complex triggers can be defined by piping into or composing with other functions. For example, to define an event that responds to a shift-click anywhere on the document, with a delay of 3 seconds before firing:
|
||||
|
||||
```fsharp
|
||||
HxTrigger.Click
|
||||
|> HxTrigger.Filter.Shift
|
||||
|> HxTrigger.FromDocument
|
||||
|> HxTrigger.Delay "3s"
|
||||
|> _hxTrigger
|
||||
HxTrigger.Click
|
||||
|> HxTrigger.Filter.Shift
|
||||
|> HxTrigger.FromDocument
|
||||
|> HxTrigger.Delay "3s"
|
||||
|> _hxTrigger
|
||||
|
||||
// or
|
||||
// or
|
||||
|
||||
(HxTrigger.Filter.Shift >> HxTrigger.FromDocument >> HxTrigger.Delay "3s") HxTrigger.Click
|
||||
|> _hxTrigger
|
||||
(HxTrigger.Filter.Shift >> HxTrigger.FromDocument >> HxTrigger.Delay "3s") HxTrigger.Click
|
||||
|> _hxTrigger
|
||||
```
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
dotnet pack Common/Giraffe.Htmx.Common.fsproj -c Release
|
||||
dotnet pack Htmx/Giraffe.Htmx.fsproj -c Release
|
||||
dotnet pack ViewEngine.Htmx/Giraffe.ViewEngine.Htmx.fsproj -c Release
|
||||
cp Common/bin/Release/Giraffe.Htmx.Common.*.nupkg .
|
||||
cp Htmx/bin/Release/Giraffe.Htmx.*.nupkg .
|
||||
cp ViewEngine.Htmx/bin/Release/Giraffe.ViewEngine.Htmx.*.nupkg .
|
||||
Reference in New Issue
Block a user