class Bezel::HtmlBuilder
include DslScope, Bezel::Elements
Writes HTML: an element per method (h.div, h.a, ...; see
HtmlBuilder in src/html/elements.qz), each taking its content as a
text (escaped) or a block, its attributes as keywords, and the global
ones (id:, class_name: for class, style:, title:, lang:,
dir:, hidden:, tabindex:, role:, data: for data-*, aria:
for aria-*, and attrs: for any other). (class cannot name a
parameter; class: waits for quarz-lang/quarz#382.) A String
attribute is escaped, nil leaves it out, true writes a boolean
attribute and false none; an attribute given twice is an
ArgumentError. A URL attribute (href, src, action,
formaction, cite, poster, a srcset's candidates, a meta
refresh's url=, and those names and the other URL attributes of
AttrList::URL_NAMES in attrs:) must be relative or of the schemes
http, https, mailto or tel (data:image/ for images): another
(javascript:) is an ArgumentError. srcdoc is refused. A style
is escaped but not checked: do not build one from input.
A form or link carries a CSRF token only when the builder has one:
Bezel.html(conn), with the CSRF plug (bezel#6) setting it.
Constants
SCHEMES: Array[String]
The schemes a URL attribute may have.
TEXT_CONTAINERS: Array[String]
The elements whose content the parser reads as text up to their end
tag (raw text and RCDATA, and noscript, raw text when scripting is
on).
Class methods
def self.new(csrf_token: String? = …) -> Bezel::HtmlBuilder
A builder whose forms carry csrf_token (when given).
Methods
def csrf_token -> String?
The CSRF token forms carry, or nil.
def to_html -> Bezel::Html
What was written, as Html (the builder is left empty).
def text(text: String) -> Void
Writes text, escaped.
def put(value: Object?) -> Void
Writes value as a template's {expr} does: Html as it is, nil
as nothing, anything else as its to_s, escaped.
def append(html: Bezel::Html) -> Void
def raw(source: String) -> Void
Writes source without escaping it: the caller vouches that it is
safe. Never pass it what a request sent.
def doctype -> Void
Writes <!DOCTYPE html>.
def component(c: Bezel::Component) -> Void
Writes the component, with the HTML the block makes (none without one) as its content.
def component(c: Bezel::Component, &block: () -> Void) -> Void
Writes the component, with the HTML the block makes (none without one) as its content.
def capture(&block: () -> Void) -> Bezel::Html
The HTML the block writes, as Html, without writing it here.
def link(text: String, *, to: String, method: Symbol = …, class_name: String | Array[String?] | Nil = …, confirm: String? = …) -> Void
A link: <a href="to">text</a> for GET. confirm writes
data-confirm for the application's script to ask with (bezel
ships none). For another method
(:post, :put, :patch, :delete), a form posting to to with a
button labelled text, carrying _method (Plug::MethodOverride)
and the CSRF token: a link cannot send another method, and a GET
must not change anything.
def csrf_input -> Void
Writes the hidden CSRF field (_csrf_token) when the builder has a
token.
def form_for[M <: Rotor::Model](cs: Rotor::Changeset[M], *, action: String, method: Symbol = …, as: String? = …, class_name: String | Array[String?] | Nil = …, &block: (Bezel::FormBuilder) -> Void) -> Void
A form over a rotor changeset: <form action=... method="post">
with _method for another method and the CSRF token, and the block
given a FormBuilder whose fields read the changeset (see
FormBuilder). The fields' names are <as>[<field>], as by
default the model's class name in snake_case (user[email]).