Pure_html
pure-html · API reference
Use this module for constructing HTML without any dependency on the Dream web framework.
since 3.5.0.
Core types
These are the types of the final values which get rendered.
type attr = string * stringE.g. id="toast".
since 3.11.0 reveal the type definition in the interface.
type nodeEither a tag, a comment, or text data in the markup.
Output
val to_string : node -> stringval to_xml : ?header:bool -> node -> stringSame as to_string but render void tags as XML-style self-closing tags.
parameter header print the XML header string if true. This is to allow both use cases where the XML code is embedded inside HTML, and standalone XML documents. Default is false. Since 3.6.0. since 3.3.0.
val pp : Format.formatter -> node -> unitval pp_xml : Format.formatter -> ?header:bool -> node -> unitSame as pp but render void tags as XML-style self-closing tags.
parameter header print the XML header string if true. This is to allow both use cases where the XML code is embedded inside HTML, and standalone XML documents. Default is false. Since 3.6.0. since 3.3.0.
Constructing nodes and attributes
type 'a to_attr = 'a -> attrAttributes can be created from typed values.
type 'a string_attr = ('a, unit, string, attr) format4 -> 'aSpecial handling for string-value attributes so they can use format strings i.e. string interpolation.
type std_tag = attr list -> node list -> nodeA 'standard' tag with attributes and children.
type void_tag = attr list -> nodeA 'void element': https://developer.mozilla.org/en-US/docs/Glossary/Void_element with no children.
type 'a text_tag = attr list -> ('a, unit, string, node) format4 -> 'aTags which can have attributes but can contain only text. The text can be formatted.
val attr : string -> attrattr name is a new attribute which does not carry any payload. E.g.
let required = attr "required"since 0.1.0.
val string_attr : string -> ?raw:bool -> _ string_attrstring_attr name fmt is a new string-valued attribute which allows formatting i.e. string interpolation of the value. Note, the fmt argument is required due to the value restriction.
parameter raw (default false) whether to inject the raw text or to escape it. Note that Dream does not support escaping inline JavaScript nor CSS, so neither does dream-html: https://github.com/aantron/dream/tree/master/example/7-template#security.
val uri_attr : string -> _ string_attrConvenience for attributes whose values should be URIs. Takes care of both URI-encoding and attribute escaping, as recommended in https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html#common-mistake.
Examples
a [href "/blog?tags=iamsafe\"></a><script>alert('Pwned')</script>"] [txt "Tags: tag1 | tag2"]
==>
<a href="/blog?tags=iamsafe%22%3E%3C/a%3E%3Cscript%3Ealert('Pwned')%3C/script%3E">Tags: tag1 | tag2</a>
a [href "/foo?a=1&b=2 3&c=4<5&d=6>5"] [txt "Test"]
==>
<a href="/foo?a=1&b=2%203&c=4%3C5&d=6%3E5">Test</a>val bool_attr : string -> bool to_attrval float_attr : string -> float to_attrval int_attr : string -> int to_attrval std_tag : string -> std_tagval void_tag : string -> void_tagval text_tag : string -> ?raw:bool -> _ text_tagBuild a tag which can contain only text.
parameter raw (default false) whether to inject the raw text or to escape it. Note that Dream does not support escaping inline JavaScript nor CSS, so neither does dream-html: https://github.com/aantron/dream/tree/master/example/7-template#security.
val uri_tag : string -> _ text_tagBuild a tag which can contain only a URI. The URI is escaped with the same rules as a uri_attr.
since 3.10.0
val txt : ?raw:bool -> ('a, unit, string, node) format4 -> 'aA text node inside the DOM e.g. the 'hi' in <b>hi</b>. Allows string interpolation using the same formatting features as Printf.sprintf:
b [] [txt "Hello, %s!" name]Or without interpolation:
b [] [txt "Bold of you."]HTML-escapes the text value. You can use the ~raw param to bypass escaping:
let user_input = "<script>alert('I like HTML injection')</script>" in
txt ~raw:true "%s" user_inputval comment : string -> nodeA comment that will be embedded in the rendered HTML, i.e. <!-- comment -->. The text is HTML-escaped.
val concat : node -> node list -> nodeconcat node list is the list of nodes joined together into a single node, with each element separated by node.
since 3.10.0
Accessors for tags
val (+@) : node -> attr -> nodeAdd an attribute to a tag.
let toast msg = p [id "toast"] [txt "%s" msg]
let toast_oob = toast "ok." +@ Hx.swap_oob "true"If adding the class attribute to a tag which already has a class attribute, join together the values of the CSS classes:
p [class_ "foo"] [] +@ class_ "bar"Result:
<p class="foo bar"></p>raises Invalid_argument if the node is not a tag (i.e. if it is a text or comment node), or if it is a duplicate attribute other than class.
since 0.0.3.
val (-@) : node -> string -> nodeRemove an attribute from a tag.
raises Invalid_argument if the node is not a tag (i.e. if it is a text or comment node).
since 0.0.3.
val (.@[]) : node -> string -> stringGet the value of an existing attribute.
let toast = p [id "toast"] [txt "OK."]
let toast_id = toast.@["id"]raises Invalid_argument if the node is not a tag (i.e. if it is a text or comment node).
raises Not_found if the tag does not have the given attribute.
since 0.0.3.
val is_null : node -> boolGet whether a node is null (empty) or not. Useful for conditional rendering of UIs when you are passed in a node and you don't know if it's empty or not.
since 1.2.0.
val is_null_ : attr -> boolGet whether an attribute is null (empty) or not.
since 1.2.0.
val fold :
tag:(string -> attr list -> 'a -> 'a) ->
txt:(string -> 'a -> 'a) ->
comment:(string -> 'a -> 'a) ->
'a ->
node ->
'afold ~tag ~txt ~comment value node is the value resulting from 'folding over' the node with an initial value and calling the following callbacks for each child in the node's tree:
parameter tag name attrs value is the value resulting from processing the given tag node's name, attrs, and the value calculated recursively from the tag's children. parameter txt string value is the value resulting from processing the given text node's string and the value calculated up until now. parameter comment string value is the value resulting from processing the given comment node's string and the value calculated up until now. Eg calculate a list of all the classes used by a node and its children:
let tag _name attrs classes =
match List.assoc_opt "class" attrs with
| Some c -> c :: classes
| None -> classes
and txt_or_comment _string classes = classes in
fold ~tag ~txt:txt_or_comment ~comment:txt_or_comment [] nodesince 3.11.0
HTML
module HTML : sig ... endAll standard HTML attributes and tags. Some attributes and tags have the same name, e.g. style. To disambiguate them, attributes have a _ (underscore) suffix.
SVG
module SVG : sig ... endMathML
module MathML : sig ... endARIA
module Aria : sig ... endhttps://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/
RSS and Atom
module Atom : sig ... endmodule RSS : sig ... endRSS support
htmx
module Hx : sig ... endhtmx support