use core::fmt;
use crate::data_type::LengthPercentage;
use crate::to_css::ToCss;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum BackgroundRepeat {
Repeat,
NoRepeat,
RepeatX,
RepeatY,
Space,
Round,
}
impl ToCss for BackgroundRepeat {
fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
dest.write_str(match self {
BackgroundRepeat::Repeat => "repeat",
BackgroundRepeat::NoRepeat => "no-repeat",
BackgroundRepeat::RepeatX => "repeat-x",
BackgroundRepeat::RepeatY => "repeat-y",
BackgroundRepeat::Space => "space",
BackgroundRepeat::Round => "round",
})
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum BackgroundClip {
BorderBox,
PaddingBox,
ContentBox,
Text,
}
impl ToCss for BackgroundClip {
fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
dest.write_str(match self {
BackgroundClip::BorderBox => "border-box",
BackgroundClip::PaddingBox => "padding-box",
BackgroundClip::ContentBox => "content-box",
BackgroundClip::Text => "text",
})
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum BackgroundOrigin {
BorderBox,
PaddingBox,
ContentBox,
}
impl ToCss for BackgroundOrigin {
fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
dest.write_str(match self {
BackgroundOrigin::BorderBox => "border-box",
BackgroundOrigin::PaddingBox => "padding-box",
BackgroundOrigin::ContentBox => "content-box",
})
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum BackgroundAttachment {
Scroll,
Fixed,
Local,
}
impl ToCss for BackgroundAttachment {
fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
dest.write_str(match self {
BackgroundAttachment::Scroll => "scroll",
BackgroundAttachment::Fixed => "fixed",
BackgroundAttachment::Local => "local",
})
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum BackgroundSize {
Auto,
Cover,
Contain,
Explicit(LengthPercentage, LengthPercentage),
}
impl ToCss for BackgroundSize {
fn to_css(&self, dest: &mut dyn fmt::Write) -> fmt::Result {
match self {
BackgroundSize::Auto => dest.write_str("auto"),
BackgroundSize::Cover => dest.write_str("cover"),
BackgroundSize::Contain => dest.write_str("contain"),
BackgroundSize::Explicit(w, h) => {
w.to_css(dest)?;
dest.write_char(' ')?;
h.to_css(dest)
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::data_type::{Length, Percentage};
#[test]
fn background_repeat_all() {
let cases = [
(BackgroundRepeat::Repeat, "repeat"),
(BackgroundRepeat::NoRepeat, "no-repeat"),
(BackgroundRepeat::RepeatX, "repeat-x"),
(BackgroundRepeat::RepeatY, "repeat-y"),
(BackgroundRepeat::Space, "space"),
(BackgroundRepeat::Round, "round"),
];
for (k, expected) in cases {
assert_eq!(k.to_css_string(), expected);
}
}
#[test]
fn background_clip_all() {
let cases = [
(BackgroundClip::BorderBox, "border-box"),
(BackgroundClip::PaddingBox, "padding-box"),
(BackgroundClip::ContentBox, "content-box"),
(BackgroundClip::Text, "text"),
];
for (k, expected) in cases {
assert_eq!(k.to_css_string(), expected);
}
}
#[test]
fn background_origin_all() {
let cases = [
(BackgroundOrigin::BorderBox, "border-box"),
(BackgroundOrigin::PaddingBox, "padding-box"),
(BackgroundOrigin::ContentBox, "content-box"),
];
for (k, expected) in cases {
assert_eq!(k.to_css_string(), expected);
}
}
#[test]
fn background_attachment_all() {
let cases = [
(BackgroundAttachment::Scroll, "scroll"),
(BackgroundAttachment::Fixed, "fixed"),
(BackgroundAttachment::Local, "local"),
];
for (k, expected) in cases {
assert_eq!(k.to_css_string(), expected);
}
}
#[test]
fn background_size_keywords_and_explicit() {
assert_eq!(BackgroundSize::Auto.to_css_string(), "auto");
assert_eq!(BackgroundSize::Cover.to_css_string(), "cover");
assert_eq!(BackgroundSize::Contain.to_css_string(), "contain");
let explicit = BackgroundSize::Explicit(Length::Px(100.0).into(), Percentage(50.0).into());
assert_eq!(explicit.to_css_string(), "100px 50%");
}
}