201 public items
The API
Generated from each package's interface file, so it cannot describe something the code does not have — or miss something it does.
moonxml
The tree, the chain, and the two engines. Everything you need unless you are reading a document.
Traits
pub impl Show for XPathValue
XPathValue implements Show.
pub(open) trait Selector {
fn select(Self, Selection, String) -> Selection raise Refused
}
One query language plugged into the chain. Implement this to add your own, register it with Document::register, and run it with selector(name, query) — or pass it straight to custom and skip the registry entirely. struct DeepLinks {} impl Selector for DeepLinks with select(_self, ctx, _query) { ctx.css("a[href]").filter(fn(node) { node is ElementNode(e) && e.attr("href").has_prefix("https://") }) }
Errors
pub fn namespace_of(String) -> String?
The URI of a {uri}local name, or None when the name carries none.
pub(all) suberror Refused {
Malformed(at~ : Int32, why~ : String)
BadQuery(at~ : Int32, why~ : String)
UnknownSelector(name~ : String)
Unsupported(what~ : String)
Exceeded(at~ : Int32, limit~ : Int32, got~ : Int32)
} derive(Eq, @debug.Debug)
Why a document could not be read, or a query could not be run. One family for the whole library, so there is one thing to catch.
Types
pub impl Show for Refused
Refused implements Show.
pub(all) struct Attribute {
owner : Element
name : String
value : String
}
An attribute as a node of its own: XPath's attribute node. It is not a child of its element — the element's attributes stay in attrs, which is what the writer reads — but a query that asks for @href or for the attribute axis answers these, so that attribute::x/self::node() has something with an identity to answer.
pub struct Document {
root : Element
// private fields
}
A document: the virtual root (named #document) holding the top-level nodes, the flavour it was read as, the selector registry, and — for HTML — the tally of what the parser repaired.
pub fn Document::append(Self, Node) -> Unit
Add a node at the end of the document: where a comment, a processing instruction or a second document element would go. The usual use is a doctype written before the root, which is what prepend is for.
pub fn Document::by_class(Self, String) -> Selection
The elements carrying the class name.
pub fn Document::by_id(Self, String) -> Selection
The elements whose id attribute is id — getElementById, over the whole document.
pub fn Document::by_name(Self, String) -> Selection
The elements whose name attribute is name.
pub fn Document::by_tag(Self, String) -> Selection
The elements named name; * answers every element.
pub fn Document::children(Self) -> Selection
The document's top-level nodes — the doctype, comments and processing instructions beside the document element, in document order.
pub fn Document::clear(Self) -> Unit
Empty the document: every top-level node goes, and the document is then what a freshly created one is.
pub fn Document::css(Self, String, namespaces? : NamespaceMap) -> Selection raise Refused
Narrow with a CSS selector over the whole document. Descendant search from the virtual root, so every element is a candidate.
pub fn Document::custom(Self, &Selector) -> Selection raise Refused
Run a selector that was never registered.
pub fn Document::descendants(Self) -> Selection
Every node in the document, in document order.
pub fn Document::document_element(Self) -> Element?
The document element — the root's first element child, documentElement in the DOM, getroot() in lxml.
pub fn Document::dump(Self) -> Bytes
A whole document as bytes, in the flavour it was read as.
pub fn Document::dump_html(Self) -> Bytes
A whole document as bytes, by HTML's rules.
pub fn Document::dumps(Self) -> String
A whole document, in the flavour it was read as.
pub fn Document::dumps_html(Self) -> String
A whole document, HTML rules whatever it was read as.
pub fn Document::events(Self) -> Array[@event.Event]
Read a finished tree back as the event stream it would have produced.
pub fn Document::flavour(Self) -> Flavour
The flavour this document writes itself back in.
pub fn Document::html(Self) -> String
The whole document as markup, in the flavour it was read as.
pub fn Document::new(flavour? : Flavour, scripting? : Bool) -> Self
A fresh document. A tree built by hand has no syntax it was read from, so it writes back with XML's exact rules; ask for flavour=Html when the tree came from HTML.
pub fn Document::prepend(Self, Node) -> Unit
Add a node before everything else in the document.
pub fn Document::register(Self, String, &Selector) -> Unit
Register a selector under a name. Re-registering a name replaces what was there, built-in or not — and a built-in stays reachable through its own method, which calls the engine directly and never consults the registry.
pub fn Document::repairs(Self) -> Repairs
What the HTML parser repaired; a tree that was not parsed as HTML has an empty tally.
pub fn Document::scripting(Self) -> Bool
Whether the reader that made this document was allowed to run what it read.
pub fn Document::selector(Self, String, String) -> Selection raise Refused
Run the selector registered under name, built-in or the caller's own.
pub fn Document::set_repairs(Self, Repairs) -> Unit
Record what a parser repaired; the parsers call this when they are done.
pub fn Document::set_unresolved(Self, Array[String]) -> Unit
Record the external entities a reader skipped; the XML parser calls this.
pub fn Document::smooth(Self) -> Unit
Fuse adjacent text nodes everywhere in the document, which is what a writer wants after editing several places.
pub fn Document::text(Self) -> String
The text of the whole document, concatenated.
pub fn Document::unresolved(Self) -> Array[String]
The external entities the document named but the parser did not read, in the order they were referenced, each name once. The library does no I/O, so a document whose entities live in a file beside it is read without them: XML 1.0 says a non-validating processor need not read them, but must tell the application which ones it skipped. That is this list. It is empty for a document that needed nothing from outside.
pub fn Document::xpath(Self, String) -> Selection raise Refused
Narrow to the nodes an XPath 1.0 expression answers, over the whole document.
pub fn Document::xpath_value(Self, String) -> XPathValue raise Refused
The typed answer of an XPath 1.0 expression, read from the whole document.
pub impl @event.Sink for Document
Document implements Sink.
pub struct Element {
name : String
attrs : Array[(String, String)]
children : Array[Node]
mut parent : Element?
mut closed : Bool
// private fields
}
An element: the {uri}local name — the James Clark spelling, chosen over registered prefixes so a node reads the same whichever prefixes the document declared — its attributes in document order, its children, its parent, and how it was closed (<a/> versus <a></a>) for the writer.
pub fn Element::add_class(Self, String) -> Unit
Add a class; an element that already carries it is untouched.
pub fn Element::append(Self, Node) -> Unit
Append a child node, claiming its parentage.
pub fn Element::attr(Self, String) -> String?
The value of an attribute, or None when the element does not carry it.
pub fn Element::by_class(Self, String) -> Selection
The descendants of this element that carry the class name.
pub fn Element::by_id(Self, String) -> Selection
The descendants of this element whose id attribute is id.
pub fn Element::by_name(Self, String) -> Selection
The descendants of this element whose name attribute is name.
pub fn Element::by_tag(Self, String) -> Selection
The descendants of this element named name; * answers every element.
pub fn Element::child_elements(Self) -> Array[Self]
The element children, in order: what a walk of the tree skips the text of.
pub fn Element::classes(Self) -> Array[String]
The class list: the class attribute split on whitespace, empty parts dropped, as HTML says.
pub fn Element::clear(Self) -> Unit
Drop every child, answering each of them to no parent: a node that is no longer in a tree must not still name the tree it left.
pub fn Element::closest(Self, String) -> Selection raise Refused
The nearest ancestor of this element, itself included, that matches a CSS selector.
pub fn Element::content(Self) -> Self?
The contents of a <template>: a fragment of its own, the way the DOM keeps them, and None for every other element. What sits in a template is inert — it is not a child of the element, so a query does not reach it and text() does not read it, which is what a browser does too. set_content is how a caller that builds a template by hand gives it contents.
pub fn Element::css(Self, String, namespaces? : NamespaceMap) -> Selection raise Refused
Narrow to the descendants of this element that match a CSS selector.
pub fn Element::custom(Self, &Selector) -> Selection raise Refused
Run a selector that was never registered, over this element's descendants.
pub fn Element::document(Self) -> Document?
The document an element belongs to, or None when it is detached: the registry a selector(name, ..) call consults lives on the document.
pub fn Element::has_attr(Self, String) -> Bool
Whether the element carries an attribute of this name.
pub fn Element::has_class(Self, String) -> Bool
Whether the element's class list names this class.
pub fn Element::html(Self) -> String
The element's inner markup, written in the flavour its document was read as.
pub fn Element::index_in_parent(Self) -> Int32
The position among the parent's children, counting every node; -1 when the element has no parent.
pub fn Element::insert(Self, Int32, Node) -> Unit
Insert node at at; an index past either end clamps to it.
pub fn Element::insert_after(Self, Node) -> Bool
Insert node after self among its siblings.
pub fn Element::insert_before(Self, Node) -> Bool
Insert node before self among its siblings.
pub fn Element::is_lang(Self, String) -> Bool
Whether an element's language is wanted, or a regional variety of it: a document in en-GB answers lang("en") and lang("en-gb").
pub fn Element::lang(Self) -> String?
The language an element declares: its lang attribute in HTML, xml:lang in XML. A tree read from either may carry both spellings, so both are read.
pub fn Element::new(String, children? : Array[Node], attrs? : Array[(String, String)], closed? : Bool) -> Self
A fresh element. The constructor claims the parentage of the children it is given, so a tree built this way keeps its parent pointers true.
pub fn Element::next_element_sibling(Self) -> Self?
The element just after this one among its siblings, skipping text and the other node kinds.
pub fn Element::prepend(Self, Node) -> Unit
Insert node as the first child.
pub fn Element::previous_element_sibling(Self) -> Self?
The element just before this one among its siblings, skipping text and the other node kinds.
pub fn Element::remove(Self) -> Bool
Detach self from the parent, answering true when there was one. bs4's extract; the collector owns the rest.
pub fn Element::remove_attr(Self, String) -> Bool
Drop an attribute; true when it was there.
pub fn Element::remove_class(Self, String) -> Unit
Remove one class. The attribute stays, empty if that is what is left — bs4's behaviour, so a caller is never surprised by a missing attribute.
pub fn Element::replace_with(Self, Node) -> Bool
Take self's place in the parent. bs4's replace_with; false when self has no parent.
pub fn Element::selector(Self, String, String) -> Selection raise Refused
Run the selector registered under name, over this element's descendants.
pub fn Element::set_attr(Self, String, String) -> Unit
Set or replace an attribute, keeping its place when it is already there.
pub fn Element::set_children(Self, Array[Node]) -> Unit
Replace the children with these, in this order. Every node named is taken out of wherever it sat, and every node the element held is let go, so the parent pointers are true afterwards whichever side of the edit a node was on.
pub fn Element::set_content(Self, Self) -> Unit
Give a <template> a content fragment. A caller who builds one by hand uses this; the parser does it on its own.
pub fn Element::set_text(Self, String) -> Unit
Replace the contents with one text node: the DOM's textContent = …, and what an edit that rewrites a label wants.
pub fn Element::smooth(Self) -> Unit
Fuse adjacent text nodes. What a writer wants after edits, and what keeps a tree that was cut and rebuilt from carrying confetti. CDATA is left alone: it is a distinct kind so it can round-trip.
pub fn Element::text(Self) -> String
The text of this element and everything under it.
pub fn Element::unwrap(Self) -> Bool
Unwrap the element: its children move up to its place, the element itself is dropped. bs4's unwrap; false when the element has no parent.
pub fn Element::walk(Self, (Node) -> Unit) -> Unit
Pre-order walk over the whole subtree. Iterative, so a deep tree cannot outrun the stack.
pub fn Element::wrap(Self, String) -> Self?
Wrap self in a fresh element with the given name, answering the wrapper. None when self has no parent to be replaced in.
pub fn Element::xpath(Self, String) -> Selection raise Refused
Narrow to the nodes an XPath 1.0 expression answers from this element.
pub fn Element::xpath_value(Self, String) -> XPathValue raise Refused
The typed answer of an XPath 1.0 expression, read from this element.
pub(all) enum Flavour {
Xml
Html
} derive(Eq, @debug.Debug)
How a document was read, and therefore how it writes itself back.
pub impl Show for Flavour
Flavour implements Show.
pub(all) struct Namespace {
owner : Element
prefix : String
uri : String
}
A namespace as a node of its own: XPath's namespace node, answered by the namespace axis. prefix is "" for the default namespace.
pub(all) struct NamespaceMap {
default : String
prefixes : Array[(String, String)]
}
The namespaces a selector's prefixes resolve against — what @namespace declares in a stylesheet, handed over by the caller. doc.css("a|ref", namespaces={ default: "", prefixes: [("a", "urn:x")] })
pub(all) enum Node {
ElementNode(Element)
TextNode(String)
CDataNode(String)
CommentNode(String)
ProcessingInstructionNode(ProcessingInstruction)
DoctypeNode(String)
AttributeNode(Attribute)
NamespaceNode(Namespace)
}
One node of the tree. Mixed content survives: text, CDATA, elements, comments, processing instructions and the doctype are all nodes, in document order — none of which fits in a JSON-shaped value, which is why this is not moonjson.
pub fn Node::by_class(Self, String) -> Selection
The descendants of this node that carry the class name.
pub fn Node::by_id(Self, String) -> Selection
The descendants of this node whose id attribute is id.
pub fn Node::by_name(Self, String) -> Selection
The descendants of this node whose name attribute is name.
pub fn Node::by_tag(Self, String) -> Selection
The descendants of this node named name.
pub fn Node::children(Self) -> Array[Self]
The children of a node that is an element; a node with no children answers an empty list.
pub fn Node::clone_deep(Self) -> Self
A copy of this node and everything under it, belonging to no tree: editing the copy leaves the original alone. An attribute or a namespace node answers itself — it is not a tree of its own, it is a name on an element, and copying the element copies it.
pub fn Node::closest(Self, String) -> Selection raise Refused
The nearest ancestor of this node, itself included, that matches a CSS selector.
pub fn Node::css(Self, String, namespaces? : NamespaceMap) -> Selection raise Refused
Narrow to the descendants of this node that match a CSS selector; a node that is not an element answers empty.
pub fn Node::custom(Self, &Selector) -> Selection raise Refused
Run a selector that was never registered, over this node's descendants.
pub fn Node::is_element(Self) -> Bool
Whether the node is an element.
pub fn Node::is_text(Self) -> Bool
Text, CDATA included: XPath's text() node test matches both.
pub fn Node::name(Self) -> String
An element's name, or "" for a node that is not an element.
pub fn Node::selector(Self, String, String) -> Selection raise Refused
Run the selector registered under name, over this node's descendants.
pub fn Node::text(Self) -> String
A node's text content — the DOM's textContent: an element answers the text of its descendants, a text, CDATA, comment or processing-instruction node answers what it carries.
pub fn Node::xpath(Self, String) -> Selection raise Refused
Narrow to the nodes an XPath 1.0 expression answers from this node.
pub fn Node::xpath_value(Self, String) -> XPathValue raise Refused
The typed answer of an XPath 1.0 expression, read from this node.
pub(all) struct ProcessingInstruction {
target : String
data : String
}
A processing instruction: <?target data?>, with data the raw text between the target and the ?>, so an instruction round-trips exactly.
pub(all) struct Repairs {
mut implied : Int32
mut dropped : Int32
mut reparented : Int32
mut closed_at_eof : Int32
}
What the HTML parser repaired. WHATWG says repairs happen silently; this tally makes them answerable afterwards, for a caller who wants to ask.
pub fn Repairs::any(Self) -> Bool
Whether the parser repaired anything at all.
pub fn Repairs::new() -> Self
A tally with nothing in it.
pub fn Repairs::total(Self) -> Int32
Every repair the parser made, added up.
pub struct Selection {
items : Array[Node]
// private fields
}
A selection: the nodes a query returned, and the document they belong to. The empty result is an empty list, never a special value, so a chain never branches — and every query method exists on Selection, on Element and on Document, so a chain never has to stop at a node. doc.xpath("//ul").css("li.external").xpath("@href") .selector("tag", "a").custom(DeepLinks::new()) .items[2].children[0].css("span").text()
pub fn Selection::add_class(Self, String) -> Unit
Add a class to every selected element.
pub fn Selection::append(Self, Node) -> Unit
Append a node to every selected element.
pub fn Selection::at(Self, Int32) -> Node
selection[i] in the chain: the node at that position, so a chain can step down into one node and carry on. Out of range answers a detached empty element rather than panicking.
pub fn Selection::attr(Self, String) -> String?
The named attribute of the first selected element that carries it.
pub fn Selection::by_class(Self, String) -> Self
The elements among the descendants that carry the class name.
pub fn Selection::by_id(Self, String) -> Self
The elements among the descendants whose id attribute is id.
pub fn Selection::by_name(Self, String) -> Self
The elements among the descendants whose name attribute is name.
pub fn Selection::by_tag(Self, String) -> Self
The elements among the descendants whose name is name; * answers them all.
pub fn Selection::children(Self) -> Self
The children of every selected node.
pub fn Selection::clear(Self) -> Unit
Drop the children of every selected element.
pub fn Selection::closest(Self, String, namespaces? : NamespaceMap) -> Self raise Refused
The nearest ancestor — self included — matching a CSS selector.
pub fn Selection::css(Self, String, namespaces? : NamespaceMap) -> Self raise Refused
Narrow with a CSS selector, Selectors Level 4's statically decidable subset. Searches the descendants of the selected nodes — the context itself is never a candidate, querySelectorAll's rule. namespaces resolves the prefixes a selector writes, the way a stylesheet's @namespace rules do; the default matches no prefix at all.
pub fn Selection::custom(Self, &Selector) -> Self raise Refused
Run a selector that was never registered: no name, no state.
pub fn Selection::descendants(Self) -> Self
Every node below the selected nodes, in document order.
pub fn Selection::eq(Self, Int32) -> Self
The selected node at i; a negative index counts from the end, the way jQuery's eq does. Out of range answers empty.
pub fn Selection::filter(Self, (Node) -> Bool) -> Self
Keep the selected nodes a closure accepts.
pub fn Selection::first(Self) -> Self
The first selected node; empty stays empty.
pub fn Selection::html(Self) -> String
The inner HTML of the first selected element, written in the flavour its document was read as; "" when the selection holds no element.
pub fn Selection::last(Self) -> Self
The last selected node; empty stays empty.
pub fn Selection::length(Self) -> Int32
How many nodes the selection holds.
pub fn Selection::new() -> Self
An empty selection, belonging to no document.
pub fn Selection::of(Array[Node]) -> Self
A selection of these nodes, in the order given, carrying the document the first of them belongs to.
pub fn Selection::parent(Self) -> Self
The parent of every selected node that has one.
pub fn Selection::prepend(Self, Node) -> Unit
Insert a node as the first child of every selected element.
pub fn Selection::remove(Self) -> Unit
Detach every selected element from its parent.
pub fn Selection::remove_attr(Self, String) -> Unit
Remove an attribute from every selected element.
pub fn Selection::remove_class(Self, String) -> Unit
Remove a class from every selected element.
pub fn Selection::replace_with(Self, Node) -> Unit
Put a node in the place of every selected element.
pub fn Selection::selector(Self, String, String) -> Self raise Refused
Run a selector by the name it was registered under, built-in or the caller's own. An unregistered name raises.
pub fn Selection::set_attr(Self, String, String) -> Unit
Set an attribute on every selected element.
pub fn Selection::set_text(Self, String) -> Unit
Replace the children of every selected element with one text node.
pub fn Selection::smooth(Self) -> Unit
Fuse adjacent text nodes under every selected element.
pub fn Selection::text(Self) -> String
The text content of every selected node, concatenated.
pub fn Selection::unwrap(Self) -> Unit
Replace every selected element with its children.
pub fn Selection::wrap(Self, String) -> Unit
Wrap every selected element in a fresh element of this name.
pub fn Selection::xpath(Self, String) -> Self raise Refused
Narrow the selection with an XPath 1.0 expression, evaluated with each selected node as the context node. The answer is a node-set; an expression that evaluates to a string, number or boolean answers its string value as one text node, so the chain never breaks — doc.xpath("string(//h1)").text() reads the way it should. xpath_value is the typed answer.
pub fn Selection::xpath_value(Self, String) -> XPathValue raise Refused
The typed XPath 1.0 answer: nodes, a string, a number or a boolean.
pub struct SelectorFn {
f : (Selection, String) -> Selection raise Refused
}
A selector that is a closure — the shape a selector with no state of its own takes. A selector that carries configuration is a struct implementing Selector directly, with a constructor of its own.
pub fn SelectorFn::new((Selection, String) -> Selection raise Refused) -> Self
A selector made of a closure: the shape one with no state of its own takes.
pub impl Selector for SelectorFn
SelectorFn implements Selector.
pub(all) enum XPathValue {
Nodes(Selection)
Str(String)
Num(Double)
Flag(Bool)
}
An XPath 1.0 result: what the expression actually answered.
Values
pub fn dump(Node) -> Bytes
The XML serialization of a node, as bytes.
pub fn dump_html(Node) -> Bytes
The HTML serialization of a node, as bytes.
pub fn dumps(Node) -> String
The XML serialization of a node.
pub fn dumps_html(Node) -> String
The HTML serialization of a node, WHATWG's rules.
pub fn local_of(String) -> String
The local part of a {uri}local name.
pub let namespace_map : NamespaceMap
No namespaces declared: type selectors match any namespace, and an unprefixed attribute selector matches the attribute with no namespace.
moonxml/html
The WHATWG parser: tolerant, repairing, and never refusing.
Traits
pub fn scan(String, &@event.Sink, scripting? : Bool) -> Unit
Read a text as the token stream the tokenizer answers, before any tree is built: the runs of characters, the start tags with their attributes, the end tags, the comments and the doctype, in the order they come. This is the tokenizer's own reading, not the tree builder's: an end tag that matches nothing is still an end tag here, a stray table cell is still a cell, and the attributes a tag repeats are still repeated. Document::events answers the same shape for a document that has been built.
Values
pub fn load(Bytes, scripting? : Bool) -> @moonxml.Document
Read a document from bytes; UTF-8, with a leading byte-order mark skipped. A byte that is not UTF-8 reads as U+FFFD, the way a browser reads it.
pub fn loads(String, scripting? : Bool) -> @moonxml.Document
Read a document. scripting is whether the reader may run what it reads. It changes one thing: a noscript's content is markup when nothing is going to run it, and raw text when something is, which is what a browser does. Off by default — a library reads documents, and reading them is not running them.
pub fn loads_fragment(String, context? : String, scripting? : Bool) -> @moonxml.Document
Read a fragment the way innerHTML does: the markup is read inside context, an element named as one would write it, and the fragment's nodes come back under the document's root. The name may be a foreign one — svg desc, math mi — in the spelling the standard's test corpus uses, since an SVG <title> is not an HTML one.
pub fn named_reference(String) -> String?
The named character references HTML defines — all 2231 spellings of them, in the table the standard keeps, generated into entities_table.mbt. A name that may be written without its semicolon is in the table twice, so named_reference("amp") and named_reference("amp;") answer the same &. An unknown name answers None, which is the tokenizer's cue to leave the text as it stands.
moonxml/xml
The XML 1.0 parser: strict, with namespaces, and with the offset of the fault.
Traits
pub fn scan(String, &@event.Sink, fragment? : Bool, limit? : Int32, entity_limit? : Int32) -> Unit raise @moonxml.Refused
Stream a document's events into a sink, building nothing. limit and entity_limit are as for loads.
Values
pub let entity_limit : Int32
How many characters of entity expansion one document may spend. A million is well past what a document that means something needs, and well under what one built to expand for ever would ask for.
pub let limit : Int32
How deep elements may nest before a document is refused. Chrome and WebKit stop at 512 nested elements; a document deeper than that is a stack to eat, not a document.
pub fn load(Bytes, limit? : Int32, entity_limit? : Int32) -> @moonxml.Document raise @moonxml.Refused
Read a document from bytes; UTF-8, with a leading byte-order mark skipped. limit and entity_limit are as for loads.
pub fn loads(String, limit? : Int32, entity_limit? : Int32) -> @moonxml.Document raise @moonxml.Refused
Read a document and answer its tree. limit bounds the element nesting, entity_limit the total expansion of entities.
pub fn loads_fragment(String, limit? : Int32, entity_limit? : Int32) -> @moonxml.Document raise @moonxml.Refused
Read content that has no single root — several elements, or text beside them — into a document whose root holds them. Not a well-formed document, which is the point: it is for a snippet.
moonxml/event
The streaming vocabulary — what a parser emits and what a sink receives.
Traits
pub(all) enum Event {
StartTag(name~ : String, attrs~ : Array[(String, String)], self_closing~ : Bool)
EndTag(name~ : String)
Text(text~ : String)
CData(text~ : String)
Comment(text~ : String)
ProcessingInstruction(target~ : String, data~ : String)
Doctype(text~ : String)
}
One event of a document being read: a tag opening, a run of text, a comment. libexpat's second reading of a document, in one type. @xml.scan and @html.scan stream these; Document::events reads a finished tree back as the same stream. HTML's stream is its tokenizer's: the tree construction that follows needs to see the tree it is building, so a document's events describe a tree that already exists.
pub(open) trait Sink {
fn push(Self, Event) -> Unit
}
Where events go. The tree builder is one implementation; a caller counting tags or writing rows to a database is another, and it never builds a tree.
moonxml/htmx
htmx's attributes, read off a tree and carried out on it: the trigger, the request, the swap.
Traits
pub fn Trigger::parse(String) -> Self
One trigger, from its spelling. The spelling is the event — possibly with a bracketed condition — followed by modifiers separated by spaces. An empty spelling is a click.
Types
pub struct Hx {
element : @moonxml.Element
}
An element's htmx attributes — its own, or the ones it inherits. htmx's attributes travel down the tree: hx-target on a container is the target of every button inside it, until something nearer says otherwise or hx-disinherit stops the inheritance at a boundary. Reading an attribute through Hx is reading it the way htmx would.
pub fn Hx::attr(Self, String) -> String?
The value of hx-<name> — attr("target") is hx-target — inherited from the nearest ancestor that declares it. None when nothing does, and when the nearest declaration is unset.
pub fn Hx::boosted(Self) -> Bool
Whether the element is boosted: hx-boost="true" somewhere on the way up.
pub fn Hx::confirm(Self) -> String?
The question to ask before asking: hx-confirm.
pub fn Hx::element(Self) -> @moonxml.Element
The element the attributes were read off.
pub fn Hx::headers(Self) -> Array[(String, String)]
The headers this element's request carries: hx-headers, a JSON object.
pub fn Hx::indicator(Self) -> @moonxml.Element?
The element that says the request is in flight: hx-indicator, resolved the way a target is.
pub fn Hx::of(@moonxml.Element) -> Self
The htmx attributes of element, as htmx would see them.
pub fn Hx::push_url(Self) -> Bool?
Whether the address bar should follow: hx-push-url. Some(true) and Some(false) are the two things htmx lets it say, None is silence.
pub fn Hx::request(Self, String) -> Request?
The request an event on this element would make, or None when it would make none: no verb, no address, or a trigger that is about something else.
pub fn Hx::target(Self) -> @moonxml.Element?
Where the answer goes: hx-target, resolved. A bare selector is looked up in the document; this, closest …, find …, next … and previous … are resolved from the element that wrote them.
pub fn Hx::triggers(Self) -> Array[Trigger]
What an event on this element asks: the hx-trigger it declares, or the event an element of its kind listens for when it declares none.
pub fn Hx::url(Self) -> String?
Where the request goes: the value of the verb's attribute, or the href of a boosted link, or the action of a boosted form.
pub fn Hx::values(Self) -> Array[(String, String)]
The values this element's request carries: what hx-include names, the element's own fields, and hx-vals over the top of them. A form contributes every control inside it that has a name and is not disabled; a field contributes itself; hx-include adds the fields of whatever its selectors answer. hx-params then keeps or drops names, the way a server would expect.
pub fn Hx::verb(Self) -> String?
The verb this element asks with, in lower case — get, post, put, patch or delete — taken from the first of those it declares. A boosted link asks with get and a boosted form with post, which is what hx-boost is: an ordinary element that becomes a request.
pub(all) struct Request {
verb : String
url : String
element : @moonxml.Element
target : @moonxml.Element
values : Array[(String, String)]
headers : Array[(String, String)]
swap : SwapSpec
select : String?
confirm : String?
push_url : Bool?
trigger : String
}
A request htmx would have made: the verb, the address, what it carries, and how the answer is put back. Nothing here is sent. A caller reads this, sends it however it likes, and hands the answer to answer.
pub fn Request::address(Self) -> String
The address to send to: a get carries its values in the query, and the other verbs carry them in the body, so only a get has them added here.
pub fn Request::answer(Self, String) -> @moonxml.Selection
Put an answer into the page: parse it, take the part the element asked for, send the out-of-band pieces to the places they name, and swap the rest into the target. The nodes that went in are answered, in the order they were put there.
pub fn Request::headers(Self) -> Array[(String, String)]
The headers to send: what the element declared, and the four htmx adds so that a server knows what it is answering.
pub fn Request::query(Self) -> String
The values as application/x-www-form-urlencoded text, without the ?.
pub(all) enum Swap {
Inner
Outer
BeforeBegin
AfterBegin
BeforeEnd
AfterEnd
Delete
NoSwap
}
Where an answer goes. The first six are the ones the DOM can be told to do without asking a stylesheet; Delete takes the target away, and NoSwap keeps the answer out of the page entirely — for a response that is only out-of-band content.
pub fn Swap::spec(Self) -> String
The position as htmx spells it.
pub(all) struct SwapSpec {
mut style : Swap
mut delay : Int32
mut settle : Int32
mut transition : Bool
mut ignore_title : Bool
}
How an answer goes there: the position, and the timing a page might want to know about.
pub fn SwapSpec::new() -> Self
The default: the inside of the target, at once.
pub fn SwapSpec::parse(String) -> Self
A hx-swap spelling: a position and, after it, modifiers. innerHTML, outerHTML, beforebegin, afterbegin, beforeend, afterend, delete and none are the positions; swap:100ms, settle:50ms, transition:true and ignoreTitle:true are the modifiers this reader keeps. scroll: and show: are the page's business, and are read but not carried out.
pub(all) struct Trigger {
mut event : String
mut filter : String?
mut once : Bool
mut changed : Bool
mut delay : Int32
mut throttle : Int32
mut from : String?
mut target : String?
mut queue : String?
mut consume : Bool
}
When an element asks: the event, and the conditions htmx puts on it. hx-trigger="keyup changed delay:500ms from:#search" is one trigger; hx-trigger="click, keyup[key=='Enter']" is two.
pub fn Trigger::default_for(@moonxml.Element) -> Self
What an element listens for when it does not say: a form is submitted, a field changes, and everything else is clicked.
pub fn Trigger::list(String) -> Array[Self]
Every trigger a spelling names, in the order it names them. click, keyup[key=='Enter'] changed delay:500ms is two triggers. A comma inside a condition does not split.
pub fn Trigger::matches(Self, String) -> Bool
Whether this trigger is about event.
❦