Minor improvements

This commit is contained in:
Pauan 2018-06-25 15:02:48 -10:00
parent f0ea4b1970
commit 2a22c75c74
3 changed files with 27 additions and 16 deletions

View File

@ -23,3 +23,9 @@ version = "0.4.6"
#git = "https://github.com/koute/stdweb"
#path = "../stdweb"
features = ["experimental_features_which_may_break_on_minor_version_bumps"]
[profile.release]
debug-assertions = true
[profile.bench]
debug-assertions = true

View File

@ -58,34 +58,38 @@ pub fn set_text(element: &TextNode, value: &str) {
}
}
#[inline]
fn set_style_raw<A: AsRef<Reference>>(element: &A, name: &str, value: &str, important: bool) {
js! { @(no_return)
@{element.as_ref()}.style.setProperty(@{name}, @{value}, (@{important} ? "important" : ""));
}
}
// TODO this should be in stdweb
// TODO handle browser prefixes
#[cfg(debug_assertions)]
pub fn set_style<A: AsRef<Reference>>(element: &A, name: &str, value: &str, important: bool) {
let is_correct: bool = js!(
var element = @{element.as_ref()};
var name = @{name};
var value = @{value};
var old_value = element.style.getPropertyValue(name);
#[inline]
fn get_style<A: AsRef<Reference>>(element: &A, name: &str) -> String {
js!( return @{element.as_ref()}.style.getPropertyValue(@{name}); ).try_into().unwrap()
}
if (old_value !== value) {
element.style.setProperty(name, value, (@{important} ? "important" : ""));
return element.style.getPropertyValue(name) !== old_value;
let old_value = get_style(element, name);
} else {
return true;
if old_value != value {
set_style_raw(element, name, value, important);
if get_style(element, name) == old_value {
panic!("style is incorrect:\n name: {}\n old value: {}\n new value: {}", name, old_value, value);
}
).try_into().unwrap();
assert!(is_correct, "style is incorrect: {} = {}", name, value);
}
}
#[cfg(not(debug_assertions))]
#[inline]
pub fn set_style<A: AsRef<Reference>>(element: &A, name: &str, value: &str, important: bool) {
js! { @(no_return)
@{element.as_ref()}.style.setProperty(@{name}, @{value}, (@{important} ? "important" : ""));
}
set_style_raw(element, name, value, important);
}
// TODO this should be in stdweb

View File

@ -17,6 +17,7 @@ impl<A, F> Mixin<A> for F where F: Fn(A) -> A {
// TODO figure out a way to implement this for all of AsRef / Borrow / etc.
// TODO implementations for &String and &mut String
pub trait IntoStr {
type Output: Deref<Target = str>;