Skip to content

Commit

Permalink
Improvements to the XmlViewEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinmoris committed Jul 5, 2017
1 parent 23e7c16 commit 5708a7e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
6 changes: 3 additions & 3 deletions samples/SampleApp/SampleApp/HtmlViews.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ open SampleApp.Models
let layout (content: XmlNode list) =
html [] [
head [] [
title [] (encodedText "Giraffe")
title [] [ encodedText "Giraffe" ]
]
body [] content
]

let partial () =
p [] (encodedText "Some partial text.")
p [] [ encodedText "Some partial text." ]

let personView (model : Person) =
[
div [] [
h3 [] (sprintf "Hello, %s" model.Name |> encodedText)
h3 [] [ sprintf "Hello, %s" model.Name |> encodedText ]
]
div [] [partial()]
] |> layout
2 changes: 1 addition & 1 deletion src/Giraffe/Giraffe.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<AssemblyName>Giraffe</AssemblyName>
<Version>0.1.0-alpha022</Version>
<Version>0.1.0-alpha023</Version>
<Description>A native functional ASP.NET Core web framework for F# developers.</Description>
<Copyright>Copyright 2017 Dustin Moris Gorski</Copyright>
<NeutralLanguage>en-GB</NeutralLanguage>
Expand Down
23 changes: 17 additions & 6 deletions src/Giraffe/XmlViewEngine.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ let voidTag (tagName : string)
(attributes : XmlAttribute list) =
VoidElement (tagName, Array.ofList attributes)

let encodedText (content : string) = [ EncodedText content ]
let rawText (content : string) = [ RawText content ]
let encodedText (content : string) = EncodedText content
let rawText (content : string) = RawText content
let emptyText = rawText ""
let comment (content : string) = RawText (sprintf "<!-- %s -->" content)
let comment (content : string) = rawText (sprintf "<!-- %s -->" content)

/// ---------------------------
/// Default HTML elements
Expand Down Expand Up @@ -226,10 +226,21 @@ let rec private nodeToString (htmlStyle : bool) (node : XmlNode) =
| ParentNode (e, nodes) -> parentNodeToString (e, nodes)
| VoidElement e -> startElementToString true e

let renderXmlString = nodeToString false
let renderHtmlString = nodeToString true
let renderXmlNode = nodeToString false

let renderXmlNodes (nodes : XmlNode list) =
nodes
|> List.map renderXmlNode
|> String.Concat

let renderHtmlNode = nodeToString true

let renderHtmlNodes (nodes : XmlNode list) =
nodes
|> List.map renderHtmlNode
|> String.Concat

let renderHtmlDocument (document : XmlNode) =
document
|> renderHtmlString
|> renderHtmlNode
|> sprintf "<!DOCTYPE html>%s%s" Environment.NewLine
2 changes: 1 addition & 1 deletion template/giraffe-template.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>giraffe-template</id>
<version>0.1.5</version>
<version>0.1.6</version>
<title>Giraffe Template for dotnet-new</title>
<summary>A dotnet-new template for Giraffe web applications.</summary>
<description>A dotnet-new template for Giraffe web applications.</description>
Expand Down
4 changes: 2 additions & 2 deletions tests/Giraffe.Tests/HttpHandlerTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -743,10 +743,10 @@ let ``GET "/person" returns rendered HTML view`` () =
let personView model =
html [] [
head [] [
title [] (encodedText "Html Node")
title [] [ encodedText "Html Node" ]
]
body [] [
p [] (sprintf "%s %s is %i years old." model.Foo model.Bar model.Age |> encodedText)
p [] [ sprintf "%s %s is %i years old." model.Foo model.Bar model.Age |> encodedText ]
]
]

Expand Down
14 changes: 7 additions & 7 deletions tests/Giraffe.Tests/XmlViewEngineTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,33 @@ let ``Single html root should compile`` () =
[<Fact>]
let ``Anchor should contain href, target and content`` () =
let anchor =
a [ "href", "http://example.org"; "target", "_blank" ] (encodedText "Example")
let html = renderXmlString anchor
a [ "href", "http://example.org"; "target", "_blank" ] [ encodedText "Example" ]
let html = renderXmlNode anchor
Assert.Equal("<a href=\"http://example.org\" target=\"_blank\">Example</a>", html)

[<Fact>]
let ``Nested content should render correctly`` () =
let nested =
div [] [
comment "this is a test"
h1 [] (encodedText "Header")
h1 [] [ encodedText "Header" ]
p [] [
EncodedText "Lorem "
strong [] (encodedText "Ipsum")
strong [] [ encodedText "Ipsum" ]
RawText " dollar"
] ]
let html =
nested
|> renderXmlString
|> renderXmlNode
|> removeNewLines
Assert.Equal("<div><!-- this is a test --><h1>Header</h1><p>Lorem <strong>Ipsum</strong> dollar</p></div>", html)

[<Fact>]
let ``Void tag in XML should be self closing tag`` () =
let unary = br [] |> renderXmlString
let unary = br [] |> renderXmlNode
Assert.Equal("<br />", unary)

[<Fact>]
let ``Void tag in HTML should be unary tag`` () =
let unary = br [] |> renderHtmlString
let unary = br [] |> renderHtmlNode
Assert.Equal("<br>", unary)

0 comments on commit 5708a7e

Please sign in to comment.