Skip to content

Commit b17e856

Browse files
forkialfonsogarciacaro
authored andcommitted
Use a single StringBuilder for rendering
1 parent fdfd7a8 commit b17e856

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/Fable.React/Fable.Helpers.ReactServer.fs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -722,31 +722,35 @@ let inline private castHTMLNode (htmlNode: ReactElement): HTMLNode =
722722
else htmlNode :?> HTMLNode
723723

724724
let renderToString (htmlNode: ReactElement): string =
725-
726725
let htmlNode = addReactMark (castHTMLNode htmlNode)
726+
let html = StringBuilder()
727+
let inline append (text:string) =
728+
html.Append(text) |> ignore
727729

728-
let rec render (htmlNode: HTMLNode): string =
729-
730-
let inline renderList (nodes: HTMLNode seq) =
731-
let html = StringBuilder()
732-
for node in nodes do
733-
html.Append(render node) |> ignore
734-
html.ToString()
735-
730+
let rec render (htmlNode: HTMLNode) : unit =
736731
match htmlNode with
737-
| HTMLNode.Text str -> escapeHtml str
738-
| HTMLNode.RawText str -> str
732+
| HTMLNode.Text str -> html.Append(escapeHtml str)
733+
| HTMLNode.RawText str -> html.Append(str)
739734
| HTMLNode.Node (tag, attrs, children) ->
740735
let attrs, child = renderAttrs attrs tag
741-
let child =
742-
match child with
743-
| Some c -> c
744-
| None -> (renderList (children |> Seq.map castHTMLNode))
736+
745737
let attrs = if attrs = "" then attrs else " " + attrs
746-
if voidTags.Contains tag
747-
then "<" + tag + attrs + "/>"
748-
else "<" + tag + attrs + ">" + child + "</" + tag + ">"
749-
| HTMLNode.List nodes -> renderList (nodes |> Seq.cast)
750-
| HTMLNode.Empty -> ""
738+
if voidTags.Contains tag then
739+
append "<"; append tag; append attrs; append "/>"
740+
else
741+
append "<"; append tag; append attrs; append ">";
742+
743+
match child with
744+
| Some c -> append c
745+
| None ->
746+
for child in children do
747+
render (castHTMLNode child)
748+
749+
append "</"; append tag; append ">"
750+
| HTMLNode.List nodes ->
751+
for node in nodes do
752+
render node
753+
| HTMLNode.Empty -> ()
751754

752755
render htmlNode
756+
html.ToString()

0 commit comments

Comments
 (0)