1
//! Shared types for CSS shadow properties (used by both `box-shadow` and `text-shadow`).
2

            
3
use alloc::string::{String, ToString};
4
use core::fmt;
5

            
6
use crate::{
7
    corety::AzString,
8
    props::{
9
        basic::{
10
            color::{parse_css_color, ColorU, CssColorParseError, CssColorParseErrorOwned},
11
            pixel::{
12
                parse_pixel_value_no_percent, CssPixelValueParseError,
13
                CssPixelValueParseErrorOwned, PixelValueNoPercent,
14
            },
15
        },
16
        formatter::PrintAsCssValue,
17
    },
18
};
19

            
20
/// What direction should a `box-shadow` be clipped in (inset or outset).
21
#[derive(Debug, Default, Copy, Clone, PartialEq, Ord, PartialOrd, Eq, Hash)]
22
#[repr(C)]
23
pub enum BoxShadowClipMode {
24
    #[default]
25
    Outset,
26
    Inset,
27
}
28

            
29
impl fmt::Display for BoxShadowClipMode {
30
11
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31
11
        match self {
32
3
            Self::Outset => Ok(()), // Outset is the default, not written
33
8
            Self::Inset => write!(f, "inset"),
34
        }
35
11
    }
36
}
37

            
38
/// Represents a single CSS shadow value, shared by both `box-shadow` and `text-shadow`.
39
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
40
#[repr(C)]
41
pub struct StyleBoxShadow {
42
    pub offset_x: PixelValueNoPercent,
43
    pub offset_y: PixelValueNoPercent,
44
    pub blur_radius: PixelValueNoPercent,
45
    pub spread_radius: PixelValueNoPercent,
46
    pub clip_mode: BoxShadowClipMode,
47
    pub color: ColorU,
48
}
49

            
50
impl Default for StyleBoxShadow {
51
5846
    fn default() -> Self {
52
5846
        Self {
53
5846
            offset_x: PixelValueNoPercent::default(),
54
5846
            offset_y: PixelValueNoPercent::default(),
55
5846
            blur_radius: PixelValueNoPercent::default(),
56
5846
            spread_radius: PixelValueNoPercent::default(),
57
5846
            clip_mode: BoxShadowClipMode::default(),
58
5846
            color: ColorU::BLACK,
59
5846
        }
60
5846
    }
61
}
62

            
63
impl StyleBoxShadow {
64
    /// Scales the pixel values of the shadow for a given DPI factor.
65
36
    pub fn scale_for_dpi(&mut self, scale_factor: f32) {
66
36
        self.offset_x.scale_for_dpi(scale_factor);
67
36
        self.offset_y.scale_for_dpi(scale_factor);
68
36
        self.blur_radius.scale_for_dpi(scale_factor);
69
36
        self.spread_radius.scale_for_dpi(scale_factor);
70
36
    }
71
}
72

            
73
impl PrintAsCssValue for StyleBoxShadow {
74
37
    fn print_as_css_value(&self) -> String {
75
37
        let mut components = Vec::new();
76

            
77
37
        if self.clip_mode == BoxShadowClipMode::Inset {
78
13
            components.push("inset".to_string());
79
24
        }
80
37
        components.push(self.offset_x.to_string());
81
37
        components.push(self.offset_y.to_string());
82

            
83
        // Only print blur, spread, and color if they are not default, for brevity
84
37
        if self.blur_radius.inner.number.get() != 0.0
85
19
            || self.spread_radius.inner.number.get() != 0.0
86
23
        {
87
23
            components.push(self.blur_radius.to_string());
88
23
        }
89
37
        if self.spread_radius.inner.number.get() != 0.0 {
90
12
            components.push(self.spread_radius.to_string());
91
25
        }
92
37
        if self.color != ColorU::BLACK {
93
19
            // Assuming black is the default
94
19
            components.push(self.color.to_hash());
95
19
        }
96

            
97
37
        components.join(" ")
98
37
    }
99
}
100

            
101
// Formatting to Rust code for StyleBoxShadow
102
impl crate::codegen::format::FormatAsRustCode for StyleBoxShadow {
103
9
    fn format_as_rust_code(&self, tabs: usize) -> String {
104
9
        let t = String::from("    ").repeat(tabs);
105
9
        format!(
106
9
            "StyleBoxShadow {{\r\n{}    offset_x: {},\r\n{}    offset_y: {},\r\n{}    color: \
107
9
             {},\r\n{}    blur_radius: {},\r\n{}    spread_radius: {},\r\n{}    clip_mode: \
108
9
             BoxShadowClipMode::{:?},\r\n{}}}",
109
            t,
110
9
            crate::codegen::format::format_pixel_value_no_percent(&self.offset_x),
111
            t,
112
9
            crate::codegen::format::format_pixel_value_no_percent(&self.offset_y),
113
            t,
114
9
            crate::codegen::format::format_color_value(&self.color),
115
            t,
116
9
            crate::codegen::format::format_pixel_value_no_percent(&self.blur_radius),
117
            t,
118
9
            crate::codegen::format::format_pixel_value_no_percent(&self.spread_radius),
119
            t,
120
            self.clip_mode,
121
            t
122
        )
123
9
    }
