f-office-xml 0.3.0

Minimal read-only XML tree for Office Open XML and SVG parts.
Documentation
//! Whether two nodes, of one document or of two, hold the same XML ([`Document::same_tree`]).

use crate::{Attribute, Document, Element, Name, NodeId, NodeKind};

#[cfg(test)]
mod tests;

/// How [`Document::same_tree`] compares two nodes. Both compare text, comments and processing
/// instructions exactly, as the reader holds them, and the children in order.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Equality {
    /// As written: each name with its prefix, local name and namespace URI, and the attributes
    /// in order, the namespace declarations among them. The bytes of an element's span read
    /// back so.
    AsWritten,
    /// By meaning: each name by its namespace URI and local name, or by its prefix and local
    /// name when it has no namespace; the attributes in any order, without the namespace
    /// declarations. Two spellings of one element, with other prefixes, quotes or attribute
    /// order, compare so as equal.
    ByNamespace,
}

impl Document {
    /// Whether the node `id` of this document and the node `other_id` of `other` hold the same
    /// XML under `equality`, with all their content.
    #[must_use]
    pub fn same_tree(
        &self,
        id: NodeId,
        (other, other_id): (&Self, NodeId),
        equality: Equality,
    ) -> bool {
        let kinds_match = match (self.kind(id), other.kind(other_id)) {
            (Some(NodeKind::Element(left)), Some(NodeKind::Element(right))) => {
                same_element((self, left), (other, right), equality)
            }
            (Some(NodeKind::Text(left)), Some(NodeKind::Text(right)))
            | (Some(NodeKind::Other(left)), Some(NodeKind::Other(right))) => left == right,
            _ => false,
        };
        let (children, other_children) = (self.children(id), other.children(other_id));
        kinds_match
            && children.len() == other_children.len()
            && children
                .iter()
                .zip(other_children)
                .all(|(child, other_child)| self.same_tree(*child, (other, *other_child), equality))
    }
}

/// Whether two elements have the same name and attributes under `equality`.
fn same_element(
    (a, left): (&Document, Element<'_>),
    (b, right): (&Document, Element<'_>),
    equality: Equality,
) -> bool {
    let names = |left: &Name<'_>, right: &Name<'_>| same_name((a, left), (b, right), equality);
    match equality {
        Equality::AsWritten => {
            names(&left.name, &right.name)
                && left.attributes.len() == right.attributes.len()
                && left
                    .attributes
                    .iter()
                    .zip(right.attributes.iter())
                    .all(|(l, r)| names(&l.name, &r.name) && l.value == r.value)
        }
        Equality::ByNamespace => {
            let (left_attributes, right_attributes) = (plain(left), plain(right));
            names(&left.name, &right.name)
                && left_attributes.len() == right_attributes.len()
                && left_attributes.iter().all(|l| {
                    right_attributes
                        .iter()
                        .any(|r| names(&l.name, &r.name) && l.value == r.value)
                })
        }
    }
}

/// Whether two names are the same under `equality`.
fn same_name(
    (a, left): (&Document, &Name<'_>),
    (b, right): (&Document, &Name<'_>),
    equality: Equality,
) -> bool {
    let left_uri = left.namespace.map(|ns| a.namespace_uri(ns));
    let right_uri = right.namespace.map(|ns| b.namespace_uri(ns));
    let prefixes_count = equality == Equality::AsWritten || left_uri.is_none();
    left.local == right.local
        && left_uri == right_uri
        && (!prefixes_count || left.prefix == right.prefix)
}

/// The attributes of `element` that declare no namespace.
fn plain(element: Element<'_>) -> Vec<Attribute<'_>> {
    element
        .attributes
        .iter()
        .filter(|attribute| !declares(attribute))
        .collect()
}

/// Whether an attribute declares a namespace: `xmlns` or `xmlns:p`.
fn declares(attribute: &Attribute<'_>) -> bool {
    match attribute.name.prefix {
        Some(prefix) => prefix == "xmlns",
        None => attribute.name.local == "xmlns",
    }
}