use std::borrow::Cow;
use std::cell::{Cell, OnceCell};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt::{self, Write as _};
use std::path::Path;
use std::rc::Rc;
use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering};
use std::sync::Arc;
use einvoice_xdm::AtomicValue;
use einvoice_xpath::{NamespaceBinding, QName, XML_NAMESPACE};
use crate::limit::{DEFAULT_MAX_DEPTH, LimitExceeded};
const NONE: u32 = u32::MAX;
static NEXT_SERIAL: AtomicU64 = AtomicU64::new(0);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NodeKind {
Document,
Element,
Attribute,
Text,
Comment,
ProcessingInstruction,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NodeAxis {
Child,
Attribute,
SelfAxis,
Descendant,
DescendantOrSelf,
FollowingSibling,
Parent,
Ancestor,
AncestorOrSelf,
PrecedingSibling,
Preceding,
}
impl NodeAxis {
#[must_use]
pub const fn is_reverse(self) -> bool {
matches!(
self,
Self::Parent
| Self::Ancestor
| Self::AncestorOrSelf
| Self::PrecedingSibling
| Self::Preceding
)
}
}
#[derive(Debug, Clone, Copy)]
struct Record {
kind: NodeKind,
name: u32,
parent: u32,
prev_sibling: u32,
first_child: u32,
end: u32,
text_start: u32,
text_len: u32,
}
impl Record {
const fn new(
kind: NodeKind,
name: u32,
parent: u32,
prev_sibling: u32,
index: u32,
(text_start, text_len): (u32, u32),
) -> Self {
Self {
kind,
name,
parent,
prev_sibling,
first_child: index + 1,
end: index + 1,
text_start,
text_len,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DocumentError {
message: String,
limit: Option<LimitExceeded>,
}
impl DocumentError {
fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
limit: None,
}
}
fn limit_exceeded(limit: LimitExceeded) -> Self {
Self {
message: limit.to_string(),
limit: Some(limit),
}
}
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
#[must_use]
pub const fn limit(&self) -> Option<LimitExceeded> {
self.limit
}
}
impl fmt::Display for DocumentError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for DocumentError {}
pub struct Document {
serial: u64,
uri: Option<String>,
base_uri: Option<String>,
has_xml_base: bool,
records: Vec<Record>,
names: Vec<QName>,
namespaces: Vec<(u32, Arc<[NamespaceBinding]>)>,
source: String,
text: String,
elements: OnceCell<ElementIndex>,
searches: Cell<u32>,
view: TreeView,
ids: OnceCell<HashMap<String, u32>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TreeView {
#[default]
Tiny,
Dom,
}
const INDEXED_SEARCH: u32 = 64;
#[derive(Debug, Default)]
struct ElementIndex {
names: Vec<IndexedName>,
elements: Vec<u32>,
}
#[derive(Debug, Clone, Copy)]
struct IndexedName {
hash: u64,
name: u32,
start: u32,
len: u32,
}
fn name_hash(namespace: Option<&str>, local: &str) -> u64 {
use std::hash::{Hash as _, Hasher as _};
let mut hasher = std::hash::DefaultHasher::new();
(namespace, local).hash(&mut hasher);
hasher.finish()
}
impl fmt::Debug for Document {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Document")
.field("serial", &self.serial)
.field("uri", &self.uri)
.field("nodes", &self.records.len())
.finish_non_exhaustive()
}
}
impl Document {
pub fn parse<'t>(
text: impl Into<Cow<'t, str>>,
base_uri: Option<&str>,
) -> Result<Rc<Self>, DocumentError> {
Self::parse_with_uris(text.into(), None, base_uri, DEFAULT_MAX_DEPTH)
}
pub fn parse_with_max_depth<'t>(
text: impl Into<Cow<'t, str>>,
base_uri: Option<&str>,
max_depth: u32,
) -> Result<Rc<Self>, DocumentError> {
Self::parse_with_uris(text.into(), None, base_uri, max_depth)
}
pub fn parse_loaded<'t>(
text: impl Into<Cow<'t, str>>,
uri: &str,
) -> Result<Rc<Self>, DocumentError> {
Self::parse_with_uris(text.into(), Some(uri), Some(uri), DEFAULT_MAX_DEPTH)
}
pub fn parse_loaded_with_max_depth<'t>(
text: impl Into<Cow<'t, str>>,
uri: &str,
max_depth: u32,
) -> Result<Rc<Self>, DocumentError> {
Self::parse_with_uris(text.into(), Some(uri), Some(uri), max_depth)
}
fn parse_with_uris(
text: Cow<'_, str>,
document_uri: Option<&str>,
base_uri: Option<&str>,
max_depth: u32,
) -> Result<Rc<Self>, DocumentError> {
let keep_source = matches!(text, Cow::Owned(_));
let builder = build_parsed(&text, keep_source, max_depth)?;
let source = match text {
Cow::Owned(source) => source,
Cow::Borrowed(_) => String::new(),
};
builder.finish_with_source(
source,
document_uri.map(str::to_owned),
base_uri.map(str::to_owned),
)
}
fn copy_with_source(&self, source: String) -> Self {
debug_assert_eq!(source, self.source);
Self {
serial: NEXT_SERIAL.fetch_add(1, AtomicOrdering::Relaxed),
uri: self.uri.clone(),
base_uri: self.base_uri.clone(),
has_xml_base: self.has_xml_base,
records: self.records.clone(),
names: self.names.clone(),
namespaces: self.namespaces.clone(),
source,
text: self.text.clone(),
elements: OnceCell::new(),
searches: Cell::new(0),
view: self.view,
ids: OnceCell::new(),
}
}
#[must_use]
pub fn root(self: &Rc<Self>) -> Node {
Node {
doc: Rc::clone(self),
index: 0,
}
}
#[must_use]
pub fn document_uri(&self) -> Option<&str> {
self.uri.as_deref()
}
#[must_use]
pub fn base_uri(&self) -> Option<&str> {
self.base_uri.as_deref()
}
#[must_use]
pub const fn has_xml_base(&self) -> bool {
self.has_xml_base
}
#[must_use]
pub fn name_count(&self) -> usize {
self.names.len()
}
#[must_use]
pub fn node_count(&self) -> usize {
self.records.len()
}
fn record(&self, index: u32) -> &Record {
&self.records[index as usize]
}
pub(crate) fn kind(&self, index: u32) -> NodeKind {
self.record(index).kind
}
pub(crate) fn name(&self, index: u32) -> Option<&QName> {
let name = self.record(index).name;
(name != NONE).then(|| &self.names[name as usize])
}
pub(crate) fn parent_index(&self, index: u32) -> Option<u32> {
let parent = self.record(index).parent;
(parent != NONE).then_some(parent)
}
fn own_text(&self, index: u32) -> &str {
let r = self.record(index);
let start = r.text_start as usize;
let end = start + r.text_len as usize;
match start.checked_sub(self.source.len()) {
None => &self.source[start..end],
Some(start) => &self.text[start..end - self.source.len()],
}
}
pub(crate) fn string_value(&self, index: u32) -> Cow<'_, str> {
match self.kind(index) {
NodeKind::Text
| NodeKind::Comment
| NodeKind::ProcessingInstruction
| NodeKind::Attribute => Cow::Borrowed(self.own_text(index)),
NodeKind::Element | NodeKind::Document => {
let mut texts = self
.cursor(index, NodeAxis::Descendant)
.walk(self)
.filter(|&i| self.kind(i) == NodeKind::Text);
let Some(first) = texts.next() else {
return Cow::Borrowed("");
};
let Some(second) = texts.next() else {
return Cow::Borrowed(self.own_text(first));
};
let mut value = String::from(self.own_text(first));
value.push_str(self.own_text(second));
for i in texts {
value.push_str(self.own_text(i));
}
Cow::Owned(value)
}
}
}
pub(crate) fn elements_named(
&self,
namespace: Option<&str>,
local: &str,
start: u32,
end: u32,
) -> Option<&[u32]> {
if end.saturating_sub(start) < INDEXED_SEARCH {
return None;
}
let index = if let Some(index) = self.elements.get() {
index
} else {
let searches = self.searches.get().saturating_add(1);
self.searches.set(searches);
if searches < 2 {
return None;
}
self.elements.get_or_init(|| self.index_elements())
};
let hash = name_hash(namespace, local);
let first = index.names.partition_point(|n| n.hash < hash);
let all = index.names[first..]
.iter()
.take_while(|n| n.hash == hash)
.find(|n| {
self.names.get(n.name as usize).is_some_and(|name| {
name.local == local && name.namespace.as_deref() == namespace
})
})
.and_then(|n| {
let start = n.start as usize;
index.elements.get(start..start + n.len as usize)
})
.unwrap_or_default();
let from = all.partition_point(|&i| i < start);
let to = all.partition_point(|&i| i < end);
all.get(from..to)
}
fn index_elements(&self) -> ElementIndex {
let mut first_of_key: HashMap<(Option<&str>, &str), u32> =
HashMap::with_capacity(self.names.len());
let list_of_name: Vec<u32> = (0_u32..)
.zip(&self.names)
.map(|(id, name)| {
*first_of_key
.entry((name.namespace.as_deref(), name.local.as_str()))
.or_insert(id)
})
.collect();
drop(first_of_key);
let element_list = |record: &Record| {
(record.kind == NodeKind::Element)
.then(|| list_of_name.get(record.name as usize).copied())
.flatten()
};
let mut counts = vec![0_u32; self.names.len()];
for record in &self.records {
if let Some(count) = element_list(record).and_then(|l| counts.get_mut(l as usize)) {
*count += 1;
}
}
let mut names = Vec::new();
let mut next = vec![0_u32; self.names.len()];
let mut total = 0_u32;
for (list, (&count, next)) in (0_u32..).zip(counts.iter().zip(next.iter_mut())) {
if count > 0 {
let name = &self.names[list as usize];
names.push(IndexedName {
hash: name_hash(name.namespace.as_deref(), &name.local),
name: list,
start: total,
len: count,
});
*next = total;
total += count;
}
}
drop(counts);
let mut elements = vec![0_u32; total as usize];
for (index, record) in (0_u32..).zip(&self.records) {
if let Some(next) = element_list(record).and_then(|l| next.get_mut(l as usize))
&& let Some(slot) = elements.get_mut(*next as usize)
{
*slot = index;
*next += 1;
}
}
names.sort_by_key(|n| n.hash);
names.shrink_to_fit();
ElementIndex { names, elements }
}
pub(crate) fn descendant_range(&self, index: u32) -> (u32, u32) {
let r = self.record(index);
(r.first_child, r.end)
}
pub(crate) fn cursor(&self, index: u32, axis: NodeAxis) -> Cursor {
let r = self.record(index);
let kind = r.kind;
let is_attribute = kind == NodeKind::Attribute;
match axis {
NodeAxis::SelfAxis => Cursor::One(index),
NodeAxis::Parent => match r.parent {
NONE => Cursor::Done,
parent => Cursor::One(parent),
},
NodeAxis::Child => Cursor::Siblings {
next: r.first_child,
end: r.end,
},
NodeAxis::Attribute => {
if kind == NodeKind::Element {
Cursor::Range {
next: index + 1,
end: r.first_child,
}
} else {
Cursor::Done
}
}
NodeAxis::Descendant => Cursor::Descendants {
next: r.first_child,
end: r.end,
},
NodeAxis::DescendantOrSelf => Cursor::Descendants {
next: index,
end: r.end,
},
NodeAxis::FollowingSibling => match r.parent {
NONE => Cursor::Done,
_ if is_attribute => Cursor::Done,
parent => Cursor::Siblings {
next: r.end,
end: self.record(parent).end,
},
},
NodeAxis::Ancestor => Cursor::Ancestors { next: r.parent },
NodeAxis::AncestorOrSelf => Cursor::Ancestors { next: index },
NodeAxis::PrecedingSibling => {
if is_attribute {
Cursor::Done
} else {
Cursor::PrecedingSiblings {
next: r.prev_sibling,
}
}
}
NodeAxis::Preceding => Cursor::Preceding {
next: index,
ancestor: r.parent,
},
}
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum Cursor {
Done,
One(u32),
Range {
next: u32,
end: u32,
},
Siblings {
next: u32,
end: u32,
},
Descendants {
next: u32,
end: u32,
},
Ancestors {
next: u32,
},
PrecedingSiblings {
next: u32,
},
Preceding {
next: u32,
ancestor: u32,
},
}
impl Cursor {
pub(crate) fn next(&mut self, doc: &Document) -> Option<u32> {
match self {
Self::Done => None,
Self::One(index) => {
let index = *index;
*self = Self::Done;
Some(index)
}
Self::Range { next, end } => {
if *next >= *end {
return None;
}
let index = *next;
*next += 1;
Some(index)
}
Self::Siblings { next, end } => {
if *next >= *end {
return None;
}
let index = *next;
*next = doc.record(index).end;
Some(index)
}
Self::Descendants { next, end } => {
if *next >= *end {
return None;
}
let index = *next;
*next = doc.record(index).first_child;
Some(index)
}
Self::Ancestors { next } => {
if *next == NONE {
return None;
}
let index = *next;
*next = doc.record(index).parent;
Some(index)
}
Self::PrecedingSiblings { next } => {
if *next == NONE {
return None;
}
let index = *next;
*next = doc.record(index).prev_sibling;
Some(index)
}
Self::Preceding { next, ancestor } => loop {
if *next == 0 {
return None;
}
*next -= 1;
let index = *next;
if index == *ancestor {
*ancestor = doc.record(index).parent;
continue;
}
if doc.record(index).kind != NodeKind::Attribute {
return Some(index);
}
},
}
}
pub(crate) fn walk(self, doc: &Document) -> impl Iterator<Item = u32> + '_ {
let mut cursor = self;
std::iter::from_fn(move || cursor.next(doc))
}
}
#[derive(Clone)]
pub struct Node {
doc: Rc<Document>,
index: u32,
}
impl PartialEq for Node {
fn eq(&self, other: &Self) -> bool {
self.index == other.index && Rc::ptr_eq(&self.doc, &other.doc)
}
}
impl Eq for Node {}
impl fmt::Debug for Node {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Node({:?}", self.kind())?;
if let Some(name) = self.name() {
write!(f, " {name}")?;
}
write!(f, " #{} of document {})", self.index, self.doc.serial)
}
}
impl Node {
pub(crate) fn new(doc: Rc<Document>, index: u32) -> Self {
Self { doc, index }
}
pub(crate) const fn index(&self) -> u32 {
self.index
}
#[must_use]
pub fn document(&self) -> &Rc<Document> {
&self.doc
}
#[must_use]
pub fn kind(&self) -> NodeKind {
self.doc.kind(self.index)
}
#[must_use]
pub fn name(&self) -> Option<&QName> {
self.doc.name(self.index)
}
#[must_use]
pub fn name_id(&self) -> Option<u32> {
let name = self.doc.record(self.index).name;
(name != NONE).then_some(name)
}
#[must_use]
pub fn parent(&self) -> Option<Self> {
self.doc
.parent_index(self.index)
.map(|p| Self::new(Rc::clone(&self.doc), p))
}
#[must_use]
pub fn root(&self) -> Self {
Self::new(Rc::clone(&self.doc), 0)
}
#[must_use]
pub fn namespaces_in_scope(&self) -> Vec<NamespaceBinding> {
let doc = &self.doc;
let mut out: Vec<NamespaceBinding> = Vec::new();
if doc.namespaces.is_empty() || self.kind() != NodeKind::Element {
return out;
}
let mut index = self.index;
while index != NONE {
if let Ok(i) = doc.namespaces.binary_search_by_key(&index, |(e, _)| *e) {
for binding in doc.namespaces[i].1.iter() {
if !out.iter().any(|b| b.prefix == binding.prefix) {
out.push(binding.clone());
}
}
}
index = doc.record(index).parent;
}
out.retain(|b| !b.uri.is_empty());
out
}
#[must_use]
pub fn string_value(&self) -> Cow<'_, str> {
self.doc.string_value(self.index)
}
#[must_use]
pub fn by_id(&self, id: &str) -> Option<Self> {
let doc = &self.doc;
if doc.view == TreeView::Dom {
return None;
}
let ids = doc.ids.get_or_init(|| {
let mut ids = HashMap::new();
for (index, record) in (0_u32..).zip(&doc.records) {
if record.kind != NodeKind::Attribute {
continue;
}
let is_xml_id = doc.name(index).is_some_and(|n| {
n.local == "id" && n.namespace.as_deref() == Some(XML_NAMESPACE)
});
if is_xml_id {
let value: Vec<&str> = doc
.own_text(index)
.split([' ', '\t', '\n', '\r'])
.filter(|t| !t.is_empty())
.collect();
ids.entry(value.join(" ")).or_insert(record.parent);
}
}
ids
});
ids.get(id).map(|&i| Self::new(Rc::clone(doc), i))
}
#[must_use]
pub fn generate_id(&self) -> String {
let doc = &self.doc;
let number = doc.serial + 1;
match doc.view {
TreeView::Tiny => {
let attributes_before = |index: u32| {
doc.records[..index as usize]
.iter()
.filter(|r| r.kind == NodeKind::Attribute)
.count()
};
let tiny = |index: u32| {
let kind = doc.kind(index);
if kind == NodeKind::Document {
return format!("d{number}");
}
let letter = match kind {
NodeKind::Text => 't',
NodeKind::Comment => 'c',
NodeKind::ProcessingInstruction => 'p',
_ => 'e',
};
let node_number = index as usize - attributes_before(index);
format!("d{number}{letter}{node_number}")
};
if self.kind() == NodeKind::Attribute {
let parent = doc.record(self.index).parent;
let owner = if parent == NONE {
format!("d{number}")
} else {
tiny(parent)
};
format!("{owner}a{}", attributes_before(self.index))
} else {
tiny(self.index)
}
}
TreeView::Dom => {
let lineage: Vec<u32> = doc
.cursor(self.index, NodeAxis::AncestorOrSelf)
.walk(doc)
.collect();
let mut id = format!("w{number}");
for &index in lineage.iter().rev() {
let record = doc.record(index);
if record.parent == NONE {
continue;
}
let position = if record.kind == NodeKind::Attribute {
id.push('a');
index - record.parent - 1
} else {
u32::try_from(
doc.cursor(index, NodeAxis::PrecedingSibling)
.walk(doc)
.count(),
)
.unwrap_or(u32::MAX)
};
id.push_str(&alpha_key(position));
}
id
}
}
}
#[must_use]
pub fn typed_value(&self) -> AtomicValue {
let value = self.string_value().into_owned();
match self.kind() {
NodeKind::Comment | NodeKind::ProcessingInstruction => AtomicValue::String(value),
_ => AtomicValue::UntypedAtomic(value),
}
}
#[must_use]
pub fn axis(&self, axis: NodeAxis) -> Axis {
Axis {
cursor: self.doc.cursor(self.index, axis),
doc: Rc::clone(&self.doc),
}
}
#[must_use]
pub fn children(&self) -> Axis {
self.axis(NodeAxis::Child)
}
#[must_use]
pub fn attributes(&self) -> Axis {
self.axis(NodeAxis::Attribute)
}
#[must_use]
pub fn cmp_document_order(&self, other: &Self) -> Ordering {
self.doc
.serial
.cmp(&other.doc.serial)
.then(self.index.cmp(&other.index))
}
}
#[derive(Debug, Clone)]
pub struct Axis {
cursor: Cursor,
doc: Rc<Document>,
}
impl Iterator for Axis {
type Item = Node;
fn next(&mut self) -> Option<Node> {
let index = self.cursor.next(&self.doc)?;
Some(Node::new(Rc::clone(&self.doc), index))
}
fn count(self) -> usize {
self.cursor.walk(&self.doc).count()
}
}
#[derive(Debug, Default)]
pub struct DocumentCache {
entries: std::sync::Mutex<Vec<CachedDocument>>,
}
#[derive(Debug)]
struct CachedDocument {
max_depth: u32,
document: Document,
}
impl DocumentCache {
pub const MAX_DOCUMENTS: usize = 64;
pub const MAX_BYTES: usize = 64 << 20;
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn parse_loaded(
&self,
text: String,
uri: &str,
max_depth: u32,
) -> Result<Rc<Document>, DocumentError> {
{
let entries = self
.entries
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if let Some(entry) = entries.iter().find(|e| {
e.max_depth == max_depth
&& e.document.uri.as_deref() == Some(uri)
&& e.document.source == text
}) {
return Ok(Rc::new(entry.document.copy_with_source(text)));
}
}
let document = Document::parse_loaded_with_max_depth(text, uri, max_depth)?;
let mut entries = self
.entries
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let bytes: usize = entries.iter().map(|e| e.document.source.len()).sum();
if entries.len() < Self::MAX_DOCUMENTS
&& bytes.saturating_add(document.source.len()) <= Self::MAX_BYTES
&& !entries.iter().any(|e| {
e.max_depth == max_depth
&& e.document.uri.as_deref() == Some(uri)
&& e.document.source == document.source
})
{
entries.push(CachedDocument {
max_depth,
document: document.copy_with_source(document.source.clone()),
});
}
Ok(document)
}
}
#[derive(Debug)]
pub struct TreeBuilder {
records: Vec<Record>,
names: Vec<QName>,
name_ids: HashMap<QName, u32>,
namespaces: Vec<(u32, Arc<[NamespaceBinding]>)>,
parentless: bool,
source_len: usize,
text: String,
open: Vec<u32>,
last_child: Vec<u32>,
has_xml_base: bool,
view: TreeView,
}
impl Default for TreeBuilder {
fn default() -> Self {
Self::new()
}
}
fn to_u32(value: usize) -> Result<u32, DocumentError> {
u32::try_from(value)
.ok()
.filter(|&v| v != NONE)
.ok_or_else(|| DocumentError::new("document too large"))
}
#[derive(Debug, Clone, Copy)]
enum NodeText<'s> {
Source(u32, u32),
Copy(&'s str),
}
impl TreeBuilder {
#[must_use]
pub fn new() -> Self {
Self {
records: vec![Record::new(NodeKind::Document, NONE, NONE, NONE, 0, (0, 0))],
names: Vec::new(),
name_ids: HashMap::new(),
namespaces: Vec::new(),
parentless: false,
source_len: 0,
text: String::new(),
open: vec![0],
last_child: vec![NONE],
has_xml_base: false,
view: TreeView::Tiny,
}
}
#[must_use]
pub const fn dom_view(mut self) -> Self {
self.view = TreeView::Dom;
self
}
#[must_use]
pub fn parentless() -> Self {
Self {
records: Vec::new(),
open: Vec::new(),
parentless: true,
..Self::new()
}
}
fn with_capacity(nodes: usize, text: usize, source_len: usize) -> Self {
let mut builder = Self::new();
builder.records.reserve_exact(nodes.saturating_sub(1));
builder.text.reserve_exact(text);
builder.source_len = source_len;
builder
}
fn intern(&mut self, name: QName) -> Result<u32, DocumentError> {
if let Some(&id) = self.name_ids.get(&name) {
return Ok(id);
}
let id = self.push_name(name.clone())?;
self.name_ids.insert(name, id);
Ok(id)
}
fn push_name(&mut self, name: QName) -> Result<u32, DocumentError> {
let id = to_u32(self.names.len())?;
self.names.push(name);
Ok(id)
}
fn store_text(&mut self, value: NodeText<'_>) -> Result<(u32, u32), DocumentError> {
match value {
NodeText::Source(start, len) => Ok((start, len)),
NodeText::Copy(value) => {
let start = to_u32(self.source_len + self.text.len())?;
let len = to_u32(value.len())?;
to_u32(self.source_len + self.text.len() + value.len())?;
self.text.push_str(value);
Ok((start, len))
}
}
}
fn top(&self) -> u32 {
self.open.last().copied().unwrap_or(NONE)
}
const fn base(&self) -> usize {
if self.parentless { 0 } else { 1 }
}
fn append(
&mut self,
kind: NodeKind,
name: u32,
text: NodeText<'_>,
) -> Result<u32, DocumentError> {
let index = to_u32(self.records.len())?;
to_u32(self.records.len() + 1)?;
if self.parentless && self.open.is_empty() && !self.records.is_empty() {
return Err(DocumentError::new(
"a second node at the top of a parentless tree",
));
}
let text = if kind == NodeKind::Element {
(0, 0)
} else {
self.store_text(text)?
};
let parent = self.top();
let last = self.last_child.last_mut().map_or(NONE, |l| {
let previous = *l;
*l = index;
previous
});
self.records
.push(Record::new(kind, name, parent, last, index, text));
Ok(index)
}
pub fn start_element(&mut self, name: QName) -> Result<(), DocumentError> {
let name = self.intern(name)?;
self.open_element(name)
}
pub fn add_namespace(&mut self, binding: NamespaceBinding) -> Result<(), DocumentError> {
let element = self.top();
let has_content = self.last_child.last().is_some_and(|&l| l != NONE);
if element == NONE
|| self.records[element as usize].kind != NodeKind::Element
|| has_content
{
return Err(DocumentError::new(format!(
"namespace {:?} outside an element start",
binding.prefix
)));
}
match self.namespaces.last_mut() {
Some((e, bindings)) if *e == element => {
let mut extended: Vec<NamespaceBinding> = bindings.to_vec();
extended.retain(|b| b.prefix != binding.prefix);
extended.push(binding);
*bindings = extended.into();
}
_ => self.namespaces.push((element, vec![binding].into())),
}
Ok(())
}
pub fn start_element_with_namespaces(
&mut self,
name: QName,
namespaces: Arc<[NamespaceBinding]>,
) -> Result<(), DocumentError> {
self.start_element(name)?;
if !namespaces.is_empty() {
let element = self.top();
self.namespaces.push((element, namespaces));
}
Ok(())
}
fn open_element(&mut self, name: u32) -> Result<(), DocumentError> {
let index = self.append(NodeKind::Element, name, NodeText::Copy(""))?;
self.open.push(index);
self.last_child.push(NONE);
Ok(())
}
pub fn attribute(&mut self, name: QName, value: &str) -> Result<(), DocumentError> {
if self.parentless && self.records.is_empty() {
let xml_base = is_xml_base(name.namespace.as_deref(), &name.local);
let id = self.intern(name)?;
let text = self.store_text(NodeText::Copy(value))?;
self.records
.push(Record::new(NodeKind::Attribute, id, NONE, NONE, 0, text));
self.has_xml_base |= xml_base;
return Ok(());
}
let element = self.top();
let has_content = self.last_child.last().is_some_and(|&l| l != NONE);
if element == NONE
|| self.records[element as usize].kind != NodeKind::Element
|| has_content
{
return Err(DocumentError::new(format!(
"attribute {name} outside an element start"
)));
}
let first = element as usize + 1;
let existing = (first..self.records.len()).find(|&i| {
let id = self.records[i].name;
self.names[id as usize].same_expanded_name(&name)
});
let xml_base = is_xml_base(name.namespace.as_deref(), &name.local);
let id = self.intern(name)?;
if let Some(i) = existing {
let (text_start, text_len) = self.store_text(NodeText::Copy(value))?;
let record = &mut self.records[i];
record.name = id;
record.text_start = text_start;
record.text_len = text_len;
self.has_xml_base |= xml_base;
return Ok(());
}
self.push_attribute(id, xml_base, NodeText::Copy(value))
}
fn push_attribute(
&mut self,
name: u32,
xml_base: bool,
value: NodeText<'_>,
) -> Result<(), DocumentError> {
let element = self.top();
let index = to_u32(self.records.len())?;
to_u32(self.records.len() + 1)?;
let previous = if index == element + 1 {
NONE
} else {
index - 1
};
let text = self.store_text(value)?;
self.records.push(Record::new(
NodeKind::Attribute,
name,
element,
previous,
index,
text,
));
self.records[element as usize].first_child = index + 1;
self.has_xml_base |= xml_base;
Ok(())
}
pub fn text(&mut self, value: &str) -> Result<(), DocumentError> {
if value.is_empty() {
if self.parentless && self.records.is_empty() {
return self
.append(NodeKind::Text, NONE, NodeText::Copy(""))
.map(drop);
}
return Ok(());
}
let last = self.last_child.last().copied().unwrap_or(NONE);
let mergeable = last != NONE
&& last as usize + 1 == self.records.len()
&& self.records[last as usize].kind == NodeKind::Text
&& {
let r = &self.records[last as usize];
r.text_start as usize + r.text_len as usize == self.source_len + self.text.len()
&& r.text_start as usize >= self.source_len
};
if mergeable {
self.store_text(NodeText::Copy(value))?;
let record = &mut self.records[last as usize];
record.text_len = to_u32(record.text_len as usize + value.len())?;
return Ok(());
}
self.append(NodeKind::Text, NONE, NodeText::Copy(value))
.map(drop)
}
pub fn comment(&mut self, value: &str) -> Result<(), DocumentError> {
self.append(NodeKind::Comment, NONE, NodeText::Copy(value))
.map(drop)
}
pub fn processing_instruction(
&mut self,
target: &str,
value: &str,
) -> Result<(), DocumentError> {
let name = self.intern(QName::local(target))?;
self.append(NodeKind::ProcessingInstruction, name, NodeText::Copy(value))
.map(drop)
}
pub fn end_element(&mut self) -> Result<(), DocumentError> {
if self.open.len() <= self.base() {
return Err(DocumentError::new("end of element without a start"));
}
let element = self.top();
self.open.pop();
self.last_child.pop();
let end = to_u32(self.records.len())?;
self.records[element as usize].end = end;
Ok(())
}
pub fn finish(
self,
document_uri: Option<String>,
base_uri: Option<String>,
) -> Result<Rc<Document>, DocumentError> {
self.finish_with_source(String::new(), document_uri, base_uri)
}
fn finish_with_source(
mut self,
source: String,
document_uri: Option<String>,
base_uri: Option<String>,
) -> Result<Rc<Document>, DocumentError> {
if self.open.len() != self.base() {
return Err(DocumentError::new("unclosed element"));
}
if source.len() != self.source_len {
return Err(DocumentError::new("source text of another length"));
}
if self.parentless {
if self.records.is_empty() {
return Err(DocumentError::new("a parentless tree without a node"));
}
} else {
self.records[0].end = to_u32(self.records.len())?;
}
self.records.shrink_to_fit();
self.names.shrink_to_fit();
self.text.shrink_to_fit();
Ok(Rc::new(Document {
serial: NEXT_SERIAL.fetch_add(1, AtomicOrdering::Relaxed),
uri: document_uri,
base_uri,
has_xml_base: self.has_xml_base,
records: self.records,
names: self.names,
namespaces: self.namespaces,
source,
text: self.text,
elements: OnceCell::new(),
searches: Cell::new(0),
view: self.view,
ids: OnceCell::new(),
}))
}
}
fn alpha_key(value: u32) -> String {
if value < 1 {
return "a".to_owned();
}
let digits = value.to_string();
let letter = char::from(b'a' + u8::try_from(digits.len()).unwrap_or(10));
format!("{letter}{digits}")
}
fn is_xml_base(namespace: Option<&str>, local: &str) -> bool {
local == "base" && namespace == Some(XML_NAMESPACE)
}
fn offset_in(input: &str, part: &str) -> Option<usize> {
let start = part.as_ptr().addr().checked_sub(input.as_ptr().addr())?;
(start.checked_add(part.len())? <= input.len()).then_some(start)
}
fn xml_error(e: &einvoice_xml::Error) -> DocumentError {
DocumentError::new(format!("XML: {e}"))
}
struct Namespaces<'a> {
input: &'a str,
ids: HashMap<Box<str>, u32>,
last: Option<(usize, usize, u32)>,
}
impl<'a> Namespaces<'a> {
fn new(input: &'a str) -> Self {
Self {
input,
ids: HashMap::new(),
last: None,
}
}
fn id(&mut self, namespace: Option<&str>) -> Result<u32, DocumentError> {
let Some(namespace) = namespace else {
return Ok(NONE);
};
let at = offset_in(self.input, namespace);
if let (Some(at), Some((last, len, id))) = (at, self.last)
&& at == last
&& namespace.len() == len
{
return Ok(id);
}
let id = if let Some(&id) = self.ids.get(namespace) {
id
} else {
let id = to_u32(self.ids.len())?;
self.ids.insert(namespace.into(), id);
id
};
if let Some(at) = at {
self.last = Some((at, namespace.len(), id));
}
Ok(id)
}
}
type NameKey<'a> = (&'a str, &'a str, u32);
struct Names<'a> {
namespaces: Namespaces<'a>,
ids: HashMap<NameKey<'a>, u32>,
}
impl<'a> Names<'a> {
fn new(input: &'a str) -> Self {
Self {
namespaces: Namespaces::new(input),
ids: HashMap::new(),
}
}
fn id(
&mut self,
builder: &mut TreeBuilder,
name: einvoice_xml::Name<'_, 'a>,
) -> Result<u32, DocumentError> {
let key = (name.prefix, name.local, self.namespaces.id(name.namespace)?);
if let Some(&id) = self.ids.get(&key) {
return Ok(id);
}
let id = builder.push_name(QName {
prefix: (!name.prefix.is_empty()).then(|| name.prefix.to_owned()),
namespace: name.namespace.map(str::to_owned),
local: name.local.to_owned(),
})?;
self.ids.insert(key, id);
Ok(id)
}
}
trait Pass<'a> {
fn start(&mut self, start: einvoice_xml::StartElement<'_, 'a>) -> Result<(), DocumentError>;
fn end(&mut self) -> Result<(), DocumentError>;
fn text(&mut self, text: &str) -> Result<(), DocumentError>;
fn comment(&mut self, value: &str) -> Result<(), DocumentError>;
fn processing_instruction(
&mut self,
pi: &einvoice_xml::ProcessingInstruction<'a>,
) -> Result<(), DocumentError>;
}
fn read<'a>(input: &'a str, max_depth: u32, pass: &mut impl Pass<'a>) -> Result<(), DocumentError> {
let mut reader = einvoice_xml::Reader::new(input);
let mut depth = 0u32;
let mut text: Option<Cow<'a, str>> = None;
let mut too_deep: Option<LimitExceeded> = None;
while let Some(event) = reader.next_event().map_err(|e| xml_error(&e))? {
if let einvoice_xml::Event::Text(t) = event {
if depth > 0 && too_deep.is_none() {
match &mut text {
None => text = Some(t.value),
Some(joined) => joined.to_mut().push_str(&t.value),
}
}
continue;
}
if let Some(joined) = text.take()
&& !joined.is_empty()
{
pass.text(&joined)?;
}
match event {
einvoice_xml::Event::Start(start) => {
depth = depth.saturating_add(1);
if depth > max_depth && too_deep.is_none() {
let at = einvoice_xml::TextPos::at(input, start.range().start);
too_deep = Some(LimitExceeded::Depth {
max: max_depth,
line: at.row,
column: at.col,
});
}
if too_deep.is_none() {
pass.start(start)?;
}
}
einvoice_xml::Event::End(_) => {
depth = depth.saturating_sub(1);
if too_deep.is_none() {
pass.end()?;
}
}
einvoice_xml::Event::Comment(comment) if too_deep.is_none() => {
pass.comment(&comment.value)?;
}
einvoice_xml::Event::ProcessingInstruction(pi) if too_deep.is_none() => {
pass.processing_instruction(&pi)?;
}
_ => {}
}
}
match too_deep {
Some(limit) => Err(DocumentError::limit_exceeded(limit)),
None => Ok(()),
}
}
struct Sizes<'a> {
input: &'a str,
keep_source: bool,
nodes: usize,
text_bytes: usize,
}
impl Sizes<'_> {
fn add(&mut self, text: &str) {
self.nodes += 1;
if !(self.keep_source && offset_in(self.input, text).is_some()) {
self.text_bytes += text.len();
}
}
}
impl<'a> Pass<'a> for Sizes<'a> {
fn start(&mut self, start: einvoice_xml::StartElement<'_, 'a>) -> Result<(), DocumentError> {
self.nodes += 1;
for attribute in start.attributes() {
self.add(attribute.value);
}
Ok(())
}
fn end(&mut self) -> Result<(), DocumentError> {
Ok(())
}
fn text(&mut self, text: &str) -> Result<(), DocumentError> {
self.add(text);
Ok(())
}
fn comment(&mut self, value: &str) -> Result<(), DocumentError> {
self.add(value);
Ok(())
}
fn processing_instruction(
&mut self,
pi: &einvoice_xml::ProcessingInstruction<'a>,
) -> Result<(), DocumentError> {
self.add(pi.value.as_deref().unwrap_or(""));
Ok(())
}
}
struct Fill<'a> {
input: &'a str,
keep_source: bool,
builder: TreeBuilder,
names: Names<'a>,
}
impl Fill<'_> {
fn node_text<'s>(&self, text: &'s str) -> Result<NodeText<'s>, DocumentError> {
Ok(
match self
.keep_source
.then(|| offset_in(self.input, text))
.flatten()
{
Some(start) => NodeText::Source(to_u32(start)?, to_u32(text.len())?),
None => NodeText::Copy(text),
},
)
}
}
impl<'a> Pass<'a> for Fill<'a> {
fn start(&mut self, start: einvoice_xml::StartElement<'_, 'a>) -> Result<(), DocumentError> {
let id = self.names.id(&mut self.builder, start.name())?;
self.builder.open_element(id)?;
let declarations = start.namespace_declarations();
if declarations.len() > 0 {
let bindings: Arc<[NamespaceBinding]> = declarations
.map(|(prefix, uri)| NamespaceBinding {
prefix: (!prefix.is_empty()).then(|| prefix.to_owned()),
uri: uri.to_owned(),
})
.collect();
let element = self.builder.top();
self.builder.namespaces.push((element, bindings));
}
for attribute in start.attributes() {
let id = self.names.id(&mut self.builder, attribute.name)?;
let text = self.node_text(attribute.value)?;
self.builder.push_attribute(
id,
is_xml_base(attribute.name.namespace, attribute.name.local),
text,
)?;
}
Ok(())
}
fn end(&mut self) -> Result<(), DocumentError> {
self.builder.end_element()
}
fn text(&mut self, text: &str) -> Result<(), DocumentError> {
let text = self.node_text(text)?;
self.builder.append(NodeKind::Text, NONE, text).map(drop)
}
fn comment(&mut self, value: &str) -> Result<(), DocumentError> {
let text = self.node_text(value)?;
self.builder.append(NodeKind::Comment, NONE, text).map(drop)
}
fn processing_instruction(
&mut self,
pi: &einvoice_xml::ProcessingInstruction<'a>,
) -> Result<(), DocumentError> {
let target = einvoice_xml::Name {
prefix: "",
local: pi.target,
namespace: None,
};
let id = self.names.id(&mut self.builder, target)?;
let text = self.node_text(pi.value.as_deref().unwrap_or(""))?;
self.builder
.append(NodeKind::ProcessingInstruction, id, text)
.map(drop)
}
}
fn build_parsed(
input: &str,
keep_source: bool,
max_depth: u32,
) -> Result<TreeBuilder, DocumentError> {
let mut sizes = Sizes {
input,
keep_source,
nodes: 1,
text_bytes: 0,
};
read(input, max_depth, &mut sizes)?;
let mut fill = Fill {
input,
keep_source,
builder: TreeBuilder::with_capacity(
sizes.nodes,
sizes.text_bytes,
if keep_source { input.len() } else { 0 },
),
names: Names::new(input),
};
read(input, max_depth, &mut fill)?;
Ok(fill.builder)
}
#[must_use]
pub fn file_uri(path: &Path) -> Option<String> {
if !path.is_absolute() {
return None;
}
let text = path.to_str()?.replace('\\', "/");
let mut uri = String::from("file:");
if !text.starts_with('/') {
uri.push('/');
}
for c in text.chars() {
let keep = if c.is_ascii() {
c.is_ascii_alphanumeric() || "-_.!~*'():@&=+$,;/".contains(c)
} else {
!(matches!(
c,
'\u{A0}' | '\u{1680}' | '\u{2000}'
..='\u{200A}' | '\u{2028}' | '\u{2029}' | '\u{202F}' | '\u{205F}' | '\u{3000}'
) || c.is_control())
};
if keep {
uri.push(c);
} else {
let mut buffer = [0; 4];
for byte in c.encode_utf8(&mut buffer).bytes() {
let _ = write!(uri, "%{byte:02X}");
}
}
}
Some(uri)
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(xml: &str) -> Rc<Document> {
Document::parse(xml, Some("file:/t.xml")).unwrap()
}
fn describe(node: &Node) -> String {
match node.kind() {
NodeKind::Document => "/".to_owned(),
NodeKind::Element => node.name().unwrap().to_string(),
NodeKind::Attribute => format!("@{}", node.name().unwrap()),
NodeKind::Text => format!("{:?}", node.string_value()),
NodeKind::Comment => format!("<!--{}-->", node.string_value()),
NodeKind::ProcessingInstruction => format!("<?{}?>", node.name().unwrap()),
}
}
fn walk(node: &Node, axis: NodeAxis) -> Vec<String> {
node.axis(axis).map(|n| describe(&n)).collect()
}
fn find(tree: &Rc<Document>, local: &str) -> Node {
tree.root()
.axis(NodeAxis::Descendant)
.find(|n| n.name().is_some_and(|name| name.local == local))
.unwrap()
}
const XML: &str = "<?xml version='1.0'?><!--c0--><r xmlns:p='urn:p' a='1' p:b='2'> \
<x>1<y q='3'>2</y>3</x><p:z/><!--c1--><?pi data?><w><v/></w></r>";
const NONE: [&str; 0] = [];
#[test]
fn document_order_and_axes() {
let tree = parse(XML);
let root = tree.root();
assert_eq!(walk(&root, NodeAxis::Child), ["<!--c0-->", "r"]);
let top = find(&tree, "r");
assert_eq!(walk(&top, NodeAxis::Attribute), ["@a", "@p:b"]);
assert_eq!(
walk(&top, NodeAxis::Child),
["\" \"", "x", "p:z", "<!--c1-->", "<?pi?>", "w"]
);
assert_eq!(
walk(&top, NodeAxis::Descendant),
[
"\" \"",
"x",
"\"1\"",
"y",
"\"2\"",
"\"3\"",
"p:z",
"<!--c1-->",
"<?pi?>",
"w",
"v"
]
);
let y = find(&tree, "y");
assert_eq!(walk(&y, NodeAxis::Ancestor), ["x", "r", "/"]);
assert_eq!(walk(&y, NodeAxis::AncestorOrSelf), ["y", "x", "r", "/"]);
assert_eq!(walk(&y, NodeAxis::PrecedingSibling), ["\"1\""]);
assert_eq!(walk(&y, NodeAxis::FollowingSibling), ["\"3\""]);
assert_eq!(
walk(&y, NodeAxis::Preceding),
["\"1\"", "\" \"", "<!--c0-->"]
);
let v = find(&tree, "v");
assert_eq!(
walk(&v, NodeAxis::Preceding),
[
"<?pi?>",
"<!--c1-->",
"p:z",
"\"3\"",
"\"2\"",
"y",
"\"1\"",
"x",
"\" \"",
"<!--c0-->"
]
);
let q = y.attributes().next().unwrap();
assert_eq!(walk(&q, NodeAxis::Parent), ["y"]);
assert_eq!(
walk(&q, NodeAxis::Preceding),
["\"1\"", "\" \"", "<!--c0-->"]
);
assert_eq!(walk(&q, NodeAxis::FollowingSibling), NONE);
assert_eq!(walk(&q, NodeAxis::PrecedingSibling), NONE);
assert_eq!(walk(&q, NodeAxis::DescendantOrSelf), ["@q"]);
assert_eq!(walk(&q, NodeAxis::Child), NONE);
let all: Vec<Node> = root.axis(NodeAxis::DescendantOrSelf).collect();
assert!(
all.windows(2)
.all(|w| w[0].cmp_document_order(&w[1]) == Ordering::Less)
);
}
#[test]
fn values_names_and_prefixes() {
let tree = parse(XML);
let top = find(&tree, "r");
assert_eq!(top.string_value(), " 123");
assert_eq!(find(&tree, "x").string_value(), "123");
assert_eq!(find(&tree, "y").string_value(), "2");
assert_eq!(find(&tree, "w").string_value(), "");
let z = find(&tree, "z");
assert_eq!(z.name().unwrap().to_string(), "p:z");
assert_eq!(z.name().unwrap().namespace.as_deref(), Some("urn:p"));
let prefixed = top.attributes().nth(1).unwrap();
assert_eq!(prefixed.name().unwrap().prefix.as_deref(), Some("p"));
assert!(matches!(prefixed.typed_value(), AtomicValue::UntypedAtomic(s) if s == "2"));
let comment = tree.root().children().next().unwrap();
assert!(matches!(comment.typed_value(), AtomicValue::String(s) if s == "c0"));
assert_eq!(tree.document_uri(), None);
assert_eq!(tree.base_uri(), Some("file:/t.xml"));
let loaded = Document::parse_loaded("<a/>", "file:/t.xml").unwrap();
assert_eq!(loaded.document_uri(), Some("file:/t.xml"));
assert_eq!(loaded.base_uri(), Some("file:/t.xml"));
assert_eq!(z.root(), tree.root());
assert_ne!(z, top);
}
#[test]
fn undeclared_default_namespace_is_no_namespace() {
let tree = parse("<r xmlns='urn:d'><n xmlns=''><m/></n></r>");
assert_eq!(
find(&tree, "r").name().unwrap().namespace.as_deref(),
Some("urn:d")
);
assert_eq!(find(&tree, "n").name().unwrap().namespace, None);
assert_eq!(find(&tree, "m").name().unwrap().namespace, None);
}
#[test]
fn same_namespace_two_prefixes() {
let tree = parse("<a:r xmlns:a='urn:x' xmlns:b='urn:x'><b:c a:d='1'/></a:r>");
let inner = find(&tree, "c");
assert_eq!(inner.name().unwrap().to_string(), "b:c");
assert_eq!(find(&tree, "r").name().unwrap().to_string(), "a:r");
let attribute = inner.attributes().next().unwrap();
assert_eq!(attribute.name().unwrap().to_string(), "a:d");
}
#[test]
fn builder_merges_text_and_replaces_attributes() {
let mut builder = TreeBuilder::new();
builder.start_element(QName::local("e")).unwrap();
builder.attribute(QName::local("a"), "1").unwrap();
builder.attribute(QName::local("a"), "2").unwrap();
builder.text("x").unwrap();
builder.text("").unwrap();
builder.text("y").unwrap();
assert!(builder.attribute(QName::local("late"), "1").is_err());
builder.end_element().unwrap();
assert!(builder.end_element().is_err());
let tree = builder.finish(None, None).unwrap();
let element = tree.root().children().next().unwrap();
let attributes: Vec<_> = element
.attributes()
.map(|a| a.string_value().into_owned())
.collect();
assert_eq!(attributes, ["2"]);
let texts: Vec<_> = element
.children()
.map(|t| t.string_value().into_owned())
.collect();
assert_eq!(texts, ["xy"]);
}
#[test]
fn whitespace_text_kept_and_dtd_refused() {
let tree = parse("<r>\n <a/>\n</r>\n");
let top = tree.root().children().next().unwrap();
assert_eq!(top.children().count(), 3);
assert!(Document::parse("<!DOCTYPE r [<!ENTITY e 'x'>]><r>&e;</r>", None).is_err());
assert!(Document::parse("<r><a></r>", None).is_err());
}
#[test]
fn adjacent_text_is_one_node() {
let text = "<r>a<![CDATA[<b>]]>c&d<!--x-->e\r\nf<s/></r>";
for tree in [parse(text), Document::parse(text.to_owned(), None).unwrap()] {
let top = tree.root().children().next().unwrap();
let children: Vec<_> = top
.children()
.map(|t| (t.kind(), t.string_value().into_owned()))
.collect();
assert_eq!(
children,
[
(NodeKind::Text, "a<b>c&d".to_owned()),
(NodeKind::Comment, "x".to_owned()),
(NodeKind::Text, "e\nf".to_owned()),
(NodeKind::Element, String::new()),
]
);
}
let tree = parse("<r><!--a\r\nb\rc--><?p a\r\nb?></r>");
let values: Vec<_> = tree
.root()
.children()
.next()
.unwrap()
.children()
.map(|n| n.string_value().into_owned())
.collect();
assert_eq!(values, ["a\nb\nc", "a\nb"]);
assert_eq!(parse("<r>x\r&y</r>").root().string_value(), "x\n&y");
}
#[test]
fn errors_are_those_of_the_reader() {
let error = Document::parse("<r>\n<a></r>", None).unwrap_err();
assert_eq!(error.message(), "XML: expected 'a' tag, not 'r' at 2:4");
assert_eq!(error.limit(), None);
let error = Document::parse("<!DOCTYPE r><r/>", None).unwrap_err();
assert_eq!(error.message(), "XML: XML with DTD detected");
}
#[test]
fn records_are_compact() {
assert_eq!(std::mem::size_of::<Record>(), 32);
}
fn dump(tree: &Rc<Document>) -> Vec<String> {
(0..tree.node_count())
.map(|i| {
let node = Node::new(Rc::clone(tree), u32::try_from(i).unwrap());
let name = node.name().map(|n| {
format!(
"{}{{{}}}{}",
n,
n.namespace.as_deref().unwrap_or(""),
n.local
)
});
format!(
"{:?} {name:?} {:?} {:?}",
node.kind(),
node.string_value(),
node.parent().map(|p| p.index())
)
})
.collect()
}
#[test]
fn owned_source_is_referred_to() {
let xml = "<?xml version='1.0'?><!--top--><?pi top data?>\
<p:r xmlns:p='urn:p' xmlns='urn:d' a='plain' b='x&y' c='t\tu'>\
verbatim<e p:f='1'>aAb</e>line\r\nend<![CDATA[<&>]]>more<!--c-->\
<?pi in?><e/>tail</p:r>";
let borrowed = Document::parse(xml, None).unwrap();
let owned = Document::parse(xml.to_owned(), None).unwrap();
assert_eq!(dump(&borrowed), dump(&owned));
assert_eq!(borrowed.source, "");
assert_eq!(owned.source, xml);
assert_eq!(owned.text, "x&yt uaAbline\nend<&>more");
let copied_whole: usize = (0..borrowed.node_count())
.map(|i| {
let i = u32::try_from(i).unwrap();
match borrowed.kind(i) {
NodeKind::Element | NodeKind::Document => 0,
_ => borrowed.own_text(i).len(),
}
})
.sum();
assert_eq!(borrowed.text.len(), copied_whole);
for tree in [&borrowed, &owned] {
assert_eq!(tree.records.capacity(), tree.records.len());
assert_eq!(tree.text.capacity(), tree.text.len());
assert_eq!(tree.names.capacity(), tree.names.len());
}
let r = find(&owned, "r");
let values: Vec<_> = r
.attributes()
.map(|a| a.string_value().into_owned())
.collect();
assert_eq!(values, ["plain", "x&y", "t u"]);
assert_eq!(r.string_value(), "verbatimaAbline\nend<&>moretail");
assert_eq!(r.name().unwrap().to_string(), "p:r");
let e = find(&owned, "e");
assert_eq!(e.name().unwrap().namespace.as_deref(), Some("urn:d"));
let f = e.attributes().next().unwrap();
assert_eq!(f.name().unwrap().to_string(), "p:f");
assert_eq!(f.name().unwrap().namespace.as_deref(), Some("urn:p"));
assert_eq!(owned.names.iter().filter(|n| n.local == "e").count(), 1);
}
#[test]
fn built_trees_merge_text_after_a_parsed_shape() {
let mut builder = TreeBuilder::new();
builder.start_element(QName::local("e")).unwrap();
builder.attribute(QName::local("a"), "v").unwrap();
builder.comment("c").unwrap();
builder.text("x").unwrap();
builder.text("y").unwrap();
builder.processing_instruction("t", "d").unwrap();
builder.text("z").unwrap();
builder.end_element().unwrap();
let tree = builder.finish(None, None).unwrap();
let e = tree.root().children().next().unwrap();
let children: Vec<_> = e
.children()
.map(|c| (c.kind(), c.string_value().into_owned()))
.collect();
assert_eq!(
children,
[
(NodeKind::Comment, "c".to_owned()),
(NodeKind::Text, "xy".to_owned()),
(NodeKind::ProcessingInstruction, "d".to_owned()),
(NodeKind::Text, "z".to_owned()),
]
);
assert_eq!(tree.text, "vcxydz");
}
#[test]
fn the_document_cache_parses_a_text_once() {
fn shape(doc: &Rc<Document>) -> Vec<String> {
doc.root()
.axis(NodeAxis::Descendant)
.flat_map(|n| std::iter::once(n.clone()).chain(n.attributes()))
.map(|n| format!("{} {}", describe(&n), n.string_value()))
.collect()
}
let cache = DocumentCache::new();
let xml = "<cl id='1'><e v='a'/>text & more<e v='b'/></cl>";
let uri = "file:/codes.xml";
let first = cache.parse_loaded(xml.to_owned(), uri, 8).unwrap();
let between = parse("<x/>");
let second = cache.parse_loaded(xml.to_owned(), uri, 8).unwrap();
assert_eq!(shape(&first), shape(&second));
assert_eq!(second.document_uri(), Some(uri));
assert_eq!(second.base_uri(), Some(uri));
assert_eq!(second.node_count(), first.node_count());
assert_ne!(first.root(), second.root());
assert_eq!(
between.root().cmp_document_order(&second.root()),
Ordering::Less
);
assert_eq!(
first.root().cmp_document_order(&between.root()),
Ordering::Less
);
assert_eq!(cache.entries.lock().unwrap().len(), 1);
let other = cache.parse_loaded("<cl/>".to_owned(), uri, 8).unwrap();
assert_eq!(other.node_count(), 2);
let elsewhere = cache
.parse_loaded(xml.to_owned(), "file:/other.xml", 8)
.unwrap();
assert_eq!(elsewhere.document_uri(), Some("file:/other.xml"));
assert_eq!(shape(&elsewhere), shape(&first));
let deeper = cache.parse_loaded(xml.to_owned(), uri, 1).unwrap_err();
assert!(deeper.limit().is_some());
assert_eq!(cache.entries.lock().unwrap().len(), 3);
assert!(cache.parse_loaded("<cl>".to_owned(), uri, 8).is_err());
assert_eq!(cache.entries.lock().unwrap().len(), 3);
}
#[test]
fn the_element_index_lists_the_elements_of_a_name_in_document_order() {
let mut xml = String::from(r#"<r xmlns:a="urn:x" xmlns:b="urn:x" xmlns="urn:x">"#);
for i in 0..40 {
let prefix = ["a:", "b:", ""][i % 3];
write!(xml, r#"<{prefix}e n="{i}"><f/><e/></{prefix}e><g>t</g>"#).unwrap();
}
xml.push_str("</r>");
let doc = parse(&xml);
let scan = |start: u32, end: u32| -> Vec<u32> {
(start..end)
.filter(|&i| {
doc.kind(i) == NodeKind::Element
&& doc.name(i).is_some_and(|n| {
n.local == "e" && n.namespace.as_deref() == Some("urn:x")
})
})
.collect()
};
let (first, end) = doc.descendant_range(0);
assert_eq!(doc.elements_named(Some("urn:x"), "e", first, end), None);
let all = doc.elements_named(Some("urn:x"), "e", first, end).unwrap();
assert_eq!(all, scan(first, end).as_slice());
assert_eq!(all.len(), 80);
let inner = doc.elements_named(Some("urn:x"), "e", 10, 200).unwrap();
assert_eq!(inner, scan(10, 200).as_slice());
assert_eq!(doc.elements_named(None, "e", first, end), Some(&[][..]));
assert_eq!(
doc.elements_named(Some("urn:x"), "zz", first, end),
Some(&[][..])
);
assert_eq!(doc.elements_named(Some("urn:x"), "e", 10, 20), None);
}
#[test]
fn file_uris() {
if cfg!(windows) {
return;
}
assert_eq!(
file_uri(Path::new("/a b/c,d%é#[1]\u{a0}.xml")).as_deref(),
Some("file:/a%20b/c,d%25é%23%5B1%5D%C2%A0.xml")
);
assert_eq!(file_uri(Path::new("rel.xml")), None);
}
}