124
}
125

            
126
// --- PARSER ---
127

            
128
/// Error returned when parsing a CSS shadow value fails.
129
#[derive(Clone, PartialEq)]
130
pub enum CssShadowParseError<'a> {
131
    TooManyOrTooFewComponents(&'a str),
132
    ValueParseErr(CssPixelValueParseError<'a>),
133
    ColorParseError(CssColorParseError<'a>),
134
}
135

            
136
impl_debug_as_display!(CssShadowParseError<'a>);
137
impl_display! { CssShadowParseError<'a>, {
138
    TooManyOrTooFewComponents(e) => format!("Expected 2 to 4 length values for box-shadow, found an invalid number of components in: \"{}\"", e),
139
    ValueParseErr(e) => format!("Invalid length value in box-shadow: {}", e),
140
    ColorParseError(e) => format!("Invalid color value in box-shadow: {}", e),
141
}}
142

            
143
impl_from!(
144
    CssPixelValueParseError<'a>,
145
    CssShadowParseError::ValueParseErr
146
);
147
impl_from!(CssColorParseError<'a>, CssShadowParseError::ColorParseError);
148

            
149
/// Owned version of `CssShadowParseError`.
150
#[derive(Debug, Clone, PartialEq)]
151
#[repr(C, u8)]
152
pub enum CssShadowParseErrorOwned {
153
    TooManyOrTooFewComponents(AzString),
154
    ValueParseErr(CssPixelValueParseErrorOwned),
155
    ColorParseError(CssColorParseErrorOwned),
156
}
157

            
158
impl CssShadowParseError<'_> {
159
    /// Converts the borrowed error into an owned version for storage.
160
    #[must_use]
161
58
    pub fn to_contained(&self) -> CssShadowParseErrorOwned {
162
58
        match self {
163
24
            CssShadowParseError::TooManyOrTooFewComponents(s) => {
164
24
                CssShadowParseErrorOwned::TooManyOrTooFewComponents((*s).to_string().into())
165
            }
166
22
            CssShadowParseError::ValueParseErr(e) => {
167
22
                CssShadowParseErrorOwned::ValueParseErr(e.to_contained())
168
            }
169
12
            CssShadowParseError::ColorParseError(e) => {
170
12
                CssShadowParseErrorOwned::ColorParseError(e.to_contained())
171
            }
172
        }
173
58
    }
174
}
175

            
176
impl CssShadowParseErrorOwned {
177
    /// Converts the owned error back into a borrowed version.
178
    #[must_use]
179
30
    pub fn to_shared(&self) -> CssShadowParseError<'_> {
180
30
        match self {
181
12
            Self::TooManyOrTooFewComponents(s) => {
182
12
                CssShadowParseError::TooManyOrTooFewComponents(s.as_str())
183
            }
184
12
            Self::ValueParseErr(e) => CssShadowParseError::ValueParseErr(e.to_shared()),
185
6
            Self::ColorParseError(e) => CssShadowParseError::ColorParseError(e.to_shared()),
186
        }
187
30
    }
188
}
189

            
190
/// Parses a CSS box-shadow, such as `"5px 10px #888 inset"`.
191
///
192
/// Note: This parser does not handle the `none` keyword, as that is handled by the
193
/// `CssPropertyValue` enum wrapper. It also does not handle comma-separated lists
194
/// of multiple shadows; it only parses a single shadow value.
195
#[cfg(feature = "parser")]
196
/// # Errors
197
///
198
/// Returns an error if `input` is not a valid CSS `box-shadow` value.
199
5808
pub fn parse_style_box_shadow(input: &str) -> Result<StyleBoxShadow, CssShadowParseError<'_>> {
200
5808
    let mut parts: Vec<&str> = input.split_whitespace().collect();
201
5808
    let mut shadow = StyleBoxShadow::default();
202

            
203
    // The `inset` keyword can appear anywhere. Find it, set the flag, and remove it.
204
89382
    if let Some(pos) = parts.iter().position(|&p| p == "inset") {
205
24
        shadow.clip_mode = BoxShadowClipMode::Inset;
206
24
        parts.remove(pos);
207
5784
    }
208

            
209
    // The color can also be anywhere. Find it, set the color, and remove it.
210
    // It's the only part that isn't a length. We iterate from the back because
211
    // it's slightly more common for the color to be last.
212
5808
    if let Some((pos, color)) = parts
213
5808
        .iter()
214
5808
        .enumerate()
215
5808
        .rev()
216
88732
        .find_map(|(i, p)| parse_css_color(p).ok().map(|c| (i, c)))
217
240
    {
218
240
        shadow.color = color;
219
240
        parts.remove(pos);
220
5568
    }
221

            
222
    // The remaining parts must be 2, 3, or 4 length values.
223
5808
    match parts.len() {
224
5781
        2..=4 => {
225
307
            shadow.offset_x = parse_pixel_value_no_percent(parts[0])?;
226
292
            shadow.offset_y = parse_pixel_value_no_percent(parts[1])?;
227
289
            if parts.len() > 2 {
228
230
                shadow.blur_radius = parse_pixel_value_no_percent(parts[2])?;
229
59
            }
230
278
            if parts.len() > 3 {
231
12
                shadow.spread_radius = parse_pixel_value_no_percent(parts[3])?;
232
266
            }
233
        }
234
5501
        _ => return Err(CssShadowParseError::TooManyOrTooFewComponents(input)),
235
    }
236

            
237
278
    Ok(shadow)
238
5808
}
239

            
240
#[cfg(all(test, feature = "parser"))]
241
mod tests {
242
    use super::*;
243
    use crate::props::basic::pixel::PixelValue;
244

            
245
17
    fn px_no_percent(val: f32) -> PixelValueNoPercent {
246
17
        PixelValueNoPercent {
247
17
            inner: PixelValue::px(val),
248
17
        }
249
17
    }
250

            
251
    #[test]
252
1
    fn test_parse_box_shadow_simple() {
253
1
        let result = parse_style_box_shadow("10px 5px").unwrap();
254
1
        assert_eq!(result.offset_x, px_no_percent(10.0));
255
1
        assert_eq!(result.offset_y, px_no_percent(5.0));
256
1
        assert_eq!(result.blur_radius, px_no_percent(0.0));
257
1
        assert_eq!(result.spread_radius, px_no_percent(0.0));
258
1
        assert_eq!(result.color, ColorU::BLACK);
259
1
        assert_eq!(result.clip_mode, BoxShadowClipMode::Outset);
260
1
    }
261

            
262
    #[test]
263
1
    fn test_parse_box_shadow_with_color() {
264
1
        let result = parse_style_box_shadow("10px 5px #888").unwrap();
265
1
        assert_eq!(result.offset_x, px_no_percent(10.0));
266
1
        assert_eq!(result.offset_y, px_no_percent(5.0));
267
1
        assert_eq!(result.color, ColorU::new_rgb(0x88, 0x88, 0x88));
268
1
    }
269

            
270
    #[test]
271
1
    fn test_parse_box_shadow_with_blur() {
272
1
        let result = parse_style_box_shadow("5px 10px 20px").unwrap();
273
1
        assert_eq!(result.offset_x, px_no_percent(5.0));
274
1
        assert_eq!(result.offset_y, px_no_percent(10.0));
275
1
        assert_eq!(result.blur_radius, px_no_percent(20.0));
276
1
    }
277

            
278
    #[test]
279
1
    fn test_parse_box_shadow_with_spread() {
280
1
        let result = parse_style_box_shadow("2px 2px 2px 1px rgba(0,0,0,0.2)").unwrap();
281
1
        assert_eq!(result.offset_x, px_no_percent(2.0));
282
1
        assert_eq!(result.offset_y, px_no_percent(2.0));
283
1
        assert_eq!(result.blur_radius, px_no_percent(2.0));
284
1
        assert_eq!(result.spread_radius, px_no_percent(1.0));
285
1
        assert_eq!(result.color, ColorU::new(0, 0, 0, 51));
286
1
    }
287

            
288
    #[test]
289
1
    fn test_parse_box_shadow_inset() {
290
1
        let result = parse_style_box_shadow("inset 0 0 10px #000").unwrap();
291
1
        assert_eq!(result.clip_mode, BoxShadowClipMode::Inset);
292
1
        assert_eq!(result.offset_x, px_no_percent(0.0));
293
1
        assert_eq!(result.offset_y, px_no_percent(0.0));
294
1
        assert_eq!(result.blur_radius, px_no_percent(10.0));
295
1
        assert_eq!(result.color, ColorU::BLACK);
296
1
    }
297

            
298
    #[test]
299
1
    fn test_parse_box_shadow_mixed_order() {
300
1
        let result = parse_style_box_shadow("5px 1em red inset").unwrap();
301
1
        assert_eq!(result.clip_mode, BoxShadowClipMode::Inset);
302
1
        assert_eq!(result.offset_x, px_no_percent(5.0));
303
1
        assert_eq!(
304
            result.offset_y,
305
1
            PixelValueNoPercent {
306
1
                inner: PixelValue::em(1.0)
307
1
            }
308
        );
309
1
        assert_eq!(result.color, ColorU::RED);
310
1
    }
311

            
312
    #[test]
313
1
    fn test_parse_box_shadow_invalid() {
314
1
        assert!(parse_style_box_shadow("10px").is_err());
315
1
        assert!(parse_style_box_shadow("10px 5px 4px 3px 2px").is_err());
316
        // Two colors: rposition picks "blue" as the color, leaving "red" which
317
        // fails to parse as a pixel value.
318
1
        assert!(parse_style_box_shadow("10px 5px red blue").is_err());
319
1
        assert!(parse_style_box_shadow("10% 5px").is_err()); // No percent allowed
320
1
    }
321
}
322

            
323
#[cfg(all(test, feature = "parser"))]
324
mod autotest_generated {
325
    use super::*;
326
    use crate::{
327
        codegen::format::FormatAsRustCode,
328
        props::basic::{
329
            pixel::{CssPixelValueParseError, PixelValue},
330
            SizeMetric,
331
        },
332
    };
333

            
334
    fn px(val: f32) -> PixelValueNoPercent {
335
        PixelValueNoPercent {
336
            inner: PixelValue::px(val),
337
        }
338
    }
339

            
340
    const fn shadow(
341
        offset_x: PixelValueNoPercent,
342
        offset_y: PixelValueNoPercent,
343
        blur_radius: PixelValueNoPercent,
344
        spread_radius: PixelValueNoPercent,
345
        clip_mode: BoxShadowClipMode,
346
        color: ColorU,
347
    ) -> StyleBoxShadow {
348
        StyleBoxShadow {
349
            offset_x,
350
            offset_y,
351
            blur_radius,
352
            spread_radius,
353
            clip_mode,
354
            color,
355
        }
356
    }
357

            
358
    /// Every component of a shadow, as raw f32s.
359
    fn numbers(s: &StyleBoxShadow) -> [f32; 4] {
360
        [
361
            s.offset_x.inner.number.get(),
362
            s.offset_y.inner.number.get(),
363
            s.blur_radius.inner.number.get(),
364
            s.spread_radius.inner.number.get(),
365
        ]
366
    }
367

            
368
    /// A representative corpus of shadows that are exactly representable in the
369
    /// fixed-point `FloatValue` encoding (<= 3 decimal places).
370
    fn round_trip_corpus() -> Vec<StyleBoxShadow> {
371
        vec![
372
            StyleBoxShadow::default(),
373
            shadow(
374
                px(1.0),
375
                px(2.0),
376
                px(0.0),
377
                px(0.0),
378
                BoxShadowClipMode::Outset,
379
                ColorU::BLACK,
380
            ),
381
            // blur only
382
            shadow(
383
                px(1.0),
384
                px(2.0),
385
                px(3.0),
386
                px(0.0),
387
                BoxShadowClipMode::Outset,
388
                ColorU::BLACK,
389
            ),
390
            // spread only: forces a "0px" blur to be printed as a placeholder
391
            shadow(
392
                px(1.0),
393
                px(2.0),
394
                px(0.0),
395
                px(4.0),
396
                BoxShadowClipMode::Outset,
397
                ColorU::BLACK,
398
            ),
399
            // negative offsets + inset + named color
400
            shadow(
401
                px(-5.5),
402
                px(-7.25),
403
                px(2.5),
404
                px(1.125),
405
                BoxShadowClipMode::Inset,
406
                ColorU::RED,
407
            ),
408
            // non-px metric
409
            shadow(
410
                PixelValueNoPercent {
411
                    inner: PixelValue::em(1.5),
412
                },
413
                PixelValueNoPercent {
414
                    inner: PixelValue::pt(2.0),
415
                },
416
                px(0.0),
417
                px(0.0),
418
                BoxShadowClipMode::Inset,
419
                ColorU::BLACK,
420
            ),
421
            // fully transparent black -- differs from ColorU::BLACK only in alpha
422
            shadow(
423
                px(0.0),
424
                px(0.0),
425
                px(0.0),
426
                px(0.0),
427
                BoxShadowClipMode::Outset,
428
                ColorU::new(0, 0, 0, 0),
429
            ),
430
            // every channel distinct, incl. a non-opaque alpha
431
            shadow(
432
                px(0.0),
433
                px(0.0),
434
                px(9.0),
435
                px(0.0),
436
                BoxShadowClipMode::Inset,
437
                ColorU::new(1, 2, 3, 4),
438
            ),
439
        ]
440
    }
441

            
442
    // ---------------------------------------------------------------
443
    // serializer: BoxShadowClipMode::fmt
444
    // ---------------------------------------------------------------
445

            
446
    #[test]
447
    fn clip_mode_display_outset_is_empty_inset_is_keyword() {
448
        // Outset is the CSS default and is deliberately NOT written out.
449
        assert_eq!(BoxShadowClipMode::Outset.to_string(), "");
450
        assert_eq!(BoxShadowClipMode::Inset.to_string(), "inset");
451
    }
452

            
453
    #[test]
454
    fn clip_mode_display_default_does_not_panic() {
455
        let default: BoxShadowClipMode = Default::default();
456
        assert_eq!(default, BoxShadowClipMode::Outset);
457
        assert_eq!(default.to_string(), "");
458
        // Debug (derived) must stay non-empty even though Display is empty.
459
        assert_eq!(format!("{:?}", BoxShadowClipMode::Outset), "Outset");
460
        assert_eq!(format!("{:?}", BoxShadowClipMode::Inset), "Inset");
461
    }
462

            
463
    #[test]
464
    fn clip_mode_display_with_format_flags_does_not_panic() {
465
        // The impl writes straight to the formatter, so width/fill/precision are
466
        // ignored rather than applied -- assert only that nothing panics and the
467
        // keyword survives.
468
        assert!(format!("{:>16}", BoxShadowClipMode::Inset).contains("inset"));
469
        assert!(format!("{:*<16}", BoxShadowClipMode::Inset).contains("inset"));
470
        assert!(format!("{:.2}", BoxShadowClipMode::Inset).contains("inset"));
471
        // Outset writes nothing at all, whatever the flags.
472
        assert_eq!(format!("{:>16}", BoxShadowClipMode::Outset), "");
473
    }
474

            
475
    #[test]
476
    fn clip_mode_display_is_repeatable_and_ordered() {
477
        // Same value must serialize identically every time.
478
        for _ in 0..4 {
479
            assert_eq!(BoxShadowClipMode::Inset.to_string(), "inset");
480
        }
481
        // Derived Ord: the default variant sorts first.
482
        assert!(BoxShadowClipMode::Outset < BoxShadowClipMode::Inset);
483
    }
484

            
485
    // ---------------------------------------------------------------
486
    // numeric: StyleBoxShadow::scale_for_dpi
487
    // ---------------------------------------------------------------
488

            
489
    fn scaled(mut s: StyleBoxShadow, factor: f32) -> StyleBoxShadow {
490
        s.scale_for_dpi(factor);
491
        s
492
    }
493

            
494
    fn all_ones() -> StyleBoxShadow {
495
        shadow(
496
            px(1.0),
497
            px(2.0),
498
            px(4.0),
499
            px(8.0),
500
            BoxShadowClipMode::Inset,
501
            ColorU::RED,
502
        )
503
    }
504

            
505
    #[test]
506
    fn scale_for_dpi_zero_zeroes_every_length() {
507
        let s = scaled(all_ones(), 0.0);
508
        assert_eq!(numbers(&s), [0.0, 0.0, 0.0, 0.0]);
509
        // -0.0 must not leak a negative zero into the fixed-point encoding.
510
        let neg = scaled(all_ones(), -0.0);
511
        assert_eq!(numbers(&neg), [0.0, 0.0, 0.0, 0.0]);
512
        assert_eq!(neg.offset_x.inner.number.number(), 0);
513
    }
514

            
515
    #[test]
516
    fn scale_for_dpi_identity_and_double() {
517
        let s = scaled(all_ones(), 1.0);
518
        assert_eq!(numbers(&s), [1.0, 2.0, 4.0, 8.0]);
519
        let d = scaled(all_ones(), 2.0);
520
        assert_eq!(numbers(&d), [2.0, 4.0, 8.0, 16.0]);
521
        let h = scaled(all_ones(), 0.5);
522
        assert_eq!(numbers(&h), [0.5, 1.0, 2.0, 4.0]);
523
    }
524

            
525
    #[test]
526
    fn scale_for_dpi_negative_flips_sign_deterministically() {
527
        let s = scaled(all_ones(), -1.5);
528
        assert_eq!(numbers(&s), [-1.5, -3.0, -6.0, -12.0]);
529
    }
530

            
531
    #[test]
532
    fn scale_for_dpi_nan_yields_zero_never_nan() {
533
        // `f32 as isize` saturates and maps NaN -> 0, so a NaN scale factor
534
        // collapses the shadow to zero instead of poisoning it with NaN.
535
        let s = scaled(all_ones(), f32::NAN);
536
        for n in numbers(&s) {
537
            assert!(!n.is_nan(), "NaN leaked into the fixed-point encoding");
538
            assert_eq!(n, 0.0);
539
        }
540
    }
541

            
542
    #[test]
543
    fn scale_for_dpi_infinity_saturates_to_finite() {
544
        let pos = scaled(all_ones(), f32::INFINITY);
545
        for n in numbers(&pos) {
546
            assert!(n.is_finite(), "+inf scale produced a non-finite value");
547
            assert!(n > 0.0);
548
        }
549

            
550
        let neg = scaled(all_ones(), f32::NEG_INFINITY);
551
        for n in numbers(&neg) {
552
            assert!(n.is_finite(), "-inf scale produced a non-finite value");
553
            assert!(n < 0.0);
554
        }
555
    }
556

            
557
    #[test]
558
    fn scale_for_dpi_float_extremes_do_not_panic() {
559
        // MAX: 1.0 * f32::MAX * 1000.0 overflows f32 -> inf -> saturates on cast.
560
        for n in numbers(&scaled(all_ones(), f32::MAX)) {
561
            assert!(n.is_finite());
562
            assert!(n > 0.0);
563
        }
564
        for n in numbers(&scaled(all_ones(), f32::MIN)) {
565
            assert!(n.is_finite());
566
            assert!(n < 0.0);
567
        }
568
        // Subnormal / tiny factors underflow to exactly zero (3-decimal precision).
569
        assert_eq!(numbers(&scaled(all_ones(), f32::MIN_POSITIVE)), [0.0; 4]);
570
        assert_eq!(numbers(&scaled(all_ones(), f32::EPSILON)), [0.0; 4]);
571
    }
572

            
573
    #[test]
574
    fn scale_for_dpi_saturation_is_stable_under_repetition() {
575
        // Scaling an already-saturated shadow again must stay finite (no panic,
576
        // no NaN, no wraparound to the opposite sign).
577
        let mut s = all_ones();
578
        for _ in 0..8 {
579
            s.scale_for_dpi(1e30);
580
            for n in numbers(&s) {
581
                assert!(n.is_finite());
582
                assert!(n > 0.0, "saturating scale wrapped to a negative value");
583
            }
584
        }
585
    }
586

            
587
    #[test]
588
    fn scale_for_dpi_quantizes_below_the_fixed_point_precision() {
589
        // FloatValue keeps 3 decimals; anything smaller truncates to zero.
590
        let s = scaled(all_ones(), 0.0001);
591
        assert_eq!(numbers(&s), [0.0, 0.0, 0.0, 0.0]);
592
    }
593

            
594
    #[test]
595
    fn scale_for_dpi_on_default_is_a_noop() {
596
        for factor in [0.0, 1.0, 2.0, -3.0, f32::NAN, f32::INFINITY, f32::MAX] {
597
            let s = scaled(StyleBoxShadow::default(), factor);
598
            assert_eq!(
599
                numbers(&s),
600
                [0.0, 0.0, 0.0, 0.0],
601
                "default shadow changed under scale {factor}"
602
            );
603
        }
604
    }
605

            
606
    #[test]
607
    fn scale_for_dpi_preserves_metric_color_and_clip_mode() {
608
        let mut s = shadow(
609
            PixelValueNoPercent {
610
                inner: PixelValue::em(2.0),
611
            },
612
            PixelValueNoPercent {
613
                inner: PixelValue::pt(3.0),
614
            },
615
            PixelValueNoPercent {
616
                inner: PixelValue::rem(4.0),
617
            },
618
            px(5.0),
619
            BoxShadowClipMode::Inset,
620
            ColorU::new(1, 2, 3, 4),
621
        );
622
        s.scale_for_dpi(2.0);
623

            
624
        // Only the *numbers* are scaled -- units are never converted.
625
        assert_eq!(s.offset_x.inner.metric, SizeMetric::Em);
626
        assert_eq!(s.offset_y.inner.metric, SizeMetric::Pt);
627
        assert_eq!(s.blur_radius.inner.metric, SizeMetric::Rem);
628
        assert_eq!(s.spread_radius.inner.metric, SizeMetric::Px);
629
        assert_eq!(numbers(&s), [4.0, 6.0, 8.0, 10.0]);
630

            
631
        // ... and the non-numeric fields are untouched.
632
        assert_eq!(s.clip_mode, BoxShadowClipMode::Inset);
633
        assert_eq!(s.color, ColorU::new(1, 2, 3, 4));
634
    }
635

            
636
    // ---------------------------------------------------------------
637
    // getters: CssShadowParseError::to_contained / ..Owned::to_shared
638
    // ---------------------------------------------------------------
639

            
640
    /// One error of each `CssShadowParseError` variant.
641
    fn error_corpus() -> Vec<CssShadowParseError<'static>> {
642
        vec![
643
            CssShadowParseError::TooManyOrTooFewComponents("1px"),
644
            CssShadowParseError::TooManyOrTooFewComponents(""),
645
            CssShadowParseError::TooManyOrTooFewComponents("\u{1F600}\u{0301} \u{202E}"),
646
            CssShadowParseError::ValueParseErr(CssPixelValueParseError::EmptyString),
647
            CssShadowParseError::ValueParseErr(CssPixelValueParseError::InvalidPixelValue("abc")),
648
            CssShadowParseError::ValueParseErr(CssPixelValueParseError::NoValueGiven(
649
                "px",
650
                SizeMetric::Px,
651
            )),
652
            CssShadowParseError::ValueParseErr(CssPixelValueParseError::ValueParseErr(
653
                "x".parse::<f32>().unwrap_err(),
654
                "x",
655
            )),
656
            CssShadowParseError::ValueParseErr(CssPixelValueParseError::ValueParseErr(
657
                "".parse::<f32>().unwrap_err(),
658
                "",
659
            )),
660
            CssShadowParseError::ColorParseError(parse_css_color("notacolor").unwrap_err()),
661
            CssShadowParseError::ColorParseError(parse_css_color("#gg").unwrap_err()),
662
            CssShadowParseError::ColorParseError(parse_css_color("rgb(1,2").unwrap_err()),
663
        ]
664
    }
