| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use super::*; |
| 6 |
use cssparser::ParserInput; |
| 7 |
use serde_json::{json, Value}; |
| 8 |
|
| 9 |
fn almost_equals(a: &Value, b: &Value) -> bool { |
| 10 |
match (a, b) { |
| 11 |
(Value::Number(a), Value::Number(b)) => { |
| 12 |
let a = a.as_f64().unwrap(); |
| 13 |
let b = b.as_f64().unwrap(); |
| 14 |
(a - b).abs() <= a.abs() * 1e-6 |
| 15 |
} |
| 16 |
|
| 17 |
(&Value::Bool(a), &Value::Bool(b)) => a == b, |
| 18 |
(Value::String(a), Value::String(b)) => a == b, |
| 19 |
(Value::Array(a), Value::Array(b)) => { |
| 20 |
a.len() == b.len() && a.iter().zip(b.iter()).all(|(a, b)| almost_equals(a, b)) |
| 21 |
} |
| 22 |
(&Value::Object(_), &Value::Object(_)) => panic!("Not implemented"), |
| 23 |
(&Value::Null, &Value::Null) => true, |
| 24 |
_ => false, |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
fn assert_json_eq(results: Value, expected: Value, message: &str) { |
| 29 |
if !almost_equals(&results, &expected) { |
| 30 |
println!( |
| 31 |
"{}", |
| 32 |
::difference::Changeset::new( |
| 33 |
&serde_json::to_string_pretty(&results).unwrap(), |
| 34 |
&serde_json::to_string_pretty(&expected).unwrap(), |
| 35 |
"\n", |
| 36 |
) |
| 37 |
); |
| 38 |
panic!("{}", message) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
fn run_raw_json_tests<F: Fn(Value, Value)>(json_data: &str, run: F) { |
| 43 |
let items = match serde_json::from_str(json_data) { |
| 44 |
Ok(Value::Array(items)) => items, |
| 45 |
other => panic!("Invalid JSON: {:?}", other), |
| 46 |
}; |
| 47 |
assert!(items.len() % 2 == 0); |
| 48 |
let mut input = None; |
| 49 |
for item in items.into_iter() { |
| 50 |
match (&input, item) { |
| 51 |
(&None, json_obj) => input = Some(json_obj), |
| 52 |
(&Some(_), expected) => { |
| 53 |
let input = input.take().unwrap(); |
| 54 |
run(input, expected) |
| 55 |
} |
| 56 |
}; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
fn run_json_tests<F: Fn(&mut Parser) -> Value>(json_data: &str, parse: F) { |
| 61 |
run_raw_json_tests(json_data, |input, expected| match input { |
| 62 |
Value::String(input) => { |
| 63 |
let mut parse_input = ParserInput::new(&input); |
| 64 |
let result = parse(&mut Parser::new(&mut parse_input)); |
| 65 |
assert_json_eq(result, expected, &input); |
| 66 |
} |
| 67 |
_ => panic!("Unexpected JSON"), |
| 68 |
}); |
| 69 |
} |
| 70 |
fn run_color_tests<F: Fn(Result<Color, ()>) -> Value>(json_data: &str, to_json: F) { |
| 71 |
run_json_tests(json_data, |input| { |
| 72 |
let result: Result<_, ParseError<()>> = |
| 73 |
input.parse_entirely(|i| Color::parse(i).map_err(Into::into)); |
| 74 |
to_json(result.map_err(|_| ())) |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn color3() { |
| 80 |
run_color_tests(include_str!("../src/css-parsing-tests/color3.json"), |c| { |
| 81 |
c.ok() |
| 82 |
.map(|v| v.to_css_string().to_json()) |
| 83 |
.unwrap_or(Value::Null) |
| 84 |
}) |
| 85 |
} |
| 86 |
|
| 87 |
#[cfg_attr(all(miri, feature = "skip_long_tests"), ignore)] |
| 88 |
#[test] |
| 89 |
fn color3_hsl() { |
| 90 |
run_color_tests( |
| 91 |
include_str!("../src/css-parsing-tests/color3_hsl.json"), |
| 92 |
|c| { |
| 93 |
c.ok() |
| 94 |
.map(|v| v.to_css_string().to_json()) |
| 95 |
.unwrap_or(Value::Null) |
| 96 |
}, |
| 97 |
) |
| 98 |
} |
| 99 |
|
| 100 |
/// color3_keywords.json is different: R, G and B are in 0..255 rather than 0..1 |
| 101 |
#[test] |
| 102 |
fn color3_keywords() { |
| 103 |
run_color_tests( |
| 104 |
include_str!("../src/css-parsing-tests/color3_keywords.json"), |
| 105 |
|c| { |
| 106 |
c.ok() |
| 107 |
.map(|v| v.to_css_string().to_json()) |
| 108 |
.unwrap_or(Value::Null) |
| 109 |
}, |
| 110 |
) |
| 111 |
} |
| 112 |
|
| 113 |
#[cfg_attr(all(miri, feature = "skip_long_tests"), ignore)] |
| 114 |
#[test] |
| 115 |
fn color4_hwb() { |
| 116 |
run_color_tests( |
| 117 |
include_str!("../src/css-parsing-tests/color4_hwb.json"), |
| 118 |
|c| { |
| 119 |
c.ok() |
| 120 |
.map(|v| v.to_css_string().to_json()) |
| 121 |
.unwrap_or(Value::Null) |
| 122 |
}, |
| 123 |
) |
| 124 |
} |
| 125 |
|
| 126 |
#[cfg_attr(all(miri, feature = "skip_long_tests"), ignore)] |
| 127 |
#[test] |
| 128 |
fn color4_lab_lch_oklab_oklch() { |
| 129 |
run_color_tests( |
| 130 |
include_str!("../src/css-parsing-tests/color4_lab_lch_oklab_oklch.json"), |
| 131 |
|c| { |
| 132 |
c.ok() |
| 133 |
.map(|v| v.to_css_string().to_json()) |
| 134 |
.unwrap_or(Value::Null) |
| 135 |
}, |
| 136 |
) |
| 137 |
} |
| 138 |
|
| 139 |
#[test] |
| 140 |
fn color4_color_function() { |
| 141 |
run_color_tests( |
| 142 |
include_str!("../src/css-parsing-tests/color4_color_function.json"), |
| 143 |
|c| { |
| 144 |
c.ok() |
| 145 |
.map(|v| v.to_css_string().to_json()) |
| 146 |
.unwrap_or(Value::Null) |
| 147 |
}, |
| 148 |
) |
| 149 |
} |
| 150 |
|
| 151 |
macro_rules! parse_single_color { |
| 152 |
($i:expr) => {{ |
| 153 |
let input = $i; |
| 154 |
let mut input = ParserInput::new(input); |
| 155 |
let mut input = Parser::new(&mut input); |
| 156 |
Color::parse(&mut input).map_err(Into::<ParseError<()>>::into) |
| 157 |
}}; |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn color4_invalid_color_space() { |
| 162 |
let result = parse_single_color!("color(invalid 1 1 1)"); |
| 163 |
assert!(result.is_err()); |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn serialize_current_color() { |
| 168 |
let c = Color::CurrentColor; |
| 169 |
assert!(c.to_css_string() == "currentcolor"); |
| 170 |
} |
| 171 |
|
| 172 |
#[test] |
| 173 |
fn serialize_rgb_full_alpha() { |
| 174 |
let c = Color::Rgba(RgbaLegacy::new(255, 230, 204, 1.0)); |
| 175 |
assert_eq!(c.to_css_string(), "rgb(255, 230, 204)"); |
| 176 |
} |
| 177 |
|
| 178 |
#[test] |
| 179 |
fn serialize_rgba() { |
| 180 |
let c = Color::Rgba(RgbaLegacy::new(26, 51, 77, 0.125)); |
| 181 |
assert_eq!(c.to_css_string(), "rgba(26, 51, 77, 0.125)"); |
| 182 |
} |
| 183 |
|
| 184 |
#[test] |
| 185 |
fn serialize_rgba_two_digit_float_if_roundtrips() { |
| 186 |
let c = Color::Rgba(RgbaLegacy::from_floats(0., 0., 0., 0.5)); |
| 187 |
assert_eq!(c.to_css_string(), "rgba(0, 0, 0, 0.5)"); |
| 188 |
} |
| 189 |
|
| 190 |
trait ToJson { |
| 191 |
fn to_json(&self) -> Value; |
| 192 |
} |
| 193 |
|
| 194 |
impl<T> ToJson for T |
| 195 |
where |
| 196 |
T: Clone, |
| 197 |
Value: From<T>, |
| 198 |
{ |
| 199 |
fn to_json(&self) -> Value { |
| 200 |
Value::from(self.clone()) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
impl ToJson for Color { |
| 205 |
fn to_json(&self) -> Value { |
| 206 |
match *self { |
| 207 |
Color::CurrentColor => "currentcolor".to_json(), |
| 208 |
Color::Rgba(ref rgba) => { |
| 209 |
json!([rgba.red, rgba.green, rgba.blue, rgba.alpha]) |
| 210 |
} |
| 211 |
Color::Hsl(ref c) => json!([c.hue, c.saturation, c.lightness, c.alpha]), |
| 212 |
Color::Hwb(ref c) => json!([c.hue, c.whiteness, c.blackness, c.alpha]), |
| 213 |
Color::Lab(ref c) => json!([c.lightness, c.a, c.b, c.alpha]), |
| 214 |
Color::Lch(ref c) => json!([c.lightness, c.chroma, c.hue, c.alpha]), |
| 215 |
Color::Oklab(ref c) => json!([c.lightness, c.a, c.b, c.alpha]), |
| 216 |
Color::Oklch(ref c) => json!([c.lightness, c.chroma, c.hue, c.alpha]), |
| 217 |
Color::ColorFunction(ref c) => { |
| 218 |
json!([c.color_space.to_css_string(), c.c1, c.c2, c.c3, c.alpha]) |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn generic_parser() { |
| 226 |
#[derive(Debug, PartialEq)] |
| 227 |
enum OutputType { |
| 228 |
CurrentColor, |
| 229 |
Rgba(u8, u8, u8, f32), |
| 230 |
Hsl(Option<f32>, Option<f32>, Option<f32>, Option<f32>), |
| 231 |
Hwb(Option<f32>, Option<f32>, Option<f32>, Option<f32>), |
| 232 |
Lab(Option<f32>, Option<f32>, Option<f32>, Option<f32>), |
| 233 |
Lch(Option<f32>, Option<f32>, Option<f32>, Option<f32>), |
| 234 |
Oklab(Option<f32>, Option<f32>, Option<f32>, Option<f32>), |
| 235 |
Oklch(Option<f32>, Option<f32>, Option<f32>, Option<f32>), |
| 236 |
ColorFunction( |
| 237 |
PredefinedColorSpace, |
| 238 |
Option<f32>, |
| 239 |
Option<f32>, |
| 240 |
Option<f32>, |
| 241 |
Option<f32>, |
| 242 |
), |
| 243 |
} |
| 244 |
|
| 245 |
impl FromParsedColor for OutputType { |
| 246 |
fn from_current_color() -> Self { |
| 247 |
OutputType::CurrentColor |
| 248 |
} |
| 249 |
|
| 250 |
fn from_rgba(red: u8, green: u8, blue: u8, alpha: f32) -> Self { |
| 251 |
OutputType::Rgba(red, green, blue, alpha) |
| 252 |
} |
| 253 |
|
| 254 |
fn from_hsl( |
| 255 |
hue: Option<f32>, |
| 256 |
saturation: Option<f32>, |
| 257 |
lightness: Option<f32>, |
| 258 |
alpha: Option<f32>, |
| 259 |
) -> Self { |
| 260 |
OutputType::Hsl(hue, saturation, lightness, alpha) |
| 261 |
} |
| 262 |
|
| 263 |
fn from_hwb( |
| 264 |
hue: Option<f32>, |
| 265 |
blackness: Option<f32>, |
| 266 |
whiteness: Option<f32>, |
| 267 |
alpha: Option<f32>, |
| 268 |
) -> Self { |
| 269 |
OutputType::Hwb(hue, blackness, whiteness, alpha) |
| 270 |
} |
| 271 |
|
| 272 |
fn from_lab( |
| 273 |
lightness: Option<f32>, |
| 274 |
a: Option<f32>, |
| 275 |
b: Option<f32>, |
| 276 |
alpha: Option<f32>, |
| 277 |
) -> Self { |
| 278 |
OutputType::Lab(lightness, a, b, alpha) |
| 279 |
} |
| 280 |
|
| 281 |
fn from_lch( |
| 282 |
lightness: Option<f32>, |
| 283 |
chroma: Option<f32>, |
| 284 |
hue: Option<f32>, |
| 285 |
alpha: Option<f32>, |
| 286 |
) -> Self { |
| 287 |
OutputType::Lch(lightness, chroma, hue, alpha) |
| 288 |
} |
| 289 |
|
| 290 |
fn from_oklab( |
| 291 |
lightness: Option<f32>, |
| 292 |
a: Option<f32>, |
| 293 |
b: Option<f32>, |
| 294 |
alpha: Option<f32>, |
| 295 |
) -> Self { |
| 296 |
OutputType::Oklab(lightness, a, b, alpha) |
| 297 |
} |
| 298 |
|
| 299 |
fn from_oklch( |
| 300 |
lightness: Option<f32>, |
| 301 |
chroma: Option<f32>, |
| 302 |
hue: Option<f32>, |
| 303 |
alpha: Option<f32>, |
| 304 |
) -> Self { |
| 305 |
OutputType::Oklch(lightness, chroma, hue, alpha) |
| 306 |
} |
| 307 |
|
| 308 |
fn from_color_function( |
| 309 |
color_space: PredefinedColorSpace, |
| 310 |
c1: Option<f32>, |
| 311 |
c2: Option<f32>, |
| 312 |
c3: Option<f32>, |
| 313 |
alpha: Option<f32>, |
| 314 |
) -> Self { |
| 315 |
OutputType::ColorFunction(color_space, c1, c2, c3, alpha) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
struct TestColorParser; |
| 320 |
impl<'i> ColorParser<'i> for TestColorParser { |
| 321 |
type Output = OutputType; |
| 322 |
type Error = (); |
| 323 |
} |
| 324 |
|
| 325 |
#[rustfmt::skip] |
| 326 |
const TESTS: &[(&str, OutputType)] = &[ |
| 327 |
("currentColor", OutputType::CurrentColor), |
| 328 |
("rgb(1, 2, 3)", OutputType::Rgba(1, 2, 3, 1.0)), |
| 329 |
("rgba(1, 2, 3, 0.4)", OutputType::Rgba(1, 2, 3, 0.4)), |
| 330 |
("rgb(none none none / none)", OutputType::Rgba(0, 0, 0, 0.0)), |
| 331 |
("rgb(1 none 3 / none)", OutputType::Rgba(1, 0, 3, 0.0)), |
| 332 |
|
| 333 |
("hsla(45deg, 20%, 30%, 0.4)", OutputType::Hsl(Some(45.0), Some(0.2), Some(0.3), Some(0.4))), |
| 334 |
("hsl(45deg none none)", OutputType::Hsl(Some(45.0), None, None, Some(1.0))), |
| 335 |
("hsl(none 10% none / none)", OutputType::Hsl(None, Some(0.1), None, None)), |
| 336 |
("hsl(120 100.0% 50.0%)", OutputType::Hsl(Some(120.0), Some(1.0), Some(0.5), Some(1.0))), |
| 337 |
|
| 338 |
("hwb(45deg 20% 30% / 0.4)", OutputType::Hwb(Some(45.0), Some(0.2), Some(0.3), Some(0.4))), |
| 339 |
|
| 340 |
("lab(100 20 30 / 0.4)", OutputType::Lab(Some(100.0), Some(20.0), Some(30.0), Some(0.4))), |
| 341 |
("lch(100 20 30 / 0.4)", OutputType::Lch(Some(100.0), Some(20.0), Some(30.0), Some(0.4))), |
| 342 |
|
| 343 |
("oklab(100 20 30 / 0.4)", OutputType::Oklab(Some(100.0), Some(20.0), Some(30.0), Some(0.4))), |
| 344 |
("oklch(100 20 30 / 0.4)", OutputType::Oklch(Some(100.0), Some(20.0), Some(30.0), Some(0.4))), |
| 345 |
|
| 346 |
("color(srgb 0.1 0.2 0.3 / 0.4)", OutputType::ColorFunction(PredefinedColorSpace::Srgb, Some(0.1), Some(0.2), Some(0.3), Some(0.4))), |
| 347 |
("color(srgb none none none)", OutputType::ColorFunction(PredefinedColorSpace::Srgb, None, None, None, Some(1.0))), |
| 348 |
("color(srgb none none none / none)", OutputType::ColorFunction(PredefinedColorSpace::Srgb, None, None, None, None)), |
| 349 |
("color(srgb-linear 0.1 0.2 0.3 / 0.4)", OutputType::ColorFunction(PredefinedColorSpace::SrgbLinear, Some(0.1), Some(0.2), Some(0.3), Some(0.4))), |
| 350 |
("color(display-p3 0.1 0.2 0.3 / 0.4)", OutputType::ColorFunction(PredefinedColorSpace::DisplayP3, Some(0.1), Some(0.2), Some(0.3), Some(0.4))), |
| 351 |
("color(a98-rgb 0.1 0.2 0.3 / 0.4)", OutputType::ColorFunction(PredefinedColorSpace::A98Rgb, Some(0.1), Some(0.2), Some(0.3), Some(0.4))), |
| 352 |
("color(prophoto-rgb 0.1 0.2 0.3 / 0.4)", OutputType::ColorFunction(PredefinedColorSpace::ProphotoRgb, Some(0.1), Some(0.2), Some(0.3), Some(0.4))), |
| 353 |
("color(rec2020 0.1 0.2 0.3 / 0.4)", OutputType::ColorFunction(PredefinedColorSpace::Rec2020, Some(0.1), Some(0.2), Some(0.3), Some(0.4))), |
| 354 |
("color(xyz-d50 0.1 0.2 0.3 / 0.4)", OutputType::ColorFunction(PredefinedColorSpace::XyzD50, Some(0.1), Some(0.2), Some(0.3), Some(0.4))), |
| 355 |
("color(xyz-d65 0.1 0.2 0.3 / 0.4)", OutputType::ColorFunction(PredefinedColorSpace::XyzD65, Some(0.1), Some(0.2), Some(0.3), Some(0.4))), |
| 356 |
]; |
| 357 |
|
| 358 |
for (input, expected) in TESTS { |
| 359 |
let mut input = ParserInput::new(input); |
| 360 |
let mut input = Parser::new(&mut input); |
| 361 |
|
| 362 |
let actual: OutputType = parse_color_with(&TestColorParser, &mut input).unwrap(); |
| 363 |
assert_eq!(actual, *expected); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
#[test] |
| 368 |
fn serialize_modern_components() { |
| 369 |
// None. |
| 370 |
assert_eq!(ModernComponent(&None).to_css_string(), "none".to_string()); |
| 371 |
|
| 372 |
// Finite values. |
| 373 |
assert_eq!( |
| 374 |
ModernComponent(&Some(10.0)).to_css_string(), |
| 375 |
"10".to_string() |
| 376 |
); |
| 377 |
assert_eq!( |
| 378 |
ModernComponent(&Some(-10.0)).to_css_string(), |
| 379 |
"-10".to_string() |
| 380 |
); |
| 381 |
assert_eq!(ModernComponent(&Some(0.0)).to_css_string(), "0".to_string()); |
| 382 |
assert_eq!( |
| 383 |
ModernComponent(&Some(-0.0)).to_css_string(), |
| 384 |
"0".to_string() |
| 385 |
); |
| 386 |
|
| 387 |
// Infinite values. |
| 388 |
assert_eq!( |
| 389 |
ModernComponent(&Some(f32::INFINITY)).to_css_string(), |
| 390 |
"calc(infinity)".to_string() |
| 391 |
); |
| 392 |
assert_eq!( |
| 393 |
ModernComponent(&Some(f32::NEG_INFINITY)).to_css_string(), |
| 394 |
"calc(-infinity)".to_string() |
| 395 |
); |
| 396 |
|
| 397 |
// NaN. |
| 398 |
assert_eq!( |
| 399 |
ModernComponent(&Some(f32::NAN)).to_css_string(), |
| 400 |
"calc(NaN)".to_string() |
| 401 |
); |
| 402 |
} |