Skip to content

Commit 75d0fde

Browse files
authored
Add custom option for the HtmlElement type (#106)
1 parent 9102720 commit 75d0fde

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/Fornax.Core/Model.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,8 @@ type CSSProperties =
973973
| Text of props: HtmlProperties list * children: HtmlElement list
974974
| Tspan of props: HtmlProperties list * children: HtmlElement list
975975
| String of string
976+
// https://github.com/ionide/Fornax/issues/104
977+
| Custom of tagName: string * props: HtmlProperties list * children: HtmlElement list
976978

977979
static member ToString tag =
978980
let rec format tag (props : HtmlProperties list) (children : HtmlElement list) level =
@@ -1129,6 +1131,7 @@ type CSSProperties =
11291131
| Text (props, children) -> format "text" props children level
11301132
| Tspan (props, children) -> format "tspan" props children level
11311133
| String str -> str
1134+
| Custom (name, props, children) -> format name props children level
11321135

11331136
helper 1 tag
11341137

@@ -1266,6 +1269,7 @@ type CSSProperties =
12661269
let tspan (props : HtmlProperties list) (children: HtmlElement list) = HtmlElement.Tspan(props,children)
12671270
let string str = HtmlElement.String str
12681271
let (!!) str = HtmlElement.String str
1272+
let custom (tagName : string) (props : HtmlProperties list) (children : HtmlElement list) = HtmlElement.Custom(tagName,props,children)
12691273

12701274

12711275

test/Fornax.Core.UnitTests/Tests.fs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,72 @@ let modelTests =
4242
"Html element with no properties and no children"
4343
|> Expect.equal actual expected
4444

45+
testCase "Custom Html element - empty" <| fun _ ->
46+
let actual = Html.custom "test" [] [] |> HtmlElement.ToString
47+
let expected = "<test></test>"
48+
"Custom Html element with no properties and no children"
49+
|> Expect.equal actual expected
50+
4551
testCase "Html element - one property" <| fun _ ->
4652
let actual = Html.a [ Href "index.html" ] [] |> HtmlElement.ToString
4753
let expected = "<a href=\"index.html\"></a>"
4854
"Html element with one property and no children"
4955
|> Expect.equal actual expected
5056

57+
testCase "Custom Html element - one property" <| fun _ ->
58+
let actual = Html.custom "test" [ Href "index.html" ] [] |> HtmlElement.ToString
59+
let expected = "<test href=\"index.html\"></test>"
60+
"Custom Html element with one property and no children"
61+
|> Expect.equal actual expected
62+
5163
testCase "Html element - multiple properties" <| fun _ ->
5264
let actual = Html.a [ Href "index.html"; Hidden true ] [] |> HtmlElement.ToString
5365
let expected = "<a href=\"index.html\" hidden=\"true\"></a>"
5466
"Html element with multiple properties and no children"
5567
|> Expect.equal actual expected
5668

69+
testCase "Custom Html element - multiple property" <| fun _ ->
70+
let actual = Html.custom "test" [ Href "index.html"; Hidden true ] [] |> HtmlElement.ToString
71+
let expected = "<test href=\"index.html\" hidden=\"true\"></test>"
72+
"Custom Html element with multiple properties and no children"
73+
|> Expect.equal actual expected
74+
5775
testCase "Html element - one child" <| fun _ ->
5876
let actual = Html.a [] [ Html.span [] [] ] |> HtmlElement.ToString
5977
let expected = "<a>\n <span></span>\n</a>"
6078
"Html element with no properties and one child"
6179
|> Expect.equal actual expected
6280

81+
testCase "Custom Html element - one child" <| fun _ ->
82+
let actual = Html.custom "test" [] [ Html.span [] [] ] |> HtmlElement.ToString
83+
let expected = "<test>\n <span></span>\n</test>"
84+
"Custom Html element with no properties and one child"
85+
|> Expect.equal actual expected
86+
6387
testCase "Html element - multiple children" <| fun _ ->
6488
let actual = Html.a [] [ Html.span [] []; Html.div [] [] ] |> HtmlElement.ToString
6589
let expected = "<a>\n <span></span>\n <div></div>\n</a>"
6690
"Html element with no properties and multiple children"
6791
|> Expect.equal actual expected
6892

93+
testCase "Custom Html element - multiple children" <| fun _ ->
94+
let actual = Html.custom "test" [] [ Html.span [] []; Html.div [] [] ] |> HtmlElement.ToString
95+
let expected = "<test>\n <span></span>\n <div></div>\n</test>"
96+
"Custom Html element with no properties and multiple children"
97+
|> Expect.equal actual expected
98+
6999
testCase "Html element - multiple properites and children" <| fun _ ->
70100
let actual = Html.a [ Href "index.html"; Hidden true] [ Html.span [] []; Html.div [] [] ] |> HtmlElement.ToString
71101
let expected = "<a href=\"index.html\" hidden=\"true\">\n <span></span>\n <div></div>\n</a>"
72102
"Html element with multiple properties and multiple children"
73103
|> Expect.equal actual expected
74104

105+
testCase "Custom Html element - multiple properites and children" <| fun _ ->
106+
let actual = Html.custom "test" [ Href "index.html"; Hidden true] [ Html.span [] []; Html.div [] [] ] |> HtmlElement.ToString
107+
let expected = "<test href=\"index.html\" hidden=\"true\">\n <span></span>\n <div></div>\n</test>"
108+
"Custom Html element with multiple properties and multiple children"
109+
|> Expect.equal actual expected
110+
75111
testCase "Html element - void element as child" <| fun _ ->
76112
let actual = Html.div [ ] [ Html.br [ ] ] |> HtmlElement.ToString
77113
let expected = "<div>\n <br/>\n</div>"
@@ -84,6 +120,18 @@ let modelTests =
84120
"Html element with one void element as child"
85121
|> Expect.equal actual expected
86122

123+
testCase "Custom Html element - mutliple properties and children (void and normal element)" <| fun _ ->
124+
let actual = Html.custom "test" [ HtmlProperties.Style [ Display "block" ] ] [ Html.br [ ]; Html.p [ ] [ ]; Html.img [ Src "https://dummyimage.com/128x128/" ] ] |> HtmlElement.ToString
125+
let expected = "<test style=\"display: block;\">\n <br/>\n <p></p>\n <img src=\"https://dummyimage.com/128x128/\"/>\n</test>"
126+
"Custom Html element with one void element as child"
127+
|> Expect.equal actual expected
128+
129+
testCase "Custom Html element - as child with property and child" <| fun _ ->
130+
let actual = Html.div [] [ Html.custom "test" [ HtmlProperties.Style [ Display "block" ] ] [ Html.span [] [] ] ] |> HtmlElement.ToString
131+
let expected = "<div>\n <test style=\"display: block;\">\n <span></span>\n </test>\n</div>"
132+
"Custom Html element with one void element as child"
133+
|> Expect.equal actual expected
134+
87135
testCase "Html void element - empty" <| fun _ ->
88136
let actual = Html.br [ ] |> HtmlElement.ToString
89137
let expected = "<br/>"

0 commit comments

Comments
 (0)