Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web-sys html conversion #817

Merged
merged 9 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ use std::any::TypeId;
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use stdweb::unstable::TryFrom;
use stdweb::web::Node;
#[cfg(feature = "std_web")]
use stdweb::{unstable::TryFrom, web::Node};
#[cfg(feature = "web_sys")]
use ::{wasm_bindgen::JsValue, web_sys::Node};

/// This type indicates that component should be rendered again.
pub type ShouldRender = bool;
Expand Down Expand Up @@ -268,8 +270,10 @@ where
/// # Example
/// Focus an `<input>` element on mount.
/// ```
/// use stdweb::web::html_element::InputElement;
/// use stdweb::web::IHtmlElement;
/// #[cfg(feature = "std_web")]
/// use stdweb::web::{html_element::InputElement, IHtmlElement};
/// #[cfg(feature = "web_sys")]
/// use web_sys::HtmlInputElement as InputElement;
///# use yew::*;
///
/// pub struct Input {
Expand Down Expand Up @@ -313,8 +317,19 @@ impl NodeRef {
}

/// Try converting the node reference into another form
pub fn try_into<INTO: TryFrom<Node>>(&self) -> Option<INTO> {
self.get().and_then(|node| INTO::try_from(node).ok())
pub fn try_into<
#[cfg(feature = "std_web")] INTO: TryFrom<Node>,
#[cfg(feature = "web_sys")] INTO: AsRef<Node> + From<JsValue>,
>(
&self,
) -> Option<INTO> {
let node = self.get();
#[cfg(feature = "std_web")]
{
node.and_then(|node| INTO::try_from(node).ok())
}
#[cfg(feature = "web_sys")]
node.map(Into::into).map(INTO::from)
}

/// Place a Node in a reference for later use
Expand Down
3 changes: 3 additions & 0 deletions src/html/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::virtual_dom::{VDiff, VNode};
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
#[cfg(feature = "std_web")]
use stdweb::web::Element;
#[cfg(feature = "web_sys")]
use web_sys::Element;

/// Updates for a `Component` instance. Used by scope sender.
pub(crate) enum ComponentUpdate<COMP: Component> {
Expand Down
11 changes: 9 additions & 2 deletions src/virtual_dom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ pub mod vnode;
pub mod vtag;
pub mod vtext;

#[cfg(feature = "std_web")]
use crate::html::EventListener;
#[cfg(feature = "web_sys")]
use gloo::events::EventListener;
use indexmap::set::IndexSet;
use std::collections::HashMap;
use std::fmt;
use std::rc::Rc;
use stdweb::web::{Element, EventListenerHandle, Node};
#[cfg(feature = "std_web")]
use stdweb::web::{Element, Node};
#[cfg(feature = "web_sys")]
use web_sys::{Element, Node};

pub use self::vcomp::{VChild, VComp};
pub use self::vlist::VList;
Expand All @@ -24,7 +31,7 @@ pub trait Listener {
/// Returns standard name of DOM's event.
fn kind(&self) -> &'static str;
/// Attaches a listener to the element.
fn attach(&self, element: &Element) -> EventListenerHandle;
fn attach(&self, element: &Element) -> EventListener;
}

impl fmt::Debug for dyn Listener {
Expand Down
22 changes: 17 additions & 5 deletions src/virtual_dom/vcomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

use super::{Transformer, VDiff, VNode};
use crate::html::{Component, ComponentUpdate, HiddenScope, NodeRef, Scope};
use crate::utils::document;
use std::any::TypeId;
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use stdweb::web::{document, Element, INode, Node, TextNode};
#[cfg(feature = "std_web")]
use stdweb::web::{Element, INode, Node, TextNode};
#[cfg(feature = "web_sys")]
use web_sys::{Element, Node, Text as TextNode};

/// The method generates an instance of a component.
type Generator = dyn Fn(GeneratorType) -> Mounted;
Expand Down Expand Up @@ -197,20 +201,28 @@ impl VDiff for VComp {
this.replace(mounted)
}
Reform::Before(next_sibling) => {
// Temporary node which will be replaced by a component's root node.
let dummy_node = document().create_text_node("");
if let Some(next_sibling) = next_sibling {
let next_sibling = &next_sibling;
#[cfg(feature = "web_sys")]
let next_sibling = Some(next_sibling);
parent
.insert_before(&dummy_node, &next_sibling)
.insert_before(&dummy_node, next_sibling)
.expect("can't insert dummy component node before next sibling");
} else if let Some(next_sibling) =
previous_sibling.and_then(|p| p.next_sibling())
{
let next_sibling = &next_sibling;
#[cfg(feature = "web_sys")]
let next_sibling = Some(next_sibling);
parent
.insert_before(&dummy_node, &next_sibling)
.insert_before(&dummy_node, next_sibling)
.expect("can't insert dummy component node before next sibling");
} else {
parent.append_child(&dummy_node);
#[cfg_attr(feature = "std_web", allow(unused_variables))]
let result = parent.append_child(&dummy_node);
#[cfg(feature = "web_sys")]
result.expect("can't append node to parent");
}
this.mount(parent.to_owned(), dummy_node)
}
Expand Down
3 changes: 3 additions & 0 deletions src/virtual_dom/vlist.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! This module contains fragments implementation.
use super::{VDiff, VNode, VText};
use std::ops::{Deref, DerefMut};
#[cfg(feature = "std_web")]
use stdweb::web::{Element, Node};
#[cfg(feature = "web_sys")]
use web_sys::{Element, Node};

/// This struct represents a fragment of the Virtual DOM tree.
#[derive(Clone, Debug, PartialEq, Default)]
Expand Down
13 changes: 11 additions & 2 deletions src/virtual_dom/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use crate::html::{Component, Renderable};
use std::cmp::PartialEq;
use std::fmt;
use std::iter::FromIterator;
#[cfg(feature = "std_web")]
use stdweb::web::{Element, INode, Node};
#[cfg(feature = "web_sys")]
use web_sys::{Element, Node};

/// Bind virtual element to a DOM reference.
#[derive(Clone)]
Expand Down Expand Up @@ -57,11 +60,17 @@ impl VDiff for VNode {
None => None,
};
if let Some(sibling) = sibling {
let sibling = &sibling;
#[cfg(feature = "web_sys")]
let sibling = Some(sibling);
parent
.insert_before(node, &sibling)
.insert_before(node, sibling)
.expect("can't insert element before sibling");
} else {
parent.append_child(node);
#[cfg_attr(feature = "std_web", allow(unused_variables))]
let result = parent.append_child(node);
#[cfg(feature = "web_sys")]
result.expect("can't append node to parent");
}

Some(node.to_owned())
Expand Down
Loading