use std::pin::Pin; use std::convert::AsRef; use std::marker::PhantomData; use std::future::Future; use std::task::{Context, Poll}; use std::sync::atomic::{AtomicU32, Ordering}; use lazy_static::lazy_static; use futures_signals::signal::{Signal, not}; use futures_signals::signal_vec::SignalVec; use futures_util::FutureExt; use futures_channel::oneshot; use discard::{Discard, DiscardOnDrop}; use wasm_bindgen::{JsValue, UnwrapThrowExt, JsCast}; use js_sys::JsString; use web_sys::{HtmlElement, Node, EventTarget, Element, CssStyleSheet, CssStyleRule}; use gloo::events::{EventListener, EventListenerOptions}; use crate::cache::intern; use crate::bindings; use crate::callbacks::Callbacks; use crate::traits::*; use crate::operations; use crate::operations::{for_each, spawn_future}; use crate::utils::{on, on_with_options, ValueDiscard, FnDiscard, EventDiscard}; pub struct RefFn where B: ?Sized { value: A, callback: C, return_value: PhantomData, } impl RefFn where B: ?Sized, C: Fn(&A) -> &B { #[inline] pub fn new(value: A, callback: C) -> Self { Self { value, callback, return_value: PhantomData, } } #[inline] pub fn call_ref(&self) -> &B { (self.callback)(&self.value) } /*pub fn map(self, callback: E) -> RefFn &D> where D: ?Sized, E: Fn(&B) -> &D { let old_callback = self.callback; RefFn { value: self.value, callback: move |value| callback(old_callback(value)), } }*/ } /*impl Deref for RefFn where B: ?Sized, C: Fn(&A) -> &B { type Target = B; #[inline] fn deref(&self) -> &Self::Target { self.call_ref() } }*/ /*impl AsRef for RefFn where B: ?Sized, C: Fn(&A) -> &B { #[inline] fn as_ref(&self) -> &B { self.call_ref() } }*/ // https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS#Valid%20Namespace%20URIs pub const HTML_NAMESPACE: &str = "http://www.w3.org/1999/xhtml"; pub const SVG_NAMESPACE: &str = "http://www.w3.org/2000/svg"; // 32-bit signed int pub const HIGHEST_ZINDEX: &str = "2147483647"; lazy_static! { static ref HIDDEN_CLASS: String = class! { .style_important("display", "none") }; } // TODO should return HtmlBodyElement ? pub fn body() -> HtmlElement { bindings::body() } pub struct DomHandle { parent: Node, dom: Dom, } impl Discard for DomHandle { #[inline] fn discard(self) { bindings::remove_child(&self.parent, &self.dom.element); self.dom.callbacks.discard(); } } #[inline] pub fn append_dom(parent: &Node, mut dom: Dom) -> DomHandle { bindings::append_child(&parent, &dom.element); dom.callbacks.trigger_after_insert(); // This prevents it from triggering after_remove dom.callbacks.leak(); DomHandle { parent: parent.clone(), dom, } } // TODO use must_use ? enum IsWindowLoaded { Initial {}, Pending { receiver: oneshot::Receiver>, _event: EventListener, }, Done {}, } impl Signal for IsWindowLoaded { type Item = bool; fn poll_change(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll> { let result = match *self { IsWindowLoaded::Initial {} => { let is_ready = bindings::ready_state() == "complete"; if is_ready { Poll::Ready(Some(true)) } else { let (sender, receiver) = oneshot::channel(); *self = IsWindowLoaded::Pending { receiver, _event: EventListener::once(&bindings::window(), "load", move |_| { // TODO test this sender.send(Some(true)).unwrap_throw(); }), }; Poll::Ready(Some(false)) } }, IsWindowLoaded::Pending { ref mut receiver, .. } => { receiver.poll_unpin(cx).map(|x| x.unwrap_throw()) }, IsWindowLoaded::Done {} => { Poll::Ready(None) }, }; if let Poll::Ready(Some(true)) = result { *self = IsWindowLoaded::Done {}; } result } } // TODO this should be moved into gloo #[inline] pub fn is_window_loaded() -> impl Signal { IsWindowLoaded::Initial {} } // TODO should this intern ? #[inline] pub fn text(value: &str) -> Dom { Dom::new(bindings::create_text_node(&JsString::from(value)).into()) } // TODO should this inline ? pub fn text_signal(value: B) -> Dom where A: AsStr, B: Signal + 'static { let element = bindings::create_text_node(&intern("")); let mut callbacks = Callbacks::new(); { let element = element.clone(); callbacks.after_remove(for_each(value, move |value| { let value = value.as_str(); // TODO maybe this should intern ? bindings::set_text(&element, &JsString::from(value)); })); } Dom { element: element.into(), callbacks: callbacks, } } // TODO better warning message for must_use #[must_use] #[derive(Debug)] pub struct Dom { pub(crate) element: Node, pub(crate) callbacks: Callbacks, } impl Dom { #[inline] pub fn new(element: Node) -> Self { Self { element, callbacks: Callbacks::new(), } } #[inline] pub fn empty() -> Self { // TODO is there a better way of doing this ? Self::new(bindings::create_comment(&intern("")).into()) } #[inline] pub fn with_state(mut state: A, initializer: F) -> Dom where A: 'static, F: FnOnce(&mut A) -> Dom { let mut dom = initializer(&mut state); dom.callbacks.after_remove(ValueDiscard::new(state)); dom } } #[inline] pub fn create_element(name: &str) -> A where A: JsCast { bindings::create_element(&intern(name)).dyn_into().unwrap_throw() } #[inline] pub fn create_element_ns(name: &str, namespace: &str) -> A where A: JsCast { bindings::create_element_ns(&intern(namespace), &intern(name)).dyn_into().unwrap_throw() } fn set_option(element: A, callbacks: &mut Callbacks, value: D, mut f: F) where A: 'static, C: OptionStr, D: Signal + 'static, F: FnMut(&A, Option) + 'static { let mut is_set = false; callbacks.after_remove(for_each(value, move |value| { let value = value.into_option(); if value.is_some() { is_set = true; } else if is_set { is_set = false; } else { return; } f(&element, value); })); } fn set_style(element: &JsValue, name: &A, value: B, important: bool, should_intern: bool) where A: MultiStr, B: MultiStr { let mut names = vec![]; let mut values = vec![]; fn try_set_style(element: &JsValue, names: &mut Vec, values: &mut Vec, name: &JsString, value: &JsString, important: bool) -> bool { // TODO move this out of this function ? let empty = intern(""); assert!(*value != empty); // TODO handle browser prefixes ? bindings::remove_style(element, name); bindings::set_style(element, name, value, important); // TODO maybe use cfg(debug_assertions) ? let is_changed = bindings::get_style(element, name) != empty; if is_changed { true } else { names.push(String::from(name)); values.push(String::from(value)); false } } let okay = name.any(|name| { let name = intern(name); value.any(|value| { // TODO maybe always intern the value ? let value = if should_intern { intern(value) } else { JsString::from(value) }; try_set_style(element, &mut names, &mut values, &name, &value, important) }) }); if !okay { // TODO maybe make this configurable panic!("style is incorrect:\n names: {}\n values: {}", names.join(", "), values.join(", ")); } } fn set_style_signal(element: &JsValue, callbacks: &mut Callbacks, name: A, value: D, important: bool) where A: MultiStr + 'static, B: MultiStr, C: OptionStr, D: Signal + 'static { // TODO verify that this is always a cheap O(1) clone let element = element.clone(); set_option(element, callbacks, value, move |element, value| { match value { Some(value) => { // TODO should this intern or not ? set_style(element, &name, value, important, true); }, None => { name.each(|name| { // TODO handle browser prefixes ? bindings::remove_style(element, &intern(name)); }); }, } }); } // TODO check that the property *actually* was changed ? // TODO maybe use AsRef ? fn set_property(element: &A, name: &B, value: C) where A: AsRef, B: MultiStr, C: Into { let element = element.as_ref(); let value = value.into(); name.each(|name| { bindings::set_property(element, &intern(name), &value); }); } // TODO better warning message for must_use #[must_use] pub struct DomBuilder { element: A, callbacks: Callbacks, // TODO verify this with static types instead ? has_children: bool, } impl DomBuilder { #[inline] pub fn new(value: A) -> Self { Self { element: value, callbacks: Callbacks::new(), has_children: false, } } #[inline] fn _event(&mut self, element: &EventTarget, listener: F) where T: StaticEvent, F: FnMut(T) + 'static { self.callbacks.after_remove(EventDiscard::new(on(element, listener))); } #[inline] fn _event_with_options(&mut self, element: &EventTarget, options: EventListenerOptions, listener: F) where T: StaticEvent, F: FnMut(T) + 'static { self.callbacks.after_remove(EventDiscard::new(on_with_options(element, options, listener))); } // TODO add this to the StylesheetBuilder and ClassBuilder too #[inline] pub fn global_event(mut self, listener: F) -> Self where T: StaticEvent, F: FnMut(T) + 'static { self._event(&bindings::window(), listener); self } #[inline] pub fn future(mut self, future: F) -> Self where F: Future + 'static { self.callbacks.after_remove(DiscardOnDrop::leak(spawn_future(future))); self } #[inline] pub fn apply(self, f: F) -> Self where F: FnOnce(Self) -> Self { self.apply_if(true, f) } #[inline] pub fn apply_if(self, test: bool, f: F) -> Self where F: FnOnce(Self) -> Self { if test { f(self) } else { self } } } impl DomBuilder where A: Clone { #[inline] pub fn with_element(self, f: F) -> B where F: FnOnce(Self, A) -> B { let element = self.element.clone(); f(self, element) } #[inline] pub fn before_inserted(self, f: F) -> Self where F: FnOnce(A) { let element = self.element.clone(); f(element); self } } impl DomBuilder where A: Clone + 'static { #[inline] pub fn after_inserted(mut self, f: F) -> Self where F: FnOnce(A) + 'static { let element = self.element.clone(); self.callbacks.after_insert(move |_| f(element)); self } #[inline] pub fn after_removed(mut self, f: F) -> Self where F: FnOnce(A) + 'static { let element = self.element.clone(); self.callbacks.after_remove(FnDiscard::new(move || f(element))); self } } impl DomBuilder where A: Into { #[inline] pub fn into_dom(self) -> Dom { Dom { element: self.element.into(), callbacks: self.callbacks, } } } impl DomBuilder where A: AsRef { #[inline] pub fn property(self, name: B, value: C) -> Self where B: MultiStr, C: Into { set_property(&self.element, &name, value); self } } impl DomBuilder where A: AsRef { fn set_property_signal(&mut self, name: B, value: D) where B: MultiStr + 'static, C: Into, D: Signal + 'static { let element = self.element.as_ref().clone(); self.callbacks.after_remove(for_each(value, move |value| { set_property(&element, &name, value); })); } #[inline] pub fn property_signal(mut self, name: B, value: D) -> Self where B: MultiStr + 'static, C: Into, D: Signal + 'static { self.set_property_signal(name, value); self } } impl DomBuilder where A: AsRef { #[inline] pub fn event(mut self, listener: F) -> Self where T: StaticEvent, F: FnMut(T) + 'static { // TODO can this clone be avoided ? self._event(&self.element.as_ref().clone(), listener); self } #[inline] pub fn event_preventable(mut self, listener: F) -> Self where T: StaticEvent, F: FnMut(T) + 'static { // TODO can this clone be avoided ? self._event_with_options(&self.element.as_ref().clone(), EventListenerOptions::enable_prevent_default(), listener); self } } impl DomBuilder where A: AsRef { #[inline] fn check_children(&mut self) { assert_eq!(self.has_children, false); self.has_children = true; } // TODO figure out how to make this owned rather than &mut #[inline] pub fn children<'a, B: IntoIterator>(mut self, children: B) -> Self { self.check_children(); operations::insert_children_iter(self.element.as_ref(), &mut self.callbacks, children); self } #[inline] pub fn text(mut self, value: &str) -> Self { self.check_children(); // TODO should this intern ? bindings::set_text_content(self.element.as_ref(), &JsString::from(value)); self } fn set_text_signal(&mut self, value: C) where B: AsStr, C: Signal + 'static { let element = self.element.as_ref().clone(); self.callbacks.after_remove(for_each(value, move |value| { let value = value.as_str(); // TODO maybe intern this ? bindings::set_text_content(&element, &JsString::from(value)); })); } #[inline] pub fn text_signal(mut self, value: C) -> Self where B: AsStr, C: Signal + 'static { self.check_children(); self.set_text_signal(value); self } } impl DomBuilder where A: AsRef { #[inline] pub fn children_signal_vec(mut self, children: B) -> Self where B: SignalVec + 'static { assert_eq!(self.has_children, false); self.has_children = true; operations::insert_children_signal_vec(self.element.as_ref().clone(), &mut self.callbacks, children); self } } impl DomBuilder where A: AsRef { #[inline] pub fn attribute(self, name: B, value: &str) -> Self where B: MultiStr { let element = self.element.as_ref(); let value = intern(value); name.each(|name| { bindings::set_attribute(element, &intern(name), &value); }); self } #[inline] pub fn attribute_namespace(self, namespace: &str, name: B, value: &str) -> Self where B: MultiStr { let element = self.element.as_ref(); let namespace = intern(namespace); let value = intern(value); name.each(|name| { bindings::set_attribute_ns(element, &namespace, &intern(name), &value); }); self } #[inline] pub fn class(self, name: B) -> Self where B: MultiStr { let element = self.element.as_ref(); name.each(|name| { bindings::add_class(element, &intern(name)); }); self } // TODO make this more efficient ? #[inline] pub fn visible(self, value: bool) -> Self { if value { // TODO remove the class somehow ? self } else { self.class(&*HIDDEN_CLASS) } } } impl DomBuilder where A: AsRef { fn set_attribute_signal(&mut self, name: B, value: E) where B: MultiStr + 'static, C: AsStr, D: OptionStr, E: Signal + 'static { set_option(self.element.as_ref().clone(), &mut self.callbacks, value, move |element, value| { match value { Some(value) => { let value = value.as_str(); // TODO should this intern ? let value = intern(value); name.each(|name| { bindings::set_attribute(element, &intern(name), &value); }); }, None => { name.each(|name| { bindings::remove_attribute(element, &intern(name)); }); }, } }); } #[inline] pub fn attribute_signal(mut self, name: B, value: E) -> Self where B: MultiStr + 'static, C: AsStr, D: OptionStr, E: Signal + 'static { self.set_attribute_signal(name, value); self } fn set_attribute_namespace_signal(&mut self, namespace: &str, name: B, value: E) where B: MultiStr + 'static, C: AsStr, D: OptionStr, E: Signal + 'static { let namespace = intern(namespace); set_option(self.element.as_ref().clone(), &mut self.callbacks, value, move |element, value| { match value { Some(value) => { let value = value.as_str(); // TODO should this intern ? let value = intern(value); name.each(|name| { bindings::set_attribute_ns(element, &namespace, &intern(name), &value); }); }, None => { name.each(|name| { bindings::remove_attribute_ns(element, &namespace, &intern(name)); }); }, } }); } #[inline] pub fn attribute_namespace_signal(mut self, namespace: &str, name: B, value: E) -> Self where B: MultiStr + 'static, C: AsStr, D: OptionStr, E: Signal + 'static { self.set_attribute_namespace_signal(namespace, name, value); self } fn set_class_signal(&mut self, name: B, value: C) where B: MultiStr + 'static, C: Signal + 'static { let element = self.element.as_ref().clone(); let mut is_set = false; self.callbacks.after_remove(for_each(value, move |value| { if value { if !is_set { is_set = true; name.each(|name| { bindings::add_class(&element, &intern(name)); }); } } else { if is_set { is_set = false; name.each(|name| { bindings::remove_class(&element, &intern(name)); }); } } })); } #[inline] pub fn class_signal(mut self, name: B, value: C) -> Self where B: MultiStr + 'static, C: Signal + 'static { self.set_class_signal(name, value); self } // TODO make this more efficient ? #[inline] pub fn visible_signal(self, value: B) -> Self where B: Signal + 'static { self.class_signal(&*HIDDEN_CLASS, not(value)) } // TODO use OptionStr ? fn set_scroll_signal(&mut self, signal: B, mut f: F) where B: Signal> + 'static, F: FnMut(&Element, i32) + 'static { let element: Element = self.element.as_ref().clone(); // This needs to use `after_insert` because scrolling an element before it is in the DOM has no effect self.callbacks.after_insert(move |callbacks| { callbacks.after_remove(for_each(signal, move |value| { if let Some(value) = value { f(&element, value); } })); }); } #[inline] pub fn scroll_left_signal(mut self, signal: B) -> Self where B: Signal> + 'static { // TODO bindings function for this ? self.set_scroll_signal(signal, Element::set_scroll_left); self } #[inline] pub fn scroll_top_signal(mut self, signal: B) -> Self where B: Signal> + 'static { // TODO bindings function for this ? self.set_scroll_signal(signal, Element::set_scroll_top); self } } impl DomBuilder where A: AsRef { #[inline] pub fn style(self, name: B, value: C) -> Self where B: MultiStr, C: MultiStr { set_style(self.element.as_ref(), &name, value, false, true); self } #[inline] pub fn style_important(self, name: B, value: C) -> Self where B: MultiStr, C: MultiStr { set_style(self.element.as_ref(), &name, value, true, true); self } } impl DomBuilder where A: AsRef { #[inline] pub fn style_signal(mut self, name: B, value: E) -> Self where B: MultiStr + 'static, C: MultiStr, D: OptionStr, E: Signal + 'static { set_style_signal(self.element.as_ref(), &mut self.callbacks, name, value, false); self } #[inline] pub fn style_important_signal(mut self, name: B, value: E) -> Self where B: MultiStr + 'static, C: MultiStr, D: OptionStr, E: Signal + 'static { set_style_signal(self.element.as_ref(), &mut self.callbacks, name, value, true); self } // TODO remove the `value` argument ? #[inline] pub fn focused(mut self, value: bool) -> Self { let element = self.element.as_ref().clone(); // This needs to use `after_insert` because calling `.focus()` on an element before it is in the DOM has no effect self.callbacks.after_insert(move |_| { // TODO avoid updating if the focused state hasn't changed ? if value { bindings::focus(&element); } else { bindings::blur(&element); } }); self } fn set_focused_signal(&mut self, value: B) where B: Signal + 'static { let element = self.element.as_ref().clone(); // This needs to use `after_insert` because calling `.focus()` on an element before it is in the DOM has no effect self.callbacks.after_insert(move |callbacks| { // TODO verify that this is correct under all circumstances callbacks.after_remove(for_each(value, move |value| { // TODO avoid updating if the focused state hasn't changed ? if value { bindings::focus(&element); } else { bindings::blur(&element); } })); }); } #[inline] pub fn focused_signal(mut self, value: B) -> Self where B: Signal + 'static { self.set_focused_signal(value); self } } // TODO better warning message for must_use #[must_use] pub struct StylesheetBuilder { element: CssStyleRule, callbacks: Callbacks, } // TODO remove the CssStyleRule when this is discarded impl StylesheetBuilder { #[inline] pub fn new(selector: &str) -> Self { // TODO can this be made faster ? // TODO somehow share this safely between threads ? thread_local! { static STYLESHEET: CssStyleSheet = bindings::create_stylesheet(); } // TODO maybe intern ? let selector = JsString::from(selector); let element = STYLESHEET.with(move |stylesheet| { bindings::make_style_rule(stylesheet, &selector) }); Self { element, callbacks: Callbacks::new(), } } #[inline] pub fn style(self, name: B, value: C) -> Self where B: MultiStr, C: MultiStr { set_style(&self.element, &name, value, false, true); self } #[inline] pub fn style_important(self, name: B, value: C) -> Self where B: MultiStr, C: MultiStr { set_style(&self.element, &name, value, true, true); self } #[inline] pub fn style_signal(mut self, name: B, value: E) -> Self where B: MultiStr + 'static, C: MultiStr, D: OptionStr, E: Signal + 'static { set_style_signal(&self.element, &mut self.callbacks, name, value, false); self } #[inline] pub fn style_important_signal(mut self, name: B, value: E) -> Self where B: MultiStr + 'static, C: MultiStr, D: OptionStr, E: Signal + 'static { set_style_signal(&self.element, &mut self.callbacks, name, value, true); self } // TODO return a Handle #[inline] pub fn done(mut self) { self.callbacks.trigger_after_insert(); // This prevents it from triggering after_remove self.callbacks.leak(); } } // TODO better warning message for must_use #[must_use] pub struct ClassBuilder { stylesheet: StylesheetBuilder, class_name: String, } impl ClassBuilder { #[inline] pub fn new() -> Self { let class_name = { // TODO replace this with a global counter in JavaScript ? // TODO can this be made more efficient ? static CLASS_ID: AtomicU32 = AtomicU32::new(0); // TODO check for overflow ? let id = CLASS_ID.fetch_add(1, Ordering::Relaxed); // TODO make this more efficient ? format!("__class_{}__", id) }; Self { // TODO make this more efficient ? stylesheet: StylesheetBuilder::new(&format!(".{}", class_name)), class_name, } } #[inline] pub fn style(mut self, name: B, value: C) -> Self where B: MultiStr, C: MultiStr { self.stylesheet = self.stylesheet.style(name, value); self } #[inline] pub fn style_important(mut self, name: B, value: C) -> Self where B: MultiStr, C: MultiStr { self.stylesheet = self.stylesheet.style_important(name, value); self } #[inline] pub fn style_signal(mut self, name: B, value: E) -> Self where B: MultiStr + 'static, C: MultiStr, D: OptionStr, E: Signal + 'static { self.stylesheet = self.stylesheet.style_signal(name, value); self } #[inline] pub fn style_important_signal(mut self, name: B, value: E) -> Self where B: MultiStr + 'static, C: MultiStr, D: OptionStr, E: Signal + 'static { self.stylesheet = self.stylesheet.style_important_signal(name, value); self } // TODO return a Handle ? #[inline] pub fn done(self) -> String { self.stylesheet.done(); self.class_name } } #[cfg(test)] mod tests { use super::{create_element_ns, DomBuilder, HTML_NAMESPACE, text_signal, RefFn}; use futures_signals::signal::{always, SignalExt}; use lazy_static::lazy_static; use web_sys::HtmlElement; #[test] fn apply() { let a: DomBuilder = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE)); fn my_mixin>(builder: DomBuilder) -> DomBuilder { builder.style("foo", "bar") } let _ = a.apply(my_mixin); } #[test] fn text_signal_types() { let _ = text_signal(always("foo")); let _ = text_signal(always("foo".to_owned())); let _ = text_signal(always("foo".to_owned()).map(|x| RefFn::new(x, |x| x.as_str()))); //text_signal(always(Arc::new("foo"))); //text_signal(always(Arc::new("foo".to_owned()))); //text_signal(always(Rc::new("foo"))); //text_signal(always(Rc::new("foo".to_owned()))); //text_signal(always(Box::new("foo"))); //text_signal(always(Box::new("foo".to_owned()))); //text_signal(always(Cow::Borrowed(&"foo"))); //text_signal(always(Cow::Owned::("foo".to_owned()))); } #[test] fn property_signal_types() { let _a: DomBuilder = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE)) .property("foo", "hi") .property("foo", 5) .property(["foo", "-webkit-foo", "-ms-foo"], "hi") .property_signal("foo", always("hi")) .property_signal("foo", always(5)) .property_signal("foo", always(Some("hi"))) .property_signal(["foo", "-webkit-foo", "-ms-foo"], always("hi")) .property_signal(["foo", "-webkit-foo", "-ms-foo"], always(5)) .property_signal(["foo", "-webkit-foo", "-ms-foo"], always(Some("hi"))) ; } #[test] fn attribute_signal_types() { let _a: DomBuilder = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE)) .attribute("foo", "hi") .attribute(["foo", "-webkit-foo", "-ms-foo"], "hi") .attribute_signal("foo", always("hi")) .attribute_signal("foo", always(Some("hi"))) .attribute_signal(["foo", "-webkit-foo", "-ms-foo"], always("hi")) .attribute_signal(["foo", "-webkit-foo", "-ms-foo"], always(Some("hi"))) ; } #[test] fn class_signal_types() { let _a: DomBuilder = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE)) .class("foo") .class(["foo", "-webkit-foo", "-ms-foo"]) .class_signal("foo", always(true)) .class_signal(["foo", "-webkit-foo", "-ms-foo"], always(true)) ; } #[test] fn style_signal_types() { lazy_static! { static ref FOO: String = "foo".to_owned(); } let _a: DomBuilder = DomBuilder::new(create_element_ns("div", HTML_NAMESPACE)) .style_signal("foo", always("bar")) .style_signal("foo", always("bar".to_owned())) .style_signal("foo", always("bar".to_owned()).map(|x| RefFn::new(x, |x| x.as_str()))) .style("foo".to_owned(), "bar".to_owned()) .style_signal("foo".to_owned(), always("bar".to_owned())) .style(&"foo".to_owned(), &"bar".to_owned()) //.style(Box::new("foo".to_owned()), Box::new("bar".to_owned())) //.style_signal(Box::new("foo".to_owned()), always(Box::new("bar".to_owned()))) .style_signal(&*FOO, always(&*FOO)) //.style(vec!["-moz-foo", "-webkit-foo", "foo"].as_slice(), vec!["bar"].as_slice()) .style_signal(RefFn::new(vec!["-moz-foo", "-webkit-foo", "foo"], |x| x.as_slice()), always(RefFn::new(vec!["bar"], |x| x.as_slice()))) .style_signal(["-moz-foo", "-webkit-foo", "foo"], always("bar")) .style_signal(["-moz-foo", "-webkit-foo", "foo"], always("bar".to_owned())) .style_signal(["-moz-foo", "-webkit-foo", "foo"], always("bar".to_owned()).map(|x| RefFn::new(x, |x| x.as_str()))) .style_signal(["-moz-foo", "-webkit-foo", "foo"], always(["bar", "qux"])) .style_signal(["-moz-foo", "-webkit-foo", "foo"], always(["bar".to_owned(), "qux".to_owned()])) //.style_signal(["-moz-foo", "-webkit-foo", "foo"], always(AsSlice::new(["foo", "bar"]))) //.style_signal(["-moz-foo", "-webkit-foo", "foo"], always(("bar".to_owned(), "qux".to_owned())).map(|x| RefFn::new(x, |x| AsSlice::new([x.0.as_str(), x.1.as_str()])))) .style_signal("foo", always(Some("bar"))) .style_signal("foo", always(Some("bar".to_owned()))) .style_signal("foo", always("bar".to_owned()).map(|x| Some(RefFn::new(x, |x| x.as_str())))) .style_signal(["-moz-foo", "-webkit-foo", "foo"], always(Some("bar"))) .style_signal(["-moz-foo", "-webkit-foo", "foo"], always(Some("bar".to_owned()))) .style_signal(["-moz-foo", "-webkit-foo", "foo"], always("bar".to_owned()).map(|x| Some(RefFn::new(x, |x| x.as_str())))) ; } }