Minor improvements
This commit is contained in:
parent
f0ea4b1970
commit
2a22c75c74
|
@ -23,3 +23,9 @@ version = "0.4.6"
|
||||||
#git = "https://github.com/koute/stdweb"
|
#git = "https://github.com/koute/stdweb"
|
||||||
#path = "../stdweb"
|
#path = "../stdweb"
|
||||||
features = ["experimental_features_which_may_break_on_minor_version_bumps"]
|
features = ["experimental_features_which_may_break_on_minor_version_bumps"]
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug-assertions = true
|
||||||
|
|
||||||
|
[profile.bench]
|
||||||
|
debug-assertions = true
|
||||||
|
|
|
@ -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 this should be in stdweb
|
||||||
// TODO handle browser prefixes
|
// TODO handle browser prefixes
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
pub fn set_style<A: AsRef<Reference>>(element: &A, name: &str, value: &str, important: bool) {
|
pub fn set_style<A: AsRef<Reference>>(element: &A, name: &str, value: &str, important: bool) {
|
||||||
let is_correct: bool = js!(
|
#[inline]
|
||||||
var element = @{element.as_ref()};
|
fn get_style<A: AsRef<Reference>>(element: &A, name: &str) -> String {
|
||||||
var name = @{name};
|
js!( return @{element.as_ref()}.style.getPropertyValue(@{name}); ).try_into().unwrap()
|
||||||
var value = @{value};
|
|
||||||
var old_value = element.style.getPropertyValue(name);
|
|
||||||
|
|
||||||
if (old_value !== value) {
|
|
||||||
element.style.setProperty(name, value, (@{important} ? "important" : ""));
|
|
||||||
return element.style.getPropertyValue(name) !== old_value;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
).try_into().unwrap();
|
|
||||||
|
|
||||||
assert!(is_correct, "style is incorrect: {} = {}", name, value);
|
let old_value = get_style(element, name);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn set_style<A: AsRef<Reference>>(element: &A, name: &str, value: &str, important: bool) {
|
pub fn set_style<A: AsRef<Reference>>(element: &A, name: &str, value: &str, important: bool) {
|
||||||
js! { @(no_return)
|
set_style_raw(element, name, value, important);
|
||||||
@{element.as_ref()}.style.setProperty(@{name}, @{value}, (@{important} ? "important" : ""));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO this should be in stdweb
|
// TODO this should be in stdweb
|
||||||
|
|
|
@ -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 figure out a way to implement this for all of AsRef / Borrow / etc.
|
||||||
|
// TODO implementations for &String and &mut String
|
||||||
pub trait IntoStr {
|
pub trait IntoStr {
|
||||||
type Output: Deref<Target = str>;
|
type Output: Deref<Target = str>;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue