Skip to content

Commit

Permalink
web-sys html conversion (#817)
Browse files Browse the repository at this point in the history
* Converting all `html` parts.

* Format.

* Move general changes to different PR.

* Removed compat.

* Rename `stdweb` feature to `std_web`.

* Remove redudant function copy.

* Some polish.

* Move to gloo's `EventListener`.

* Replace `unwrap`s with `expect`s.
  • Loading branch information
daxpedda authored and jstarry committed Jan 9, 2020
1 parent 7adfef1 commit 2aa747e
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 42 deletions.
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

0 comments on commit 2aa747e

Please sign in to comment.