diff --git a/src/ViewEngine.Htmx/Htmx.fs b/src/ViewEngine.Htmx/Htmx.fs index 55abe6f..d8784c9 100644 --- a/src/ViewEngine.Htmx/Htmx.fs +++ b/src/ViewEngine.Htmx/Htmx.fs @@ -519,28 +519,28 @@ module HtmxAttrs = let _sseConnect = attr "sse-connect" -/// Script tags to pull htmx into a web page +/// Script tags to pull htmx into a web page module Script = - /// Script tag to load the minified version from unpkg.com + /// Script tag to load the minified version from unpkg.com let minified = script [ _src "https://unpkg.com/htmx.org@2.0.4" _integrity "sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+" _crossorigin "anonymous" ] [] - /// Script tag to load the unminified version from unpkg.com + /// Script tag to load the unminified version from unpkg.com let unminified = script [ _src "https://unpkg.com/htmx.org@2.0.4/dist/htmx.js" _integrity "sha384-oeUn82QNXPuVkGCkcrInrS1twIxKhkZiFfr2TdiuObZ3n3yIeMiqcRzkIcguaof1" _crossorigin "anonymous" ] [] -/// Functions to extract and render an HTML fragment from a document +/// Functions to extract and render an HTML fragment from a document [] module RenderFragment = /// Does this element have an ID matching the requested ID name? - let private isIdElement nodeId (elt : XmlElement) = + let private isIdElement nodeId (elt: XmlElement) = snd elt |> Array.exists (fun attr -> match attr with @@ -548,62 +548,83 @@ module RenderFragment = | Boolean _ -> false) /// Generate a message if the requested ID node is not found - let private nodeNotFound (nodeId : string) = + let private nodeNotFound (nodeId: string) = $"– ID {nodeId} not found –" - /// Find the node with the named ID - let rec findIdNode nodeId (node : XmlNode) : XmlNode option = + /// Find the node with the named ID + /// The id attribute to find + /// The node tree to search + /// The node with the requested id attribute, or None if it was not found + 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) + if isIdElement nodeId elt then Some node else children |> List.tryPick (findIdNode nodeId) - /// Functions to render a fragment as a string + /// Functions to render a fragment as a string [] 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 + /// Render to HTML for the given ID + /// The id attribute for the node to be rendered + /// The node trees to search + /// The HTML for the given id node, or an error message if it was not found + let htmlFromNodes nodeId (nodes: XmlNode list) = + match nodes |> List.tryPick (findIdNode nodeId) with | Some idNode -> RenderView.AsString.htmlNode idNode | None -> nodeNotFound nodeId - /// Render to HTML for the given ID + /// Render to HTML for the given ID + /// The id attribute for the node to be rendered + /// The node tree to search + /// The HTML for the given id node, or an error message if it was not found 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 + /// Functions to render a fragment as bytes [] 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 + /// Render to bytes for the given ID + /// The id attribute for the node to be rendered + /// The node trees to search + /// The bytes for the given id node, or an error message if it was not found + let htmlFromNodes nodeId (nodes: XmlNode list) = + match nodes |> List.tryPick (findIdNode nodeId) with | Some idNode -> RenderView.AsBytes.htmlNode idNode | None -> nodeNotFound nodeId |> utf8.GetBytes - /// Render to HTML for the given ID + /// Render to bytes for the given ID + /// The id attribute for the node to be rendered + /// The node tree to search + /// The bytes for the given id node, or an error message if it was not found 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 + /// Functions to render a fragment into a StringBuilder [] 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 + /// Render HTML into a StringBuilder for the given ID + /// The StringBuilder into which the bytes will be rendered + /// The id attribute for the node to be rendered + /// The node trees to search + let htmlFromNodes sb nodeId (nodes: XmlNode list) = + match nodes |> List.tryPick (findIdNode nodeId) with | Some idNode -> RenderView.IntoStringBuilder.htmlNode sb idNode | None -> nodeNotFound nodeId |> sb.Append |> ignore - /// Render to HTML for the given ID + /// Render HTML into a StringBuilder for the given ID + /// The StringBuilder into which the bytes will be rendered + /// The id attribute for the node to be rendered + /// The node tree to search let htmlFromNode sb nodeId node = match findIdNode nodeId node with | Some idNode -> RenderView.IntoStringBuilder.htmlNode sb idNode