665

            
666
    #[test]
667
    fn shadow_error_to_contained_to_shared_is_lossless() {
668
        for e in error_corpus() {
669
            let owned = e.to_contained();
670
            assert_eq!(owned.to_shared(), e, "borrow -> own -> borrow lost data");
671
        }
672
    }
673

            
674
    #[test]
675
    fn shadow_error_owned_to_shared_to_contained_is_lossless() {
676
        for e in error_corpus() {
677
            let owned = e.to_contained();
678
            assert_eq!(
679
                owned.to_shared().to_contained(),
680
                owned,
681
                "own -> borrow -> own lost data"
682
            );
683
        }
684
    }
685

            
686
    #[test]
687
    fn shadow_error_to_contained_preserves_the_input_string_verbatim() {
688
        for input in [
689
            "",
690
            "   ",
691
            "\u{1F600}",
692
            "e\u{0301}\u{0301}\u{0301}",
693
            "10px 5px 4px 3px 2px",
694
        ] {
695
            let owned = CssShadowParseError::TooManyOrTooFewComponents(input).to_contained();
696
            match owned {
697
                CssShadowParseErrorOwned::TooManyOrTooFewComponents(s) => {
698
                    assert_eq!(s.as_str(), input);
699
                }
700
                other => panic!("wrong variant: {other:?}"),
701
            }
702
        }
703
    }
