Skip to main content

Node

Struct Node 

Source
#[repr(C)]
pub struct Node<'doc> {
Show 13 fields pub kind: NodeKind, pub name: &'doc str, pub namespace: Cell<Option<&'doc Namespace<'doc>>>, pub first_attribute: Cell<Option<&'doc Attribute<'doc>>>, pub last_attribute: Cell<Option<&'doc Attribute<'doc>>>, pub first_child: Cell<Option<&'doc Node<'doc>>>, pub last_child: Cell<Option<&'doc Node<'doc>>>, pub parent: Cell<Option<&'doc Node<'doc>>>, pub next_sibling: Cell<Option<&'doc Node<'doc>>>, pub prev_sibling: Cell<Option<&'doc Node<'doc>>>, pub content: Cell<Option<&'doc str>>, pub line: u32, pub source_offset: u32,
}
Expand description

A node in the XML tree. One struct for every kind; fields are used per-kind.

§Layout (libxml2 mirror)

The struct is #[repr(C)] with field order chosen to align with _xmlNode in libxml2. When we later expose an extern "C" shim, callers can treat *mut Node as xmlNode* (modulo a few private slots we may still need to add — _private, psvi, etc.).

§Field usage by kind

Kindnamecontentfirst_* / last_*
Elementtag nameunused ("")children + attrs
Text""text dataunused
CData""CDATA payloadunused
Comment""comment textunused
Pitargetcontent after targetunused

Fields§

§kind: NodeKind§name: &'doc str§namespace: Cell<Option<&'doc Namespace<'doc>>>§first_attribute: Cell<Option<&'doc Attribute<'doc>>>

Linked-list head for element attributes (None for non-elements).

§last_attribute: Cell<Option<&'doc Attribute<'doc>>>§first_child: Cell<Option<&'doc Node<'doc>>>§last_child: Cell<Option<&'doc Node<'doc>>>§parent: Cell<Option<&'doc Node<'doc>>>§next_sibling: Cell<Option<&'doc Node<'doc>>>§prev_sibling: Cell<Option<&'doc Node<'doc>>>§content: Cell<Option<&'doc str>>

Text/CData/Comment/Pi payload. None mirrors libxml2’s NULL content — notably a processing instruction with no data (<?foo?>), which serializes without the trailing space that a PI carrying a (possibly empty) data section gets.

§line: u32

1-based source line of the opening tag. 0 for nodes constructed programmatically.

§source_offset: u32

0-based byte offset of the opening tag’s name in the source buffer. 0 for nodes not produced by the parser. Saturates at u32::MAX for inputs past 4 GiB. Line and column are derived from this offset against the document’s source buffer (Document::source) — the offset is the single ground truth.

Implementations§

Source§

impl<'doc> Node<'doc>

Source

pub fn is_element(&self) -> bool

Source

pub fn is_text(&self) -> bool

Source

pub fn is_entity_ref(&self) -> bool

Source

pub fn name(&self) -> &'doc str

Element name (or PI target for PI nodes). Empty "" for Text/CData/Comment.

Prefer this method over direct node.name field access in internal / downstream code. The pub name field stays stable on the lean rlib build, but when sup-xml is built with the c-abi feature the storage type changes to a thin NUL-terminated ArenaCStr. This method returns &str on both configs — direct field access does not.

Source

pub fn line_no(&self) -> u32

Source line number as recorded by the parser (0 for nodes built through the tree API). Abstracts the per-config storage: a plain u32 on the lean build, a Cell<u16> (libxml2’s unsigned short) on the c-abi build — so callers don’t reach into the field.

Source

pub fn content(&self) -> &'doc str

Text payload for Text/CData/Comment/Pi nodes; "" for elements. See name for why to prefer this method over direct field access.

Source

pub fn content_opt(&self) -> Option<&'doc str>

The payload as libxml2 stores it: None when the underlying content pointer is NULL, Some (possibly "") otherwise. Most callers want content; the serializer uses this to reproduce libxml2’s PI rule (a NULL-data PI omits the space a non-NULL-but-empty-data PI emits).

Source

pub fn set_content(&self, arena: &'doc DocumentBuilder, content: &'doc str)

Replace the node’s content (text/CData/comment/PI payload). Used by the HTML sink and other in-place tree mutators. Allocates a NUL-terminated copy in the arena when the c-abi feature is on.

arena must be the same DocumentBuilder that owns this node (lifetimes enforce it).

Source

pub fn children(&self) -> ChildIter<'doc>

Iterate child nodes in document order.

Source

pub fn attributes(&self) -> AttrIter<'doc>

Iterate attributes in document order. Returns an empty iterator for non-elements.

Source

pub fn local_name(&self) -> &'doc str

Local part of this element/attr-like node’s name — "foo" for both <foo/> and <ns:foo/>. On the lean build name returns the full QName including any prefix; on the c-abi build the prefix is stripped at parse time and name() is already the local part. local_name() is the layout-agnostic accessor: callers that want to match by local name should use this.

Source

pub fn ns_declarations(&self) -> NsDeclIter<'doc>

Iterate this element’s xmlns declarations as (prefix, href)prefix is None for the default namespace (xmlns="..."), Some("dc") for xmlns:dc="...". Empty for non-elements.

Storage differs by build: the c-abi build keeps xmlns declarations on the element’s ns_def chain (matching libxml2’s _xmlNode); the lean build keeps them in attributes instead. A namespace-blind parse (namespace_aware: false) does no namespace processing and leaves xmlns declarations in the attribute list under either build, so the c-abi iterator walks ns_def first and then the attribute list — the two are mutually exclusive (a given parse populates one or the other), so nothing is reported twice. Callers see every declaration regardless of build or parse mode.

Source

pub fn find_child(&self, name: &str) -> Option<&'doc Node<'doc>>

First child element matching name, or None. O(n) walk; for repeated lookups, iterate children() yourself.

Source

pub fn text_content(&self) -> Option<&'doc str>

For text-bearing kinds returns the content; for elements returns the first text-or-CDATA child’s content; otherwise None.

Trait Implementations§

Source§

impl Debug for Node<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'doc> !Freeze for Node<'doc>

§

impl<'doc> !RefUnwindSafe for Node<'doc>

§

impl<'doc> !Send for Node<'doc>

§

impl<'doc> !Sync for Node<'doc>

§

impl<'doc> !UnwindSafe for Node<'doc>

§

impl<'doc> Unpin for Node<'doc>

§

impl<'doc> UnsafeUnpin for Node<'doc>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.