Skip to main content

Document

Struct Document 

Source
pub struct Document { /* private fields */ }
Expand description

A parsed XML part stored in flat contiguous arenas (nodes, child indices, attributes, and a single UTF-8 string buffer with interned tag and attribute names).

Implementations§

Source§

impl Document

Source

pub fn same_tree( &self, id: NodeId, (other, other_id): (&Self, NodeId), equality: Equality, ) -> bool

Whether the node id of this document and the node other_id of other hold the same XML under equality, with all their content.

Source§

impl Document

Source

pub fn parse_fragment( bytes: &[u8], in_scope: &[(String, String)], limits: &Limits, ) -> Result<Fragment, Error>

Parses bytes as the content of an element at which the namespace declarations in_scope are in scope, such as the bytes of an element’s span or the new XML of an edit in its place: each prefix ("" for the default namespace) with its URI, as Document::namespaces_in_scope gives them. Each URI binds as given: the reader binds a namespace to its declaration’s value as written, with no reference resolved, so a URI of a part binds the same in the fragment. A prefix that neither the fragment nor in_scope declares is an error here, unlike in a part, because the fragment is to stand in a part where it must read.

§Errors

Returns Error::UndeclaredPrefix for a prefix without a declaration, and every error of Document::parse; a prefix of in_scope that is no name, a URI that holds both quotation marks, and a fragment that closes the element that it stands in, are Error::Syntax.

Source

pub fn parse_fragment_with_aliases_at( bytes: &[u8], in_scope: &[(String, String)], aliases: &[(&str, &str)], base_offset: u64, limits: &Limits, ) -> Result<Fragment, Error>

Parses bytes as a slice of a larger XML part starting at base_offset, resolving namespace declarations in_scope and namespace aliases, while recording undeclared prefixes on Document::undeclared_prefixes instead of failing.

Node byte spans returned by Document::span are shifted by base_offset so they match exact byte offsets in the enclosing part.

§Errors

Returns the errors of Document::parse_with_aliases, or Error::Syntax when in_scope contains an invalid prefix/URI or bytes closes the synthetic container.

Source§

impl Document

Source

pub fn parse(bytes: &[u8], limits: &Limits) -> Result<Self, Error>

Parses a part.

§Errors

Returns an error when the XML is malformed, has a DOCTYPE, or breaks a limit.

Source

pub fn parse_with_aliases( bytes: &[u8], limits: &Limits, aliases: &[(&str, &str)], ) -> Result<Self, Error>

Parses a part and resolves each namespace URI on the left of an alias pair to the URI on the right, so that two spellings of one vocabulary read the same. The namespace declarations stay in the element attributes as written.

§Errors

Returns an error when the XML is malformed, has a DOCTYPE, or breaks a limit.

Source

pub fn root(&self) -> Result<NodeId, Error>

Returns the root element.

§Errors

Returns Error::NoRoot when the document has no element at the top level.

Source

pub fn kind(&self, id: NodeId) -> Option<NodeKind<'_>>

Returns what a node holds.

Source

pub fn element(&self, id: NodeId) -> Option<Element<'_>>

Returns the element at a node, or None for other nodes.

Source

pub fn text(&self, id: NodeId) -> Option<&str>

Returns the text at a node, or None for other nodes.

Source

pub fn children(&self, id: NodeId) -> &[NodeId]

Returns the children of a node in document order.

Source

pub fn child_elements(&self, id: NodeId) -> impl Iterator<Item = NodeId> + '_

Returns the child elements of a node in document order.

Source

pub fn set_children(&mut self, id: NodeId, children: Vec<NodeId>)

Replaces the children of a node, in document order. The new children are nodes of this document, so that an edit can drop, keep and move nodes; a node that no parent lists any more drops out of the tree. Nothing changes for a node that the document lacks.

Source

pub fn set_attribute( &mut self, id: NodeId, namespace: Option<&str>, local: &str, value: &str, )

Sets an attribute of an element, so that an edit can leave a note on it: the value of an attribute with this namespace URI and local name changes, or the attribute is added. Pass None as the namespace for an unprefixed attribute. Nothing changes for a node that is no element.

Source

pub fn undeclared_prefixes(&self) -> &[String]

Returns the namespace prefixes that the part uses on an element or an attribute without a declaration in scope, which XML’s namespaces forbid, each once in the order of its first use. xml and xmlns need no declaration. Such a name keeps its prefix and has no namespace: an element of it is none that Document::is or Document::child finds, and an attribute of it reads as an attribute without a prefix.

Source

pub fn prefix_namespace(&self, id: NodeId, prefix: &str) -> Option<&str>

Returns the namespace that the prefix prefix ("" for the default namespace) is bound to at the element id: by the nearest declaration of the element or of an ancestor, as the part was parsed, after the aliases of Document::parse_with_aliases. The prefix xml is bound as XML defines it. None for a prefix that no declaration in scope binds, for a declaration of the empty name, and for a node that is no element. A format crate reads a prefix in an attribute’s value through it, such as the namespaces that a choice of alternate content requires.

Source

pub fn span(&self, id: NodeId) -> Result<Range<usize>, Error>

Returns where a node’s markup lies in the bytes that were parsed, as offsets into those bytes, before any normalization: an element from the < of its start tag to the byte after the > of its end tag or of its empty tag; a text node from its first byte to the byte after its last, with the character and entity references and the CDATA sections that it joins; and a comment, a processing instruction or the XML declaration as written. A span keeps the bytes as parsed: Document::set_children does not change it. Replacing the bytes of a span replaces the node in the part, so that an edit keeps every other byte as it was.

§Errors

Returns Error::NotUtf8 when the part is not in UTF-8, and Error::NoSuchNode for a node that the document lacks.

Source

pub fn namespaces_in_scope(&self, id: NodeId) -> Vec<(String, String)>

Returns the namespace declarations in scope at the element id, its own among them: each prefix ("" for the default namespace) with the URI that it binds after the aliases of the parse, from the nearest declaration of the prefix only, the element’s own first and then outward. Empty for a node that is no element. A fragment that takes the place of the element’s content, or of a child, is parsed with them (Document::parse_fragment).

Source

pub fn namespace_uri(&self, id: NamespaceId) -> &str

Returns the URI of an interned namespace.

Source

pub fn is(&self, id: NodeId, namespace: &str, local: &str) -> bool

Tells whether a node is an element with this namespace URI and local name.

Source

pub fn child(&self, id: NodeId, namespace: &str, local: &str) -> Option<NodeId>

Returns the first child element with this namespace URI and local name.

Source

pub fn attribute( &self, id: NodeId, namespace: Option<&str>, local: &str, ) -> Option<&str>

Returns an attribute value. Pass None as the namespace for an unprefixed attribute.

Source

pub fn text_content(&self, id: NodeId) -> String

Returns all text below a node, in document order.

Trait Implementations§

Source§

impl Clone for Document

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Document

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.