704

            
705
    #[test]
706
    fn shadow_error_to_contained_survives_a_very_long_input() {
707
        let long = "x".repeat(200_000);
708
        let owned = CssShadowParseError::TooManyOrTooFewComponents(&long).to_contained();
709
        match owned.to_shared() {
710
            CssShadowParseError::TooManyOrTooFewComponents(s) => {
711
                assert_eq!(s.len(), 200_000);
712
            }
713
            other => panic!("wrong variant: {other:?}"),
714
        }
715
    }
716

            
717
    #[test]
718
    fn shadow_error_display_and_debug_are_non_empty() {
719
        for e in error_corpus() {
720
            assert!(!e.to_string().is_empty(), "empty Display for {e:?}");
721
            // Debug is implemented as Display for this type.
722
            assert_eq!(format!("{e:?}"), e.to_string());
723
            assert!(!format!("{:?}", e.to_contained()).is_empty());
724
        }
725
    }
726

            
727
    #[test]
728
    fn shadow_error_to_contained_round_trips_real_parser_errors() {
729
        // Errors as they are actually produced by the parser, not hand-built.
730
        for bad in ["", "1px", "abc 1px", "10% 5px", "1 2 3 4 5"] {
731
            let e = parse_style_box_shadow(bad).unwrap_err();
732
            assert_eq!(e.to_contained().to_shared(), e);
733
        }
734
    }
