#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NodeId(pub(crate) usize);
impl NodeId {
pub const DOCUMENT: NodeId = NodeId(0);
pub fn from_raw(v: u32) -> Self {
Self(v as usize)
}
pub fn to_raw(self) -> u32 {
self.0 as u32
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QualName {
pub ns: Option<String>,
pub local: String,
}
impl QualName {
pub fn new(local: impl Into<String>) -> Self {
Self {
ns: None,
local: local.into(),
}
}
pub fn with_ns(ns: impl Into<String>, local: impl Into<String>) -> Self {
Self {
ns: Some(ns.into()),
local: local.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Attribute {
pub name: QualName,
pub value: String,
}
#[derive(Debug, Clone)]
pub enum NodeData {
Document,
DocumentType {
name: String,
public_id: String,
system_id: String,
},
Element(ElementData),
Text(String),
Comment(String),
ProcessingInstruction {
target: String,
data: String,
},
DocumentFragment,
ShadowRoot {
mode: ShadowRootMode,
host: NodeId,
},
}
#[derive(Debug, Clone)]
pub struct ElementData {
pub name: QualName,
pub attrs: Vec<Attribute>,
pub shadow_root: Option<NodeId>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShadowRootMode {
Open,
Closed,
}
#[derive(Debug, Clone)]
pub struct Node {
pub id: NodeId,
pub data: NodeData,
pub parent: Option<NodeId>,
pub first_child: Option<NodeId>,
pub last_child: Option<NodeId>,
pub prev_sibling: Option<NodeId>,
pub next_sibling: Option<NodeId>,
}
impl Node {
pub fn new(id: NodeId, data: NodeData) -> Self {
Self {
id,
data,
parent: None,
first_child: None,
last_child: None,
prev_sibling: None,
next_sibling: None,
}
}
pub fn is_element(&self) -> bool {
matches!(self.data, NodeData::Element(_))
}
pub fn as_element(&self) -> Option<&ElementData> {
match &self.data {
NodeData::Element(data) => Some(data),
_ => None,
}
}
pub fn as_element_mut(&mut self) -> Option<&mut ElementData> {
match &mut self.data {
NodeData::Element(data) => Some(data),
_ => None,
}
}
pub fn as_text(&self) -> Option<&str> {
match &self.data {
NodeData::Text(t) => Some(t),
_ => None,
}
}
}