ui/md_dom.rs#
Source: lib/ui/md_dom.rs
md_dom -- the elements of an HTML document of the viewer (MdDoc::html, md_doc.rs): a tree whose
nodes keep their identity across changes, queries by simple selectors (also the selectors of
the style sheets, rich_html.rs), changes of text, attributes, classes and structure; the
blocks follow at MdDoc::update (md_view.rs does it every frame)
doc.root() -> MdNode the document: its children are the HTML's top level
doc.by_id(id) -> Option<MdNode> the first element in the document with this id (an
index: O(log n))
doc.select(sel) / select_first(sel) the elements of the document a selector list matches,
in document order
doc.select_in(n, sel) / matches(n, sel) among n's descendants / n itself
doc.parent(n) / children(n) / next_sibling(n) / prev_sibling(n)
doc.tag(n) ("" for a text) / is_text(n) / alive(n) / attached(n) (in the document)
doc.attr(n, name) / attrs(n) / has_class(n, c) / text_of(n) (all its text, textContent) /
inner_html(n)
changes (true / a node: done; false / MdNode::NONE: a node that is gone, a Markdown
document, a name that is none, the cap of nodes):
doc.set_text(n, s) (an element's children become one text; they are freed) /
set_attr(n, name, value) / remove_attr(n, name) / add_class(n, c) / remove_class(n, c) /
toggle_class(n, c) -> on / create_element(tag) / create_text(s) (not in the document yet) /
insert(parent, before, n) (before: None = at the end; a node in the document moves) /
remove(n) (out of the document, kept: it may go in again) / free(n) (gone: its handles and
those of its descendants refer to nothing) / set_inner_html(n, html)
MdNode::raw() / MdNode::from_raw(v): a handle as a number (the objects of a script host)
doc.element_at(block, byte) -> the innermost element around that byte of a block's text (an
inline element, else MdBlock::node; a click: MdEv::click)
doc.dom_size() -> (live nodes, bytes of their names, texts and attributes): what a host that
lets others change the tree limits
The tree: HTML read the way browsers do, simplified -- void elements (br, img, input ...),
raw text (script, style, textarea, title, template, noscript, svg), a <p> closed by a block,
<li> by <li>, <dd> / <dt>, <td> / <th>, <tr>, <option> by their like, <head> by the first
element of the body; an end tag closes the elements above its own, but not across a table
cell or a button, and an inline element's end tag does not close a block (it is dropped); a
stray </p> is an empty paragraph; comments go. Depth (DEPTH_MAX open elements: deeper ones
stay empty, their content follows them) and nodes (NODES_MAX: the rest is cut, truncated)
are capped, so parsing stays linear.
Selectors: a list (a, b) of compounds -- a tag or *, #id, .class, [attr], [attr=v] (also ~=
|= ^= $= *=, values in quotes or not) -- joined by descendant (blank) and child (>)
combinators; tags and attribute names ignore case, ids, classes and values do not. Anything
else (pseudo-classes, sibling combinators, escapes) makes the selector match nothing. A
match costs O(depth x compounds) per element (each run of child combinators is matched at
the nearest ancestor that takes it), a query at most a budget proportional to the tree.
struct MdNodea node of an HTML document's element tree (an element or a text): the same across changes of the document; a freed node's handle refers to nothing (MdDoc::alive)
impl MdNode
const NONE: MdNode = MdNode { i: 0, g: 0 }no nodefn is_none(self) -> boolit is MdNode::NONEfn raw(self) -> u64the handle as a number (0: none), e.g. for a script host's objectsfn from_raw(v: u64) -> MdNodethe handle of raw()
impl MdDoc
fn root(&self) -> MdNodethe document's root (MdNode::NONE for a document from Markdown): its children are the top level of the HTMLfn dom_size(&self) -> (usize, usize)the live nodes of the tree (out of the document too) and the bytes of their names, texts and attributes; linear in the treefn element_at(&self, k: usize, byte: usize) -> MdNodethe innermost element around bytebyteof block k's text: blocks know only their own element (MdBlock::node), so this reads the inline content of that element again and lines its characters up with the block's text, blanks aside (linear in the element). An image's or a field's U+FFFC is its element; a byte at the end, or content that does not line up (hidden by a style sheet, say), gives the block's element; NONE: no block, or none of an element (a document from Markdown)fn by_id(&self, id: &str) -> Option<MdNode>the first element in the document with this idfn select(&self, sel: &str) -> Vec<MdNode>the elements of the document a selector list matches, in document order (see md_dom.rs; a selector it does not support matches nothing)fn select_first(&self, sel: &str) -> Option<MdNode>the first element select(sel) would givefn select_in(&self, n: MdNode, sel: &str) -> Vec<MdNode>the elements below n a selector list matches, in document orderfn matches(&self, n: MdNode, sel: &str) -> boolelement n matches a selector listfn parent(&self, n: MdNode) -> Option<MdNode>n's parent (None: the root, a node out of the document, a node that is gone)fn children(&self, n: MdNode) -> Vec<MdNode>n's children (elements and texts) in orderfn next_sibling(&self, n: MdNode) -> Option<MdNode>the node after n under the same parentfn prev_sibling(&self, n: MdNode) -> Option<MdNode>the node before n under the same parentfn tag(&self, n: MdNode) -> &stran element's tag in lower case ("" for a text, "#document" for the root)fn is_text(&self, n: MdNode) -> booln is a textfn alive(&self, n: MdNode) -> booln exists (it was not freed)fn attached(&self, n: MdNode) -> booln is in the documentfn attr(&self, n: MdNode, name: &str) -> Option<&str>an attribute of element n (names in lower case)fn attrs(&self, n: MdNode) -> &[(String, String)]the attributes of element n in order (names in lower case, values decoded)fn has_class(&self, n: MdNode, c: &str) -> boolelement n has class cfn text_of(&self, n: MdNode) -> Stringall the text of n (a text: its own; an element: that of its descendants, as textContent)fn inner_html(&self, n: MdNode) -> Stringthe HTML of n's childrenfn changed(&self) -> boolthe tree changed since the blocks were derived (update() derives them)fn set_text(&mut self, n: MdNode, s: &str) -> boolset n's text: a text's own; an element's children (freed) become one textfn set_attr(&mut self, n: MdNode, name: &str, value: &str) -> boolset an attribute of element n (the name in lower case; "id" updates the index)fn remove_attr(&mut self, n: MdNode, name: &str) -> boolremove an attribute of element n -> it had onefn add_class(&mut self, n: MdNode, c: &str) -> booladd class c to element nfn remove_class(&mut self, n: MdNode, c: &str) -> boolremove class c from element n -> it had itfn toggle_class(&mut self, n: MdNode, c: &str) -> boolclass c on element n off when it has it, else on -> onfn create_element(&mut self, tag: &str) -> MdNodea new element (not in the document: insert puts it there) -> MdNode::NONE: a Markdown document, a tag that is none, the cap of nodesfn create_text(&mut self, s: &str) -> MdNodea new text (not in the document)fn insert(&mut self, parent: MdNode, before: Option<MdNode>, n: MdNode) -> boolput n under elementparentbefore its childbefore(None: at the end); a node with a parent moves -> false: a node that is gone, the root, n above parent, before no child of parentfn remove(&mut self, n: MdNode) -> booltake n out of the document (it stays: insert may put it back; free drops it)fn free(&mut self, n: MdNode) -> booldrop n and its descendants (out of the document first): their handles refer to nothingfn set_inner_html(&mut self, n: MdNode, html: &str) -> boolelement n's children (freed) become the nodes of an HTML text (raw text elements: the text)