package pure-html
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=d1ac64685188a5b41834c8e1ffc4187f8dfdbc27a972898e6bb833ca4c715811
sha512=43ae60f49d3ada8cd633e0fbe6bf892094eb46a8f8ff1c7605c33724e6f44abcb3466dc61511994a8ac63a5ccd9421c53ce6f5f703bd307d9da3de7b6dbdf85e
doc/pure-html/Pure_html/index.html
Module Pure_htmlSource
Use this module for constructing HTML without any dependency on the Dream web framework.
Core types
These are the types of the final values which get rendered.
E.g. id="toast".
Either a tag, a comment, or text data in the markup.
Output
Same as to_string but render void tags as XML-style self-closing tags.
Same as pp but render void tags as XML-style self-closing tags.
Constructing nodes and attributes
Special handling for string-value attributes so they can use format strings i.e. string interpolation.
A 'void element': https://developer.mozilla.org/en-US/docs/Glossary/Void_element with no children.
Tags which can have attributes but can contain only text. The text can be formatted.
attr name is a new attribute which does not carry any payload. E.g.
let required = attr "required"string_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.
Convenience 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>Build a tag which can contain only a URI. The URI is escaped with the same rules as a uri_attr.
A 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_inputA comment that will be embedded in the rendered HTML, i.e. <!-- comment -->. The text is HTML-escaped.
concat node list is the list of nodes joined together into a single node, with each element separated by node.
Accessors for tags
Add 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>Get the value of an existing attribute.
let toast = p [id "toast"] [txt "OK."]
let toast_id = toast.@["id"]Get 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.
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:
Eg calculate a list of all the classes used by a node and its children:
let tag _name attrs classes =
match List.find_opt (fun (n, _) -> n = "class") attrs with
| Some (_name, c) -> c :: classes
| None -> classes
and txt_or_comment _string classes = classes in
fold ~tag ~txt:txt_or_comment ~comment:txt_or_comment [] nodeHTML
All standard HTML attributes and tags. Some attributes and tags have the same name, e.g. style. To disambiguate them, attributes have a _ (underscore) suffix.