Skip to main content

parsec_xpath/
tree.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 James Conway
3
4//! What a query reads a document through: [`Tree`], which any tree can
5//! implement, so XPath doesn't depend on one parser's node layout.
6
7/// A node's id in a [`Tree`]: a u32 below [`Tree::len`].
8pub type NodeId = u32;
9
10/// A node's kind, numbered as the DOM's `nodeType` numbers them.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12#[repr(u8)]
13pub enum NodeType {
14    Element = 0x01,
15    Text = 0x03,
16    Cdata = 0x04,
17    ProcessingInstruction = 0x07,
18    Comment = 0x08,
19    Document = 0x09,
20    Doctype = 0x0A,
21    /// Anything XPath has no node for, such as a fragment or a shadow
22    /// root. Indexed as a root, it's the top of a tree, as a document is.
23    Other = 0x0B,
24}
25
26/// What [`crate::Index`] groups elements by for name tests. Elements with
27/// the same name lowercased share a group however the tree names them, so
28/// a query's name is looked up once, not compared on every element.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum TagKey<'t> {
31    /// A number the tree gives every element of one name, as an HTML
32    /// parser numbers the tags it knows. It saves the index reading and
33    /// hashing the name of every element.
34    Known(u16),
35    /// The element's local name.
36    Name(&'t str),
37}
38
39/// An attribute as XPath's data model has it.
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub struct Attr<'t> {
42    /// The name as written, with any prefix: what `name()` gives.
43    pub qualified_name: &'t str,
44    /// The name without its prefix: what `local-name()` gives.
45    pub local_name: &'t str,
46    /// "" for no namespace.
47    pub namespace_uri: &'t str,
48    pub value: &'t str,
49}
50
51/// A tree XPath can query. Every method takes a node id below
52/// [`Tree::len`]; a method about elements gives "", 0 or None for a node
53/// of another kind.
54pub trait Tree: Sync {
55    /// How many node ids there are, including those of removed nodes.
56    fn len(&self) -> usize;
57
58    fn is_empty(&self) -> bool {
59        self.len() == 0
60    }
61
62    fn kind(&self, n: NodeId) -> NodeType;
63    fn parent(&self, n: NodeId) -> Option<NodeId>;
64    fn first_child(&self, n: NodeId) -> Option<NodeId>;
65    fn next_sibling(&self, n: NodeId) -> Option<NodeId>;
66    fn prev_sibling(&self, n: NodeId) -> Option<NodeId>;
67
68    /// An element's name as written, with any prefix: what `name()`
69    /// gives. Lowercase in HTML.
70    fn qualified_name(&self, n: NodeId) -> &str;
71    /// An element's name without its prefix: what `local-name()` gives.
72    fn local_name(&self, n: NodeId) -> &str;
73    /// An element's namespace URI; "" for no namespace.
74    fn namespace_uri(&self, n: NodeId) -> &str;
75    /// What [`crate::Index`] keys an element's name tests on.
76    fn tag_key(&self, n: NodeId) -> TagKey<'_>;
77
78    /// How many attributes an element has. In XPath's data model,
79    /// `xmlns` and `xmlns:*` are namespace nodes rather than attributes, so
80    /// an XML tree leaves them out of these. An HTML tree keeps them, as
81    /// libxml2's HTML parser does.
82    fn attribute_count(&self, n: NodeId) -> u32;
83    /// An element's `i`th attribute, in document order. A query reads
84    /// attributes by place, so this should take constant time.
85    fn attribute(&self, n: NodeId, i: u32) -> Option<Attr<'_>>;
86
87    /// How many namespaces node `n` declares: an XML element's `xmlns`
88    /// and `xmlns:*` attributes. A root that isn't an element may declare
89    /// the namespaces in scope where its tree was cut from, which its
90    /// elements then inherit. The namespace axis works out what's in scope
91    /// from these. None by default, as an HTML tree has none: its elements
92    /// have only the `xml` namespace, which is always in scope.
93    fn namespace_declaration_count(&self, _n: NodeId) -> u32 {
94        0
95    }
96    /// Node `n`'s `i`th declaration, in the order written: the prefix (""
97    /// for the default namespace) and the URI ("" for `xmlns=""`, which
98    /// takes the binding away). Of two for one prefix on a node, the
99    /// first counts. This should take constant time.
100    fn namespace_declaration(&self, _n: NodeId, _i: u32) -> Option<(&str, &str)> {
101        None
102    }
103
104    /// A text, CDATA section, comment or processing instruction's text.
105    fn data(&self, n: NodeId) -> &str;
106    /// A processing instruction's target.
107    fn pi_target(&self, n: NodeId) -> Option<&str>;
108
109    /// Whether names compare exactly (XML), rather than ignoring ASCII
110    /// case with prefixes left as part of the name (HTML).
111    fn is_xml(&self) -> bool;
112    /// The value `id()` finds an element by: HTML's `id`, or XML's
113    /// `xml:id` or an attribute the DTD declares an ID.
114    fn id(&self, n: NodeId) -> Option<&str>;
115    /// The language an element declares, if it declares one, for
116    /// `lang()`: `xml:lang`, or HTML's `lang`.
117    fn lang(&self, n: NodeId) -> Option<&str>;
118}