package b0
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha512=e9aa779e66c08fc763019f16d4706f465d16c05d6400b58fbd0313317ef33ddea51952e2b058db28e65f7ddb7012f328c8bf02d8f1da17bb543348541a2587f0
doc/b0.std/B0_html/El/index.html
Module B0_html.ElSource
HTML elements and fragments.
See the element constructors and minimal page generation.
HTML fragments
The type for HTML fragments. A fragment is either:
- A single HTML element.
- Text: character data.
- A splice: a list of fragments.
- Unsafe raw data: character data output as is, without escaping.
Elements
The type for element names.
v ?at n cs is an element with name n, attributes at and children cs. Favour element constructors whenever possible. Regarding at:
atdefaults to[].- Except for
At.class'andAt.styleattributes,atmust not specify an attribute more than once. This is not checked by the module and what happens on multiple definitions is undefined. - For
At.class'attributes, multiple specifications are gathered to form a single, space separated, attribute value for theclassHTML attribute. - For
At.styleattributes, multiple specifications are gathered to form a single, semi-colon separated, attribute value for thestyleHTML attribute.
Text
txt_of f v is txt (f v). Cuts a bit on delimiter orgies.
Splices
splice ?sep hs is the list of fragments hs separated by sep (if any). When added to an element's children, the list is spliced in the element's children.
Unsafe raw data
unsafe_raw s is the raw string s without escaping markup delimiters. s must be well-formed HTML otherwise invalid markup is generated. This can be used to:
- Include foreign markup, for example markup generated by another mechanism.
- Avoid unpleasant surprises with the
styleelement. - Let user-generated content create XSS attacks in your application.
Voids
Like void attributes, void fragments render nothing.
is_void h is true iff h is an empty splice, an empty txt or an empty unsafe_raw. These fragments render nothing. See also void.
Rendering
buffer_add ~doctype b h adds the HTML fragment h to b. If doc_type is true an HTML doctype declaration is prepended.
to_string is like buffer_add but returns directly a string.
Page
There's more than one way to generate a basic minimal HTML page. The following provides good defaults for quick minimal pages.
val page :
?lang:string ->
?generator:string ->
?styles:string list ->
?scripts:string list ->
?more_head:html ->
title:string ->
html ->
htmlpage ~lang ~title body is an El.html element with an At.lang attribute of lang (if specified and non-empty) containing a El.head element followed by body which must be a El.body element.
The children of the El.head element are in order:
- An
El.metacharset with valueutf-8, unconditional. - An
El.metagenerator with valuegenerator, if specified and non-empty. - An
El.metaviewport with valuewidth=device-width, initial-scale=1, viewport-fit=cover, unconditional. - For each non-empty element
hrefofstyles(defaults to[]), anEl.linkwithAt.type'text/cssandAt.hrefvaluehref. In order. - For each non-empty element
srcofscripts(defaults to[]), anEl.scriptwithAt.defer,At.type'valuetext/javascriptandAt.srcvaluesrc. In order. more_headfragment (defaults toEl.void). Be careful if you addstyletags with direct CSS source.- An
El.titlewith valuetitlewhich isString.trimed. If the result is empty falls back to"Untitled". See alsotitle_of_filepathto derive titles from file paths.
title_of_filepath f is a non-empty page title for filepath f. Either the basename of f without extension or if that results in "index" or "" the basename of the parent directory without extension or if that results in "" the value "Untitled". Directory separators can be '/' or '\\' regardless of the platform.
Element constructors
See the MDN HTML element reference.
The type for element constructors. This is simply El.v with a pre-applied element name.
The type for void element constructors. This is simply El.v with a pre-applied element name and without children.
Warning. If your style element contains CSS source, use unsafe_raw to specify it. Otherwise the CSS child combinator '>' gets escaped.