Skip to content

Commit

Permalink
Fix for rendering views with for non-ascii charactes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lanayx committed Dec 21, 2024
1 parent 7666f98 commit 5e9aad8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 39 deletions.
18 changes: 6 additions & 12 deletions src/Oxpecker.Solid.FablePlugin/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,10 @@ module internal rec AST =
let transformException (pluginHelper: PluginHelper) (range: SourceLocation option) =
let childrenExpression =
Value(
NewList (
Some (
Value (
StringConstant $"Fable compilation error in {pluginHelper.CurrentFile}", None
),
Value (
NewList(None, Type.Tuple([ Type.String; Type.Any ], false)), None
)
NewList(
Some(
Value(StringConstant $"Fable compilation error in {pluginHelper.CurrentFile}", None),
Value(NewList(None, Type.Tuple([ Type.String; Type.Any ], false)), None)
),
Type.Tuple([ Type.String; Type.Any ], false)
),
Expand Down Expand Up @@ -468,10 +464,8 @@ type SolidComponentAttribute() =
// Console.WriteLine("!End! MemberDecl")
let newBody =
match memberDecl.Body with
| Extended (Throw _, range) ->
AST.transformException pluginHelper range
| _ ->
AST.transform memberDecl.Body
| Extended(Throw _, range) -> AST.transformException pluginHelper range
| _ -> AST.transform memberDecl.Body
{ memberDecl with Body = newBody }

override _.TransformCall(_: PluginHelper, _: MemberFunctionOrValue, expr: Expr) : Expr = expr
6 changes: 3 additions & 3 deletions src/Oxpecker.ViewEngine/Oxpecker.ViewEngine.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>1.0.0</Version>
<PackageVersion>1.0.0</PackageVersion>
<PackageReleaseNotes>Major release</PackageReleaseNotes>
<Version>1.0.1</Version>
<PackageVersion>1.0.1</PackageVersion>
<PackageReleaseNotes>Added explicit flush when rendering to Stream and TextWriter</PackageReleaseNotes>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="9.0.0" />
Expand Down
12 changes: 8 additions & 4 deletions src/Oxpecker.ViewEngine/Render.fs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ let toStreamAsync (stream: Stream) (view: #HtmlElement) =
use _ = streamWriter :> IAsyncDisposable
try
view.Render sb
return! streamWriter.WriteAsync(sb)
do! streamWriter.WriteAsync(sb)
return! streamWriter.FlushAsync()
finally
StringBuilderPool.Return(sb)
}
Expand All @@ -73,7 +74,8 @@ let toHtmlDocStreamAsync (stream: Stream) (view: #HtmlElement) =
use _ = streamWriter :> IAsyncDisposable
try
view.Render sb
return! streamWriter.WriteAsync(sb)
do! streamWriter.WriteAsync(sb)
return! streamWriter.FlushAsync()
finally
StringBuilderPool.Return(sb)
}
Expand All @@ -84,7 +86,8 @@ let toTextWriterAsync (textWriter: TextWriter) (view: #HtmlElement) =
task {
try
view.Render sb
return! textWriter.WriteAsync(sb)
do! textWriter.WriteAsync(sb)
return! textWriter.FlushAsync()
finally
StringBuilderPool.Return(sb)
}
Expand All @@ -96,7 +99,8 @@ let toHtmlDocTextWriterAsync (textWriter: TextWriter) (view: #HtmlElement) =
task {
try
view.Render sb
return! textWriter.WriteAsync(sb)
do! textWriter.WriteAsync(sb)
return! textWriter.FlushAsync()
finally
StringBuilderPool.Return(sb)
}
28 changes: 14 additions & 14 deletions src/Oxpecker/HttpContextExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Oxpecker

open System
open System.Collections.Generic
open System.IO
open System.Runtime.CompilerServices
open System.Text
open System.Threading.Tasks
Expand Down Expand Up @@ -285,27 +286,27 @@ type HttpContextExtensions() =
/// <returns>Task of writing to the body of the response.</returns>
[<Extension>]
static member WriteHtmlView(ctx: HttpContext, htmlView: #HtmlElement) =
let sb = Tools.StringBuilderPool.Get().AppendLine("<!DOCTYPE html>")
let memoryStream = recyclableMemoryStreamManager.Value.GetStream()
ctx.Response.ContentType <- "text/html; charset=utf-8"
if ctx.Request.Method <> HttpMethods.Head then
let textWriter = new HttpResponseStreamWriter(ctx.Response.Body, Encoding.UTF8)
task {
use _ = textWriter :> IAsyncDisposable
try
htmlView.Render(sb)
ctx.Response.ContentLength <- sb.Length
return! textWriter.WriteAsync(sb)
do! Render.toHtmlDocStreamAsync memoryStream htmlView
ctx.Response.ContentLength <- memoryStream.Length
memoryStream.Seek(0, SeekOrigin.Begin) |> ignore
return! memoryStream.CopyToAsync ctx.Response.Body
finally
Tools.StringBuilderPool.Return(sb)
memoryStream.Dispose()
}
:> Task
else
try
htmlView.Render(sb)
ctx.Response.ContentLength <- sb.Length
Task.CompletedTask
finally
Tools.StringBuilderPool.Return(sb)
task {
try
do! Render.toHtmlDocStreamAsync memoryStream htmlView
ctx.Response.ContentLength <- memoryStream.Length
finally
memoryStream.Dispose()
}

/// <summary>
/// <para>Serializes a stream of HTML elements and writes the output to the body of the HTTP response using chunked transfer encoding.</para>
Expand All @@ -323,7 +324,6 @@ type HttpContextExtensions() =
use _ = textWriter :> IAsyncDisposable
while! enumerator.MoveNextAsync() do
do! Render.toTextWriterAsync textWriter enumerator.Current
do! textWriter.FlushAsync()
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Oxpecker/Oxpecker.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>1.1.1</Version>
<PackageVersion>1.1.1</PackageVersion>
<PackageReleaseNotes>Oxpecker.ModelValidation module added</PackageReleaseNotes>
<Version>1.1.2</Version>
<PackageVersion>1.1.2</PackageVersion>
<PackageReleaseNotes>Fixed WriteHtmlView for non-ascii charactes</PackageReleaseNotes>
</PropertyGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
Expand Down
6 changes: 3 additions & 3 deletions tests/Oxpecker.ViewEngine.Tests/Tools.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ let ``HTMLEncoding.encodeCharsInto and WebUtility.HtmlEncode are exactly the sam

[<Fact>]
let ``indexOfHtmlEncodingChar works correctly`` () =
CustomWebUtility.indexOfHtmlEncodingChar ("test".AsSpan()) |> shouldEqual -1
CustomWebUtility.indexOfHtmlEncodingChar ("test<sd".AsSpan()) |> shouldEqual 4
CustomWebUtility.indexOfHtmlEncodingChar ("test😀sd".AsSpan()) |> shouldEqual 4
CustomWebUtility.indexOfHtmlEncodingChar("test".AsSpan()) |> shouldEqual -1
CustomWebUtility.indexOfHtmlEncodingChar("test<sd".AsSpan()) |> shouldEqual 4
CustomWebUtility.indexOfHtmlEncodingChar("test😀sd".AsSpan()) |> shouldEqual 4

0 comments on commit 5e9aad8

Please sign in to comment.