735

            
736
    // ---------------------------------------------------------------
737
    // parser: parse_style_box_shadow -- malformed / boundary / unicode
738
    // ---------------------------------------------------------------
739

            
740
    #[test]
741
    fn parse_valid_minimal_positive_control() {
742
        let s = parse_style_box_shadow("10px 5px").unwrap();
743
        assert_eq!(
744
            s,
745
            shadow(
746
                px(10.0),
747
                px(5.0),
748
                px(0.0),
749
                px(0.0),
750
                BoxShadowClipMode::Outset,
751
                ColorU::BLACK,
752
            )
753
        );
754
    }
755

            
756
    #[test]
757
    fn parse_empty_and_whitespace_only_input_is_err() {
758
        for input in [
759
            "", " ", "   ", "\t", "\n", "\t\n\r ", "\u{00a0}", "\u{3000}",
760
        ] {
761
            let e = parse_style_box_shadow(input).unwrap_err();
762
            assert_eq!(
763
                e,
764
                CssShadowParseError::TooManyOrTooFewComponents(input),
765
                "unexpected error for {input:?}"
766
            );
767
        }
768
    }
769

            
770
    #[test]
771
    fn parse_garbage_is_err_and_never_panics() {
772
        for input in [
773
            "!!!",
774
            "@#$%^&*",
775
            "; drop table",
776
            "{}{}{}",
777
            "\\\\\\",
778
            "\0\0",
779
            "1px",                 // too few
780
            "1px 2px 3px 4px 5px", // too many
781
            "inset",               // keyword only
782
            "red",                 // color only
783
            "inset red",           // keyword + color, no lengths
784
            "inset red 1px",       // only one length left
785
            "1px 2px red blue",    // two colors: "red" is left over as a length
786
            "inset inset 1px 2px", // second "inset" is not removed
787
            "10% 5px",             // percent is rejected
788
            "1px 10%",
789
            "1px 2px 3%",
790
            "px px",
791
            "in in",
792
            "1px 2px 3px 4px 5px red inset",
793
        ] {
794
            assert!(
795
                parse_style_box_shadow(input).is_err(),
796
                "expected Err for {input:?}"
797
            );
798
        }
799
    }
