use std::collections::{HashMap, HashSet};
use std::fmt;
use std::rc::Rc;
pub mod virtual_node_test_utils;
use web_sys::{self, Element, EventTarget, Node, Text};
use wasm_bindgen::JsCast;
use wasm_bindgen::JsValue;
use lazy_static::lazy_static;
use std::iter::FromIterator;
use std::ops::Deref;
use std::option::Iter;
use std::sync::Mutex;
lazy_static! {
static ref ELEM_UNIQUE_ID: Mutex<u32> = Mutex::new(0);
static ref SELF_CLOSING_TAGS: HashSet<&'static str> = [
"area", "base", "br", "col", "hr", "img", "input", "link", "meta", "param", "command",
"keygen", "source",
]
.iter()
.cloned()
.collect();
}
#[derive(PartialEq)]
pub enum VirtualNode {
Element(VElement),
Text(VText),
}
#[derive(PartialEq)]
pub struct VElement {
pub tag: String,
pub attrs: HashMap<String, String>,
pub events: Events,
pub children: Vec<VirtualNode>,
}
#[derive(PartialEq)]
pub struct VText {
pub text: String,
}
impl VirtualNode {
pub fn element<S>(tag: S) -> Self
where
S: Into<String>,
{
VirtualNode::Element(VElement::new(tag))
}
pub fn text<S>(text: S) -> Self
where
S: Into<String>,
{
VirtualNode::Text(VText::new(text.into()))
}
pub fn as_velement_ref(&self) -> Option<&VElement> {
match self {
VirtualNode::Element(ref element_node) => Some(element_node),
_ => None,
}
}
pub fn as_velement_mut(&mut self) -> Option<&mut VElement> {
match self {
VirtualNode::Element(ref mut element_node) => Some(element_node),
_ => None,
}
}
pub fn as_vtext_ref(&self) -> Option<&VText> {
match self {
VirtualNode::Text(ref text_node) => Some(text_node),
_ => None,
}
}
pub fn as_vtext_mut(&mut self) -> Option<&mut VText> {
match self {
VirtualNode::Text(ref mut text_node) => Some(text_node),
_ => None,
}
}
pub fn create_dom_node(&self) -> CreatedNode<Node> {
match self {
VirtualNode::Text(text_node) => {
CreatedNode::without_closures(text_node.create_text_node())
}
VirtualNode::Element(element_node) => element_node.create_element_node().into(),
}
}
}
impl VElement {
pub fn new<S>(tag: S) -> Self
where
S: Into<String>,
{
VElement {
tag: tag.into(),
attrs: HashMap::new(),
events: Events(HashMap::new()),
children: vec![],
}
}
pub fn is_self_closing(&self) -> bool {
SELF_CLOSING_TAGS.contains(self.tag.as_str())
}
pub fn create_element_node(&self) -> CreatedNode<Element> {
let document = web_sys::window().unwrap().document().unwrap();
let element = document.create_element(&self.tag).unwrap();
let mut closures = HashMap::new();
self.attrs.iter().for_each(|(name, value)| {
element
.set_attribute(name, value)
.expect("Set element attribute in create element");
});
if self.events.0.len() > 0 {
let unique_id = create_unique_identifier();
element
.set_attribute("data-vdom-id".into(), &unique_id.to_string())
.expect("Could not set attribute on element");
closures.insert(unique_id, vec![]);
self.events.0.iter().for_each(|(onevent, callback)| {
let event = &onevent[2..];
let current_elem: &EventTarget = element.dyn_ref().unwrap();
current_elem
.add_event_listener_with_callback(
event,
callback.as_ref().as_ref().unchecked_ref(),
)
.unwrap();
closures
.get_mut(&unique_id)
.unwrap()
.push(Rc::clone(callback));
});
}
let mut previous_node_was_text = false;
self.children.iter().for_each(|child| {
match child {
VirtualNode::Text(text_node) => {
let current_node = element.as_ref() as &web_sys::Node;
if previous_node_was_text {
let separator = document.create_comment("ptns");
current_node
.append_child(separator.as_ref() as &web_sys::Node)
.unwrap();
}
current_node
.append_child(&text_node.create_text_node())
.unwrap();
previous_node_was_text = true;
}
VirtualNode::Element(element_node) => {
previous_node_was_text = false;
let child = element_node.create_element_node();
let child_elem: Element = child.node;
closures.extend(child.closures);
element.append_child(&child_elem).unwrap();
}
}
});
CreatedNode {
node: element,
closures,
}
}
}
impl VText {
pub fn new<S>(text: S) -> Self
where
S: Into<String>,
{
VText { text: text.into() }
}
pub fn create_text_node(&self) -> Text {
let document = web_sys::window().unwrap().document().unwrap();
document.create_text_node(&self.text)
}
}
pub struct CreatedNode<T> {
pub node: T,
pub closures: HashMap<u32, Vec<DynClosure>>,
}
impl<T> CreatedNode<T> {
pub fn without_closures<N: Into<T>>(node: N) -> Self {
CreatedNode {
node: node.into(),
closures: HashMap::with_capacity(0),
}
}
}
impl<T> Deref for CreatedNode<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.node
}
}
impl From<CreatedNode<Element>> for CreatedNode<Node> {
fn from(other: CreatedNode<Element>) -> CreatedNode<Node> {
CreatedNode {
node: other.node.into(),
closures: other.closures,
}
}
}
fn create_unique_identifier() -> u32 {
let mut elem_unique_id = ELEM_UNIQUE_ID.lock().unwrap();
*elem_unique_id += 1;
*elem_unique_id
}
pub struct IterableNodes(Vec<VirtualNode>);
impl IntoIterator for IterableNodes {
type Item = VirtualNode;
type IntoIter = ::std::vec::IntoIter<VirtualNode>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl From<VirtualNode> for IterableNodes {
fn from(other: VirtualNode) -> Self {
IterableNodes(vec![other])
}
}
impl From<&str> for IterableNodes {
fn from(other: &str) -> Self {
IterableNodes(vec![VirtualNode::text(other)])
}
}
impl From<String> for IterableNodes {
fn from(other: String) -> Self {
IterableNodes(vec![VirtualNode::text(other.as_str())])
}
}
impl From<Vec<VirtualNode>> for IterableNodes {
fn from(other: Vec<VirtualNode>) -> Self {
IterableNodes(other)
}
}
impl From<VText> for VirtualNode {
fn from(other: VText) -> Self {
VirtualNode::Text(other)
}
}
impl From<VElement> for VirtualNode {
fn from(other: VElement) -> Self {
VirtualNode::Element(other)
}
}
impl From<&str> for VirtualNode {
fn from(other: &str) -> Self {
VirtualNode::text(other)
}
}
impl From<String> for VirtualNode {
fn from(other: String) -> Self {
VirtualNode::text(other.as_str())
}
}
impl From<&str> for VText {
fn from(text: &str) -> Self {
VText {
text: text.to_string(),
}
}
}
impl From<String> for VText {
fn from(text: String) -> Self {
VText { text }
}
}
impl IntoIterator for VirtualNode {
type Item = VirtualNode;
type IntoIter = ::std::vec::IntoIter<VirtualNode>;
fn into_iter(self) -> Self::IntoIter {
vec![self].into_iter()
}
}
impl Into<::std::vec::IntoIter<VirtualNode>> for VirtualNode {
fn into(self) -> ::std::vec::IntoIter<VirtualNode> {
self.into_iter()
}
}
impl fmt::Debug for VirtualNode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
VirtualNode::Element(e) => write!(f, "Node::{:?}", e),
VirtualNode::Text(t) => write!(f, "Node::{:?}", t),
}
}
}
impl fmt::Debug for VElement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Element(<{}>, attrs: {:?}, children: {:?})",
self.tag, self.attrs, self.children,
)
}
}
impl fmt::Debug for VText {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Text({})", self.text)
}
}
impl fmt::Display for VElement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<{}", self.tag).unwrap();
for (attr, value) in self.attrs.iter() {
write!(f, r#" {}="{}""#, attr, value)?;
}
write!(f, ">")?;
for child in self.children.iter() {
write!(f, "{}", child.to_string())?;
}
if !self.is_self_closing() {
write!(f, "</{}>", self.tag)?;
}
Ok(())
}
}
impl fmt::Display for VText {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.text)
}
}
impl fmt::Display for VirtualNode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
VirtualNode::Element(element) => write!(f, "{}", element),
VirtualNode::Text(text) => write!(f, "{}", text),
}
}
}
pub type DynClosure = Rc<dyn AsRef<JsValue>>;
pub struct Events(pub HashMap<String, DynClosure>);
impl PartialEq for Events {
fn eq(&self, _rhs: &Self) -> bool {
true
}
}
impl fmt::Debug for Events {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let events: String = self.0.keys().map(|key| " ".to_string() + key).collect();
write!(f, "{}", events)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn self_closing_tag_to_string() {
let node = VirtualNode::element("br");
assert_eq!(&node.to_string(), "<br>");
}
}