use std::{
cell::RefCell,
collections::HashMap,
mem::replace,
rc::{Rc, Weak},
sync::Arc,
};
use super::{
DOMException, NodeType,
document::{Document, DocumentRef},
node::{Node, NodeConnection, NodeRef},
user_data::{DOMUserData, OperationType, UserDataHandler},
};
pub enum NotationIdentifier {
ExternalID {
public_id: Option<Rc<str>>,
system_id: Rc<str>,
},
PublicID {
public_id: Rc<str>,
},
}
pub struct Notation {
owner_document: Weak<RefCell<Document>>,
name: Rc<str>,
public_id: Option<Rc<str>>,
system_id: Option<Rc<str>>,
user_data: Option<HashMap<String, (DOMUserData, Option<Arc<dyn UserDataHandler>>)>>,
}
impl Notation {
pub fn owner_document(&self) -> Option<DocumentRef> {
self.owner_document.upgrade().map(From::from)
}
}
#[derive(Clone)]
pub struct NotationRef(
pub(super) Rc<RefCell<Notation>>,
pub(super) Option<DocumentRef>,
);
impl NotationRef {
pub(crate) fn new(doc: Option<DocumentRef>, name: Rc<str>, id: NotationIdentifier) -> Self {
let (public_id, system_id) = match id {
NotationIdentifier::ExternalID {
public_id,
system_id,
} => (public_id, Some(system_id)),
NotationIdentifier::PublicID { public_id } => (Some(public_id), None),
};
Self(
Rc::new(RefCell::new(Notation {
owner_document: doc
.as_ref()
.map(|doc| Rc::downgrade(&doc.0))
.unwrap_or_default(),
name,
public_id,
system_id,
user_data: None,
})),
doc.clone(),
)
}
pub fn name(&self) -> Rc<str> {
self.0.borrow().name.clone()
}
pub fn public_id(&self) -> Option<Rc<str>> {
self.0.borrow().public_id.clone()
}
pub fn set_public_id(&mut self, public_id: impl Into<Rc<str>>) {
self.0.borrow_mut().system_id = None;
self.0.borrow_mut().public_id = Some(public_id.into());
}
pub fn system_id(&self) -> Option<Rc<str>> {
self.0.borrow().system_id.clone()
}
pub fn set_external_id(
&mut self,
public_id: Option<impl Into<Rc<str>>>,
system_id: impl Into<Rc<str>>,
) {
self.0.borrow_mut().public_id = public_id.map(|id| id.into());
self.0.borrow_mut().system_id = Some(system_id.into());
}
}
impl Node for NotationRef {
fn node_name(&self) -> Rc<str> {
self.0.borrow().name.clone()
}
fn node_value(&self) -> Option<Rc<str>> {
None
}
fn set_node_value(&mut self, _: impl Into<String>) -> Result<(), DOMException> {
Ok(())
}
fn node_type(&self) -> NodeType {
NodeType::Notation
}
fn owner_document(&self) -> Option<DocumentRef> {
self.1.clone()
}
fn clone_node(&self, _deep: bool) -> NodeRef {
let notation = NotationRef(
Rc::new(RefCell::new(Notation {
owner_document: self.0.borrow().owner_document.clone(),
name: self.0.borrow().name.clone(),
public_id: self.0.borrow().public_id.clone(),
system_id: self.0.borrow().system_id.clone(),
user_data: None,
})),
self.1.clone(),
);
self.handle_user_data(OperationType::NodeCloned, Some(notation.clone().into()));
notation.into()
}
fn text_content(&self) -> Option<String> {
None
}
fn set_text_content(&mut self, _text: impl Into<String>) -> Result<(), DOMException> {
Ok(())
}
fn is_same_node(&self, other: &NodeRef) -> bool {
let NodeRef::Notation(other) = other else {
return false;
};
Rc::ptr_eq(&self.0, &other.0)
}
fn lookup_prefix(&self, _ns_uri: &str) -> Option<Rc<str>> {
None
}
fn is_default_namespace(&self, _ns_uri: &str) -> bool {
false
}
fn lookup_namespace_uri(&self, _prefix: Option<&str>) -> Option<Rc<str>> {
None
}
fn set_user_data(
&mut self,
key: impl Into<String>,
data: DOMUserData,
handler: Option<Arc<dyn UserDataHandler>>,
) -> Option<DOMUserData> {
self.0
.borrow_mut()
.user_data
.get_or_insert_default()
.insert(key.into(), (data, handler))
.map(|v| v.0)
}
fn get_user_data(&self, key: &str) -> Option<DOMUserData> {
self.0
.borrow()
.user_data
.as_ref()
.and_then(|user_data| user_data.get(key))
.map(|v| v.0.clone())
}
fn is_read_only(&self) -> bool {
true
}
}
impl NodeConnection for NotationRef {
fn set_parent_node(&mut self, _: Option<NodeRef>) -> Option<NodeRef> {
None
}
fn set_first_child(&mut self, _: Option<NodeRef>) -> Option<NodeRef> {
None
}
fn set_last_child(&mut self, _: Option<NodeRef>) -> Option<NodeRef> {
None
}
fn set_previous_sibling(&mut self, _: Option<NodeRef>) -> Option<NodeRef> {
None
}
fn set_next_sibling(&mut self, _: Option<NodeRef>) -> Option<NodeRef> {
None
}
fn set_owner_document(&mut self, new_doc: DocumentRef) -> Option<DocumentRef> {
self.0.borrow_mut().owner_document = Rc::downgrade(&new_doc.0);
replace(&mut self.1, Some(new_doc))
}
fn adopted_to(&mut self, _new_doc: DocumentRef) {
}
fn handle_user_data(&self, operation: OperationType, dst: Option<NodeRef>) {
if let Some(user_data) = self.0.borrow().user_data.as_ref() {
for (key, value) in user_data.iter() {
if let Some(handler) = value.1.clone() {
handler.handle(
operation,
key.as_str(),
value.0.clone(),
self.clone().into(),
dst.clone(),
);
}
}
}
}
}
impl From<NotationRef> for NodeRef {
fn from(value: NotationRef) -> Self {
NodeRef::Notation(value)
}
}
impl From<Rc<RefCell<Notation>>> for NotationRef {
fn from(value: Rc<RefCell<Notation>>) -> Self {
let doc = value.borrow().owner_document();
Self(value, doc)
}
}
impl From<NotationRef> for Rc<RefCell<Notation>> {
fn from(value: NotationRef) -> Self {
value.0
}
}