800

            
801
    #[test]
802
    fn parse_leading_trailing_junk_is_trimmed_or_rejected_deterministically() {
803
        // Surrounding whitespace is absorbed by split_whitespace.
804
        let padded = parse_style_box_shadow("   10px    5px   ").unwrap();
805
        assert_eq!(padded, parse_style_box_shadow("10px 5px").unwrap());
806

            
807
        // Trailing punctuation is NOT stripped -- it makes the length unparseable.
808
        for input in ["10px 5px;", "10px, 5px", "10px 5px !important", "10px 5px}"] {
809
            let e = parse_style_box_shadow(input).unwrap_err();
810
            assert!(
811
                matches!(e, CssShadowParseError::ValueParseErr(_))
812
                    || matches!(e, CssShadowParseError::TooManyOrTooFewComponents(_)),
813
                "unexpected error kind for {input:?}: {e:?}"
814
            );
815
        }
816
    }
817

            
818
    #[test]
819
    fn parse_component_count_boundaries() {
820
        assert!(parse_style_box_shadow("1px").is_err()); // 1 -> too few
821
        assert!(parse_style_box_shadow("1px 2px").is_ok()); // 2 -> ok
822
        assert!(parse_style_box_shadow("1px 2px 3px").is_ok()); // 3 -> ok
823
        assert!(parse_style_box_shadow("1px 2px 3px 4px").is_ok()); // 4 -> ok
824
        assert!(parse_style_box_shadow("1px 2px 3px 4px 5px").is_err()); // 5 -> too many
825

            
826
        // The `inset` keyword and the color do not count toward the 2..=4 budget.
827
        assert!(parse_style_box_shadow("inset red 1px 2px 3px 4px").is_ok());
828
        // ... but five lengths are still too many, even with them present.
829
        assert!(parse_style_box_shadow("inset red 1px 2px 3px 4px 5px").is_err());
830
    }
831

            
832
    #[test]
833
    fn parse_is_insensitive_to_keyword_and_color_position() {
834
        let expected = shadow(
835
            px(1.0),
836
            px(2.0),
837
            px(0.0),
838
            px(0.0),
839
            BoxShadowClipMode::Inset,
840
            ColorU::RED,
841
        );
842
        for input in [
843
            "inset red 1px 2px",
844
            "red inset 1px 2px",
845
            "1px 2px red inset",
846
            "1px 2px inset red",
847
            "red 1px 2px inset",
848
            "1px red 2px inset",
849
            "inset 1px red 2px",
850
        ] {
851
            assert_eq!(
852
                parse_style_box_shadow(input).unwrap(),
853
                expected,
854
                "position of inset/color changed the result for {input:?}"
855
            );
856
        }
857
    }
858

            
859
    #[test]
860
    fn parse_accepts_every_color_syntax() {
861
        for (input, expected) in [
862
            ("1px 2px #888", ColorU::new_rgb(0x88, 0x88, 0x88)),
863
            ("1px 2px #ff0000", ColorU::RED),
864
            ("1px 2px #ff0000ff", ColorU::RED),
865
            ("1px 2px #f00f", ColorU::RED),
866
            ("1px 2px rgb(255,0,0)", ColorU::RED),
867
            ("1px 2px rgba(255,0,0,1.0)", ColorU::RED),
868
            ("1px 2px red", ColorU::RED),
869
            ("1px 2px RED", ColorU::RED),
870
            ("1px 2px transparent", ColorU::new(0, 0, 0, 0)),
871
        ] {
872
            let s = parse_style_box_shadow(input).unwrap();
873
            assert_eq!(s.color, expected, "wrong color for {input:?}");
874
            assert_eq!(s.offset_x, px(1.0));
875
            assert_eq!(s.offset_y, px(2.0));
876
        }
877
    }
878

            
879
    #[test]
880
    fn parse_bare_zero_is_a_pixel_value_not_a_color() {
881
        let s = parse_style_box_shadow("0 0").unwrap();
882
        assert_eq!(s.offset_x, px(0.0));
883
        assert_eq!(s.offset_y, px(0.0));
884
        assert_eq!(s.offset_x.inner.metric, SizeMetric::Px);
885
        assert_eq!(
886
            s.color,
887
            ColorU::BLACK,
888
            "a bare 0 was eaten by the color parser"
889
        );
890
    }
