use crate::properties::{PropertyId, PropertyList, PropertyValue};
use crate::Result;
pub(super) fn expand_background_shorthand(
properties: &mut PropertyList,
value: &PropertyValue,
) -> Result<()> {
match value {
PropertyValue::List(values) => {
for val in values {
match val {
PropertyValue::Color(_) => {
properties.set(PropertyId::BackgroundColor, val.clone());
}
PropertyValue::String(s) => {
let s_str = s.as_ref();
if s_str.starts_with("url(") && s_str.ends_with(')') {
properties.set(PropertyId::BackgroundImage, val.clone());
} else if is_background_repeat(s_str) {
properties.set(PropertyId::BackgroundRepeat, val.clone());
} else if is_background_attachment(s_str) {
properties.set(PropertyId::BackgroundAttachment, val.clone());
} else if is_background_position(s_str) {
properties.set(PropertyId::BackgroundPosition, val.clone());
}
}
PropertyValue::Percentage(_) | PropertyValue::Length(_) => {
properties.set(PropertyId::BackgroundPosition, val.clone());
}
_ => {}
}
}
}
PropertyValue::Color(_) => {
properties.set(PropertyId::BackgroundColor, value.clone());
}
PropertyValue::Percentage(_) | PropertyValue::Length(_) => {
properties.set(PropertyId::BackgroundPosition, value.clone());
}
PropertyValue::String(s) => {
let s_str = s.as_ref();
if s_str.starts_with("url(") && s_str.ends_with(')') {
properties.set(PropertyId::BackgroundImage, value.clone());
} else if is_background_repeat(s_str) {
properties.set(PropertyId::BackgroundRepeat, value.clone());
} else if is_background_attachment(s_str) {
properties.set(PropertyId::BackgroundAttachment, value.clone());
} else if is_background_position(s_str) {
properties.set(PropertyId::BackgroundPosition, value.clone());
}
}
_ => {}
}
Ok(())
}
pub(super) fn is_background_repeat(s: &str) -> bool {
matches!(
s,
"repeat" | "repeat-x" | "repeat-y" | "no-repeat" | "space" | "round"
)
}
pub(super) fn is_background_attachment(s: &str) -> bool {
matches!(s, "scroll" | "fixed" | "local")
}
pub(super) fn is_background_position(s: &str) -> bool {
matches!(s, "top" | "bottom" | "left" | "right" | "center")
}