use std::cell::RefCell;
use std::fmt;
use std::ops::Deref;
use crate::tree::{Doctype, DocumentData, ElementData, Node, NodeRef};
impl NodeRef {
#[inline]
pub fn into_element_ref(self) -> Option<NodeDataRef<ElementData>> {
NodeDataRef::new_opt(self, Node::as_element)
}
#[inline]
pub fn into_text_ref(self) -> Option<NodeDataRef<RefCell<String>>> {
NodeDataRef::new_opt(self, Node::as_text)
}
#[inline]
pub fn into_comment_ref(self) -> Option<NodeDataRef<RefCell<String>>> {
NodeDataRef::new_opt(self, Node::as_comment)
}
#[inline]
pub fn into_doctype_ref(self) -> Option<NodeDataRef<Doctype>> {
NodeDataRef::new_opt(self, Node::as_doctype)
}
#[inline]
pub fn into_document_ref(self) -> Option<NodeDataRef<DocumentData>> {
NodeDataRef::new_opt(self, Node::as_document)
}
}
#[derive(Eq)]
pub struct NodeDataRef<T> {
_keep_alive: NodeRef,
_reference: *const T,
}
impl<T> NodeDataRef<T> {
#[inline]
pub fn new<F>(rc: NodeRef, f: F) -> NodeDataRef<T>
where
F: FnOnce(&Node) -> &T,
{
NodeDataRef {
_reference: f(&*rc),
_keep_alive: rc,
}
}
#[inline]
pub fn new_opt<F>(rc: NodeRef, f: F) -> Option<NodeDataRef<T>>
where
F: FnOnce(&Node) -> Option<&T>,
{
f(&*rc).map(|r| r as *const T).map(move |r| NodeDataRef {
_reference: r,
_keep_alive: rc,
})
}
#[inline]
pub fn as_node(&self) -> &NodeRef {
&self._keep_alive
}
}
impl<T> Deref for NodeDataRef<T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
unsafe { &*self._reference }
}
}
impl<T> PartialEq for NodeDataRef<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self._keep_alive == other._keep_alive
}
}
impl<T> Clone for NodeDataRef<T> {
#[inline]
fn clone(&self) -> Self {
NodeDataRef {
_keep_alive: self._keep_alive.clone(),
_reference: self._reference,
}
}
}
impl<T: fmt::Debug> fmt::Debug for NodeDataRef<T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
fmt::Debug::fmt(&**self, f)
}
}
impl NodeDataRef<ElementData> {
pub fn text_contents(&self) -> String {
self.as_node().text_contents()
}
}