891

            
892
    #[test]
893
    fn parse_boundary_numbers_are_finite_and_defined() {
894
        // Signed zeroes collapse to a single +0 in the fixed-point encoding.
895
        let zeroes = parse_style_box_shadow("-0 -0").unwrap();
896
        assert_eq!(numbers(&zeroes)[0], 0.0);
897
        assert_eq!(numbers(&zeroes)[1], 0.0);
898
        assert!(!numbers(&zeroes)[0].is_sign_negative());
899

            
900
        // Huge finite values saturate on the f32 -> isize cast rather than wrap.
901
        for input in [
902
            "1e30px 1e30px",
903
            "9223372036854775807px 9223372036854775807px", // i64::MAX
904
            "340282350000000000000000000000000000000px 1px", // ~f32::MAX
905
        ] {
906
            let s = parse_style_box_shadow(input).unwrap();
907
            let n = numbers(&s)[0];
908
            assert!(n.is_finite(), "{input:?} produced a non-finite offset");
909
            assert!(n > 0.0, "{input:?} saturated to the wrong sign");
910
        }
911
        let neg = parse_style_box_shadow("-1e30px 1px").unwrap();
912
        assert!(numbers(&neg)[0].is_finite() && numbers(&neg)[0] < 0.0);
913

            
914
        // Tiny values underflow to exactly zero (3 decimals of precision).
915
        let tiny = parse_style_box_shadow("1e-30px 0.00001px").unwrap();
916
        assert_eq!(numbers(&tiny)[0], 0.0);
917
        assert_eq!(numbers(&tiny)[1], 0.0);
918
    }
919

            
920
    #[test]
921
    fn parse_nan_and_infinity_tokens_are_defined_not_poisonous() {
922
        // f32::from_str accepts "NaN"/"inf", so these reach the fixed-point cast.
923
        // NaN as isize == 0, so the value collapses to zero rather than staying NaN.
924
        let nan = parse_style_box_shadow("NaN NaN").unwrap();
925
        for n in numbers(&nan) {
926
            assert!(!n.is_nan(), "NaN survived into a parsed shadow");
927
            assert_eq!(n, 0.0);
928
        }
929
        let nan_px = parse_style_box_shadow("NaNpx 1px").unwrap();
930
        assert_eq!(numbers(&nan_px)[0], 0.0);
931

            
932
        // inf saturates to isize::MAX / 1000 -- finite, signed correctly.
933
        let inf = parse_style_box_shadow("inf 1px").unwrap();
934
        assert!(numbers(&inf)[0].is_finite() && numbers(&inf)[0] > 0.0);
935
        let neg_inf = parse_style_box_shadow("-inf 1px").unwrap();
936
        assert!(numbers(&neg_inf)[0].is_finite() && numbers(&neg_inf)[0] < 0.0);
937
        let infinity = parse_style_box_shadow("1px infinity").unwrap();
938
        assert!(numbers(&infinity)[1].is_finite());
939

            
940
        // A bare "in" is the inch metric with no value -> Err, not a panic.
941
        assert!(parse_style_box_shadow("in 1px").is_err());
942
    }
943

            
944
    #[test]
945
    fn parse_never_yields_a_percent_metric() {
946
        for input in [
947
            "1px 2px",
948
            "0 0",
949
            "1em 2rem 3pt 4in",
950
            "5vw 5vh 5vmin 5vmax",
951
            "1cm 2mm",
952
            "inset 1px 2px red",
953
        ] {
954
            let s = parse_style_box_shadow(input).unwrap();
955
            for m in [
956
                s.offset_x.inner.metric,
957
                s.offset_y.inner.metric,
958
                s.blur_radius.inner.metric,
959
                s.spread_radius.inner.metric,
960
            ] {
961
                assert_ne!(
962
                    m,
963
                    SizeMetric::Percent,
964
                    "percent metric leaked from {input:?}"
965
                );
966
            }
967
        }
968
    }
969

            
970
    #[test]
971
    fn parse_unicode_input_does_not_panic() {
972
        // parse_color_no_hash indexes by BYTE length, so multi-byte tokens behind
973
        // a '#' must error instead of slicing through a char boundary.
974
        for input in [
975
            "\u{1F600}",
976
            "\u{1F600} \u{1F600}",
977
            "1px 2px \u{1F600}",
978
            "1px 2px #\u{1F600}", // 4-byte char -> hits the len==4 hex branch
979
            "1px 2px #\u{e9}1",   // 3 bytes -> hits the len==3 hex branch
980
            "1px 2px #\u{e9}\u{e9}\u{e9}\u{e9}", // 8 bytes -> hits the len==8 branch
981
            "e\u{0301}\u{0301} 1px",
982
            "\u{202E}1px 2px",
983
            "\u{0661}px \u{0662}px", // arabic-indic digits
984
            "1px 2px",             // fullwidth digits
985
            "1px 2px \u{fffd}",
986
        ] {
987
            let _ = parse_style_box_shadow(input); // must not panic
988
        }
989

            
990
        // Unicode whitespace still splits tokens, so this is a valid shadow.
991
        let nbsp = parse_style_box_shadow("10px\u{00a0}5px").unwrap();
992
        assert_eq!(nbsp, parse_style_box_shadow("10px 5px").unwrap());
993
    }
994

            
995
    #[test]
