1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Authors: Robert Lopez
use super::Style;
use crate::error::WasmCssError;
impl Style {
/// Remove `keys` from `Style`, re-rendering if at-least one key is removed.
///
/// Returns error if missing access to: `Head`, `Window`, `Document`.
///
/// ---
/// Example Usage:
/// ```
///
/// let mut style = named_style!(
/// "my_style",
/// "
/// font-size: 18px;
///
/// &:hover {{
/// background-color: red;
/// }}
///
/// background-color: blue;
/// "
/// )?;
///
/// style.remove_keys(vec!["font-size", "&:hover"])?;
/// ```
pub fn remove_keys<K: Into<String>>(&mut self, keys: Vec<K>) -> Result<(), WasmCssError> {
let mut mutated = false;
for key in keys {
let (key, is_effect) = unsafe {
let mut formatted_key_bytes = b"\t".to_vec();
let mut key_string = key.into();
let is_effect = key_string.starts_with('@') || key_string.starts_with('&');
let mut ending_bytes = if is_effect {
b" {\n".to_vec()
} else {
b": ".to_vec()
};
formatted_key_bytes.append(key_string.as_mut_vec());
formatted_key_bytes.append(&mut ending_bytes);
(String::from_utf8_unchecked(formatted_key_bytes), is_effect)
};
if is_effect {
for index in 0..self.components.effects.len() {
if self.components.effects[index].key == key {
self.components.effects.remove(index);
mutated = true;
break;
}
}
} else if self.components.fields.remove(&key).is_some() {
mutated = true;
}
}
if mutated {
self.css = self.components.to_css(&self.css_name);
self.render()?;
}
Ok(())
}
}