dom/core
the shared Document / Node vocabulary for tur-xml and tur-html.
Since: 0.1.0
Document
(Document)
an owned parsed tree. Owns every Node and every borrowed cstr
Node
(Node)
a borrowed handle to one node of a Document. Dangles once its
SourcePos
(defstruct SourcePos :copy [line : int col : int])
a 1-based line and byte column in the parser's input.
syntax-kind
(syntax-kind :)
the shared Syntax node.
ParseErrorKind -- the shared Syntax node.
(ParseError (syntax-kind) (from-cstr "mismatched tag") 3 7)
Since: 0.1.0
limit-kind
(limit-kind :)
the shared Limit node (see syntax-kind).
refused-kind
(refused-kind :)
the shared Refused node (see syntax-kind).
io-kind
(io-kind :)
the shared Io node (see syntax-kind).
ParseError
(defstruct ParseError :copy [kind : ParseErrorKind msg : String line : int col : int])
why a parse failed and where.
parse-error-pos
(parse-error-pos [e : ParseError] :)
where a ParseError happened.
| e | the error |
SourcePos -- 1-based line and byte column; (SourcePos 0 0) when the error has no position (an Io error).
(.line (parse-error-pos e)) ; => 3
Since: 0.1.0
parse-error-message
(parse-error-message [e : ParseError] :)
borrow the message of a ParseError.
| e | the error |
cstr -- valid until `parse-error-release`.
(println (parse-error-message e)) ; e.g. "mismatched tag"
Since: 0.1.0
parse-error-release
(parse-error-release [e : ParseError] :)
release the String a ParseError owns.
| e | the error, not used afterwards |
(parse-error-release e)
Since: 0.1.0
dom-root
(dom-root [d : Document] :)
the document element (the outermost element).
| d | the document |
(Option Node) -- none only for a document with no element at all (an empty HTML fragment, say).
(match (dom-root d)
(Some r) (node-name r) ; => (Some "feed")
(None) (None))
Since: 0.1.0
dom-node
(dom-node [d : Document] :)
the document node itself (kind DocumentNode).
| d | the document |
Node -- the invisible root.
(node-children (dom-node d)) ; => doctype, comments, root element
Since: 0.1.0
dom-free
(dom-free [d : Document] :)
release a document and everything it owns.
| d | the document, not used afterwards |
(dom-free d)
Since: 0.1.0
dom-node-count
(dom-node-count [d : Document] :)
how many nodes the document holds (the document node
| d | the document |
int -- elements, text, comments, CDATA, PIs and doctypes together.
(dom-node-count d) ; => 3 for <a>x<b/></a>
Since: 0.1.0
dom-live-bytes
(dom-live-bytes :)
arena bytes held by every live Document in the process.
int -- bytes, including each arena chunk's header.
(let [before (dom-live-bytes)]
(dom-free (ok-val (dom-parse-string "<a/>")))
(= before (dom-live-bytes))) ; => true
Since: 0.1.0
node-kind
(node-kind [n : Node] :)
what kind of node this is.
| n | the node |
NodeKind
(match (node-kind n) (ElementNode) "element" (TextNode) "text" ...)
Since: 0.1.0
node-element?
(node-element? [n : Node] :)
is this node an element?
| n | the node |
bool
(node-element? (dom-node d)) ; => false (the document node)
Since: 0.1.0
node-name
(node-name [n : Node] :)
the name of an element, PI target, or doctype.
| n | the node |
(Option cstr) -- borrowed from the document; none for text, comment, CDATA and document nodes.
(node-name root) ; => (Some "rss")
Since: 0.1.0
node-local-name
(node-local-name [n : Node] :)
an element's name without its namespace prefix.
| n | the node |
(Option cstr) -- `link` for `<atom:link>`; none for non-elements.
(node-local-name n) ; => (Some "link")
Since: 0.1.0
node-ns-uri
(node-ns-uri [n : Node] :)
an element's namespace URI.
| n | the node |
(Option cstr) -- none for an element in no namespace, and for non-elements.
(node-ns-uri feed) ; => (Some "http://www.w3.org/2005/Atom")
Since: 0.1.0
node-value
(node-value [n : Node] :)
the character data of a text, comment, CDATA or PI node.
| n | the node |
(Option cstr) -- borrowed from the document; none for elements, the doctype and the document node.
(match (node-first-child p) ; <p>hello</p>
(Some t) (node-value t) ; => (Some "hello")
(None) (None))
Since: 0.1.0
node-text
(node-text [n : Node] :)
the text of a node and all its descendants, concatenated.
| n | the node |
String -- owned; release it when done.
(node-text p) ; => "abc" for <p>a<b>b</b>c</p>
Since: 0.1.0
node-attr
(node-attr [n : Node name : cstr] :)
look up an element's attribute by name.
| n | the element | |
| name | the attribute name |
(Option cstr) -- borrowed from the document; none when absent or when `n` is not an element.
(node-attr link "href") ; => (Some "https://example.com/")
Since: 0.1.0
node-attr-count
(node-attr-count [n : Node] :)
how many attributes an element has.
| n | the node |
int -- 0 for non-elements.
(node-attr-count (dom-root d)) ; => 2 for <a x="1" y="2"/>
Since: 0.1.0
node-attr-name-at
(node-attr-name-at [n : Node i : int] :)
the name of an element's i-th attribute (document
| n | the element | |
| i | 0 <= i < (node-attr-count n) |
(Option cstr) -- borrowed; none when out of range.
(node-attr-name-at el 0) ; => (Some "x")
Since: 0.1.0
node-attr-value-at
(node-attr-value-at [n : Node i : int] :)
the value of an element's i-th attribute.
| n | the element | |
| i | 0 <= i < (node-attr-count n) |
(Option cstr) -- borrowed; none when out of range.
(node-attr-value-at el 0) ; => (Some "1")
Since: 0.1.0
node-parent
(node-parent [n : Node] :)
the node's parent.
| n | the node |
(Option Node) -- the root element's parent is the document node; the document node has none.
(node-parent title) ; => (Some head)
Since: 0.1.0
node-first-child
(node-first-child [n : Node] :)
the node's first child.
| n | the node |
(Option Node) -- none for a leaf.
(node-first-child (dom-node d))
Since: 0.1.0
node-last-child
(node-last-child [n : Node] :)
the node's last child.
| n | the node |
(Option Node) -- none for a leaf.
(node-last-child body)
Since: 0.1.0
node-next-sibling
(node-next-sibling [n : Node] :)
the next node under the same parent.
| n | the node |
(Option Node) -- none for the last child.
(node-next-sibling item)
Since: 0.1.0
node-prev-sibling
(node-prev-sibling [n : Node] :)
the previous node under the same parent.
| n | the node |
(Option Node) -- none for the first child.
(node-prev-sibling item)
Since: 0.1.0
node-child-count
(node-child-count [n : Node] :)
how many children the node has.
| n | the node |
int
(node-child-count (dom-node d))
Since: 0.1.0
node-children
(node-children [n : Node] :)
the node's children, in document order.
| n | the node |
(Cons Node) -- `(tnil)` for a leaf.
(tlength (node-children root)) ; => 3
Since: 0.1.0
node-document
(node-document [n : Node] :)
the Document that owns this node.
| n | the node |
Document -- the same handle `dom-free` takes.
(dom-free (node-document root))
Since: 0.1.0
node-pos
(node-pos [n : Node] :)
where the node started in the source.
| n | the node |
(Option SourcePos) -- XML nodes carry their start position; HTML nodes and the document node do not (lexbor's tree does not record them).
(node-pos item) ; => (Some (SourcePos 4 3))
Since: 0.1.0
node-depth
(node-depth [n : Node] :)
how many ancestors the node has.
| n | the node |
int -- 0 for the document node, 1 for the root element.
(node-depth (dom-node d)) ; => 0
Since: 0.1.0
node-matches?
(node-matches? [n : Node name : cstr] :)
is `n` an element that answers to `name`?
| n | the node | |
| name | the name to test |
bool -- false for every non-element.
(node-matches? n "{http://www.w3.org/2005/Atom}entry")
Since: 0.1.0
find-all
(find-all [n : Node tag : cstr] :)
every descendant element named `tag`, in document order.
| n | where to search (often `(dom-node d)` for the whole document) | |
| tag | the element name |
(Cons Node) -- `(tnil)` when nothing matches.
(tlength (find-all (dom-node d) "item")) ; => number of <item>s
Since: 0.1.0
find-first
(find-first [n : Node tag : cstr] :)
the first descendant element named `tag`, in document order.
| n | where to search | |
| tag | the element name (as in `find-all`) |
(Option Node) -- none when nothing matches.
(find-first (dom-node d) "title")
Since: 0.1.0
dom-serialize
(dom-serialize [n : Node] :)
markup for a node and its subtree.
| n | the node (an element, the document node, or any leaf) |
String -- owned; release it when done.
(to-cstr (dom-serialize root)) ; => "<a x=\"1\"><b/></a>"
Since: 0.1.0
dom-equal?
(dom-equal? [a : Node b : Node] :)
are two subtrees structurally equal?
| a | a node | |
| b | another node, possibly from another document |
bool
(dom-equal? (dom-node d1) (dom-node d2))
Since: 0.1.0
Internal definitions
__null?-- internal: is this a null pointer?__null-ptr-- internal: the null pointer.__null-cstr?-- internal: is this a null cstr?__opt-node-- internal: a nullable node pointer as an (Option Node).__opt-cstr-- internal: a nullable borrowed cstr as an (Option cstr).__doc-node-raw-- internal: the document's invisible root node.__root-raw-- internal: the root element, or NULL.__kind-raw-- internal: the C kind code.__name-raw-- internal: the C name, or NULL.__local-raw-- internal: the C local name, or NULL.__ns-raw-- internal: the C namespace URI, or NULL.__value-raw-- internal: the C character data, or NULL.__text-raw-- internal: malloc'd concatenated text.__attr-raw-- internal: the attribute value, or NULL.__attr-name-at-raw-- internal: i-th attribute name, or NULL.__attr-value-at-raw-- internal: i-th attribute value, or NULL.__parent-raw-- internal.__first-child-raw-- internal.__last-child-raw-- internal.__next-raw-- internal.__prev-raw-- internal.__children-acc-- internal: build the child list back to front.__doc-of-raw-- internal.__line-raw-- internal.__col-raw-- internal.__find-prev-raw-- internal: previous match in reverse document order.__find-next-raw-- internal: next match in document order.__find-all-acc-- internal: collect matches back to front.__serialize-raw-- internal: malloc'd markup.