996
    fn parse_extremely_long_input_terminates() {
997
        // 50k length tokens: rejected on the component count, no hang.
998
        let many = "1px ".repeat(50_000);
999
        assert!(matches!(
            parse_style_box_shadow(&many),
            Err(CssShadowParseError::TooManyOrTooFewComponents(_))
        ));
        // A single 1M-char garbage token.
        let long_garbage = "a".repeat(1_000_000);
        assert!(parse_style_box_shadow(&long_garbage).is_err());
        // A single token of 50k digits: f32 parses it as inf, which then saturates.
        let huge = format!("{}px 1px", "9".repeat(50_000));
        let s = parse_style_box_shadow(&huge).unwrap();
        assert!(numbers(&s)[0].is_finite());
        assert!(numbers(&s)[0] > 0.0);
        // A very long *valid* input padded with whitespace.
        let padded = format!("{}1px 2px{}", " ".repeat(100_000), " ".repeat(100_000));
        assert_eq!(
            parse_style_box_shadow(&padded).unwrap(),
            parse_style_box_shadow("1px 2px").unwrap()
        );
    }
    #[test]
    fn parse_deeply_nested_input_does_not_stack_overflow() {
        let open = "(".repeat(10_000);
        let nested = format!("{}{}", open, ")".repeat(10_000));
        assert!(parse_style_box_shadow(&nested).is_err());
        assert!(parse_style_box_shadow(&open).is_err());
        assert!(parse_style_box_shadow(&format!("1px 2px rgb{nested}")).is_err());
        assert!(parse_style_box_shadow(&format!("1px 2px {}", "[".repeat(10_000))).is_err());
    }
    // ---------------------------------------------------------------
    // round-trip: print_as_css_value <-> parse_style_box_shadow
    // ---------------------------------------------------------------
    #[test]
    fn print_then_parse_is_the_identity() {
        for original in round_trip_corpus() {
            let printed = original.print_as_css_value();
            let reparsed = parse_style_box_shadow(&printed)
                .unwrap_or_else(|e| panic!("printed {printed:?} does not re-parse: {e:?}"));
            assert_eq!(reparsed, original, "round-trip changed {printed:?}");
        }
    }
    #[test]
    fn parse_then_print_then_parse_is_idempotent() {
        for input in [
            "10px 5px",
            "5px 10px 20px",
            "2px 2px 2px 1px rgba(0,0,0,0.2)",
            "inset 0 0 10px #000",
            "5px 1em red inset",
            "1px 2px transparent",
            "-3px -4px 0 2px #12345678",
        ] {
            let first = parse_style_box_shadow(input).unwrap();
            let printed = first.print_as_css_value();
            let second = parse_style_box_shadow(&printed).unwrap();
            assert_eq!(first, second, "{input:?} -> {printed:?} was not idempotent");
            // Printing is stable: same value, same string.
            assert_eq!(printed, second.print_as_css_value());
        }
    }
    #[test]
    fn print_omits_defaults_for_brevity() {
        let default = StyleBoxShadow::default();
        assert_eq!(default.print_as_css_value(), "0px 0px");
        // A black shadow never writes a color...
        let black = shadow(
            px(1.0),
            px(2.0),
            px(3.0),
            px(0.0),
            BoxShadowClipMode::Outset,
            ColorU::BLACK,
        );
        assert_eq!(black.print_as_css_value(), "1px 2px 3px");
        assert!(!black.print_as_css_value().contains('#'));
        // ...and an outset shadow never writes the `inset` keyword.
        assert!(!black.print_as_css_value().contains("inset"));
    }
    #[test]
    fn print_writes_inset_first_and_color_last() {
        let s = shadow(
            px(1.0),
            px(2.0),
            px(0.0),
            px(0.0),
            BoxShadowClipMode::Inset,
            ColorU::RED,
        );
        assert_eq!(s.print_as_css_value(), "inset 1px 2px #ff0000ff");
    }
    #[test]
    fn print_emits_a_placeholder_blur_when_only_the_spread_is_set() {
        // A spread cannot be positional without a blur before it, so a zero blur
        // must still be written -- otherwise the spread would re-parse as a blur.
        let s = shadow(
            px(1.0),
            px(2.0),
            px(0.0),
            px(4.0),
            BoxShadowClipMode::Outset,
            ColorU::BLACK,
        );
        assert_eq!(s.print_as_css_value(), "1px 2px 0px 4px");
        let reparsed = parse_style_box_shadow(&s.print_as_css_value()).unwrap();
        assert_eq!(reparsed.blur_radius, px(0.0));
        assert_eq!(reparsed.spread_radius, px(4.0));
    }
    #[test]
    fn print_of_extreme_values_does_not_panic() {
        // Saturated / NaN-scaled shadows must still serialize to *something*.
        for factor in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, f32::MAX, 1e30] {
            let s = scaled(all_ones(), factor);
            let printed = s.print_as_css_value();
            assert!(!printed.is_empty());
            assert!(
                !printed.contains("NaN"),
                "NaN reached the CSS output: {printed}"
            );
            assert!(
                !printed.contains("inf"),
                "inf reached the CSS output: {printed}"
            );
        }
    }
    #[test]
    fn format_as_rust_code_is_well_formed_for_extremes() {
        for s in round_trip_corpus() {
            let code = s.format_as_rust_code(0);
            assert!(code.starts_with("StyleBoxShadow {"));
            assert!(code.contains("offset_x:"));
            assert!(code.contains("offset_y:"));
            assert!(code.contains("blur_radius:"));
            assert!(code.contains("spread_radius:"));
            assert!(code.contains("clip_mode: BoxShadowClipMode::"));
            assert!(code.contains("color:"));
        }
        // Indentation is applied, and extreme values do not panic the formatter.
        let inset = scaled(all_ones(), f32::INFINITY);
        let code = inset.format_as_rust_code(3);
        assert!(code.contains("clip_mode: BoxShadowClipMode::Inset"));
        assert!(code.contains("            offset_x:"));
    }
    // ---------------------------------------------------------------
    // predicates / invariants on StyleBoxShadow itself
    // ---------------------------------------------------------------
    #[test]
    fn default_shadow_is_an_opaque_black_outset_at_the_origin() {
        let d = StyleBoxShadow::default();
        assert_eq!(d.clip_mode, BoxShadowClipMode::Outset);
        assert_eq!(d.color, ColorU::BLACK);
        assert_eq!(numbers(&d), [0.0, 0.0, 0.0, 0.0]);
        assert_eq!(d.offset_x.inner.metric, SizeMetric::Px);
        // The parser's baseline must agree with Default.
        assert_eq!(parse_style_box_shadow("0 0").unwrap(), d);
    }
    #[test]
    fn equality_is_component_wise() {
        let base = all_ones();
        assert_eq!(base, base);
        assert_ne!(base, StyleBoxShadow::default());
        let mut clip = base;
        clip.clip_mode = BoxShadowClipMode::Outset;
        assert_ne!(base, clip, "clip_mode is ignored by PartialEq");
        let mut color = base;
        color.color = ColorU::BLACK;
        assert_ne!(base, color, "color is ignored by PartialEq");
        let mut spread = base;
        spread.spread_radius = px(9.0);
        assert_ne!(base, spread, "spread_radius is ignored by PartialEq");
    }
}