mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
style: Unbox a bunch of color properties.
This builds on https://github.com/servo/rust-cssparser/pull/118.
This commit is contained in:
parent
e394334739
commit
0c102e2350
37 changed files with 185 additions and 195 deletions
|
@ -14,7 +14,7 @@ testing = ["style/testing"]
|
|||
|
||||
[dependencies]
|
||||
app_units = "0.3"
|
||||
cssparser = {version = "0.7", features = ["heap_size"]}
|
||||
cssparser = {version = "0.8", features = ["heap_size"]}
|
||||
euclid = "0.10.1"
|
||||
html5ever-atoms = "0.1"
|
||||
matches = "0.1"
|
||||
|
|
|
@ -6,31 +6,45 @@ use cssparser::{Color, RGBA};
|
|||
use style::properties::animated_properties::Interpolate;
|
||||
|
||||
#[test]
|
||||
fn test_rgba_color_interepolation() {
|
||||
assert_eq!(Color::RGBA(RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0 }).interpolate(
|
||||
&Color::RGBA(RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0 }), 1.0).unwrap(),
|
||||
Color::RGBA(RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0 }));
|
||||
fn test_rgba_color_interepolation_preserves_transparent() {
|
||||
assert_eq!(Color::RGBA(RGBA::transparent())
|
||||
.interpolate(&Color::RGBA(RGBA::transparent()), 0.5).unwrap(),
|
||||
Color::RGBA(RGBA::transparent()));
|
||||
}
|
||||
|
||||
assert_eq!(Color::RGBA(RGBA { red: 1.0, green: 0.0, blue: 0.0, alpha: 0.6 }).interpolate(
|
||||
&Color::RGBA(RGBA { red: 0.0, green: 1.0, blue: 0.0, alpha: 0.4 }), 0.5).unwrap(),
|
||||
Color::RGBA(RGBA { red: 0.6, green: 0.4, blue: 0.0, alpha: 0.5 }));
|
||||
#[test]
|
||||
fn test_rgba_color_interepolation_alpha() {
|
||||
assert_eq!(Color::RGBA(RGBA::new(200, 0, 0, 100))
|
||||
.interpolate(&Color::RGBA(RGBA::new(0, 200, 0, 200)), 0.5).unwrap(),
|
||||
Color::RGBA(RGBA::new(66, 133, 0, 150)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rgba_color_interepolation_out_of_range_1() {
|
||||
// Some cubic-bezier functions produce values that are out of range [0, 1].
|
||||
// Unclamped cases.
|
||||
assert_eq!(Color::RGBA(RGBA { red: 0.3, green: 0.0, blue: 0.0, alpha: 0.4 }).interpolate(
|
||||
&Color::RGBA(RGBA { red: 0.0, green: 1.0, blue: 0.0, alpha: 0.6 }), -0.5).unwrap(),
|
||||
Color::RGBA(RGBA { red: 0.6, green: 0.0, blue: 0.0, alpha: 0.3 }));
|
||||
|
||||
assert_eq!(Color::RGBA(RGBA { red: 1.0, green: 0.0, blue: 0.0, alpha: 0.6 }).interpolate(
|
||||
&Color::RGBA(RGBA { red: 0.0, green: 0.3, blue: 0.0, alpha: 0.4 }), 1.5).unwrap(),
|
||||
Color::RGBA(RGBA { red: 0.0, green: 0.6, blue: 0.0, alpha: 0.3 }));
|
||||
|
||||
// Clamped cases.
|
||||
assert_eq!(Color::RGBA(RGBA { red: 1.0, green: 0.0, blue: 0.0, alpha: 0.8 }).interpolate(
|
||||
&Color::RGBA(RGBA { red: 0.0, green: 1.0, blue: 0.0, alpha: 0.2 }), -0.5).unwrap(),
|
||||
Color::RGBA(RGBA { red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0 }));
|
||||
|
||||
assert_eq!(Color::RGBA(RGBA { red: 1.0, green: 0.0, blue: 0.0, alpha: 0.8 }).interpolate(
|
||||
&Color::RGBA(RGBA { red: 0.0, green: 1.0, blue: 0.0, alpha: 0.2 }), 1.5).unwrap(),
|
||||
Color::RGBA(RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0 }));
|
||||
assert_eq!(Color::RGBA(RGBA::from_floats(0.3, 0.0, 0.0, 0.4)).interpolate(
|
||||
&Color::RGBA(RGBA::from_floats(0.0, 1.0, 0.0, 0.6)), -0.5).unwrap(),
|
||||
Color::RGBA(RGBA::new(152, 0, 0, 76)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rgba_color_interepolation_out_of_range_2() {
|
||||
assert_eq!(Color::RGBA(RGBA::from_floats(1.0, 0.0, 0.0, 0.6)).interpolate(
|
||||
&Color::RGBA(RGBA::from_floats(0.0, 0.3, 0.0, 0.4)), 1.5).unwrap(),
|
||||
Color::RGBA(RGBA::new(0, 152, 0, 76)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rgba_color_interepolation_out_of_range_clamped_1() {
|
||||
assert_eq!(Color::RGBA(RGBA::from_floats(1.0, 0.0, 0.0, 0.8)).interpolate(
|
||||
&Color::RGBA(RGBA::from_floats(0.0, 1.0, 0.0, 0.2)), -0.5).unwrap(),
|
||||
Color::RGBA(RGBA::from_floats(1.0, 0.0, 0.0, 1.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rgba_color_interepolation_out_of_range_clamped_2() {
|
||||
assert_eq!(Color::RGBA(RGBA::from_floats(1.0, 0.0, 0.0, 0.8)).interpolate(
|
||||
&Color::RGBA(RGBA::from_floats(0.0, 1.0, 0.0, 0.2)), 1.5).unwrap(),
|
||||
Color::RGBA(RGBA::from_floats(0.0, 0.0, 0.0, 0.0)));
|
||||
}
|
||||
|
|
|
@ -231,15 +231,15 @@ mod shorthand_serialization {
|
|||
fn border_color_should_serialize_correctly() {
|
||||
let mut properties = Vec::new();
|
||||
|
||||
let red = DeclaredValue::Value(Box::new(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA { red: 1f32, green: 0f32, blue: 0f32, alpha: 1f32 }),
|
||||
let red = DeclaredValue::Value(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA::new(255, 0, 0, 255)),
|
||||
authored: None
|
||||
}));
|
||||
});
|
||||
|
||||
let blue = DeclaredValue::Value(Box::new(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA { red: 0f32, green: 0f32, blue: 1f32, alpha: 1f32 }),
|
||||
let blue = DeclaredValue::Value(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA::new(0, 0, 255, 255)),
|
||||
authored: None
|
||||
}));
|
||||
});
|
||||
|
||||
properties.push(PropertyDeclaration::BorderTopColor(blue.clone()));
|
||||
properties.push(PropertyDeclaration::BorderRightColor(red.clone()));
|
||||
|
@ -281,10 +281,10 @@ mod shorthand_serialization {
|
|||
|
||||
let width = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(4f32)));
|
||||
let style = DeclaredValue::Value(BorderStyle::solid);
|
||||
let color = DeclaredValue::Value(Box::new(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA { red: 1f32, green: 0f32, blue: 0f32, alpha: 1f32 }),
|
||||
let color = DeclaredValue::Value(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA::new(255, 0, 0, 255)),
|
||||
authored: None
|
||||
}));
|
||||
});
|
||||
|
||||
properties.push(PropertyDeclaration::BorderTopWidth(width));
|
||||
properties.push(PropertyDeclaration::BorderTopStyle(style));
|
||||
|
@ -300,10 +300,10 @@ mod shorthand_serialization {
|
|||
|
||||
let width = DeclaredValue::Value(BorderWidth::from_length(Length::from_px(4f32)));
|
||||
let style = DeclaredValue::Initial;
|
||||
let color = DeclaredValue::Value(Box::new(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA { red: 1f32, green: 0f32, blue: 0f32, alpha: 1f32 }),
|
||||
let color = DeclaredValue::Value(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA::new(255, 0, 0, 255)),
|
||||
authored: None
|
||||
}));
|
||||
});
|
||||
|
||||
properties.push(PropertyDeclaration::BorderTopWidth(width));
|
||||
properties.push(PropertyDeclaration::BorderTopStyle(style));
|
||||
|
@ -469,10 +469,10 @@ mod shorthand_serialization {
|
|||
|
||||
let width = DeclaredValue::Value(WidthContainer(Length::from_px(4f32)));
|
||||
let style = DeclaredValue::Value(Either::Second(BorderStyle::solid));
|
||||
let color = DeclaredValue::Value(Box::new(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA { red: 1f32, green: 0f32, blue: 0f32, alpha: 1f32 }),
|
||||
let color = DeclaredValue::Value(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA::new(255, 0, 0, 255)),
|
||||
authored: None
|
||||
}));
|
||||
});
|
||||
|
||||
properties.push(PropertyDeclaration::OutlineWidth(width));
|
||||
properties.push(PropertyDeclaration::OutlineStyle(style));
|
||||
|
@ -504,10 +504,10 @@ mod shorthand_serialization {
|
|||
|
||||
let width = DeclaredValue::Value(WidthContainer(Length::from_px(4f32)));
|
||||
let style = DeclaredValue::Initial;
|
||||
let color = DeclaredValue::Value(Box::new(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA { red: 1f32, green: 0f32, blue: 0f32, alpha: 1f32 }),
|
||||
let color = DeclaredValue::Value(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA::new(255, 0, 0, 255)),
|
||||
authored: None
|
||||
}));
|
||||
});
|
||||
properties.push(PropertyDeclaration::OutlineWidth(width));
|
||||
properties.push(PropertyDeclaration::OutlineStyle(style));
|
||||
properties.push(PropertyDeclaration::OutlineColor(color));
|
||||
|
@ -522,10 +522,10 @@ mod shorthand_serialization {
|
|||
|
||||
let width = DeclaredValue::Value(WidthContainer(Length::from_px(4f32)));
|
||||
let style = DeclaredValue::Value(Either::First(Auto));
|
||||
let color = DeclaredValue::Value(Box::new(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA { red: 1f32, green: 0f32, blue: 0f32, alpha: 1f32 }),
|
||||
let color = DeclaredValue::Value(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA::new(255, 0, 0, 255)),
|
||||
authored: None
|
||||
}));
|
||||
});
|
||||
properties.push(PropertyDeclaration::OutlineWidth(width));
|
||||
properties.push(PropertyDeclaration::OutlineStyle(style));
|
||||
properties.push(PropertyDeclaration::OutlineColor(color));
|
||||
|
@ -730,10 +730,10 @@ mod shorthand_serialization {
|
|||
fn background_should_serialize_all_available_properties_when_specified() {
|
||||
let mut properties = Vec::new();
|
||||
|
||||
let color = DeclaredValue::Value(Box::new(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA { red: 1f32, green: 0f32, blue: 0f32, alpha: 1f32 }),
|
||||
let color = DeclaredValue::Value(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA::new(255, 0, 0, 255)),
|
||||
authored: None
|
||||
}));
|
||||
});
|
||||
|
||||
let position_x = single_vec_value_typedef!(position_x,
|
||||
HorizontalPosition {
|
||||
|
@ -790,10 +790,10 @@ mod shorthand_serialization {
|
|||
fn background_should_combine_origin_and_clip_properties_when_equal() {
|
||||
let mut properties = Vec::new();
|
||||
|
||||
let color = DeclaredValue::Value(Box::new(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA { red: 1f32, green: 0f32, blue: 0f32, alpha: 1f32 }),
|
||||
let color = DeclaredValue::Value(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA::new(255, 0, 0, 255)),
|
||||
authored: None
|
||||
}));
|
||||
});
|
||||
|
||||
let position_x = single_vec_value_typedef!(position_x,
|
||||
HorizontalPosition {
|
||||
|
@ -849,10 +849,10 @@ mod shorthand_serialization {
|
|||
fn background_should_always_print_color_and_url_and_repeat_and_attachment_and_position() {
|
||||
let mut properties = Vec::new();
|
||||
|
||||
let color = DeclaredValue::Value(Box::new(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA { red: 1f32, green: 0f32, blue: 0f32, alpha: 1f32 }),
|
||||
let color = DeclaredValue::Value(CSSColor {
|
||||
parsed: ComputedColor::RGBA(RGBA::new(255, 0, 0, 255)),
|
||||
authored: None
|
||||
}));
|
||||
});
|
||||
|
||||
let position_x = single_vec_value_typedef!(position_x,
|
||||
HorizontalPosition {
|
||||
|
|
|
@ -28,17 +28,25 @@ fn size_of_specified_values() {
|
|||
let threshold = 40;
|
||||
let longhands = specified_value_sizes();
|
||||
|
||||
let mut failing_messages = vec![];
|
||||
|
||||
for specified_value in longhands {
|
||||
if specified_value.1 >= threshold && !specified_value.2 {
|
||||
panic!("Your changes have increased the size of {} SpecifiedValue to {}. The threshold is \
|
||||
currently {}. SpecifiedValues are affect size of PropertyDeclaration enum and \
|
||||
increasing the size may dramatically affect our memory footprint. Please consider \
|
||||
using `boxed=\"True\"` in this longhand.",
|
||||
specified_value.0, specified_value.1, threshold)
|
||||
failing_messages.push(
|
||||
format!("Your changes have increased the size of {} SpecifiedValue to {}. The threshold is \
|
||||
currently {}. SpecifiedValues are affect size of PropertyDeclaration enum and \
|
||||
increasing the size may dramatically affect our memory footprint. Please consider \
|
||||
using `boxed=\"True\"` in this longhand.",
|
||||
specified_value.0, specified_value.1, threshold));
|
||||
} else if specified_value.1 < threshold && specified_value.2 {
|
||||
panic!("Your changes have decreased the size of {} SpecifiedValue to {}. Good work! \
|
||||
The threshold is currently {}. Please consider removing `boxed=\"True\"` from this longhand.",
|
||||
specified_value.0, specified_value.1, threshold)
|
||||
failing_messages.push(
|
||||
format!("Your changes have decreased the size of {} SpecifiedValue to {}. Good work! \
|
||||
The threshold is currently {}. Please consider removing `boxed=\"True\"` from this longhand.",
|
||||
specified_value.0, specified_value.1, threshold));
|
||||
}
|
||||
}
|
||||
|
||||
if !failing_messages.is_empty() {
|
||||
panic!("{}", failing_messages.join("\n\n"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -183,12 +183,10 @@ fn test_parse_stylesheet() {
|
|||
block: Arc::new(RwLock::new(PropertyDeclarationBlock {
|
||||
declarations: vec![
|
||||
(PropertyDeclaration::BackgroundColor(DeclaredValue::Value(
|
||||
Box::new(longhands::background_color::SpecifiedValue {
|
||||
authored: Some("blue".to_owned()),
|
||||
parsed: cssparser::Color::RGBA(cssparser::RGBA {
|
||||
red: 0., green: 0., blue: 1., alpha: 1.
|
||||
}),
|
||||
})
|
||||
longhands::background_color::SpecifiedValue {
|
||||
authored: Some("blue".to_owned().into_boxed_str()),
|
||||
parsed: cssparser::Color::RGBA(cssparser::RGBA::new(0, 0, 255, 255)),
|
||||
}
|
||||
)),
|
||||
Importance::Normal),
|
||||
(PropertyDeclaration::BackgroundPositionX(DeclaredValue::Value(
|
||||
|
|
|
@ -14,7 +14,7 @@ doctest = false
|
|||
[dependencies]
|
||||
app_units = "0.3"
|
||||
atomic_refcell = "0.1"
|
||||
cssparser = {version = "0.7"}
|
||||
cssparser = {version = "0.8"}
|
||||
env_logger = "0.4"
|
||||
euclid = "0.10.1"
|
||||
lazy_static = "0.2"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue