Skip to content
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
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::env;

pub fn main() {
if cfg!(all(feature = "web_sys", feature = "std_web")) {
panic!("don't use `web_sys` and `std_web` simultaneously")
panic!("the `web_sys` and `std_web` cargo features cannot be used simultaneously")
} else if cfg!(not(any(feature = "web_sys", feature = "std_web"))) {
panic!("please select either `web_sys` or `std_web`")
panic!("please select either the `web_sys` or `std_web` cargo feature")
}

let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
Expand Down
35 changes: 8 additions & 27 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
//! a component in an isolated scope.

use crate::html::{Component, NodeRef, Scope};
use crate::utils::document;
#[cfg(feature = "std_web")]
use stdweb::web::{document, Element, INode, IParentNode};
use stdweb::web::{Element, INode, IParentNode};
#[cfg(feature = "web_sys")]
use web_sys::Element;

Expand Down Expand Up @@ -45,13 +46,8 @@ where

/// Alias to `mount("body", ...)`.
pub fn mount_to_body(self) -> Scope<COMP> {
#[cfg(feature = "std_web")]
let document = document();
#[cfg(feature = "web_sys")]
let document = web_sys::window().unwrap().document().unwrap();

// Bootstrap the component for `Window` environment only (not for `Worker`)
let element = document
let element = document()
.query_selector("body")
.expect("can't get body node for rendering")
.expect("can't unwrap body node");
Expand All @@ -63,16 +59,11 @@ where
/// need to manipulate the body element. For example, adding/removing app-wide
/// CSS classes of the body element.
pub fn mount_as_body(self) -> Scope<COMP> {
#[cfg(feature = "std_web")]
let document = document();
#[cfg(feature = "web_sys")]
let document = web_sys::window().unwrap().document().unwrap();

let html_element = document
let html_element = document()
.query_selector("html")
.expect("can't get html node for rendering")
.expect("can't unwrap html node");
let body_element = document
let body_element = document()
.query_selector("body")
.expect("can't get body node for rendering")
.expect("can't unwrap body node");
Expand Down Expand Up @@ -110,13 +101,8 @@ where

/// Alias to `mount_with_props("body", ...)`.
pub fn mount_to_body_with_props(self, props: COMP::Properties) -> Scope<COMP> {
#[cfg(feature = "std_web")]
let document = document();
#[cfg(feature = "web_sys")]
let document = web_sys::window().unwrap().document().unwrap();

// Bootstrap the component for `Window` environment only (not for `Worker`)
let element = document
let element = document()
.query_selector("body")
.expect("can't get body node for rendering")
.expect("can't unwrap body node");
Expand All @@ -128,16 +114,11 @@ where
/// when you need to manipulate the body element. For example, adding/removing app-wide
/// CSS classes of the body element.
pub fn mount_as_body_with_props(self, props: COMP::Properties) -> Scope<COMP> {
#[cfg(feature = "std_web")]
let document = document();
#[cfg(feature = "web_sys")]
let document = web_sys::window().unwrap().document().unwrap();

let html_element = document
let html_element = document()
.query_selector("html")
.expect("can't get html node for rendering")
.expect("can't unwrap html node");
let body_element = document
let body_element = document()
.query_selector("body")
.expect("can't get body node for rendering")
.expect("can't unwrap body node");
Expand Down
46 changes: 25 additions & 21 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
//! This module contains useful utils to get information about the current document.

use failure::{err_msg, Error};

#[cfg(feature = "std_web")]
use stdweb::web::document;
/// Returns current document.
pub fn document() -> stdweb::web::Document {
stdweb::web::document()
}

#[cfg(feature = "web_sys")]
/// Returns current document.
pub fn document() -> web_sys::Document {
web_sys::window().unwrap().document().unwrap()
}

/// Returns `host` for the current document. Useful to connect to a server that server the app.
pub fn host() -> Result<String, Error> {
let location = document()
.location()
.ok_or_else(|| err_msg("can't get location"))?;

#[cfg(feature = "std_web")]
{
document()
.location()
.ok_or_else(|| err_msg("can't get location"))
.and_then(|l| l.host().map_err(Error::from))
}
let host = location.host().map_err(Error::from)?;

#[cfg(feature = "web_sys")]
web_sys::window()
.unwrap()
.document()
.unwrap()
.location()
.ok_or_else(|| err_msg("can't get location"))
.and_then(|l| {
l.host().map_err(|e| {
err_msg(
e.as_string()
.unwrap_or_else(|| String::from("error not recoverable")),
)
})
})
let host = location.host().map_err(|e| {
err_msg(
e.as_string()
.unwrap_or_else(|| String::from("error not recoverable")),
)
})?;

Ok(host)
}