It now checks if styles are valid
This commit is contained in:
parent
bfd990c12e
commit
f0ea4b1970
10
src/dom.rs
10
src/dom.rs
|
@ -525,7 +525,10 @@ impl<A: IHtmlElement + Clone + 'static> DomBuilder<A> {
|
|||
let name = name.to_owned();
|
||||
|
||||
set_option_str(&self.element, &mut self.callbacks, value, move |element, value| {
|
||||
dom_operations::set_style(element, &name, value.unwrap_or(""), important);
|
||||
match value {
|
||||
Some(value) => dom_operations::set_style(element, &name, value, important),
|
||||
None => dom_operations::remove_style(element, &name),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -644,7 +647,10 @@ impl StylesheetBuilder {
|
|||
let name = name.to_owned();
|
||||
|
||||
set_option_str(&self.element, &mut self.callbacks, value, move |element, value| {
|
||||
dom_operations::set_style(element, &name, value.unwrap_or(""), important);
|
||||
match value {
|
||||
Some(value) => dom_operations::set_style(element, &name, value, important),
|
||||
None => dom_operations::remove_style(element, &name),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -59,19 +59,41 @@ pub fn set_text(element: &TextNode, value: &str) {
|
|||
}
|
||||
|
||||
// TODO this should be in stdweb
|
||||
// TODO check that the style *actually* was changed
|
||||
// TODO handle browser prefixes
|
||||
#[inline]
|
||||
#[cfg(debug_assertions)]
|
||||
pub fn set_style<A: AsRef<Reference>>(element: &A, name: &str, value: &str, important: bool) {
|
||||
if important {
|
||||
js! { @(no_return)
|
||||
@{element.as_ref()}.style.setProperty(@{name}, @{value}, "important");
|
||||
}
|
||||
let is_correct: bool = js!(
|
||||
var element = @{element.as_ref()};
|
||||
var name = @{name};
|
||||
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 {
|
||||
js! { @(no_return)
|
||||
@{element.as_ref()}.style.setProperty(@{name}, @{value}, "");
|
||||
return true;
|
||||
}
|
||||
).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" : ""));
|
||||
}
|
||||
}
|
||||
|
||||
// TODO this should be in stdweb
|
||||
// TODO handle browser prefixes
|
||||
#[inline]
|
||||
pub fn remove_style<A: AsRef<Reference>>(element: &A, name: &str) {
|
||||
js! { @(no_return)
|
||||
@{element.as_ref()}.style.removeProperty(@{name});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue