Compare commits
48 Commits
Author | SHA1 | Date | |
---|---|---|---|
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 | |||
86defea3c1 | |||
9a9f159cab |
57
.github/workflows/ci.yml
vendored
Normal file
57
.github/workflows/ci.yml
vendored
Normal file
|
@ -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
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -5,3 +5,5 @@
|
|||
.ionide
|
||||
.idea
|
||||
*.user
|
||||
.vscode
|
||||
src/*.nupkg
|
||||
|
|
33
README.md
33
README.md
|
@ -21,19 +21,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
|
||||
|
@ -48,8 +49,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,18 +60,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.
|
||||
|
||||
## 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|
|
||||
|
||||
|
|
28
src/Common/Common.fs
Normal file
28
src/Common/Common.fs
Normal file
|
@ -0,0 +1,28 @@
|
|||
/// Common definitions shared between attribute values and response headers
|
||||
[<AutoOpen>]
|
||||
module Giraffe.Htmx.Common
|
||||
|
||||
/// Valid values for the `hx-swap` attribute / `HX-Reswap` header (may be combined with swap/settle/scroll/show config)
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxSwap =
|
||||
|
||||
/// The default, replace the inner html of the target element
|
||||
let InnerHtml = "innerHTML"
|
||||
|
||||
/// Replace the entire target element with the response
|
||||
let OuterHtml = "outerHTML"
|
||||
|
||||
/// Insert the response before the target element
|
||||
let BeforeBegin = "beforebegin"
|
||||
|
||||
/// Insert the response before the first child of the target element
|
||||
let AfterBegin = "afterbegin"
|
||||
|
||||
/// Insert the response after the last child of the target element
|
||||
let BeforeEnd = "beforeend"
|
||||
|
||||
/// Insert the response after the target element
|
||||
let AfterEnd = "afterend"
|
||||
|
||||
/// Does not append content from response (out of band items will still be processed).
|
||||
let None = "none"
|
19
src/Common/Giraffe.Htmx.Common.fsproj
Normal file
19
src/Common/Giraffe.Htmx.Common.fsproj
Normal file
|
@ -0,0 +1,19 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<Description>Common definitions for Giraffe.Htmx</Description>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
</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>
|
||||
|
||||
</Project>
|
5
src/Common/README.md
Normal file
5
src/Common/README.md
Normal file
|
@ -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.
|
||||
|
||||
**htmx version: 2.0.3**
|
|
@ -1,13 +1,14 @@
|
|||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>0.9.3</VersionPrefix>
|
||||
<PackageReleaseNotes>Add support for HX-Retarget header (added in htmx 1.6.1)</PackageReleaseNotes>
|
||||
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
|
||||
<VersionPrefix>2.0.3</VersionPrefix>
|
||||
<PackageReleaseNotes>Update script tags to pull htmx 2.0.3 (no header or attribute changes)</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>
|
||||
|
|
40
src/Giraffe.Htmx.sln
Normal file
40
src/Giraffe.Htmx.sln
Normal file
|
@ -0,0 +1,40 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30114.105
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Htmx", "Htmx\Giraffe.Htmx.fsproj", "{8AB3085C-5236-485A-8565-A09106E72E1E}"
|
||||
EndProject
|
||||
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.ViewEngine.Htmx", "ViewEngine.Htmx\Giraffe.ViewEngine.Htmx.fsproj", "{F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}"
|
||||
EndProject
|
||||
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Htmx.Common", "Common\Giraffe.Htmx.Common.fsproj", "{75D66845-F93A-4463-AD29-A8B16E4D4BA9}"
|
||||
EndProject
|
||||
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Tests", "Tests\Tests.fsproj", "{39823773-4311-4E79-9CA0-F9DDC40CAF6A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{8AB3085C-5236-485A-8565-A09106E72E1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8AB3085C-5236-485A-8565-A09106E72E1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8AB3085C-5236-485A-8565-A09106E72E1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8AB3085C-5236-485A-8565-A09106E72E1E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{75D66845-F93A-4463-AD29-A8B16E4D4BA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{75D66845-F93A-4463-AD29-A8B16E4D4BA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{75D66845-F93A-4463-AD29-A8B16E4D4BA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{75D66845-F93A-4463-AD29-A8B16E4D4BA9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{39823773-4311-4E79-9CA0-F9DDC40CAF6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{39823773-4311-4E79-9CA0-F9DDC40CAF6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{39823773-4311-4E79-9CA0-F9DDC40CAF6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{39823773-4311-4E79-9CA0-F9DDC40CAF6A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
|
@ -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,7 +1,6 @@
|
|||
<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>
|
||||
|
@ -10,10 +9,16 @@
|
|||
<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>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
module Giraffe.Htmx
|
||||
module Giraffe.Htmx
|
||||
|
||||
open Microsoft.AspNetCore.Http
|
||||
open Microsoft.Extensions.Primitives
|
||||
|
@ -6,7 +6,7 @@ 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
|
||||
|
@ -39,10 +39,10 @@ type IHeaderDictionary with
|
|||
/// Extensions for the request object
|
||||
type HttpRequest with
|
||||
|
||||
/// Whether this request was initiated from HTMX
|
||||
/// Whether this request was initiated from htmx
|
||||
member this.IsHtmx with get () = this.Headers.HxRequest |> Option.defaultValue false
|
||||
|
||||
/// Whether this request is an HTMX history-miss refresh request
|
||||
/// Whether this request is an htmx history-miss refresh request
|
||||
member this.IsHtmxRefresh with get () =
|
||||
this.IsHtmx && (this.Headers.HxHistoryRestoreRequest |> Option.defaultValue false)
|
||||
|
||||
|
@ -62,9 +62,21 @@ module Handlers =
|
|||
|> String.concat ", "
|
||||
|> sprintf "{ %s }"
|
||||
|
||||
// Pushes a new url into the history stack
|
||||
let withHxPush : string -> HttpHandler =
|
||||
setHttpHeader "HX-Push"
|
||||
/// Pushes a new url into the history stack
|
||||
let withHxPushUrl : string -> HttpHandler =
|
||||
setHttpHeader "HX-Push-Url"
|
||||
|
||||
/// Explicitly do not push a new URL into the history stack
|
||||
let withHxNoPushUrl : HttpHandler =
|
||||
toLowerBool false |> withHxPushUrl
|
||||
|
||||
/// Pushes a new url into the history stack
|
||||
[<Obsolete "Use withHxPushUrl; HX-Push was replaced by HX-Push-Url in v1.8.0">]
|
||||
let withHxPush = withHxPushUrl
|
||||
|
||||
/// Explicitly do not push a new URL into the history stack
|
||||
[<Obsolete "Use withHxNoPushUrl; HX-Push was replaced by HX-Push-Url in v1.8.0">]
|
||||
let withHxNoPush = withHxNoPushUrl
|
||||
|
||||
/// Can be used to do a client-side redirect to a new location
|
||||
let withHxRedirect : string -> HttpHandler =
|
||||
|
@ -74,6 +86,22 @@ module Handlers =
|
|||
let withHxRefresh : bool -> HttpHandler =
|
||||
toLowerBool >> setHttpHeader "HX-Refresh"
|
||||
|
||||
/// Replaces the current URL in the history stack
|
||||
let withHxReplaceUrl : string -> HttpHandler =
|
||||
setHttpHeader "HX-Replace-Url"
|
||||
|
||||
/// Explicitly do not replace the current URL in the history stack
|
||||
let withHxNoReplaceUrl : HttpHandler =
|
||||
toLowerBool false |> withHxReplaceUrl
|
||||
|
||||
/// Override which portion of the response will be swapped into the target document
|
||||
let withHxReselect : string -> HttpHandler =
|
||||
setHttpHeader "HX-Reselect"
|
||||
|
||||
/// Override the `hx-swap` attribute from the initiating element
|
||||
let withHxReswap : string -> HttpHandler =
|
||||
setHttpHeader "HX-Reswap"
|
||||
|
||||
/// Allows you to override the `hx-target` attribute
|
||||
let withHxRetarget : string -> HttpHandler =
|
||||
setHttpHeader "HX-Retarget"
|
||||
|
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
This package enables server-side support for [htmx](https://htmx.org) within [Giraffe](https://giraffe.wiki) and ASP.NET's `HttpContext`.
|
||||
|
||||
**htmx version: 2.0.3**
|
||||
|
||||
_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
|
||||
|
||||
1. Install the package.
|
||||
|
@ -12,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
|
||||
|
@ -22,12 +26,14 @@ 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.
|
||||
|
||||
### 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.
|
33
src/Tests/Common.fs
Normal file
33
src/Tests/Common.fs
Normal file
|
@ -0,0 +1,33 @@
|
|||
module Common
|
||||
|
||||
open Expecto
|
||||
open Giraffe.Htmx
|
||||
|
||||
/// 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" [ swap ]
|
350
src/Tests/Htmx.fs
Normal file
350
src/Tests/Htmx.fs
Normal file
|
@ -0,0 +1,350 @@
|
|||
module Htmx
|
||||
|
||||
open System
|
||||
open Expecto
|
||||
open Giraffe.Htmx
|
||||
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 "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"
|
||||
}
|
||||
]
|
||||
|
||||
/// All tests for this module
|
||||
let allTests = testList "Htmx" [ dictExtensions; reqExtensions; handlers ]
|
6
src/Tests/Program.fs
Normal file
6
src/Tests/Program.fs
Normal file
|
@ -0,0 +1,6 @@
|
|||
open Expecto
|
||||
|
||||
let allTests = testList "Giraffe" [ Common.allTests; Htmx.allTests; ViewEngine.allTests ]
|
||||
|
||||
[<EntryPoint>]
|
||||
let main args = runTestsWithCLIArgs [] args allTests
|
26
src/Tests/Tests.fsproj
Normal file
26
src/Tests/Tests.fsproj
Normal file
|
@ -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>
|
994
src/Tests/ViewEngine.fs
Normal file
994
src/Tests/ViewEngine.fs
Normal file
|
@ -0,0 +1,994 @@
|
|||
module ViewEngine
|
||||
|
||||
open Expecto
|
||||
open Giraffe.ViewEngine
|
||||
open Giraffe.ViewEngine.Htmx
|
||||
|
||||
/// Tests for the HxEncoding module
|
||||
let hxEncoding =
|
||||
testList "HxEncoding" [
|
||||
test "Form is correct" {
|
||||
Expect.equal HxEncoding.Form "application/x-www-form-urlencoded" "Form encoding not correct"
|
||||
}
|
||||
test "MultipartForm is correct" {
|
||||
Expect.equal HxEncoding.MultipartForm "multipart/form-data" "Multipart form encoding not correct"
|
||||
}
|
||||
]
|
||||
|
||||
let hxEvent =
|
||||
testList "HxEvent" [
|
||||
testList "Abort" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string Abort) "abort" "Abort event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (Abort.ToHxOnString()) "abort" "Abort hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "AfterOnLoad" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string AfterOnLoad) "afterOnLoad" "AfterOnLoad event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (AfterOnLoad.ToHxOnString()) "after-on-load" "AfterOnLoad hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "AfterProcessNode" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string AfterProcessNode) "afterProcessNode" "AfterProcessNode event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(AfterProcessNode.ToHxOnString())
|
||||
"after-process-node"
|
||||
"AfterProcessNode hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "AfterRequest" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string AfterRequest) "afterRequest" "AfterRequest event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (AfterRequest.ToHxOnString()) "after-request" "AfterRequest hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "AfterSettle" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string AfterSettle) "afterSettle" "AfterSettle event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (AfterSettle.ToHxOnString()) "after-settle" "AfterSettle hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "AfterSwap" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string AfterSwap) "afterSwap" "AfterSwap event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (AfterSwap.ToHxOnString()) "after-swap" "AfterSwap hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "BeforeCleanupElement" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal
|
||||
(string BeforeCleanupElement) "beforeCleanupElement" "BeforeCleanupElement event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(BeforeCleanupElement.ToHxOnString())
|
||||
"before-cleanup-element"
|
||||
"BeforeCleanupElement hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "BeforeOnLoad" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string BeforeOnLoad) "beforeOnLoad" "BeforeOnLoad event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (BeforeOnLoad.ToHxOnString()) "before-on-load" "BeforeOnLoad hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "BeforeProcessNode" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string BeforeProcessNode) "beforeProcessNode" "BeforeProcessNode event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(BeforeProcessNode.ToHxOnString())
|
||||
"before-process-node"
|
||||
"BeforeProcessNode hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "BeforeRequest" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string BeforeRequest) "beforeRequest" "BeforeRequest event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(BeforeRequest.ToHxOnString()) "before-request" "BeforeRequest hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "BeforeSwap" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string BeforeSwap) "beforeSwap" "BeforeSwap event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (BeforeSwap.ToHxOnString()) "before-swap" "BeforeSwap hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "BeforeSend" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string BeforeSend) "beforeSend" "BeforeSend event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (BeforeSend.ToHxOnString()) "before-send" "BeforeSend hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "ConfigRequest" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string ConfigRequest) "configRequest" "ConfigRequest event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(ConfigRequest.ToHxOnString()) "config-request" "ConfigRequest hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "Confirm" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string Confirm) "confirm" "Confirm event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (Confirm.ToHxOnString()) "confirm" "Confirm hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "HistoryCacheError" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string HistoryCacheError) "historyCacheError" "HistoryCacheError event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(HistoryCacheError.ToHxOnString())
|
||||
"history-cache-error"
|
||||
"HistoryCacheError hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "HistoryCacheMiss" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string HistoryCacheMiss) "historyCacheMiss" "HistoryCacheMiss event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(HistoryCacheMiss.ToHxOnString())
|
||||
"history-cache-miss"
|
||||
"HistoryCacheMiss hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "HistoryCacheMissError" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal
|
||||
(string HistoryCacheMissError)
|
||||
"historyCacheMissError"
|
||||
"HistoryCacheMissError event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(HistoryCacheMissError.ToHxOnString())
|
||||
"history-cache-miss-error"
|
||||
"HistoryCacheMissError hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "HistoryCacheMissLoad" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal
|
||||
(string HistoryCacheMissLoad) "historyCacheMissLoad" "HistoryCacheMissLoad event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(HistoryCacheMissLoad.ToHxOnString())
|
||||
"history-cache-miss-load"
|
||||
"HistoryCacheMissLoad hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "HistoryRestore" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string HistoryRestore) "historyRestore" "HistoryRestore event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(HistoryRestore.ToHxOnString()) "history-restore" "HistoryRestore hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "BeforeHistorySave" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string BeforeHistorySave) "beforeHistorySave" "BeforeHistorySave event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(BeforeHistorySave.ToHxOnString())
|
||||
"before-history-save"
|
||||
"BeforeHistorySave hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "Load" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string Load) "load" "Load event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (Load.ToHxOnString()) "load" "Load hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "NoSseSourceError" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string NoSseSourceError) "noSSESourceError" "NoSseSourceError event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(NoSseSourceError.ToHxOnString())
|
||||
"no-sse-source-error"
|
||||
"NoSseSourceError hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "OnLoadError" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string OnLoadError) "onLoadError" "OnLoadError event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (OnLoadError.ToHxOnString()) "on-load-error" "OnLoadError hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "OobAfterSwap" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string OobAfterSwap) "oobAfterSwap" "OobAfterSwap event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (OobAfterSwap.ToHxOnString()) "oob-after-swap" "OobAfterSwap hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "OobBeforeSwap" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string OobBeforeSwap) "oobBeforeSwap" "OobBeforeSwap event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(OobBeforeSwap.ToHxOnString()) "oob-before-swap" "OobBeforeSwap hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "OobErrorNoTarget" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string OobErrorNoTarget) "oobErrorNoTarget" "OobErrorNoTarget event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(OobErrorNoTarget.ToHxOnString())
|
||||
"oob-error-no-target"
|
||||
"OobErrorNoTarget hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "Prompt" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string Prompt) "prompt" "Prompt event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (Prompt.ToHxOnString()) "prompt" "Prompt hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "PushedIntoHistory" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string PushedIntoHistory) "pushedIntoHistory" "PushedIntoHistory event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(PushedIntoHistory.ToHxOnString())
|
||||
"pushed-into-history"
|
||||
"PushedIntoHistory hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "ResponseError" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string ResponseError) "responseError" "ResponseError event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(ResponseError.ToHxOnString()) "response-error" "ResponseError hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "SendError" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string SendError) "sendError" "SendError event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (SendError.ToHxOnString()) "send-error" "SendError hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "SseError" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string SseError) "sseError" "SseError event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (SseError.ToHxOnString()) "sse-error" "SseError hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "SseOpen" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string SseOpen) "sseOpen" "SseOpen event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (SseOpen.ToHxOnString()) "sse-open" "SseOpen hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "SwapError" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string SwapError) "swapError" "SwapError event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (SwapError.ToHxOnString()) "swap-error" "SwapError hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "TargetError" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string TargetError) "targetError" "TargetError event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (TargetError.ToHxOnString()) "target-error" "TargetError hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "Timeout" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string Timeout) "timeout" "Timeout event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (Timeout.ToHxOnString()) "timeout" "Timeout hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "ValidationValidate" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal
|
||||
(string ValidationValidate) "validation:validate" "ValidationValidate event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(ValidationValidate.ToHxOnString())
|
||||
"validation:validate"
|
||||
"ValidationValidate hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "ValidationFailed" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string ValidationFailed) "validation:failed" "ValidationFailed event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(ValidationFailed.ToHxOnString())
|
||||
"validation:failed"
|
||||
"ValidationFailed hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "ValidationHalted" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string ValidationHalted) "validation:halted" "ValidationHalted event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal
|
||||
(ValidationHalted.ToHxOnString())
|
||||
"validation:halted"
|
||||
"ValidationHalted hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "XhrAbort" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string XhrAbort) "xhr:abort" "XhrAbort event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (XhrAbort.ToHxOnString()) "xhr:abort" "XhrAbort hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "XhrLoadEnd" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string XhrLoadEnd) "xhr:loadend" "XhrLoadEnd event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (XhrLoadEnd.ToHxOnString()) "xhr:loadend" "XhrLoadEnd hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "XhrLoadStart" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string XhrLoadStart) "xhr:loadstart" "XhrLoadStart event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (XhrLoadStart.ToHxOnString()) "xhr:loadstart" "XhrLoadStart hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
testList "XhrProgress" [
|
||||
test "ToString succeeds" {
|
||||
Expect.equal (string XhrProgress) "xhr:progress" "XhrProgress event name not correct"
|
||||
}
|
||||
test "ToHxOnString succeeds" {
|
||||
Expect.equal (XhrProgress.ToHxOnString()) "xhr:progress" "XhrProgress hx-on event name not correct"
|
||||
}
|
||||
]
|
||||
|
||||
]
|
||||
/// Tests for the HxHeaders module
|
||||
let hxHeaders =
|
||||
testList "HxHeaders" [
|
||||
testList "From" [
|
||||
test "succeeds with an empty list" {
|
||||
Expect.equal (HxHeaders.From []) "{ }" "Empty headers not correct"
|
||||
}
|
||||
test "succeeds and escapes quotes" {
|
||||
Expect.equal
|
||||
(HxHeaders.From [ "test", "one two three"; "again", """four "five" six""" ])
|
||||
"""{ "test": "one two three", "again": "four \"five\" six" }""" "Headers not correct"
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
/// Tests for the HxParams module
|
||||
let hxParams =
|
||||
testList "HxParams" [
|
||||
test "All is correct" {
|
||||
Expect.equal HxParams.All "*" "All is not correct"
|
||||
}
|
||||
test "None is correct" {
|
||||
Expect.equal HxParams.None "none" "None is not correct"
|
||||
}
|
||||
testList "With" [
|
||||
test "succeeds with empty list" {
|
||||
Expect.equal (HxParams.With []) "" "With with empty list should have been blank"
|
||||
}
|
||||
test "succeeds with one list item" {
|
||||
Expect.equal (HxParams.With [ "boo" ]) "boo" "With single item incorrect"
|
||||
}
|
||||
test "succeeds with multiple list items" {
|
||||
Expect.equal (HxParams.With [ "foo"; "bar"; "baz" ]) "foo,bar,baz" "With multiple items incorrect"
|
||||
}
|
||||
]
|
||||
testList "Except" [
|
||||
test "succeeds with empty list" {
|
||||
Expect.equal (HxParams.Except []) "not " "Except with empty list incorrect"
|
||||
}
|
||||
test "succeeds with one list item" {
|
||||
Expect.equal (HxParams.Except [ "that" ]) "not that" "Except single item incorrect"
|
||||
}
|
||||
test "succeeds with multiple list items" {
|
||||
Expect.equal (HxParams.Except [ "blue"; "green" ]) "not blue,green" "Except multiple items incorrect"
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
/// Tests for the HxRequest module
|
||||
let hxRequest =
|
||||
testList "HxRequest" [
|
||||
testList "Configure" [
|
||||
test "succeeds with an empty list" {
|
||||
Expect.equal (HxRequest.Configure []) "{ }" "Configure with empty list incorrect"
|
||||
}
|
||||
test "succeeds with a non-empty list" {
|
||||
Expect.equal
|
||||
(HxRequest.Configure [ "\"a\": \"b\""; "\"c\": \"d\"" ]) """{ "a": "b", "c": "d" }"""
|
||||
"Configure with a non-empty list incorrect"
|
||||
}
|
||||
test "succeeds with all known params configured" {
|
||||
Expect.equal
|
||||
(HxRequest.Configure
|
||||
[ HxRequest.Timeout 1000; HxRequest.Credentials false; HxRequest.NoHeaders true ])
|
||||
"""{ "timeout": 1000, "credentials": false, "noHeaders": true }"""
|
||||
"Configure with all known params incorrect"
|
||||
}
|
||||
]
|
||||
test "Timeout succeeds" {
|
||||
Expect.equal (HxRequest.Timeout 50) "\"timeout\": 50" "Timeout value incorrect"
|
||||
}
|
||||
testList "Credentials" [
|
||||
test "succeeds when set to true" {
|
||||
Expect.equal (HxRequest.Credentials true) "\"credentials\": true" "Credentials value incorrect"
|
||||
}
|
||||
test "succeeds when set to false" {
|
||||
Expect.equal (HxRequest.Credentials false) "\"credentials\": false" "Credentials value incorrect"
|
||||
}
|
||||
]
|
||||
testList "NoHeaders" [
|
||||
test "succeeds when set to true" {
|
||||
Expect.equal (HxRequest.NoHeaders true) "\"noHeaders\": true" "NoHeaders value incorrect"
|
||||
}
|
||||
test "succeeds when set to false" {
|
||||
Expect.equal (HxRequest.NoHeaders false) "\"noHeaders\": false" "NoHeaders value incorrect"
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
/// Tests for the HxTrigger module
|
||||
let hxTrigger =
|
||||
testList "HxTrigger" [
|
||||
test "Click is correct" {
|
||||
Expect.equal HxTrigger.Click "click" "Click is incorrect"
|
||||
}
|
||||
test "Load is correct" {
|
||||
Expect.equal HxTrigger.Load "load" "Load is incorrect"
|
||||
}
|
||||
test "Revealed is correct" {
|
||||
Expect.equal HxTrigger.Revealed "revealed" "Revealed is incorrect"
|
||||
}
|
||||
test "Every succeeds" {
|
||||
Expect.equal (HxTrigger.Every "3s") "every 3s" "Every is incorrect"
|
||||
}
|
||||
testList "Filter" [
|
||||
test "Alt succeeds" {
|
||||
Expect.equal (HxTrigger.Filter.Alt HxTrigger.Click) "click[altKey]" "Alt filter incorrect"
|
||||
}
|
||||
test "Ctrl succeeds" {
|
||||
Expect.equal (HxTrigger.Filter.Ctrl HxTrigger.Click) "click[ctrlKey]" "Ctrl filter incorrect"
|
||||
}
|
||||
test "Shift succeeds" {
|
||||
Expect.equal (HxTrigger.Filter.Shift HxTrigger.Click) "click[shiftKey]" "Shift filter incorrect"
|
||||
}
|
||||
test "CtrlAlt succeeds" {
|
||||
Expect.equal
|
||||
(HxTrigger.Filter.CtrlAlt HxTrigger.Click) "click[ctrlKey&&altKey]" "Ctrl+Alt filter incorrect"
|
||||
}
|
||||
test "CtrlShift succeeds" {
|
||||
Expect.equal
|
||||
(HxTrigger.Filter.CtrlShift HxTrigger.Click) "click[ctrlKey&&shiftKey]"
|
||||
"Ctrl+Shift filter incorrect"
|
||||
}
|
||||
test "CtrlAltShift succeeds" {
|
||||
Expect.equal
|
||||
(HxTrigger.Filter.CtrlAltShift HxTrigger.Click) "click[ctrlKey&&altKey&&shiftKey]"
|
||||
"Ctrl+Alt+Shift filter incorrect"
|
||||
}
|
||||
test "AltShift succeeds" {
|
||||
Expect.equal
|
||||
(HxTrigger.Filter.AltShift HxTrigger.Click) "click[altKey&&shiftKey]" "Alt+Shift filter incorrect"
|
||||
}
|
||||
]
|
||||
testList "Once" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.Once "") "once" "Once modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.Once "click") "click once" "Once modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "Changed" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.Changed "") "changed" "Changed modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.Changed "click") "click changed" "Changed modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "Delay" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.Delay "1s" "") "delay:1s" "Delay modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.Delay "2s" "click") "click delay:2s" "Delay modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "Throttle" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.Throttle "4s" "") "throttle:4s" "Throttle modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.Throttle "7s" "click") "click throttle:7s" "Throttle modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "From" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.From ".nav" "") "from:.nav" "From modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.From "#somewhere" "click") "click from:#somewhere" "From modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "FromDocument" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.FromDocument "") "from:document" "FromDocument modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.FromDocument "click") "click from:document" "FromDocument modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "FromWindow" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.FromWindow "") "from:window" "FromWindow modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.FromWindow "click") "click from:window" "FromWindow modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "FromClosest" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.FromClosest "div" "") "from:closest div" "FromClosest modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.FromClosest "p" "click") "click from:closest p" "FromClosest modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "FromFind" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.FromFind "li" "") "from:find li" "FromFind modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.FromFind ".spot" "click") "click from:find .spot" "FromFind modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "Target" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.Target "main" "") "target:main" "Target modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.Target "footer" "click") "click target:footer" "Target modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "Consume" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.Consume "") "consume" "Consume modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.Consume "click") "click consume" "Consume modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "Queue" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.Queue "abc" "") "queue:abc" "Queue modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.Queue "def" "click") "click queue:def" "Queue modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "QueueFirst" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.QueueFirst "") "queue:first" "QueueFirst modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.QueueFirst "click") "click queue:first" "QueueFirst modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "QueueLast" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.QueueLast "") "queue:last" "QueueLast modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.QueueLast "click") "click queue:last" "QueueLast modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "QueueAll" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.QueueAll "") "queue:all" "QueueAll modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.QueueAll "click") "click queue:all" "QueueAll modifier incorrect"
|
||||
}
|
||||
]
|
||||
testList "QueueNone" [
|
||||
test "succeeds when it is the first modifier" {
|
||||
Expect.equal (HxTrigger.QueueNone "") "queue:none" "QueueNone modifier incorrect"
|
||||
}
|
||||
test "succeeds when it is not the first modifier" {
|
||||
Expect.equal (HxTrigger.QueueNone "click") "click queue:none" "QueueNone modifier incorrect"
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
/// Tests for the HxVals module
|
||||
let hxVals =
|
||||
testList "HxVals" [
|
||||
testList "From" [
|
||||
test "succeeds with an empty list" {
|
||||
Expect.equal (HxVals.From []) "{ }" "From with an empty list is incorrect"
|
||||
}
|
||||
test "succeeds and escapes quotes" {
|
||||
Expect.equal
|
||||
(HxVals.From [ "test", """a "b" c"""; "2", "d e f" ])
|
||||
"""{ "test": "a \"b\" c", "2": "d e f" }""" "From value is incorrect"
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
/// Pipe-able assertion for a rendered node
|
||||
let shouldRender expected node =
|
||||
Expect.equal (RenderView.AsString.htmlNode node) expected "Rendered HTML incorrect"
|
||||
|
||||
/// Tests for the HtmxAttrs module
|
||||
let attributes =
|
||||
testList "Attributes" [
|
||||
|
||||
test "_hxBoost succeeds" {
|
||||
div [ _hxBoost ] [] |> shouldRender """<div hx-boost="true"></div>"""
|
||||
}
|
||||
test "_hxConfirm succeeds" {
|
||||
button [ _hxConfirm "REALLY?!?" ] [] |> shouldRender """<button hx-confirm="REALLY?!?"></button>"""
|
||||
}
|
||||
test "_hxDelete succeeds" {
|
||||
span [ _hxDelete "/this-endpoint" ] [] |> shouldRender """<span hx-delete="/this-endpoint"></span>"""
|
||||
}
|
||||
test "_hxDisable succeeds" {
|
||||
p [ _hxDisable ] [] |> shouldRender """<p hx-disable></p>"""
|
||||
}
|
||||
test "_hxDisabledElt succeeds" {
|
||||
button [ _hxDisabledElt "this" ] [] |> shouldRender """<button hx-disabled-elt="this"></button>"""
|
||||
}
|
||||
test "_hxDisinherit succeeds" {
|
||||
strong [ _hxDisinherit "*" ] [] |> shouldRender """<strong hx-disinherit="*"></strong>"""
|
||||
}
|
||||
test "_hxEncoding succeeds" {
|
||||
form [ _hxEncoding "utf-7" ] [] |> shouldRender """<form hx-encoding="utf-7"></form>"""
|
||||
}
|
||||
test "_hxExt succeeds" {
|
||||
section [ _hxExt "extendme" ] [] |> shouldRender """<section hx-ext="extendme"></section>"""
|
||||
}
|
||||
test "_hxGet succeeds" {
|
||||
article [ _hxGet "/the-text" ] [] |> shouldRender """<article hx-get="/the-text"></article>"""
|
||||
}
|
||||
test "_hxHeaders succeeds" {
|
||||
figure [ _hxHeaders """{ "X-Special-Header": "some-header" }""" ] []
|
||||
|> shouldRender """<figure hx-headers="{ "X-Special-Header": "some-header" }"></figure>"""
|
||||
}
|
||||
test "_hxHistory succeeds" {
|
||||
span [ _hxHistory "false" ] [] |> shouldRender """<span hx-history="false"></span>"""
|
||||
}
|
||||
test "_hxHistoryElt succeeds" {
|
||||
table [ _hxHistoryElt ] [] |> shouldRender """<table hx-history-elt></table>"""
|
||||
}
|
||||
test "_hxInclude succeeds" {
|
||||
a [ _hxInclude ".extra-stuff" ] [] |> shouldRender """<a hx-include=".extra-stuff"></a>"""
|
||||
}
|
||||
test "_hxIndicator succeeds" {
|
||||
aside [ _hxIndicator "#spinner" ] [] |> shouldRender """<aside hx-indicator="#spinner"></aside>"""
|
||||
}
|
||||
test "_hxNoBoost succeeds" {
|
||||
td [ _hxNoBoost ] [] |> shouldRender """<td hx-boost="false"></td>"""
|
||||
}
|
||||
test "_hxOnEvent succeeds" {
|
||||
a [ _hxOnEvent "click" "doThis()" ] [] |> shouldRender """<a hx-on:click="doThis()"></a>"""
|
||||
}
|
||||
test "_hxOnHxEvent succeeds" {
|
||||
strong [ _hxOnHxEvent BeforeSwap "changeStuff()" ] []
|
||||
|> shouldRender """<strong hx-on::before-swap="changeStuff()"></strong>"""
|
||||
}
|
||||
test "_hxParams succeeds" {
|
||||
br [ _hxParams "[p1,p2]" ] |> shouldRender """<br hx-params="[p1,p2]">"""
|
||||
}
|
||||
test "_hxPatch succeeds" {
|
||||
div [ _hxPatch "/arrrgh" ] [] |> shouldRender """<div hx-patch="/arrrgh"></div>"""
|
||||
}
|
||||
test "_hxPost succeeds" {
|
||||
hr [ _hxPost "/hear-ye-hear-ye" ] |> shouldRender """<hr hx-post="/hear-ye-hear-ye">"""
|
||||
}
|
||||
test "_hxPreserve succeeds" {
|
||||
img [ _hxPreserve ] |> shouldRender """<img hx-preserve="true">"""
|
||||
}
|
||||
test "_hxPrompt succeeds" {
|
||||
strong [ _hxPrompt "Who goes there?" ] []
|
||||
|> shouldRender """<strong hx-prompt="Who goes there?"></strong>"""
|
||||
}
|
||||
test "_hxPushUrl succeeds" {
|
||||
dl [ _hxPushUrl "/a-b-c" ] [] |> shouldRender """<dl hx-push-url="/a-b-c"></dl>"""
|
||||
}
|
||||
test "_hxPut succeeds" {
|
||||
s [ _hxPut "/take-this" ] [] |> shouldRender """<s hx-put="/take-this"></s>"""
|
||||
}
|
||||
test "_hxReplaceUrl succeeds" {
|
||||
p [ _hxReplaceUrl "/something-else" ] [] |> shouldRender """<p hx-replace-url="/something-else"></p>"""
|
||||
}
|
||||
test "_hxRequest succeeds" {
|
||||
u [ _hxRequest "noHeaders" ] [] |> shouldRender """<u hx-request="noHeaders"></u>"""
|
||||
}
|
||||
test "_hxSelect succeeds" {
|
||||
nav [ _hxSelect "#navbar" ] [] |> shouldRender """<nav hx-select="#navbar"></nav>"""
|
||||
}
|
||||
test "_hxSelectOob succeeds" {
|
||||
section [ _hxSelectOob "#oob" ] [] |> shouldRender """<section hx-select-oob="#oob"></section>"""
|
||||
}
|
||||
test "_hxSwap succeeds" {
|
||||
del [ _hxSwap "innerHTML" ] [] |> shouldRender """<del hx-swap="innerHTML"></del>"""
|
||||
}
|
||||
test "_hxSwapWithTransition succeeds" {
|
||||
del [ _hxSwapWithTransition "innerHTML" ] []
|
||||
|> shouldRender """<del hx-swap="innerHTML transition:true"></del>"""
|
||||
}
|
||||
test "_hxSwapOob succeeds" {
|
||||
li [ _hxSwapOob "true" ] [] |> shouldRender """<li hx-swap-oob="true"></li>"""
|
||||
}
|
||||
test "_hxSync succeeds" {
|
||||
nav [ _hxSync "closest form:abort" ] [] |> shouldRender """<nav hx-sync="closest form:abort"></nav>"""
|
||||
}
|
||||
test "_hxTarget succeeds" {
|
||||
header [ _hxTarget "#somewhereElse" ] [] |> shouldRender """<header hx-target="#somewhereElse"></header>"""
|
||||
}
|
||||
test "_hxTrigger succeeds" {
|
||||
figcaption [ _hxTrigger "load" ] [] |> shouldRender """<figcaption hx-trigger="load"></figcaption>"""
|
||||
}
|
||||
test "_hxVals succeeds" {
|
||||
dt [ _hxVals """{ "extra": "values" }""" ] []
|
||||
|> shouldRender """<dt hx-vals="{ "extra": "values" }"></dt>"""
|
||||
}
|
||||
test "_sseSwap succeeds" {
|
||||
ul [ _sseSwap "sseMessageName" ] [] |> shouldRender """<ul sse-swap="sseMessageName"></ul>"""
|
||||
}
|
||||
test "_sseConnect succeeds" {
|
||||
div [ _sseConnect "/gps/sse" ] [] |> shouldRender """<div sse-connect="/gps/sse"></div>"""
|
||||
}
|
||||
]
|
||||
|
||||
/// Tests for the Script module
|
||||
let script =
|
||||
testList "Script" [
|
||||
test "minified succeeds" {
|
||||
let html = RenderView.AsString.htmlNode Script.minified
|
||||
Expect.equal
|
||||
html
|
||||
"""<script src="https://unpkg.com/htmx.org@2.0.3" integrity="sha384-0895/pl2MU10Hqc6jd4RvrthNlDiE9U1tWmX7WRESftEDRosgxNsQG/Ze9YMRzHq" crossorigin="anonymous"></script>"""
|
||||
"Minified script tag is incorrect"
|
||||
}
|
||||
test "unminified succeeds" {
|
||||
let html = RenderView.AsString.htmlNode Script.unminified
|
||||
Expect.equal
|
||||
html
|
||||
"""<script src="https://unpkg.com/htmx.org@2.0.3/dist/htmx.js" integrity="sha384-BBDmZzVt6vjz5YbQqZPtFZW82o8QotoM7RUp5xOxV3nSJ8u2pSdtzFAbGKzTlKtg" crossorigin="anonymous"></script>"""
|
||||
"Unminified script tag is incorrect"
|
||||
}
|
||||
]
|
||||
|
||||
open System.Text
|
||||
|
||||
/// Tests for the RenderFragment module
|
||||
let renderFragment =
|
||||
testList "RenderFragment" [
|
||||
|
||||
/// Validate that the two object references are the same object
|
||||
let isSame obj1 obj2 message =
|
||||
Expect.isTrue (obj.ReferenceEquals (obj1, obj2)) message
|
||||
|
||||
testList "findIdNode" [
|
||||
test "fails with a Text node" {
|
||||
Expect.isNone (RenderFragment.findIdNode "blue" (Text "")) "There should not have been a node found"
|
||||
}
|
||||
test "fails with a VoidElement without a matching ID" {
|
||||
Expect.isNone
|
||||
(RenderFragment.findIdNode "purple" (br [ _id "mauve" ])) "There should not have been a node found"
|
||||
}
|
||||
test "fails with a ParentNode with no children with a matching ID" {
|
||||
Expect.isNone
|
||||
(RenderFragment.findIdNode "green" (p [] [ str "howdy"; span [] [ str "huh" ] ]))
|
||||
"There should not have been a node found"
|
||||
}
|
||||
test "succeeds with a VoidElement with a matching ID" {
|
||||
let leNode = hr [ _id "groovy" ]
|
||||
let foundNode = RenderFragment.findIdNode "groovy" leNode
|
||||
Expect.isSome foundNode "There should have been a node found"
|
||||
isSame leNode foundNode.Value "The node should have been the same object"
|
||||
}
|
||||
test "succeeds with a ParentNode with a child with a matching ID" {
|
||||
let leNode = span [ _id "its-me" ] [ str "Mario" ]
|
||||
let foundNode =
|
||||
RenderFragment.findIdNode "its-me" (p [] [ str "test"; str "again"; leNode; str "un mas" ])
|
||||
Expect.isSome foundNode "There should have been a node found"
|
||||
isSame leNode foundNode.Value "The node should have been the same object"
|
||||
}
|
||||
]
|
||||
|
||||
/// Generate a message if the requested ID node is not found
|
||||
let nodeNotFound (nodeId : string) =
|
||||
$"<em>– ID {nodeId} not found –</em>"
|
||||
|
||||
testList "AsString" [
|
||||
testList "htmlFromNodes" [
|
||||
test "succeeds when an ID is matched" {
|
||||
let html =
|
||||
RenderFragment.AsString.htmlFromNodes "needle"
|
||||
[ p [] []
|
||||
p [ _id "haystack" ] [ str "hay"; span [ _id "needle" ] [ str "ouch" ]; str "hay" ]
|
||||
]
|
||||
Expect.equal html """<span id="needle">ouch</span>""" "HTML is incorrect"
|
||||
}
|
||||
test "fails when an ID is not matched" {
|
||||
Expect.equal
|
||||
(RenderFragment.AsString.htmlFromNodes "oops" []) (nodeNotFound "oops") "HTML is incorrect"
|
||||
}
|
||||
]
|
||||
testList "htmlFromNode" [
|
||||
test "succeeds when ID is matched at top level" {
|
||||
let html = RenderFragment.AsString.htmlFromNode "wow" (p [ _id "wow" ] [ str "found it" ])
|
||||
Expect.equal html """<p id="wow">found it</p>""" "HTML is incorrect"
|
||||
}
|
||||
test "succeeds when ID is matched in child element" {
|
||||
let html =
|
||||
div [] [ p [] [ str "not it" ]; p [ _id "hey" ] [ str "ta-da" ]]
|
||||
|> RenderFragment.AsString.htmlFromNode "hey"
|
||||
Expect.equal html """<p id="hey">ta-da</p>""" "HTML is incorrect"
|
||||
}
|
||||
test "fails when an ID is not matched" {
|
||||
Expect.equal
|
||||
(RenderFragment.AsString.htmlFromNode "me" (hr [])) (nodeNotFound "me") "HTML is incorrect"
|
||||
}
|
||||
]
|
||||
]
|
||||
testList "AsBytes" [
|
||||
|
||||
/// Alias for UTF-8 encoding
|
||||
let utf8 = Encoding.UTF8
|
||||
|
||||
testList "htmlFromNodes" [
|
||||
test "succeeds when an ID is matched" {
|
||||
let bytes =
|
||||
RenderFragment.AsBytes.htmlFromNodes "found"
|
||||
[ p [] []
|
||||
p [ _id "not-it" ] [ str "nope"; span [ _id "found" ] [ str "boo" ]; str "nope" ]
|
||||
]
|
||||
Expect.equal bytes (utf8.GetBytes """<span id="found">boo</span>""") "HTML bytes are incorrect"
|
||||
}
|
||||
test "fails when an ID is not matched" {
|
||||
Expect.equal
|
||||
(RenderFragment.AsBytes.htmlFromNodes "whiff" []) (utf8.GetBytes (nodeNotFound "whiff"))
|
||||
"HTML bytes are incorrect"
|
||||
}
|
||||
]
|
||||
testList "htmlFromNode" [
|
||||
test "succeeds when ID is matched at top level" {
|
||||
let bytes = RenderFragment.AsBytes.htmlFromNode "first" (p [ _id "first" ] [ str "!!!" ])
|
||||
Expect.equal bytes (utf8.GetBytes """<p id="first">!!!</p>""") "HTML bytes are incorrect"
|
||||
}
|
||||
test "succeeds when ID is matched in child element" {
|
||||
let bytes =
|
||||
div [] [ p [] [ str "not me" ]; p [ _id "child" ] [ str "node" ]]
|
||||
|> RenderFragment.AsBytes.htmlFromNode "child"
|
||||
Expect.equal bytes (utf8.GetBytes """<p id="child">node</p>""") "HTML bytes are incorrect"
|
||||
}
|
||||
test "fails when an ID is not matched" {
|
||||
Expect.equal
|
||||
(RenderFragment.AsBytes.htmlFromNode "foo" (hr [])) (utf8.GetBytes (nodeNotFound "foo"))
|
||||
"HTML bytes are incorrect"
|
||||
}
|
||||
]
|
||||
]
|
||||
testList "IntoStringBuilder" [
|
||||
testList "htmlFromNodes" [
|
||||
test "succeeds when an ID is matched" {
|
||||
let sb = StringBuilder ()
|
||||
RenderFragment.IntoStringBuilder.htmlFromNodes sb "find-me"
|
||||
[ p [] []; p [ _id "peekaboo" ] [ str "bzz"; str "nope"; span [ _id "find-me" ] [ str ";)" ] ]]
|
||||
Expect.equal (string sb) """<span id="find-me">;)</span>""" "HTML is incorrect"
|
||||
}
|
||||
test "fails when an ID is not matched" {
|
||||
let sb = StringBuilder ()
|
||||
RenderFragment.IntoStringBuilder.htmlFromNodes sb "missing" []
|
||||
Expect.equal (string sb) (nodeNotFound "missing") "HTML is incorrect"
|
||||
}
|
||||
]
|
||||
testList "htmlFromNode" [
|
||||
test "succeeds when ID is matched at top level" {
|
||||
let sb = StringBuilder ()
|
||||
RenderFragment.IntoStringBuilder.htmlFromNode sb "top" (p [ _id "top" ] [ str "pinnacle" ])
|
||||
Expect.equal (string sb) """<p id="top">pinnacle</p>""" "HTML is incorrect"
|
||||
}
|
||||
test "succeeds when ID is matched in child element" {
|
||||
let sb = StringBuilder ()
|
||||
div [] [ p [] [ str "nada" ]; p [ _id "it" ] [ str "is here" ]]
|
||||
|> RenderFragment.IntoStringBuilder.htmlFromNode sb "it"
|
||||
Expect.equal (string sb) """<p id="it">is here</p>""" "HTML is incorrect"
|
||||
}
|
||||
test "fails when an ID is not matched" {
|
||||
let sb = StringBuilder ()
|
||||
RenderFragment.IntoStringBuilder.htmlFromNode sb "bar" (hr [])
|
||||
Expect.equal (string sb) (nodeNotFound "bar") "HTML is incorrect"
|
||||
}
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
/// All tests in this module
|
||||
let allTests =
|
||||
testList "ViewEngine.Htmx" [
|
||||
hxEncoding
|
||||
hxEvent
|
||||
hxHeaders
|
||||
hxParams
|
||||
hxRequest
|
||||
hxTrigger
|
||||
hxVals
|
||||
attributes
|
||||
script
|
||||
renderFragment
|
||||
]
|
|
@ -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,7 +1,6 @@
|
|||
<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>
|
||||
|
@ -10,10 +9,16 @@
|
|||
<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>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
module Giraffe.ViewEngine.Htmx
|
||||
module Giraffe.ViewEngine.Htmx
|
||||
|
||||
/// Serialize a list of key/value pairs to JSON (very rudimentary)
|
||||
let private toJson (kvps : (string * string) list) =
|
||||
|
@ -11,15 +11,159 @@ let private toJson (kvps : (string * string) list) =
|
|||
/// Valid values for the `hx-encoding` attribute
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxEncoding =
|
||||
|
||||
/// A standard HTTP form
|
||||
let Form = "application/x-www-form-urlencoded"
|
||||
|
||||
/// A multipart form (used for file uploads)
|
||||
let MultipartForm = "multipart/form-data"
|
||||
|
||||
|
||||
/// The events recognized by htmx
|
||||
[<Struct>]
|
||||
type HxEvent =
|
||||
/// Send this event to an element to abort a request
|
||||
| Abort
|
||||
/// Triggered after an AJAX request has completed processing a successful response
|
||||
| AfterOnLoad
|
||||
/// Triggered after htmx has initialized a node
|
||||
| AfterProcessNode
|
||||
/// Triggered after an AJAX request has completed
|
||||
| AfterRequest
|
||||
/// Triggered after the DOM has settled
|
||||
| AfterSettle
|
||||
/// Triggered after new content has been swapped in
|
||||
| AfterSwap
|
||||
/// Triggered before htmx disables an element or removes it from the DOM
|
||||
| BeforeCleanupElement
|
||||
/// Triggered before any response processing occurs
|
||||
| BeforeOnLoad
|
||||
/// Triggered before htmx initializes a node
|
||||
| BeforeProcessNode
|
||||
/// Triggered before an AJAX request is made
|
||||
| BeforeRequest
|
||||
/// Triggered before a swap is done, allows you to configure the swap
|
||||
| BeforeSwap
|
||||
/// Triggered just before an ajax request is sent
|
||||
| BeforeSend
|
||||
/// Triggered before the request, allows you to customize parameters, headers
|
||||
| ConfigRequest
|
||||
/// Triggered after a trigger occurs on an element, allows you to cancel (or delay) issuing the AJAX request
|
||||
| Confirm
|
||||
/// Triggered on an error during cache writing
|
||||
| HistoryCacheError
|
||||
/// Triggered on a cache miss in the history subsystem
|
||||
| HistoryCacheMiss
|
||||
/// Triggered on a unsuccessful remote retrieval
|
||||
| HistoryCacheMissError
|
||||
/// Triggered on a successful remote retrieval
|
||||
| HistoryCacheMissLoad
|
||||
/// Triggered when htmx handles a history restoration action
|
||||
| HistoryRestore
|
||||
/// Triggered before content is saved to the history cache
|
||||
| BeforeHistorySave
|
||||
/// Triggered when new content is added to the DOM
|
||||
| Load
|
||||
/// Triggered when an element refers to a SSE event in its trigger, but no parent SSE source has been defined
|
||||
| NoSseSourceError
|
||||
/// Triggered when an exception occurs during the onLoad handling in htmx
|
||||
| OnLoadError
|
||||
/// Triggered after an out of band element as been swapped in
|
||||
| OobAfterSwap
|
||||
/// Triggered before an out of band element swap is done, allows you to configure the swap
|
||||
| OobBeforeSwap
|
||||
/// Triggered when an out of band element does not have a matching ID in the current DOM
|
||||
| OobErrorNoTarget
|
||||
/// Triggered after a prompt is shown
|
||||
| Prompt
|
||||
/// Triggered after an url is pushed into history
|
||||
| PushedIntoHistory
|
||||
/// Triggered when an HTTP response error (non-200 or 300 response code) occurs
|
||||
| ResponseError
|
||||
/// Triggered when a network error prevents an HTTP request from happening
|
||||
| SendError
|
||||
/// Triggered when an error occurs with a SSE source
|
||||
| SseError
|
||||
/// Triggered when a SSE source is opened
|
||||
| SseOpen
|
||||
/// Triggered when an error occurs during the swap phase
|
||||
| SwapError
|
||||
/// Triggered when an invalid target is specified
|
||||
| TargetError
|
||||
/// Triggered when a request timeout occurs
|
||||
| Timeout
|
||||
/// Triggered before an element is validated
|
||||
| ValidationValidate
|
||||
/// Triggered when an element fails validation
|
||||
| ValidationFailed
|
||||
/// Triggered when a request is halted due to validation errors
|
||||
| ValidationHalted
|
||||
/// Triggered when an ajax request aborts
|
||||
| XhrAbort
|
||||
/// Triggered when an ajax request ends
|
||||
| XhrLoadEnd
|
||||
/// Triggered when an ajax request starts
|
||||
| XhrLoadStart
|
||||
/// Triggered periodically during an ajax request that supports progress events
|
||||
| XhrProgress
|
||||
|
||||
/// The htmx event name (fst) and kebab-case name (snd, for use with hx-on)
|
||||
static member private Values = Map [
|
||||
Abort, ("abort", "abort")
|
||||
AfterOnLoad, ("afterOnLoad", "after-on-load")
|
||||
AfterProcessNode, ("afterProcessNode", "after-process-node")
|
||||
AfterRequest, ("afterRequest", "after-request")
|
||||
AfterSettle, ("afterSettle", "after-settle")
|
||||
AfterSwap, ("afterSwap", "after-swap")
|
||||
BeforeCleanupElement, ("beforeCleanupElement", "before-cleanup-element")
|
||||
BeforeOnLoad, ("beforeOnLoad", "before-on-load")
|
||||
BeforeProcessNode, ("beforeProcessNode", "before-process-node")
|
||||
BeforeRequest, ("beforeRequest", "before-request")
|
||||
BeforeSwap, ("beforeSwap", "before-swap")
|
||||
BeforeSend, ("beforeSend", "before-send")
|
||||
ConfigRequest, ("configRequest", "config-request")
|
||||
Confirm, ("confirm", "confirm")
|
||||
HistoryCacheError, ("historyCacheError", "history-cache-error")
|
||||
HistoryCacheMiss, ("historyCacheMiss", "history-cache-miss")
|
||||
HistoryCacheMissError, ("historyCacheMissError", "history-cache-miss-error")
|
||||
HistoryCacheMissLoad, ("historyCacheMissLoad", "history-cache-miss-load")
|
||||
HistoryRestore, ("historyRestore", "history-restore")
|
||||
BeforeHistorySave, ("beforeHistorySave", "before-history-save")
|
||||
Load, ("load", "load")
|
||||
NoSseSourceError, ("noSSESourceError", "no-sse-source-error")
|
||||
OnLoadError, ("onLoadError", "on-load-error")
|
||||
OobAfterSwap, ("oobAfterSwap", "oob-after-swap")
|
||||
OobBeforeSwap, ("oobBeforeSwap", "oob-before-swap")
|
||||
OobErrorNoTarget, ("oobErrorNoTarget", "oob-error-no-target")
|
||||
Prompt, ("prompt", "prompt")
|
||||
PushedIntoHistory, ("pushedIntoHistory", "pushed-into-history")
|
||||
ResponseError, ("responseError", "response-error")
|
||||
SendError, ("sendError", "send-error")
|
||||
SseError, ("sseError", "sse-error")
|
||||
SseOpen, ("sseOpen", "sse-open")
|
||||
SwapError, ("swapError", "swap-error")
|
||||
TargetError, ("targetError", "target-error")
|
||||
Timeout, ("timeout", "timeout")
|
||||
ValidationValidate, ("validation:validate", "validation:validate")
|
||||
ValidationFailed, ("validation:failed", "validation:failed")
|
||||
ValidationHalted, ("validation:halted", "validation:halted")
|
||||
XhrAbort, ("xhr:abort", "xhr:abort")
|
||||
XhrLoadEnd, ("xhr:loadend", "xhr:loadend")
|
||||
XhrLoadStart, ("xhr:loadstart", "xhr:loadstart")
|
||||
XhrProgress, ("xhr:progress", "xhr:progress")
|
||||
]
|
||||
|
||||
/// The htmx event name
|
||||
override this.ToString() = fst HxEvent.Values[this]
|
||||
|
||||
/// The hx-on variant of the htmx event name
|
||||
member this.ToHxOnString() = snd HxEvent.Values[this]
|
||||
|
||||
|
||||
/// Helper to create the `hx-headers` attribute
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxHeaders =
|
||||
|
||||
/// Create headers from a list of key/value pairs
|
||||
let From = toJson
|
||||
|
||||
|
@ -27,12 +171,16 @@ module HxHeaders =
|
|||
/// Values / helpers for the `hx-params` attribute
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxParams =
|
||||
|
||||
/// Include all parameters
|
||||
let All = "*"
|
||||
|
||||
/// Include no parameters
|
||||
let None = "none"
|
||||
|
||||
/// Include the specified parameters
|
||||
let With fields = match fields with [] -> "" | _ -> fields |> List.reduce (fun acc it -> $"{acc},{it}")
|
||||
|
||||
/// Exclude the specified parameters
|
||||
let Except fields = With fields |> sprintf "not %s"
|
||||
|
||||
|
@ -40,108 +188,124 @@ module HxParams =
|
|||
/// Helpers to define `hx-request` attribute values
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxRequest =
|
||||
|
||||
/// Convert a boolean to its lowercase string equivalent
|
||||
let private toLowerBool (it : bool) =
|
||||
(string it).ToLowerInvariant ()
|
||||
|
||||
/// Configure the request with various options
|
||||
let Configure (opts : string list) =
|
||||
opts
|
||||
|> String.concat ", "
|
||||
|> sprintf "{ %s }"
|
||||
|
||||
/// Set a timeout (in milliseconds)
|
||||
let Timeout (ms : int) = $"\"timeout\": {ms}"
|
||||
|
||||
/// Include or exclude credentials from the request
|
||||
let Credentials = toLowerBool >> sprintf "\"credentials\": %s"
|
||||
|
||||
/// Exclude or include headers from the request
|
||||
let NoHeaders = toLowerBool >> sprintf "\"noHeaders\": %s"
|
||||
|
||||
|
||||
/// Valid values for the `hx-swap` attribute (may be combined with swap/settle/scroll/show config)
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxSwap =
|
||||
/// The default, replace the inner html of the target element
|
||||
let InnerHtml = "innerHTML"
|
||||
/// Replace the entire target element with the response
|
||||
let OuterHtml = "outerHTML"
|
||||
/// Insert the response before the target element
|
||||
let BeforeBegin = "beforebegin"
|
||||
/// Insert the response before the first child of the target element
|
||||
let AfterBegin = "afterbegin"
|
||||
/// Insert the response after the last child of the target element
|
||||
let BeforeEnd = "beforeend"
|
||||
/// Insert the response after the target element
|
||||
let AfterEnd = "afterend"
|
||||
/// Does not append content from response (out of band items will still be processed).
|
||||
let None = "none"
|
||||
|
||||
|
||||
/// Helpers for the `hx-trigger` attribute
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxTrigger =
|
||||
|
||||
/// Append a filter to a trigger
|
||||
let private appendFilter filter (trigger : string) =
|
||||
match trigger.Contains "[" with
|
||||
| true ->
|
||||
let parts = trigger.Split ('[', ']')
|
||||
$"{parts.[0]}[{parts.[1]}&&{filter}]"
|
||||
$"{parts[0]}[{parts[1]}&&{filter}]"
|
||||
| false -> $"{trigger}[{filter}]"
|
||||
|
||||
/// Trigger the event on a click
|
||||
let Click = "click"
|
||||
|
||||
/// Trigger the event on page load
|
||||
let Load = "load"
|
||||
|
||||
/// Trigger the event when the item is visible
|
||||
let Revealed = "revealed"
|
||||
|
||||
/// Trigger this event every [timing declaration]
|
||||
let Every (duration : string) = $"every {duration}"
|
||||
|
||||
/// Helpers for defining filters
|
||||
module Filter =
|
||||
|
||||
/// Only trigger the event if the `ALT` key is pressed
|
||||
let Alt = appendFilter "altKey"
|
||||
|
||||
/// Only trigger the event if the `CTRL` key is pressed
|
||||
let Ctrl = appendFilter "ctrlKey"
|
||||
|
||||
/// Only trigger the event if the `SHIFT` key is pressed
|
||||
let Shift = appendFilter "shiftKey"
|
||||
|
||||
/// Only trigger the event if `CTRL+ALT` are pressed
|
||||
let CtrlAlt = Ctrl >> Alt
|
||||
|
||||
/// Only trigger the event if `CTRL+SHIFT` are pressed
|
||||
let CtrlShift = Ctrl >> Shift
|
||||
|
||||
/// Only trigger the event if `CTRL+ALT+SHIFT` are pressed
|
||||
let CtrlAltShift = CtrlAlt >> Shift
|
||||
|
||||
/// Only trigger the event if `ALT+SHIFT` are pressed
|
||||
let AltShift = Alt >> Shift
|
||||
|
||||
/// Append a modifier to the current trigger
|
||||
let private appendModifier modifier current =
|
||||
match current with "" -> modifier | _ -> $"{current} {modifier}"
|
||||
if current = "" then modifier else $"{current} {modifier}"
|
||||
|
||||
/// Only trigger once
|
||||
let Once = appendModifier "once"
|
||||
|
||||
/// Trigger when changed
|
||||
let Changed = appendModifier "changed"
|
||||
|
||||
/// Delay execution; resets every time the event is seen
|
||||
let Delay = sprintf "delay:%s" >> appendModifier
|
||||
|
||||
/// Throttle execution; ignore other events, fire when duration passes
|
||||
let Throttle = sprintf "throttle:%s" >> appendModifier
|
||||
|
||||
/// Trigger this event from a CSS selector
|
||||
let From = sprintf "from:%s" >> appendModifier
|
||||
|
||||
/// Trigger this event from the `document` object
|
||||
let FromDocument = From "document"
|
||||
|
||||
/// Trigger this event from the `window` object
|
||||
let FromWindow = From "window"
|
||||
|
||||
/// Trigger this event from the closest parent CSS selector
|
||||
let FromClosest = sprintf "closest %s" >> From
|
||||
|
||||
/// Trigger this event from the closest child CSS selector
|
||||
let FromFind = sprintf "find %s" >> From
|
||||
|
||||
/// Target the given CSS selector with the results of this event
|
||||
let Target = sprintf "target:%s" >> appendModifier
|
||||
|
||||
/// Prevent any further events from occurring after this one fires
|
||||
let Consume = appendModifier "consume"
|
||||
|
||||
/// Configure queueing when events fire when others are in flight; if unspecified, the default is "last"
|
||||
let Queue = sprintf "queue:%s" >> appendModifier
|
||||
|
||||
/// Queue the first event, discard all others (i.e., a FIFO queue of 1)
|
||||
let QueueFirst = Queue "first"
|
||||
|
||||
/// Queue the last event; discards current when another is received (i.e., a LIFO queue of 1)
|
||||
let QueueLast = Queue "last"
|
||||
|
||||
/// Queue all events; discard none
|
||||
let QueueAll = Queue "all"
|
||||
|
||||
/// Queue no events; discard all
|
||||
let QueueNone = Queue "none"
|
||||
|
||||
|
@ -149,66 +313,219 @@ module HxTrigger =
|
|||
/// Helper to create the `hx-vals` attribute
|
||||
[<RequireQualifiedAccess>]
|
||||
module HxVals =
|
||||
|
||||
/// Create values from a list of key/value pairs
|
||||
let From = toJson
|
||||
|
||||
|
||||
/// Attributes and flags for HTMX
|
||||
/// Attributes and flags for htmx
|
||||
[<AutoOpen>]
|
||||
module HtmxAttrs =
|
||||
|
||||
/// Progressively enhances anchors and forms to use AJAX requests (use `_hxNoBoost` to set to false)
|
||||
let _hxBoost = attr "hx-boost" "true"
|
||||
|
||||
/// Shows a confirm() dialog before issuing a request
|
||||
let _hxConfirm = attr "hx-confirm"
|
||||
|
||||
/// Issues a DELETE to the specified URL
|
||||
let _hxDelete = attr "hx-delete"
|
||||
|
||||
/// Disables htmx processing for the given node and any children nodes
|
||||
let _hxDisable = flag "hx-disable"
|
||||
|
||||
/// Specifies elements that should be disabled when an htmx request is in flight
|
||||
let _hxDisabledElt = attr "hx-disabled-elt"
|
||||
|
||||
/// Disinherit all ("*") or specific htmx attributes
|
||||
let _hxDisinherit = attr "hx-disinherit"
|
||||
|
||||
/// Changes the request encoding type
|
||||
let _hxEncoding = attr "hx-encoding"
|
||||
|
||||
/// Extensions to use for this element
|
||||
let _hxExt = attr "hx-ext"
|
||||
|
||||
/// Issues a GET to the specified URL
|
||||
let _hxGet = attr "hx-get"
|
||||
|
||||
/// Adds to the headers that will be submitted with the request
|
||||
let _hxHeaders = attr "hx-headers"
|
||||
|
||||
/// Set to "false" to prevent pages with sensitive information from being stored in the history cache
|
||||
let _hxHistory = attr "hx-history"
|
||||
|
||||
/// The element to snapshot and restore during history navigation
|
||||
let _hxHistoryElt = flag "hx-history-elt"
|
||||
|
||||
/// Includes additional data in AJAX requests
|
||||
let _hxInclude = attr "hx-include"
|
||||
|
||||
/// The element to put the htmx-request class on during the AJAX request
|
||||
let _hxIndicator = attr "hx-indicator"
|
||||
|
||||
/// Overrides a previous `hx-boost`
|
||||
let _hxNoBoost = attr "hx-boost" "false"
|
||||
|
||||
/// Attach an event handler for DOM events
|
||||
let _hxOnEvent evtName =
|
||||
attr $"hx-on:%s{evtName}"
|
||||
|
||||
/// Attach an event handler for htmx events
|
||||
let _hxOnHxEvent (hxEvent: HxEvent) =
|
||||
_hxOnEvent $":{hxEvent.ToHxOnString()}"
|
||||
|
||||
/// Filters the parameters that will be submitted with a request
|
||||
let _hxParams = attr "hx-params"
|
||||
|
||||
/// Issues a PATCH to the specified URL
|
||||
let _hxPatch = attr "hx-patch"
|
||||
|
||||
/// Issues a POST to the specified URL
|
||||
let _hxPost = attr "hx-post"
|
||||
|
||||
/// Preserves an element between requests
|
||||
let _hxPreserve = attr "hx-preserve" "true"
|
||||
|
||||
/// Shows a prompt before submitting a request
|
||||
let _hxPrompt = attr "hx-prompt"
|
||||
|
||||
/// Pushes the URL into the location bar, creating a new history entry
|
||||
let _hxPushUrl = flag "hx-push-url"
|
||||
let _hxPushUrl = attr "hx-push-url"
|
||||
|
||||
/// Issues a PUT to the specified URL
|
||||
let _hxPut = attr "hx-put"
|
||||
|
||||
/// Replaces the current URL in the browser's history stack
|
||||
let _hxReplaceUrl = attr "hx-replace-url"
|
||||
|
||||
/// Configures various aspects of the request
|
||||
let _hxRequest = attr "hx-request"
|
||||
|
||||
/// Selects a subset of the server response to process
|
||||
let _hxSelect = attr "hx-select"
|
||||
/// Establishes and listens to Server Sent Event (SSE) sources for events
|
||||
let _hxSse = attr "hx-sse"
|
||||
|
||||
/// Selects a subset of an out-of-band server response
|
||||
let _hxSelectOob = attr "hx-select-oob"
|
||||
|
||||
/// Controls how the response content is swapped into the DOM (e.g. 'outerHTML' or 'beforeEnd')
|
||||
let _hxSwap = attr "hx-swap"
|
||||
|
||||
/// Controls how the response content is swapped into the DOM (e.g. 'outerHTML' or 'beforeEnd'), enabling CSS
|
||||
/// transitions
|
||||
let _hxSwapWithTransition = sprintf "%s transition:true" >> _hxSwap
|
||||
|
||||
/// Marks content in a response as being "Out of Band", i.e. swapped somewhere other than the target
|
||||
let _hxSwapOob = attr "hx-swap-oob"
|
||||
|
||||
/// Synchronize events based on another element
|
||||
let _hxSync = attr "hx-sync"
|
||||
|
||||
/// Specifies the target element to be swapped
|
||||
let _hxTarget = attr "hx-target"
|
||||
|
||||
/// Specifies the event that triggers the request
|
||||
let _hxTrigger = attr "hx-trigger"
|
||||
|
||||
/// Validate an input element (uses HTML5 validation API)
|
||||
let _hxValidate = flag "hx-validate"
|
||||
|
||||
/// Adds to the parameters that will be submitted with the request
|
||||
let _hxVals = attr "hx-vals"
|
||||
/// Establishes a WebSocket or sends information to one
|
||||
let _hxWs = attr "hx-ws"
|
||||
|
||||
/// The name of the message to swap into the DOM.
|
||||
let _sseSwap = attr "sse-swap"
|
||||
|
||||
/// The URL of the SSE server.
|
||||
let _sseConnect = attr "sse-connect"
|
||||
|
||||
|
||||
/// Script tags to pull htmx into a web page
|
||||
module Script =
|
||||
|
||||
/// Script tag to load the minified version from unpkg.com
|
||||
let minified =
|
||||
script [ _src "https://unpkg.com/htmx.org@2.0.3"
|
||||
_integrity "sha384-0895/pl2MU10Hqc6jd4RvrthNlDiE9U1tWmX7WRESftEDRosgxNsQG/Ze9YMRzHq"
|
||||
_crossorigin "anonymous" ] []
|
||||
|
||||
/// Script tag to load the unminified version from unpkg.com
|
||||
let unminified =
|
||||
script [ _src "https://unpkg.com/htmx.org@2.0.3/dist/htmx.js"
|
||||
_integrity "sha384-BBDmZzVt6vjz5YbQqZPtFZW82o8QotoM7RUp5xOxV3nSJ8u2pSdtzFAbGKzTlKtg"
|
||||
_crossorigin "anonymous" ] []
|
||||
|
||||
|
||||
/// Functions to extract and render an HTML fragment from a document
|
||||
[<RequireQualifiedAccess>]
|
||||
module RenderFragment =
|
||||
|
||||
/// Does this element have an ID matching the requested ID name?
|
||||
let private isIdElement nodeId (elt : XmlElement) =
|
||||
snd elt
|
||||
|> Array.exists (fun attr ->
|
||||
match attr with
|
||||
| KeyValue (name, value) -> name = "id" && value = nodeId
|
||||
| Boolean _ -> false)
|
||||
|
||||
/// Generate a message if the requested ID node is not found
|
||||
let private nodeNotFound (nodeId : string) =
|
||||
$"<em>– ID {nodeId} not found –</em>"
|
||||
|
||||
/// Find the node with the named ID
|
||||
let rec findIdNode nodeId (node : XmlNode) : XmlNode option =
|
||||
match node with
|
||||
| Text _ -> None
|
||||
| VoidElement elt -> if isIdElement nodeId elt then Some node else None
|
||||
| ParentNode (elt, children) ->
|
||||
if isIdElement nodeId elt then Some node else children |> List.tryPick (fun c -> findIdNode nodeId c)
|
||||
|
||||
/// Functions to render a fragment as a string
|
||||
[<RequireQualifiedAccess>]
|
||||
module AsString =
|
||||
|
||||
/// Render to HTML for the given ID
|
||||
let htmlFromNodes nodeId (nodes : XmlNode list) =
|
||||
match nodes |> List.tryPick(fun node -> findIdNode nodeId node) with
|
||||
| Some idNode -> RenderView.AsString.htmlNode idNode
|
||||
| None -> nodeNotFound nodeId
|
||||
|
||||
/// Render to HTML for the given ID
|
||||
let htmlFromNode nodeId node =
|
||||
match findIdNode nodeId node with
|
||||
| Some idNode -> RenderView.AsString.htmlNode idNode
|
||||
| None -> nodeNotFound nodeId
|
||||
|
||||
/// Functions to render a fragment as bytes
|
||||
[<RequireQualifiedAccess>]
|
||||
module AsBytes =
|
||||
|
||||
let private utf8 = System.Text.Encoding.UTF8
|
||||
|
||||
/// Render to HTML for the given ID
|
||||
let htmlFromNodes nodeId (nodes : XmlNode list) =
|
||||
match nodes |> List.tryPick(fun node -> findIdNode nodeId node) with
|
||||
| Some idNode -> RenderView.AsBytes.htmlNode idNode
|
||||
| None -> nodeNotFound nodeId |> utf8.GetBytes
|
||||
|
||||
/// Render to HTML for the given ID
|
||||
let htmlFromNode nodeId node =
|
||||
match findIdNode nodeId node with
|
||||
| Some idNode -> RenderView.AsBytes.htmlNode idNode
|
||||
| None -> nodeNotFound nodeId |> utf8.GetBytes
|
||||
|
||||
/// Functions to render a fragment into a StringBuilder
|
||||
[<RequireQualifiedAccess>]
|
||||
module IntoStringBuilder =
|
||||
|
||||
/// Render to HTML for the given ID
|
||||
let htmlFromNodes sb nodeId (nodes : XmlNode list) =
|
||||
match nodes |> List.tryPick(fun node -> findIdNode nodeId node) with
|
||||
| Some idNode -> RenderView.IntoStringBuilder.htmlNode sb idNode
|
||||
| None -> nodeNotFound nodeId |> sb.Append |> ignore
|
||||
|
||||
/// Render to HTML for the given ID
|
||||
let htmlFromNode sb nodeId node =
|
||||
match findIdNode nodeId node with
|
||||
| Some idNode -> RenderView.IntoStringBuilder.htmlNode sb idNode
|
||||
| None -> nodeNotFound nodeId |> sb.Append |> ignore
|
||||
|
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
This package enables [htmx](https://htmx.org) support within the [Giraffe](https://giraffe.wiki) view engine.
|
||||
|
||||
**htmx version: 2.0.3**
|
||||
|
||||
_Upgrading from v1.x: see [the migration guide](https://htmx.org/migration-guide-htmx-1/) for changes_
|
||||
|
||||
### Setup
|
||||
|
||||
1. Install the package.
|
||||
|
@ -12,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..." ]
|
||||
```
|
||||
|
||||
|
@ -21,29 +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).
|
||||
|
||||
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
|
||||
```
|
||||
|
|
BIN
src/icon.png
Normal file
BIN
src/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
7
src/pack.sh
Executable file
7
src/pack.sh
Executable file
|
@ -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 .
|
Loading…
Reference in New Issue
Block a user