Skip to main content

azul_css/props/style/
text.rs

1//! CSS properties for styling text.
2//!
3//! Each property type implements `PrintAsCssValue` for CSS serialization and
4//! (behind the `parser` feature) has a corresponding `parse_style_*` function
5//! with borrowed/owned error type pairs.
6
7use alloc::string::{String, ToString};
8use core::fmt;
9use crate::corety::AzString;
10
11use crate::{
12    codegen::format::FormatAsRustCode,
13    props::{
14        basic::{
15            error::{InvalidValueErr, InvalidValueErrOwned},
16            length::{PercentageParseError, PercentageParseErrorOwned, PercentageValue},
17            pixel::{CssPixelValueParseError, CssPixelValueParseErrorOwned, PixelValue},
18            ColorU, CssDuration,
19        },
20        formatter::PrintAsCssValue,
21        macros::PixelValueTaker,
22    },
23};
24
25// -- StyleTextColor (color property) --
26// NOTE: `color` is a text property, but the `ColorU` type itself is in `basic/color.rs`.
27// This is a newtype wrapper for type safety.
28
29/// Represents a `color` attribute.
30#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
31#[repr(C)]
32pub struct StyleTextColor {
33    pub inner: ColorU,
34}
35
36impl fmt::Debug for StyleTextColor {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        write!(f, "{}", self.print_as_css_value())
39    }
40}
41
42impl StyleTextColor {
43    #[must_use] pub fn interpolate(&self, other: &Self, t: f32) -> Self {
44        Self {
45            inner: self.inner.interpolate(&other.inner, t),
46        }
47    }
48}
49
50impl PrintAsCssValue for StyleTextColor {
51    fn print_as_css_value(&self) -> String {
52        self.inner.to_hash()
53    }
54}
55
56// -- StyleTextAlign --
57
58/// Horizontal text alignment enum (left, center, right) - default: `Left`
59#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
60#[repr(C)]
61pub enum StyleTextAlign {
62    Left,
63    Center,
64    Right,
65    Justify,
66    #[default]
67    Start,
68    End,
69}
70
71impl_option!(
72    StyleTextAlign,
73    OptionStyleTextAlign,
74    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
75);
76
77impl PrintAsCssValue for StyleTextAlign {
78    fn print_as_css_value(&self) -> String {
79        String::from(match self {
80            Self::Left => "left",
81            Self::Center => "center",
82            Self::Right => "right",
83            Self::Justify => "justify",
84            Self::Start => "start",
85            Self::End => "end",
86        })
87    }
88}
89
90// -- StyleLetterSpacing --
91
92/// Represents a `letter-spacing` attribute
93#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
94#[repr(C)]
95pub struct StyleLetterSpacing {
96    pub inner: PixelValue,
97}
98
99impl fmt::Debug for StyleLetterSpacing {
100    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101        write!(f, "{}", self.inner)
102    }
103}
104impl Default for StyleLetterSpacing {
105    fn default() -> Self {
106        Self {
107            inner: PixelValue::const_px(0),
108        }
109    }
110}
111impl_pixel_value!(StyleLetterSpacing);
112impl PixelValueTaker for StyleLetterSpacing {
113    fn from_pixel_value(inner: PixelValue) -> Self {
114        Self { inner }
115    }
116}
117impl PrintAsCssValue for StyleLetterSpacing {
118    fn print_as_css_value(&self) -> String {
119        format!("{}", self.inner)
120    }
121}
122
123// -- StyleWordSpacing --
124
125/// Represents a `word-spacing` attribute
126#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
127#[repr(C)]
128pub struct StyleWordSpacing {
129    pub inner: PixelValue,
130}
131
132impl fmt::Debug for StyleWordSpacing {
133    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134        write!(f, "{}", self.inner)
135    }
136}
137impl Default for StyleWordSpacing {
138    fn default() -> Self {
139        Self {
140            inner: PixelValue::const_px(0),
141        }
142    }
143}
144impl_pixel_value!(StyleWordSpacing);
145impl PixelValueTaker for StyleWordSpacing {
146    fn from_pixel_value(inner: PixelValue) -> Self {
147        Self { inner }
148    }
149}
150impl PrintAsCssValue for StyleWordSpacing {
151    fn print_as_css_value(&self) -> String {
152        format!("{}", self.inner)
153    }
154}
155
156// -- StyleLineHeight --
157
158/// Represents a `line-height` attribute
159#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
160#[repr(C)]
161pub struct StyleLineHeight {
162    pub inner: PercentageValue,
163}
164impl Default for StyleLineHeight {
165    fn default() -> Self {
166        Self {
167            inner: PercentageValue::const_new(120),
168        }
169    }
170}
171impl_percentage_value!(StyleLineHeight);
172impl PrintAsCssValue for StyleLineHeight {
173    fn print_as_css_value(&self) -> String {
174        format!("{}", self.inner)
175    }
176}
177
178// -- StyleTabSize --
179
180/// Represents a `tab-size` attribute
181#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
182#[repr(C)]
183pub struct StyleTabSize {
184    pub inner: PixelValue, // Can be a number (space characters, em-based) or a length
185}
186
187impl fmt::Debug for StyleTabSize {
188    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189        write!(f, "{}", self.inner)
190    }
191}
192impl Default for StyleTabSize {
193    fn default() -> Self {
194        Self {
195            inner: PixelValue::em(8.0),
196        }
197    }
198}
199impl_pixel_value!(StyleTabSize);
200impl PixelValueTaker for StyleTabSize {
201    fn from_pixel_value(inner: PixelValue) -> Self {
202        Self { inner }
203    }
204}
205impl PrintAsCssValue for StyleTabSize {
206    fn print_as_css_value(&self) -> String {
207        format!("{}", self.inner)
208    }
209}
210
211// -- StyleWhiteSpace --
212
213/// How to handle white space inside an element.
214/// 
215/// CSS Text Level 3: <https://www.w3.org/TR/css-text-3/#white-space-property>
216#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
217#[repr(C)]
218#[derive(Default)]
219pub enum StyleWhiteSpace {
220    /// Collapse whitespace, wrap lines
221    #[default]
222    Normal,
223    /// Preserve whitespace, no wrap (except for explicit breaks)
224    Pre,
225    /// Collapse whitespace, no wrap
226    Nowrap,
227    /// Preserve whitespace, wrap lines
228    PreWrap,
229    /// Collapse whitespace (except newlines), wrap lines
230    PreLine,
231    /// Preserve whitespace, allow breaking at spaces
232    BreakSpaces,
233}
234impl_option!(
235    StyleWhiteSpace,
236    OptionStyleWhiteSpace,
237    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
238);
239impl PrintAsCssValue for StyleWhiteSpace {
240    fn print_as_css_value(&self) -> String {
241        String::from(match self {
242            Self::Normal => "normal",
243            Self::Pre => "pre",
244            Self::Nowrap => "nowrap",
245            Self::PreWrap => "pre-wrap",
246            Self::PreLine => "pre-line",
247            Self::BreakSpaces => "break-spaces",
248        })
249    }
250}
251
252// -- StyleHyphens --
253
254/// Hyphenation rules.
255#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
256#[repr(C)]
257#[derive(Default)]
258pub enum StyleHyphens {
259    /// No hyphenation: words are not broken at hyphenation opportunities.
260    None,
261    /// Manual hyphenation: words are only broken at explicit soft hyphens (U+00AD)
262    /// or unconditional hyphens (U+2010).
263    #[default]
264    Manual,
265    /// Automatic hyphenation: words may be broken at automatic hyphenation
266    /// opportunities determined by a language-appropriate hyphenation resource,
267    /// in addition to explicit opportunities.
268    Auto,
269}
270impl_option!(
271    StyleHyphens,
272    OptionStyleHyphens,
273    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
274);
275impl PrintAsCssValue for StyleHyphens {
276    fn print_as_css_value(&self) -> String {
277        String::from(match self {
278            Self::None => "none",
279            Self::Manual => "manual",
280            Self::Auto => "auto",
281        })
282    }
283}
284
285// -- StyleLineBreak --
286
287/// Controls the strictness of line breaking rules.
288///
289/// CSS Text Level 3: <https://www.w3.org/TR/css-text-3/#line-break-property>
290#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
291#[repr(C)]
292#[derive(Default)]
293pub enum StyleLineBreak {
294    /// The browser determines the set of line-breaking restrictions to use.
295    #[default]
296    Auto,
297    /// Breaks text using the least restrictive set of line-breaking rules.
298    Loose,
299    /// Breaks text using the most common set of line-breaking rules.
300    Normal,
301    /// Breaks text using the most stringent set of line-breaking rules.
302    Strict,
303    /// There is a soft wrap opportunity around every typographic character unit,
304    /// including around any punctuation character or preserved white spaces,
305    /// or in the middle of words, disregarding any prohibition against line breaks.
306    Anywhere,
307}
308impl_option!(
309    StyleLineBreak,
310    OptionStyleLineBreak,
311    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
312);
313impl PrintAsCssValue for StyleLineBreak {
314    fn print_as_css_value(&self) -> String {
315        String::from(match self {
316            Self::Auto => "auto",
317            Self::Loose => "loose",
318            Self::Normal => "normal",
319            Self::Strict => "strict",
320            Self::Anywhere => "anywhere",
321        })
322    }
323}
324
325// -- StyleWordBreak --
326
327/// Controls line breaking rules within words.
328///
329/// CSS Text Level 3 §5.2: <https://www.w3.org/TR/css-text-3/#word-break-property>
330#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
331#[repr(C)]
332#[derive(Default)]
333pub enum StyleWordBreak {
334    /// Use default line break rules.
335    #[default]
336    Normal,
337    /// Allow break opportunities between any two characters (CJK and non-CJK).
338    BreakAll,
339    /// Forbid break opportunities within CJK character sequences.
340    KeepAll,
341    // +spec:line-breaking:815882 - deprecated break-word keyword: same as normal + overflow-wrap: anywhere
342    /// Deprecated: equivalent to word-break: normal and overflow-wrap: anywhere.
343    BreakWord,
344}
345impl_option!(
346    StyleWordBreak,
347    OptionStyleWordBreak,
348    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
349);
350impl PrintAsCssValue for StyleWordBreak {
351    fn print_as_css_value(&self) -> String {
352        String::from(match self {
353            Self::Normal => "normal",
354            Self::BreakAll => "break-all",
355            Self::KeepAll => "keep-all",
356            Self::BreakWord => "break-word",
357        })
358    }
359}
360
361// -- StyleOverflowWrap --
362
363/// Controls whether the browser may break at otherwise disallowed points
364/// to prevent overflow.
365///
366/// CSS Text Level 3 §3.3: <https://www.w3.org/TR/css-text-3/#overflow-wrap-property>
367#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
368#[repr(C)]
369#[derive(Default)]
370pub enum StyleOverflowWrap {
371    /// Lines may only break at allowed break points.
372    #[default]
373    Normal,
374    /// An otherwise unbreakable sequence may be broken at an arbitrary point
375    /// if there are no otherwise acceptable break points.
376    Anywhere,
377    /// Same as `anywhere` but soft wrap opportunities introduced are not
378    /// considered when calculating min-content intrinsic sizes.
379    BreakWord,
380}
381impl_option!(
382    StyleOverflowWrap,
383    OptionStyleOverflowWrap,
384    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
385);
386impl PrintAsCssValue for StyleOverflowWrap {
387    fn print_as_css_value(&self) -> String {
388        String::from(match self {
389            Self::Normal => "normal",
390            Self::Anywhere => "anywhere",
391            Self::BreakWord => "break-word",
392        })
393    }
394}
395
396// -- StyleTextAlignLast --
397
398/// Controls alignment of the last line of a block or a line right before
399/// a forced line break.
400///
401/// CSS Text Level 3 §7.2: <https://www.w3.org/TR/css-text-3/#text-align-last-property>
402#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
403#[repr(C)]
404#[derive(Default)]
405pub enum StyleTextAlignLast {
406    /// Alignment of the last line is determined by text-align (or start if justify).
407    #[default]
408    Auto,
409    /// Align to the start edge of the line box.
410    Start,
411    /// Align to the end edge of the line box.
412    End,
413    /// Align to the line left.
414    Left,
415    /// Align to the line right.
416    Right,
417    /// Center the content.
418    Center,
419    /// Justify the content.
420    Justify,
421}
422impl_option!(
423    StyleTextAlignLast,
424    OptionStyleTextAlignLast,
425    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
426);
427impl PrintAsCssValue for StyleTextAlignLast {
428    fn print_as_css_value(&self) -> String {
429        String::from(match self {
430            Self::Auto => "auto",
431            Self::Start => "start",
432            Self::End => "end",
433            Self::Left => "left",
434            Self::Right => "right",
435            Self::Center => "center",
436            Self::Justify => "justify",
437        })
438    }
439}
440
441// -- StyleTextTransform --
442
443/// Controls capitalization of a text run (applied before shaping).
444///
445/// CSS Text Level 3 §2.1: <https://www.w3.org/TR/css-text-3/#text-transform-property>
446#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
447#[repr(C)]
448#[derive(Default)]
449pub enum StyleTextTransform {
450    /// No capitalization effect.
451    #[default]
452    None,
453    /// Uppercase the first typographic letter unit of each word.
454    Capitalize,
455    /// Uppercase every typographic letter unit.
456    Uppercase,
457    /// Lowercase every typographic letter unit.
458    Lowercase,
459    /// Map to the full-width form where available.
460    FullWidth,
461}
462impl_option!(
463    StyleTextTransform,
464    OptionStyleTextTransform,
465    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
466);
467impl PrintAsCssValue for StyleTextTransform {
468    fn print_as_css_value(&self) -> String {
469        String::from(match self {
470            Self::None => "none",
471            Self::Capitalize => "capitalize",
472            Self::Uppercase => "uppercase",
473            Self::Lowercase => "lowercase",
474            Self::FullWidth => "full-width",
475        })
476    }
477}
478
479// -- StyleDirection --
480
481/// Text direction.
482// +spec:writing-modes:46fed3 - direction property provides explicit bidi controls in CSS
483#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
484#[repr(C)]
485#[derive(Default)]
486pub enum StyleDirection {
487    /// Left-to-right text direction
488    #[default]
489    Ltr,
490    /// Right-to-left text direction
491    Rtl,
492}
493impl_option!(
494    StyleDirection,
495    OptionStyleDirection,
496    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
497);
498impl PrintAsCssValue for StyleDirection {
499    fn print_as_css_value(&self) -> String {
500        String::from(match self {
501            Self::Ltr => "ltr",
502            Self::Rtl => "rtl",
503        })
504    }
505}
506
507// -- StyleUserSelect --
508
509/// Controls whether the user can select text.
510/// Used to prevent accidental text selection on UI controls like buttons.
511#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
512#[repr(C)]
513#[derive(Default)]
514pub enum StyleUserSelect {
515    /// Browser determines selectability (default)
516    #[default]
517    Auto,
518    /// Text is selectable
519    Text,
520    /// Text is not selectable
521    None,
522    /// User can select all text with a single action
523    All,
524}
525impl_option!(
526    StyleUserSelect,
527    OptionStyleUserSelect,
528    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
529);
530impl PrintAsCssValue for StyleUserSelect {
531    fn print_as_css_value(&self) -> String {
532        String::from(match self {
533            Self::Auto => "auto",
534            Self::Text => "text",
535            Self::None => "none",
536            Self::All => "all",
537        })
538    }
539}
540
541// -- StyleTextDecoration --
542
543/// Text decoration (underline, overline, line-through).
544#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
545#[repr(C)]
546#[derive(Default)]
547pub enum StyleTextDecoration {
548    /// No decoration
549    #[default]
550    None,
551    /// Underline
552    Underline,
553    /// Line above text
554    Overline,
555    /// Strike-through line
556    LineThrough,
557}
558impl_option!(
559    StyleTextDecoration,
560    OptionStyleTextDecoration,
561    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
562);
563impl PrintAsCssValue for StyleTextDecoration {
564    fn print_as_css_value(&self) -> String {
565        String::from(match self {
566            Self::None => "none",
567            Self::Underline => "underline",
568            Self::Overline => "overline",
569            Self::LineThrough => "line-through",
570        })
571    }
572}
573
574// -- StyleVerticalAlign --
575
576/// CSS 2.2 §10.8.1 vertical-align property values
577#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
578#[repr(C, u8)]
579#[derive(Default)]
580pub enum StyleVerticalAlign {
581    /// CSS default - align baselines
582    #[default]
583    Baseline,
584    /// Align top of element with top of line box
585    Top,
586    /// Align middle of element with baseline + half x-height
587    Middle,
588    /// Align bottom of element with bottom of line box
589    Bottom,
590    /// Align baseline with parent's subscript baseline
591    Sub,
592    /// Align baseline with parent's superscript baseline
593    Superscript,
594    /// Align top with top of parent's font
595    TextTop,
596    /// Align bottom with bottom of parent's font
597    TextBottom,
598    /// <percentage> refers to line-height of the element itself
599    Percentage(PercentageValue),
600    /// <length> offset from baseline
601    Length(PixelValue),
602}
603
604impl_option!(
605    StyleVerticalAlign,
606    OptionStyleVerticalAlign,
607    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
608);
609
610impl PrintAsCssValue for StyleVerticalAlign {
611    fn print_as_css_value(&self) -> String {
612        match self {
613            Self::Baseline => String::from("baseline"),
614            Self::Top => String::from("top"),
615            Self::Middle => String::from("middle"),
616            Self::Bottom => String::from("bottom"),
617            Self::Sub => String::from("sub"),
618            Self::Superscript => String::from("super"),
619            Self::TextTop => String::from("text-top"),
620            Self::TextBottom => String::from("text-bottom"),
621            Self::Percentage(p) => format!("{}%", p.normalized() * 100.0),
622            Self::Length(l) => l.print_as_css_value(),
623        }
624    }
625}
626
627impl FormatAsRustCode for StyleVerticalAlign {
628    fn format_as_rust_code(&self, indent: usize) -> String {
629        match self {
630            Self::Baseline => "StyleVerticalAlign::Baseline".to_string(),
631            Self::Top => "StyleVerticalAlign::Top".to_string(),
632            Self::Middle => "StyleVerticalAlign::Middle".to_string(),
633            Self::Bottom => "StyleVerticalAlign::Bottom".to_string(),
634            Self::Sub => "StyleVerticalAlign::Sub".to_string(),
635            Self::Superscript => "StyleVerticalAlign::Superscript".to_string(),
636            Self::TextTop => "StyleVerticalAlign::TextTop".to_string(),
637            Self::TextBottom => "StyleVerticalAlign::TextBottom".to_string(),
638            Self::Percentage(p) => format!("StyleVerticalAlign::Percentage(PercentageValue::new({}))", p.normalized() * 100.0),
639            Self::Length(l) => format!("StyleVerticalAlign::Length({l})"),
640        }
641    }
642}
643
644// --- PARSERS ---
645
646#[cfg(feature = "parser")]
647use crate::props::basic::{
648    color::{parse_css_color, CssColorParseError, CssColorParseErrorOwned},
649    DurationParseError,
650};
651
652#[cfg(feature = "parser")]
653#[derive(Clone, PartialEq)]
654pub enum StyleTextColorParseError<'a> {
655    ColorParseError(CssColorParseError<'a>),
656}
657#[cfg(feature = "parser")]
658impl_debug_as_display!(StyleTextColorParseError<'a>);
659#[cfg(feature = "parser")]
660impl_display! { StyleTextColorParseError<'a>, {
661    ColorParseError(e) => format!("Invalid color: {}", e),
662}}
663#[cfg(feature = "parser")]
664impl_from!(
665    CssColorParseError<'a>,
666    StyleTextColorParseError::ColorParseError
667);
668
669#[cfg(feature = "parser")]
670#[derive(Debug, Clone, PartialEq)]
671#[repr(C, u8)]
672pub enum StyleTextColorParseErrorOwned {
673    ColorParseError(CssColorParseErrorOwned),
674}
675
676#[cfg(feature = "parser")]
677impl StyleTextColorParseError<'_> {
678    #[must_use] pub fn to_contained(&self) -> StyleTextColorParseErrorOwned {
679        match self {
680            Self::ColorParseError(e) => {
681                StyleTextColorParseErrorOwned::ColorParseError(e.to_contained())
682            }
683        }
684    }
685}
686
687#[cfg(feature = "parser")]
688impl StyleTextColorParseErrorOwned {
689    #[must_use] pub fn to_shared(&self) -> StyleTextColorParseError<'_> {
690        match self {
691            Self::ColorParseError(e) => StyleTextColorParseError::ColorParseError(e.to_shared()),
692        }
693    }
694}
695
696#[cfg(feature = "parser")]
697/// # Errors
698///
699/// Returns an error if `input` is not a valid CSS `text-color` value.
700pub fn parse_style_text_color(input: &str) -> Result<StyleTextColor, StyleTextColorParseError<'_>> {
701    parse_css_color(input)
702        .map(|inner| StyleTextColor { inner })
703        .map_err(StyleTextColorParseError::ColorParseError)
704}
705
706#[cfg(feature = "parser")]
707#[derive(Clone, PartialEq, Eq)]
708pub enum StyleTextAlignParseError<'a> {
709    InvalidValue(InvalidValueErr<'a>),
710}
711#[cfg(feature = "parser")]
712impl_debug_as_display!(StyleTextAlignParseError<'a>);
713#[cfg(feature = "parser")]
714impl_display! { StyleTextAlignParseError<'a>, {
715    InvalidValue(e) => format!("Invalid text-align value: \"{}\"", e.0),
716}}
717#[cfg(feature = "parser")]
718impl_from!(InvalidValueErr<'a>, StyleTextAlignParseError::InvalidValue);
719
720#[cfg(feature = "parser")]
721#[derive(Debug, Clone, PartialEq, Eq)]
722#[repr(C, u8)]
723pub enum StyleTextAlignParseErrorOwned {
724    InvalidValue(InvalidValueErrOwned),
725}
726
727#[cfg(feature = "parser")]
728impl StyleTextAlignParseError<'_> {
729    #[must_use] pub fn to_contained(&self) -> StyleTextAlignParseErrorOwned {
730        match self {
731            Self::InvalidValue(e) => StyleTextAlignParseErrorOwned::InvalidValue(e.to_contained()),
732        }
733    }
734}
735
736#[cfg(feature = "parser")]
737impl StyleTextAlignParseErrorOwned {
738    #[must_use] pub fn to_shared(&self) -> StyleTextAlignParseError<'_> {
739        match self {
740            Self::InvalidValue(e) => StyleTextAlignParseError::InvalidValue(e.to_shared()),
741        }
742    }
743}
744
745#[cfg(feature = "parser")]
746/// # Errors
747///
748/// Returns an error if `input` is not a valid CSS `text-align` value.
749pub fn parse_style_text_align(input: &str) -> Result<StyleTextAlign, StyleTextAlignParseError<'_>> {
750    match input.trim() {
751        "left" => Ok(StyleTextAlign::Left),
752        "center" => Ok(StyleTextAlign::Center),
753        "right" => Ok(StyleTextAlign::Right),
754        "justify" => Ok(StyleTextAlign::Justify),
755        "start" => Ok(StyleTextAlign::Start),
756        "end" => Ok(StyleTextAlign::End),
757        other => Err(StyleTextAlignParseError::InvalidValue(InvalidValueErr(
758            other,
759        ))),
760    }
761}
762
763#[cfg(feature = "parser")]
764#[derive(Clone, PartialEq, Eq)]
765pub enum StyleLetterSpacingParseError<'a> {
766    PixelValue(CssPixelValueParseError<'a>),
767}
768#[cfg(feature = "parser")]
769impl_debug_as_display!(StyleLetterSpacingParseError<'a>);
770#[cfg(feature = "parser")]
771impl_display! { StyleLetterSpacingParseError<'a>, {
772    PixelValue(e) => format!("Invalid letter-spacing value: {}", e),
773}}
774#[cfg(feature = "parser")]
775impl_from!(
776    CssPixelValueParseError<'a>,
777    StyleLetterSpacingParseError::PixelValue
778);
779
780#[cfg(feature = "parser")]
781#[derive(Debug, Clone, PartialEq, Eq)]
782#[repr(C, u8)]
783pub enum StyleLetterSpacingParseErrorOwned {
784    PixelValue(CssPixelValueParseErrorOwned),
785}
786
787#[cfg(feature = "parser")]
788impl StyleLetterSpacingParseError<'_> {
789    #[must_use] pub fn to_contained(&self) -> StyleLetterSpacingParseErrorOwned {
790        match self {
791            Self::PixelValue(e) => StyleLetterSpacingParseErrorOwned::PixelValue(e.to_contained()),
792        }
793    }
794}
795
796#[cfg(feature = "parser")]
797impl StyleLetterSpacingParseErrorOwned {
798    #[must_use] pub fn to_shared(&self) -> StyleLetterSpacingParseError<'_> {
799        match self {
800            Self::PixelValue(e) => StyleLetterSpacingParseError::PixelValue(e.to_shared()),
801        }
802    }
803}
804
805#[cfg(feature = "parser")]
806/// # Errors
807///
808/// Returns an error if `input` is not a valid CSS `letter-spacing` value.
809pub fn parse_style_letter_spacing(
810    input: &str,
811) -> Result<StyleLetterSpacing, StyleLetterSpacingParseError<'_>> {
812    crate::props::basic::pixel::parse_pixel_value(input)
813        .map(|inner| StyleLetterSpacing { inner })
814        .map_err(StyleLetterSpacingParseError::PixelValue)
815}
816
817// -- StyleTextIndent (text-indent property) --
818
819/// Represents a `text-indent` attribute (indentation of first line in a block).
820#[derive(Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
821#[repr(C)]
822pub struct StyleTextIndent {
823    pub inner: PixelValue,
824    /// `each-line` keyword: indent first line of each block container
825    /// AND each line after a forced line break (but not after soft wrap).
826    pub each_line: bool,
827    /// `hanging` keyword: inverts which lines are affected by the indent.
828    pub hanging: bool,
829}
830
831impl fmt::Debug for StyleTextIndent {
832    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
833        write!(f, "{}", self.print_as_css_value())
834    }
835}
836
837impl StyleTextIndent {
838    #[inline]
839    #[must_use] pub const fn zero() -> Self {
840        Self { inner: PixelValue::zero(), each_line: false, hanging: false }
841    }
842    #[inline]
843    #[must_use] pub const fn const_px(value: isize) -> Self {
844        Self { inner: PixelValue::const_px(value), each_line: false, hanging: false }
845    }
846    #[inline]
847    #[must_use] pub const fn const_em(value: isize) -> Self {
848        Self { inner: PixelValue::const_em(value), each_line: false, hanging: false }
849    }
850    #[inline]
851    #[must_use] pub const fn const_pt(value: isize) -> Self {
852        Self { inner: PixelValue::const_pt(value), each_line: false, hanging: false }
853    }
854    #[inline]
855    #[must_use] pub const fn const_percent(value: isize) -> Self {
856        Self { inner: PixelValue::const_percent(value), each_line: false, hanging: false }
857    }
858    #[inline]
859    #[must_use] pub const fn const_in(value: isize) -> Self {
860        Self { inner: PixelValue::const_in(value), each_line: false, hanging: false }
861    }
862    #[inline]
863    #[must_use] pub const fn const_cm(value: isize) -> Self {
864        Self { inner: PixelValue::const_cm(value), each_line: false, hanging: false }
865    }
866    #[inline]
867    #[must_use] pub const fn const_mm(value: isize) -> Self {
868        Self { inner: PixelValue::const_mm(value), each_line: false, hanging: false }
869    }
870    #[inline]
871    #[must_use] pub const fn const_from_metric(metric: crate::props::basic::length::SizeMetric, value: isize) -> Self {
872        Self { inner: PixelValue::const_from_metric(metric, value), each_line: false, hanging: false }
873    }
874    #[inline]
875    #[must_use] pub fn px(value: f32) -> Self {
876        Self { inner: PixelValue::px(value), each_line: false, hanging: false }
877    }
878    #[inline]
879    #[must_use] pub fn em(value: f32) -> Self {
880        Self { inner: PixelValue::em(value), each_line: false, hanging: false }
881    }
882    #[inline]
883    #[must_use] pub fn pt(value: f32) -> Self {
884        Self { inner: PixelValue::pt(value), each_line: false, hanging: false }
885    }
886    #[inline]
887    #[must_use] pub fn percent(value: f32) -> Self {
888        Self { inner: PixelValue::percent(value), each_line: false, hanging: false }
889    }
890    #[inline]
891    #[must_use] pub fn from_metric(metric: crate::props::basic::length::SizeMetric, value: f32) -> Self {
892        Self { inner: PixelValue::from_metric(metric, value), each_line: false, hanging: false }
893    }
894    #[inline]
895    #[must_use] pub fn interpolate(&self, other: &Self, t: f32) -> Self {
896        Self { inner: self.inner.interpolate(&other.inner, t), each_line: self.each_line, hanging: self.hanging }
897    }
898}
899
900impl PrintAsCssValue for StyleTextIndent {
901    fn print_as_css_value(&self) -> String {
902        let mut s = self.inner.to_string();
903        if self.hanging {
904            s.push_str(" hanging");
905        }
906        if self.each_line {
907            s.push_str(" each-line");
908        }
909        s
910    }
911}
912
913impl FormatAsRustCode for StyleTextIndent {
914    fn format_as_rust_code(&self, _tabs: usize) -> String {
915        format!(
916            "StyleTextIndent {{ inner: {}, each_line: {}, hanging: {} }}",
917            self.inner.format_as_rust_code(0), self.each_line, self.hanging
918        )
919    }
920}
921
922#[cfg(feature = "parser")]
923#[derive(Clone, PartialEq, Eq)]
924pub enum StyleTextIndentParseError<'a> {
925    PixelValue(CssPixelValueParseError<'a>),
926}
927#[cfg(feature = "parser")]
928impl_debug_as_display!(StyleTextIndentParseError<'a>);
929#[cfg(feature = "parser")]
930impl_display! { StyleTextIndentParseError<'a>, {
931    PixelValue(e) => format!("Invalid text-indent value: {}", e),
932}}
933#[cfg(feature = "parser")]
934impl_from!(
935    CssPixelValueParseError<'a>,
936    StyleTextIndentParseError::PixelValue
937);
938
939#[cfg(feature = "parser")]
940#[derive(Debug, Clone, PartialEq, Eq)]
941#[repr(C, u8)]
942pub enum StyleTextIndentParseErrorOwned {
943    PixelValue(CssPixelValueParseErrorOwned),
944}
945
946#[cfg(feature = "parser")]
947impl StyleTextIndentParseError<'_> {
948    #[must_use] pub fn to_contained(&self) -> StyleTextIndentParseErrorOwned {
949        match self {
950            Self::PixelValue(e) => StyleTextIndentParseErrorOwned::PixelValue(e.to_contained()),
951        }
952    }
953}
954
955#[cfg(feature = "parser")]
956impl StyleTextIndentParseErrorOwned {
957    #[must_use] pub fn to_shared(&self) -> StyleTextIndentParseError<'_> {
958        match self {
959            Self::PixelValue(e) => StyleTextIndentParseError::PixelValue(e.to_shared()),
960        }
961    }
962}
963
964#[cfg(feature = "parser")]
965/// # Errors
966///
967/// Returns an error if `input` is not a valid CSS `text-indent` value.
968pub fn parse_style_text_indent(input: &str) -> Result<StyleTextIndent, StyleTextIndentParseError<'_>> {
969    let mut each_line = false;
970    let mut hanging = false;
971    let mut pixel_part: Option<&str> = None;
972
973    for token in input.split_whitespace() {
974        match token {
975            "each-line" => each_line = true,
976            "hanging" => hanging = true,
977            _ => {
978                pixel_part = Some(token);
979            }
980        }
981    }
982
983    let pixel_str = pixel_part.unwrap_or("0px");
984
985    crate::props::basic::pixel::parse_pixel_value(pixel_str)
986        .map(|inner| StyleTextIndent { inner, each_line, hanging })
987        .map_err(StyleTextIndentParseError::PixelValue)
988}
989
990/// initial-letter property for drop caps
991#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
992#[repr(C)]
993pub struct StyleInitialLetter {
994    pub size: u32,
995    pub sink: crate::corety::OptionU32,
996}
997
998impl FormatAsRustCode for StyleInitialLetter {
999    fn format_as_rust_code(&self, _tabs: usize) -> String {
1000        format!("{self:?}")
1001    }
1002}
1003
1004impl PrintAsCssValue for StyleInitialLetter {
1005    fn print_as_css_value(&self) -> String {
1006        if let crate::corety::OptionU32::Some(sink) = self.sink {
1007            format!("{} {}", self.size, sink)
1008        } else {
1009            format!("{}", self.size)
1010        }
1011    }
1012}
1013
1014#[cfg(feature = "parser")]
1015#[derive(Clone, PartialEq, Eq)]
1016pub enum StyleInitialLetterParseError<'a> {
1017    InvalidFormat(&'a str),
1018    InvalidSize(&'a str),
1019    InvalidSink(&'a str),
1020}
1021#[cfg(feature = "parser")]
1022impl_debug_as_display!(StyleInitialLetterParseError<'a>);
1023#[cfg(feature = "parser")]
1024impl_display! { StyleInitialLetterParseError<'a>, {
1025    InvalidFormat(e) => format!("Invalid initial-letter format: {}", e),
1026    InvalidSize(e) => format!("Invalid initial-letter size: {}", e),
1027    InvalidSink(e) => format!("Invalid initial-letter sink: {}", e),
1028}}
1029
1030#[cfg(feature = "parser")]
1031#[derive(Debug, Clone, PartialEq, Eq)]
1032#[repr(C, u8)]
1033pub enum StyleInitialLetterParseErrorOwned {
1034    InvalidFormat(AzString),
1035    InvalidSize(AzString),
1036    InvalidSink(AzString),
1037}
1038
1039#[cfg(feature = "parser")]
1040impl StyleInitialLetterParseError<'_> {
1041    #[must_use] pub fn to_contained(&self) -> StyleInitialLetterParseErrorOwned {
1042        match self {
1043            Self::InvalidFormat(s) => {
1044                StyleInitialLetterParseErrorOwned::InvalidFormat((*s).to_string().into())
1045            }
1046            Self::InvalidSize(s) => StyleInitialLetterParseErrorOwned::InvalidSize((*s).to_string().into()),
1047            Self::InvalidSink(s) => StyleInitialLetterParseErrorOwned::InvalidSink((*s).to_string().into()),
1048        }
1049    }
1050}
1051
1052#[cfg(feature = "parser")]
1053impl StyleInitialLetterParseErrorOwned {
1054    #[must_use] pub fn to_shared(&self) -> StyleInitialLetterParseError<'_> {
1055        match self {
1056            Self::InvalidFormat(s) => StyleInitialLetterParseError::InvalidFormat(s.as_str()),
1057            Self::InvalidSize(s) => StyleInitialLetterParseError::InvalidSize(s.as_str()),
1058            Self::InvalidSink(s) => StyleInitialLetterParseError::InvalidSink(s.as_str()),
1059        }
1060    }
1061}
1062
1063#[cfg(feature = "parser")]
1064impl From<StyleInitialLetterParseError<'_>> for StyleInitialLetterParseErrorOwned {
1065    fn from(e: StyleInitialLetterParseError<'_>) -> Self {
1066        match e {
1067            StyleInitialLetterParseError::InvalidFormat(s) => {
1068                Self::InvalidFormat(s.to_string().into())
1069            }
1070            StyleInitialLetterParseError::InvalidSize(s) => {
1071                Self::InvalidSize(s.to_string().into())
1072            }
1073            StyleInitialLetterParseError::InvalidSink(s) => {
1074                Self::InvalidSink(s.to_string().into())
1075            }
1076        }
1077    }
1078}
1079
1080#[cfg(feature = "parser")]
1081impl_display! { StyleInitialLetterParseErrorOwned, {
1082    InvalidFormat(e) => format!("Invalid initial-letter format: {}", e),
1083    InvalidSize(e) => format!("Invalid initial-letter size: {}", e),
1084    InvalidSink(e) => format!("Invalid initial-letter sink: {}", e),
1085}}
1086
1087#[cfg(feature = "parser")]
1088/// # Errors
1089///
1090/// Returns an error if `input` is not a valid CSS `initial-letter` value.
1091pub fn parse_style_initial_letter(
1092    input: &str,
1093) -> Result<StyleInitialLetter, StyleInitialLetterParseError<'_>> {
1094    let input = input.trim();
1095    let parts: Vec<&str> = input.split_whitespace().collect();
1096
1097    if parts.is_empty() {
1098        return Err(StyleInitialLetterParseError::InvalidFormat(input));
1099    }
1100
1101    // Parse size (required)
1102    let size = parts[0]
1103        .parse::<u32>()
1104        .map_err(|_| StyleInitialLetterParseError::InvalidSize(parts[0]))?;
1105
1106    if size == 0 {
1107        return Err(StyleInitialLetterParseError::InvalidSize(parts[0]));
1108    }
1109
1110    // Parse sink (optional)
1111    let sink = if parts.len() > 1 {
1112        crate::corety::OptionU32::Some(
1113            parts[1]
1114                .parse::<u32>()
1115                .map_err(|_| StyleInitialLetterParseError::InvalidSink(parts[1]))?,
1116        )
1117    } else {
1118        crate::corety::OptionU32::None
1119    };
1120
1121    Ok(StyleInitialLetter { size, sink })
1122}
1123
1124/// line-clamp property for limiting visible lines
1125#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1126#[repr(C)]
1127pub struct StyleLineClamp {
1128    pub max_lines: usize,
1129}
1130
1131impl FormatAsRustCode for StyleLineClamp {
1132    fn format_as_rust_code(&self, _tabs: usize) -> String {
1133        format!("{self:?}")
1134    }
1135}
1136
1137impl PrintAsCssValue for StyleLineClamp {
1138    fn print_as_css_value(&self) -> String {
1139        format!("{}", self.max_lines)
1140    }
1141}
1142
1143#[cfg(feature = "parser")]
1144#[derive(Clone, PartialEq, Eq)]
1145pub enum StyleLineClampParseError<'a> {
1146    InvalidValue(&'a str),
1147    ZeroValue,
1148}
1149#[cfg(feature = "parser")]
1150impl_debug_as_display!(StyleLineClampParseError<'a>);
1151#[cfg(feature = "parser")]
1152impl_display! { StyleLineClampParseError<'a>, {
1153    InvalidValue(e) => format!("Invalid line-clamp value: {}", e),
1154    ZeroValue => format!("line-clamp cannot be zero"),
1155}}
1156#[allow(variant_size_differences)] // repr(C,u8) FFI enum: boxing the large variant would change the C ABI (api.json bindings); size disparity accepted
1157#[cfg(feature = "parser")]
1158#[derive(Debug, Clone, PartialEq, Eq)]
1159#[repr(C, u8)]
1160pub enum StyleLineClampParseErrorOwned {
1161    InvalidValue(AzString),
1162    ZeroValue,
1163}
1164
1165#[cfg(feature = "parser")]
1166impl StyleLineClampParseError<'_> {
1167    #[must_use] pub fn to_contained(&self) -> StyleLineClampParseErrorOwned {
1168        match self {
1169            Self::InvalidValue(s) => StyleLineClampParseErrorOwned::InvalidValue((*s).to_string().into()),
1170            Self::ZeroValue => StyleLineClampParseErrorOwned::ZeroValue,
1171        }
1172    }
1173}
1174
1175#[cfg(feature = "parser")]
1176impl StyleLineClampParseErrorOwned {
1177    #[must_use] pub fn to_shared(&self) -> StyleLineClampParseError<'_> {
1178        match self {
1179            Self::InvalidValue(s) => StyleLineClampParseError::InvalidValue(s.as_str()),
1180            Self::ZeroValue => StyleLineClampParseError::ZeroValue,
1181        }
1182    }
1183}
1184
1185#[cfg(feature = "parser")]
1186impl From<StyleLineClampParseError<'_>> for StyleLineClampParseErrorOwned {
1187    fn from(e: StyleLineClampParseError<'_>) -> Self {
1188        e.to_contained()
1189    }
1190}
1191
1192#[cfg(feature = "parser")]
1193impl_display! { StyleLineClampParseErrorOwned, {
1194    InvalidValue(e) => format!("Invalid line-clamp value: {}", e),
1195    ZeroValue => format!("line-clamp cannot be zero"),
1196}}
1197
1198#[cfg(feature = "parser")]
1199/// # Errors
1200///
1201/// Returns an error if `input` is not a valid CSS `line-clamp` value.
1202pub fn parse_style_line_clamp(
1203    input: &str,
1204) -> Result<StyleLineClamp, StyleLineClampParseError<'_>> {
1205    let input = input.trim();
1206
1207    let max_lines = input
1208        .parse::<usize>()
1209        .map_err(|_| StyleLineClampParseError::InvalidValue(input))?;
1210
1211    if max_lines == 0 {
1212        return Err(StyleLineClampParseError::ZeroValue);
1213    }
1214
1215    Ok(StyleLineClamp { max_lines })
1216}
1217
1218/// hanging-punctuation property for hanging punctuation marks
1219///
1220/// CSS Text 3 §8: `none | [ first || [ force-end | allow-end ] || last ]`
1221#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1222#[repr(C)]
1223#[derive(Default)]
1224pub struct StyleHangingPunctuation {
1225    pub first: bool,
1226    pub force_end: bool,
1227    pub allow_end: bool,
1228    pub last: bool,
1229}
1230
1231impl StyleHangingPunctuation {
1232    #[must_use] pub const fn is_enabled(&self) -> bool {
1233        self.first || self.force_end || self.allow_end || self.last
1234    }
1235}
1236
1237impl FormatAsRustCode for StyleHangingPunctuation {
1238    fn format_as_rust_code(&self, _tabs: usize) -> String {
1239        format!("{self:?}")
1240    }
1241}
1242
1243impl PrintAsCssValue for StyleHangingPunctuation {
1244    fn print_as_css_value(&self) -> String {
1245        if !self.is_enabled() {
1246            return "none".to_string();
1247        }
1248        let mut parts = Vec::new();
1249        if self.first { parts.push("first"); }
1250        if self.force_end { parts.push("force-end"); }
1251        if self.allow_end { parts.push("allow-end"); }
1252        if self.last { parts.push("last"); }
1253        parts.join(" ")
1254    }
1255}
1256
1257#[cfg(feature = "parser")]
1258#[derive(Clone, PartialEq, Eq)]
1259pub enum StyleHangingPunctuationParseError<'a> {
1260    InvalidValue(&'a str),
1261}
1262#[cfg(feature = "parser")]
1263impl_debug_as_display!(StyleHangingPunctuationParseError<'a>);
1264#[cfg(feature = "parser")]
1265impl_display! { StyleHangingPunctuationParseError<'a>, {
1266    InvalidValue(e) => format!("Invalid hanging-punctuation value: {}", e),
1267}}
1268
1269#[cfg(feature = "parser")]
1270#[derive(Debug, Clone, PartialEq, Eq)]
1271#[repr(C, u8)]
1272pub enum StyleHangingPunctuationParseErrorOwned {
1273    InvalidValue(AzString),
1274}
1275
1276#[cfg(feature = "parser")]
1277impl StyleHangingPunctuationParseError<'_> {
1278    #[must_use] pub fn to_contained(&self) -> StyleHangingPunctuationParseErrorOwned {
1279        match self {
1280            Self::InvalidValue(s) => {
1281                StyleHangingPunctuationParseErrorOwned::InvalidValue((*s).to_string().into())
1282            }
1283        }
1284    }
1285}
1286
1287#[cfg(feature = "parser")]
1288impl StyleHangingPunctuationParseErrorOwned {
1289    #[must_use] pub fn to_shared(&self) -> StyleHangingPunctuationParseError<'_> {
1290        match self {
1291            Self::InvalidValue(s) => StyleHangingPunctuationParseError::InvalidValue(s.as_str()),
1292        }
1293    }
1294}
1295
1296#[cfg(feature = "parser")]
1297impl From<StyleHangingPunctuationParseError<'_>> for StyleHangingPunctuationParseErrorOwned {
1298    fn from(e: StyleHangingPunctuationParseError<'_>) -> Self {
1299        e.to_contained()
1300    }
1301}
1302
1303#[cfg(feature = "parser")]
1304impl_display! { StyleHangingPunctuationParseErrorOwned, {
1305    InvalidValue(e) => format!("Invalid hanging-punctuation value: {}", e),
1306}}
1307
1308#[cfg(feature = "parser")]
1309/// # Errors
1310///
1311/// Returns an error if `input` is not a valid CSS `hanging-punctuation` value.
1312pub fn parse_style_hanging_punctuation(
1313    input: &str,
1314) -> Result<StyleHangingPunctuation, StyleHangingPunctuationParseError<'_>> {
1315    let input = input.trim();
1316
1317    if input.eq_ignore_ascii_case("none") {
1318        return Ok(StyleHangingPunctuation::default());
1319    }
1320
1321    let mut first = false;
1322    let mut force_end = false;
1323    let mut allow_end = false;
1324    let mut last = false;
1325
1326    for token in input.split_whitespace() {
1327        match token.to_lowercase().as_str() {
1328            "first" => first = true,
1329            "force-end" => force_end = true,
1330            "allow-end" => allow_end = true,
1331            "last" => last = true,
1332            _ => return Err(StyleHangingPunctuationParseError::InvalidValue(input)),
1333        }
1334    }
1335
1336    if force_end && allow_end {
1337        return Err(StyleHangingPunctuationParseError::InvalidValue(input));
1338    }
1339
1340    Ok(StyleHangingPunctuation { first, force_end, allow_end, last })
1341}
1342
1343/// text-combine-upright property for combining horizontal text in vertical layout
1344#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1345#[repr(C, u8)]
1346#[derive(Default)]
1347pub enum StyleTextCombineUpright {
1348    #[default]
1349    None,
1350    All,
1351    Digits(u8),
1352}
1353
1354
1355impl FormatAsRustCode for StyleTextCombineUpright {
1356    fn format_as_rust_code(&self, _tabs: usize) -> String {
1357        format!("{self:?}")
1358    }
1359}
1360
1361impl PrintAsCssValue for StyleTextCombineUpright {
1362    fn print_as_css_value(&self) -> String {
1363        match self {
1364            Self::None => "none".to_string(),
1365            Self::All => "all".to_string(),
1366            Self::Digits(n) => format!("digits {n}"),
1367        }
1368    }
1369}
1370
1371#[cfg(feature = "parser")]
1372#[derive(Clone, PartialEq, Eq)]
1373pub enum StyleTextCombineUprightParseError<'a> {
1374    InvalidValue(&'a str),
1375    InvalidDigits(&'a str),
1376}
1377#[cfg(feature = "parser")]
1378impl_debug_as_display!(StyleTextCombineUprightParseError<'a>);
1379#[cfg(feature = "parser")]
1380impl_display! { StyleTextCombineUprightParseError<'a>, {
1381    InvalidValue(e) => format!("Invalid text-combine-upright value: {}", e),
1382    InvalidDigits(e) => format!("Invalid text-combine-upright digits: {}", e),
1383}}
1384
1385#[cfg(feature = "parser")]
1386#[derive(Debug, Clone, PartialEq, Eq)]
1387#[repr(C, u8)]
1388pub enum StyleTextCombineUprightParseErrorOwned {
1389    InvalidValue(AzString),
1390    InvalidDigits(AzString),
1391}
1392
1393#[cfg(feature = "parser")]
1394impl StyleTextCombineUprightParseError<'_> {
1395    #[must_use] pub fn to_contained(&self) -> StyleTextCombineUprightParseErrorOwned {
1396        match self {
1397            Self::InvalidValue(s) => {
1398                StyleTextCombineUprightParseErrorOwned::InvalidValue((*s).to_string().into())
1399            }
1400            Self::InvalidDigits(s) => {
1401                StyleTextCombineUprightParseErrorOwned::InvalidDigits((*s).to_string().into())
1402            }
1403        }
1404    }
1405}
1406
1407#[cfg(feature = "parser")]
1408impl StyleTextCombineUprightParseErrorOwned {
1409    #[must_use] pub fn to_shared(&self) -> StyleTextCombineUprightParseError<'_> {
1410        match self {
1411            Self::InvalidValue(s) => StyleTextCombineUprightParseError::InvalidValue(s.as_str()),
1412            Self::InvalidDigits(s) => StyleTextCombineUprightParseError::InvalidDigits(s.as_str()),
1413        }
1414    }
1415}
1416
1417#[cfg(feature = "parser")]
1418impl From<StyleTextCombineUprightParseError<'_>> for StyleTextCombineUprightParseErrorOwned {
1419    fn from(e: StyleTextCombineUprightParseError<'_>) -> Self {
1420        e.to_contained()
1421    }
1422}
1423
1424#[cfg(feature = "parser")]
1425impl_display! { StyleTextCombineUprightParseErrorOwned, {
1426    InvalidValue(e) => format!("Invalid text-combine-upright value: {}", e),
1427    InvalidDigits(e) => format!("Invalid text-combine-upright digits: {}", e),
1428}}
1429
1430#[cfg(feature = "parser")]
1431/// # Errors
1432///
1433/// Returns an error if `input` is not a valid CSS `text-combine-upright` value.
1434pub fn parse_style_text_combine_upright(
1435    input: &str,
1436) -> Result<StyleTextCombineUpright, StyleTextCombineUprightParseError<'_>> {
1437    let trimmed = input.trim();
1438
1439    if trimmed.eq_ignore_ascii_case("none") {
1440        Ok(StyleTextCombineUpright::None)
1441    } else if trimmed.eq_ignore_ascii_case("all") {
1442        Ok(StyleTextCombineUpright::All)
1443    } else if trimmed.starts_with("digits") {
1444        let parts: Vec<&str> = trimmed.split_whitespace().collect();
1445        if parts.len() == 2 {
1446            let n = parts[1]
1447                .parse::<u8>()
1448                .map_err(|_| StyleTextCombineUprightParseError::InvalidDigits(input))?;
1449            if (2..=4).contains(&n) {
1450                Ok(StyleTextCombineUpright::Digits(n))
1451            } else {
1452                Err(StyleTextCombineUprightParseError::InvalidDigits(input))
1453            }
1454        } else {
1455            // Default to "digits 2"
1456            Ok(StyleTextCombineUpright::Digits(2))
1457        }
1458    } else {
1459        Err(StyleTextCombineUprightParseError::InvalidValue(input))
1460    }
1461}
1462
1463#[cfg(feature = "parser")]
1464#[derive(Clone, PartialEq, Eq)]
1465pub enum StyleWordSpacingParseError<'a> {
1466    PixelValue(CssPixelValueParseError<'a>),
1467}
1468#[cfg(feature = "parser")]
1469impl_debug_as_display!(StyleWordSpacingParseError<'a>);
1470#[cfg(feature = "parser")]
1471impl_display! { StyleWordSpacingParseError<'a>, {
1472    PixelValue(e) => format!("Invalid word-spacing value: {}", e),
1473}}
1474#[cfg(feature = "parser")]
1475impl_from!(
1476    CssPixelValueParseError<'a>,
1477    StyleWordSpacingParseError::PixelValue
1478);
1479
1480#[cfg(feature = "parser")]
1481#[derive(Debug, Clone, PartialEq, Eq)]
1482#[repr(C, u8)]
1483pub enum StyleWordSpacingParseErrorOwned {
1484    PixelValue(CssPixelValueParseErrorOwned),
1485}
1486
1487#[cfg(feature = "parser")]
1488impl StyleWordSpacingParseError<'_> {
1489    #[must_use] pub fn to_contained(&self) -> StyleWordSpacingParseErrorOwned {
1490        match self {
1491            Self::PixelValue(e) => StyleWordSpacingParseErrorOwned::PixelValue(e.to_contained()),
1492        }
1493    }
1494}
1495
1496#[cfg(feature = "parser")]
1497impl StyleWordSpacingParseErrorOwned {
1498    #[must_use] pub fn to_shared(&self) -> StyleWordSpacingParseError<'_> {
1499        match self {
1500            Self::PixelValue(e) => StyleWordSpacingParseError::PixelValue(e.to_shared()),
1501        }
1502    }
1503}
1504
1505#[cfg(feature = "parser")]
1506/// # Errors
1507///
1508/// Returns an error if `input` is not a valid CSS `word-spacing` value.
1509pub fn parse_style_word_spacing(
1510    input: &str,
1511) -> Result<StyleWordSpacing, StyleWordSpacingParseError<'_>> {
1512    crate::props::basic::pixel::parse_pixel_value(input)
1513        .map(|inner| StyleWordSpacing { inner })
1514        .map_err(StyleWordSpacingParseError::PixelValue)
1515}
1516
1517#[cfg(feature = "parser")]
1518#[derive(Clone, PartialEq, Eq)]
1519#[repr(C, u8)]
1520pub enum StyleLineHeightParseError {
1521    Percentage(PercentageParseError),
1522}
1523#[cfg(feature = "parser")]
1524impl_debug_as_display!(StyleLineHeightParseError);
1525#[cfg(feature = "parser")]
1526impl_display! { StyleLineHeightParseError, {
1527    Percentage(e) => format!("Invalid line-height value: {}", e),
1528}}
1529#[cfg(feature = "parser")]
1530impl_from!(PercentageParseError, StyleLineHeightParseError::Percentage);
1531
1532#[cfg(feature = "parser")]
1533#[derive(Debug, Clone, PartialEq, Eq)]
1534pub enum StyleLineHeightParseErrorOwned {
1535    Percentage(PercentageParseErrorOwned),
1536}
1537
1538#[cfg(feature = "parser")]
1539impl StyleLineHeightParseError {
1540    #[must_use] pub fn to_contained(&self) -> StyleLineHeightParseErrorOwned {
1541        match self {
1542            Self::Percentage(e) => StyleLineHeightParseErrorOwned::Percentage(e.to_contained()),
1543        }
1544    }
1545}
1546
1547#[cfg(feature = "parser")]
1548impl StyleLineHeightParseErrorOwned {
1549    #[must_use] pub fn to_shared(&self) -> StyleLineHeightParseError {
1550        match self {
1551            Self::Percentage(e) => StyleLineHeightParseError::Percentage(e.to_shared()),
1552        }
1553    }
1554}
1555
1556#[cfg(feature = "parser")]
1557/// # Errors
1558///
1559/// Returns an error if `input` is not a valid CSS `line-height` value.
1560pub fn parse_style_line_height(input: &str) -> Result<StyleLineHeight, StyleLineHeightParseError> {
1561    // Try <number> or <percentage> first (multiplier of font-size)
1562    if let Ok(inner) = crate::props::basic::length::parse_percentage_value(input) {
1563        return Ok(StyleLineHeight { inner });
1564    }
1565    // Try <length> (e.g., "50px") — store as NEGATIVE PercentageValue to signal absolute px.
1566    // Convention: negative normalized() = absolute pixel value (CSS line-height can't be negative).
1567    // Resolved at layout time in fc.rs where font_size is known.
1568    if let Ok(px) = crate::props::basic::pixel::parse_pixel_value(input) {
1569        if px.metric == crate::props::basic::length::SizeMetric::Px {
1570            let px_val = px.number.get();
1571            return Ok(StyleLineHeight {
1572                inner: PercentageValue::new(-px_val * 100.0),
1573            });
1574        }
1575    }
1576    Err(StyleLineHeightParseError::Percentage(
1577        PercentageParseError::InvalidUnit(String::new().into()),
1578    ))
1579}
1580
1581#[cfg(feature = "parser")]
1582#[derive(Clone, PartialEq, Eq)]
1583pub enum StyleTabSizeParseError<'a> {
1584    PixelValue(CssPixelValueParseError<'a>),
1585}
1586#[cfg(feature = "parser")]
1587impl_debug_as_display!(StyleTabSizeParseError<'a>);
1588#[cfg(feature = "parser")]
1589impl_display! { StyleTabSizeParseError<'a>, {
1590    PixelValue(e) => format!("Invalid tab-size value: {}", e),
1591}}
1592#[cfg(feature = "parser")]
1593impl_from!(
1594    CssPixelValueParseError<'a>,
1595    StyleTabSizeParseError::PixelValue
1596);
1597
1598#[cfg(feature = "parser")]
1599#[derive(Debug, Clone, PartialEq, Eq)]
1600#[repr(C, u8)]
1601pub enum StyleTabSizeParseErrorOwned {
1602    PixelValue(CssPixelValueParseErrorOwned),
1603}
1604
1605#[cfg(feature = "parser")]
1606impl StyleTabSizeParseError<'_> {
1607    #[must_use] pub fn to_contained(&self) -> StyleTabSizeParseErrorOwned {
1608        match self {
1609            Self::PixelValue(e) => StyleTabSizeParseErrorOwned::PixelValue(e.to_contained()),
1610        }
1611    }
1612}
1613
1614#[cfg(feature = "parser")]
1615impl StyleTabSizeParseErrorOwned {
1616    #[must_use] pub fn to_shared(&self) -> StyleTabSizeParseError<'_> {
1617        match self {
1618            Self::PixelValue(e) => StyleTabSizeParseError::PixelValue(e.to_shared()),
1619        }
1620    }
1621}
1622
1623#[cfg(feature = "parser")]
1624/// # Errors
1625///
1626/// Returns an error if `input` is not a valid CSS `tab-size` value.
1627pub fn parse_style_tab_size(input: &str) -> Result<StyleTabSize, StyleTabSizeParseError<'_>> {
1628    input.trim().parse::<f32>().map_or_else(
1629        |_| {
1630            crate::props::basic::pixel::parse_pixel_value(input)
1631                .map(|v| StyleTabSize { inner: v })
1632                .map_err(StyleTabSizeParseError::PixelValue)
1633        },
1634        |number| {
1635            Ok(StyleTabSize {
1636                inner: PixelValue::em(number),
1637            })
1638        },
1639    )
1640}
1641
1642#[cfg(feature = "parser")]
1643#[derive(Clone, PartialEq, Eq)]
1644pub enum StyleWhiteSpaceParseError<'a> {
1645    InvalidValue(InvalidValueErr<'a>),
1646}
1647#[cfg(feature = "parser")]
1648impl_debug_as_display!(StyleWhiteSpaceParseError<'a>);
1649#[cfg(feature = "parser")]
1650impl_display! { StyleWhiteSpaceParseError<'a>, {
1651    InvalidValue(e) => format!("Invalid white-space value: \"{}\"", e.0),
1652}}
1653#[cfg(feature = "parser")]
1654impl_from!(InvalidValueErr<'a>, StyleWhiteSpaceParseError::InvalidValue);
1655
1656#[cfg(feature = "parser")]
1657#[derive(Debug, Clone, PartialEq, Eq)]
1658#[repr(C, u8)]
1659pub enum StyleWhiteSpaceParseErrorOwned {
1660    InvalidValue(InvalidValueErrOwned),
1661}
1662
1663#[cfg(feature = "parser")]
1664impl StyleWhiteSpaceParseError<'_> {
1665    #[must_use] pub fn to_contained(&self) -> StyleWhiteSpaceParseErrorOwned {
1666        match self {
1667            Self::InvalidValue(e) => StyleWhiteSpaceParseErrorOwned::InvalidValue(e.to_contained()),
1668        }
1669    }
1670}
1671
1672#[cfg(feature = "parser")]
1673impl StyleWhiteSpaceParseErrorOwned {
1674    #[must_use] pub fn to_shared(&self) -> StyleWhiteSpaceParseError<'_> {
1675        match self {
1676            Self::InvalidValue(e) => StyleWhiteSpaceParseError::InvalidValue(e.to_shared()),
1677        }
1678    }
1679}
1680
1681#[cfg(feature = "parser")]
1682/// # Errors
1683///
1684/// Returns an error if `input` is not a valid CSS `white-space` value.
1685pub fn parse_style_white_space(input: &str) -> Result<StyleWhiteSpace, StyleWhiteSpaceParseError<'_>> {
1686    match input.trim() {
1687        "normal" => Ok(StyleWhiteSpace::Normal),
1688        "pre" => Ok(StyleWhiteSpace::Pre),
1689        "nowrap" | "no-wrap" => Ok(StyleWhiteSpace::Nowrap),
1690        "pre-wrap" => Ok(StyleWhiteSpace::PreWrap),
1691        "pre-line" => Ok(StyleWhiteSpace::PreLine),
1692        "break-spaces" => Ok(StyleWhiteSpace::BreakSpaces),
1693        other => Err(StyleWhiteSpaceParseError::InvalidValue(InvalidValueErr(
1694            other,
1695        ))),
1696    }
1697}
1698
1699#[cfg(feature = "parser")]
1700#[derive(Clone, PartialEq, Eq)]
1701pub enum StyleHyphensParseError<'a> {
1702    InvalidValue(InvalidValueErr<'a>),
1703}
1704#[cfg(feature = "parser")]
1705impl_debug_as_display!(StyleHyphensParseError<'a>);
1706#[cfg(feature = "parser")]
1707impl_display! { StyleHyphensParseError<'a>, {
1708    InvalidValue(e) => format!("Invalid hyphens value: \"{}\"", e.0),
1709}}
1710#[cfg(feature = "parser")]
1711impl_from!(InvalidValueErr<'a>, StyleHyphensParseError::InvalidValue);
1712
1713#[cfg(feature = "parser")]
1714#[derive(Debug, Clone, PartialEq, Eq)]
1715#[repr(C, u8)]
1716pub enum StyleHyphensParseErrorOwned {
1717    InvalidValue(InvalidValueErrOwned),
1718}
1719
1720#[cfg(feature = "parser")]
1721impl StyleHyphensParseError<'_> {
1722    #[must_use] pub fn to_contained(&self) -> StyleHyphensParseErrorOwned {
1723        match self {
1724            Self::InvalidValue(e) => StyleHyphensParseErrorOwned::InvalidValue(e.to_contained()),
1725        }
1726    }
1727}
1728
1729#[cfg(feature = "parser")]
1730impl StyleHyphensParseErrorOwned {
1731    #[must_use] pub fn to_shared(&self) -> StyleHyphensParseError<'_> {
1732        match self {
1733            Self::InvalidValue(e) => StyleHyphensParseError::InvalidValue(e.to_shared()),
1734        }
1735    }
1736}
1737
1738#[cfg(feature = "parser")]
1739/// # Errors
1740///
1741/// Returns an error if `input` is not a valid CSS `hyphens` value.
1742pub fn parse_style_hyphens(input: &str) -> Result<StyleHyphens, StyleHyphensParseError<'_>> {
1743    match input.trim() {
1744        "none" => Ok(StyleHyphens::None),
1745        "manual" => Ok(StyleHyphens::Manual),
1746        "auto" => Ok(StyleHyphens::Auto),
1747        other => Err(StyleHyphensParseError::InvalidValue(InvalidValueErr(other))),
1748    }
1749}
1750
1751// -- StyleLineBreak parse --
1752
1753#[cfg(feature = "parser")]
1754#[derive(Clone, PartialEq, Eq)]
1755pub enum StyleLineBreakParseError<'a> {
1756    InvalidValue(InvalidValueErr<'a>),
1757}
1758#[cfg(feature = "parser")]
1759impl_debug_as_display!(StyleLineBreakParseError<'a>);
1760#[cfg(feature = "parser")]
1761impl_display! { StyleLineBreakParseError<'a>, {
1762    InvalidValue(e) => format!("Invalid line-break value: \"{}\"", e.0),
1763}}
1764#[cfg(feature = "parser")]
1765impl_from!(InvalidValueErr<'a>, StyleLineBreakParseError::InvalidValue);
1766
1767#[cfg(feature = "parser")]
1768#[derive(Debug, Clone, PartialEq, Eq)]
1769#[repr(C, u8)]
1770pub enum StyleLineBreakParseErrorOwned {
1771    InvalidValue(InvalidValueErrOwned),
1772}
1773
1774#[cfg(feature = "parser")]
1775impl StyleLineBreakParseError<'_> {
1776    #[must_use] pub fn to_contained(&self) -> StyleLineBreakParseErrorOwned {
1777        match self {
1778            Self::InvalidValue(e) => StyleLineBreakParseErrorOwned::InvalidValue(e.to_contained()),
1779        }
1780    }
1781}
1782
1783#[cfg(feature = "parser")]
1784impl StyleLineBreakParseErrorOwned {
1785    #[must_use] pub fn to_shared(&self) -> StyleLineBreakParseError<'_> {
1786        match self {
1787            Self::InvalidValue(e) => StyleLineBreakParseError::InvalidValue(e.to_shared()),
1788        }
1789    }
1790}
1791
1792#[cfg(feature = "parser")]
1793/// # Errors
1794///
1795/// Returns an error if `input` is not a valid CSS `line-break` value.
1796pub fn parse_style_line_break(input: &str) -> Result<StyleLineBreak, StyleLineBreakParseError<'_>> {
1797    match input.trim() {
1798        "auto" => Ok(StyleLineBreak::Auto),
1799        "loose" => Ok(StyleLineBreak::Loose),
1800        "normal" => Ok(StyleLineBreak::Normal),
1801        "strict" => Ok(StyleLineBreak::Strict),
1802        "anywhere" => Ok(StyleLineBreak::Anywhere),
1803        other => Err(StyleLineBreakParseError::InvalidValue(InvalidValueErr(other))),
1804    }
1805}
1806
1807// -- StyleWordBreak parse --
1808
1809#[cfg(feature = "parser")]
1810#[derive(Clone, PartialEq, Eq)]
1811pub enum StyleWordBreakParseError<'a> {
1812    InvalidValue(InvalidValueErr<'a>),
1813}
1814#[cfg(feature = "parser")]
1815impl_debug_as_display!(StyleWordBreakParseError<'a>);
1816#[cfg(feature = "parser")]
1817impl_display! { StyleWordBreakParseError<'a>, {
1818    InvalidValue(e) => format!("Invalid word-break value: \"{}\"", e.0),
1819}}
1820#[cfg(feature = "parser")]
1821impl_from!(InvalidValueErr<'a>, StyleWordBreakParseError::InvalidValue);
1822
1823#[cfg(feature = "parser")]
1824#[derive(Debug, Clone, PartialEq, Eq)]
1825#[repr(C, u8)]
1826pub enum StyleWordBreakParseErrorOwned {
1827    InvalidValue(InvalidValueErrOwned),
1828}
1829
1830#[cfg(feature = "parser")]
1831impl StyleWordBreakParseError<'_> {
1832    #[must_use] pub fn to_contained(&self) -> StyleWordBreakParseErrorOwned {
1833        match self {
1834            Self::InvalidValue(e) => StyleWordBreakParseErrorOwned::InvalidValue(e.to_contained()),
1835        }
1836    }
1837}
1838
1839#[cfg(feature = "parser")]
1840impl StyleWordBreakParseErrorOwned {
1841    #[must_use] pub fn to_shared(&self) -> StyleWordBreakParseError<'_> {
1842        match self {
1843            Self::InvalidValue(e) => StyleWordBreakParseError::InvalidValue(e.to_shared()),
1844        }
1845    }
1846}
1847
1848#[cfg(feature = "parser")]
1849/// # Errors
1850///
1851/// Returns an error if `input` is not a valid CSS `word-break` value.
1852pub fn parse_style_word_break(input: &str) -> Result<StyleWordBreak, StyleWordBreakParseError<'_>> {
1853    match input.trim() {
1854        "normal" => Ok(StyleWordBreak::Normal),
1855        "break-all" => Ok(StyleWordBreak::BreakAll),
1856        "keep-all" => Ok(StyleWordBreak::KeepAll),
1857        "break-word" => Ok(StyleWordBreak::BreakWord),
1858        other => Err(StyleWordBreakParseError::InvalidValue(InvalidValueErr(other))),
1859    }
1860}
1861
1862// -- StyleOverflowWrap parse --
1863
1864#[cfg(feature = "parser")]
1865#[derive(Clone, PartialEq, Eq)]
1866pub enum StyleOverflowWrapParseError<'a> {
1867    InvalidValue(InvalidValueErr<'a>),
1868}
1869#[cfg(feature = "parser")]
1870impl_debug_as_display!(StyleOverflowWrapParseError<'a>);
1871#[cfg(feature = "parser")]
1872impl_display! { StyleOverflowWrapParseError<'a>, {
1873    InvalidValue(e) => format!("Invalid overflow-wrap value: \"{}\"", e.0),
1874}}
1875#[cfg(feature = "parser")]
1876impl_from!(InvalidValueErr<'a>, StyleOverflowWrapParseError::InvalidValue);
1877
1878#[cfg(feature = "parser")]
1879#[derive(Debug, Clone, PartialEq, Eq)]
1880#[repr(C, u8)]
1881pub enum StyleOverflowWrapParseErrorOwned {
1882    InvalidValue(InvalidValueErrOwned),
1883}
1884
1885#[cfg(feature = "parser")]
1886impl StyleOverflowWrapParseError<'_> {
1887    #[must_use] pub fn to_contained(&self) -> StyleOverflowWrapParseErrorOwned {
1888        match self {
1889            Self::InvalidValue(e) => StyleOverflowWrapParseErrorOwned::InvalidValue(e.to_contained()),
1890        }
1891    }
1892}
1893
1894#[cfg(feature = "parser")]
1895impl StyleOverflowWrapParseErrorOwned {
1896    #[must_use] pub fn to_shared(&self) -> StyleOverflowWrapParseError<'_> {
1897        match self {
1898            Self::InvalidValue(e) => StyleOverflowWrapParseError::InvalidValue(e.to_shared()),
1899        }
1900    }
1901}
1902
1903#[cfg(feature = "parser")]
1904/// # Errors
1905///
1906/// Returns an error if `input` is not a valid CSS `overflow-wrap` value.
1907pub fn parse_style_overflow_wrap(input: &str) -> Result<StyleOverflowWrap, StyleOverflowWrapParseError<'_>> {
1908    match input.trim() {
1909        "normal" => Ok(StyleOverflowWrap::Normal),
1910        "anywhere" => Ok(StyleOverflowWrap::Anywhere),
1911        "break-word" => Ok(StyleOverflowWrap::BreakWord),
1912        other => Err(StyleOverflowWrapParseError::InvalidValue(InvalidValueErr(other))),
1913    }
1914}
1915
1916// -- StyleTextAlignLast parse --
1917
1918#[cfg(feature = "parser")]
1919#[derive(Clone, PartialEq, Eq)]
1920pub enum StyleTextAlignLastParseError<'a> {
1921    InvalidValue(InvalidValueErr<'a>),
1922}
1923#[cfg(feature = "parser")]
1924impl_debug_as_display!(StyleTextAlignLastParseError<'a>);
1925#[cfg(feature = "parser")]
1926impl_display! { StyleTextAlignLastParseError<'a>, {
1927    InvalidValue(e) => format!("Invalid text-align-last value: \"{}\"", e.0),
1928}}
1929#[cfg(feature = "parser")]
1930impl_from!(InvalidValueErr<'a>, StyleTextAlignLastParseError::InvalidValue);
1931
1932#[cfg(feature = "parser")]
1933#[derive(Debug, Clone, PartialEq, Eq)]
1934#[repr(C, u8)]
1935pub enum StyleTextAlignLastParseErrorOwned {
1936    InvalidValue(InvalidValueErrOwned),
1937}
1938
1939#[cfg(feature = "parser")]
1940impl StyleTextAlignLastParseError<'_> {
1941    #[must_use] pub fn to_contained(&self) -> StyleTextAlignLastParseErrorOwned {
1942        match self {
1943            Self::InvalidValue(e) => StyleTextAlignLastParseErrorOwned::InvalidValue(e.to_contained()),
1944        }
1945    }
1946}
1947
1948#[cfg(feature = "parser")]
1949impl StyleTextAlignLastParseErrorOwned {
1950    #[must_use] pub fn to_shared(&self) -> StyleTextAlignLastParseError<'_> {
1951        match self {
1952            Self::InvalidValue(e) => StyleTextAlignLastParseError::InvalidValue(e.to_shared()),
1953        }
1954    }
1955}
1956
1957#[cfg(feature = "parser")]
1958/// # Errors
1959///
1960/// Returns an error if `input` is not a valid CSS `text-align-last` value.
1961pub fn parse_style_text_align_last(input: &str) -> Result<StyleTextAlignLast, StyleTextAlignLastParseError<'_>> {
1962    match input.trim() {
1963        "auto" => Ok(StyleTextAlignLast::Auto),
1964        "start" => Ok(StyleTextAlignLast::Start),
1965        "end" => Ok(StyleTextAlignLast::End),
1966        "left" => Ok(StyleTextAlignLast::Left),
1967        "right" => Ok(StyleTextAlignLast::Right),
1968        "center" => Ok(StyleTextAlignLast::Center),
1969        "justify" => Ok(StyleTextAlignLast::Justify),
1970        other => Err(StyleTextAlignLastParseError::InvalidValue(InvalidValueErr(other))),
1971    }
1972}
1973
1974// -- StyleTextTransform parse --
1975
1976#[cfg(feature = "parser")]
1977#[derive(Clone, PartialEq, Eq)]
1978pub enum StyleTextTransformParseError<'a> {
1979    InvalidValue(InvalidValueErr<'a>),
1980}
1981#[cfg(feature = "parser")]
1982impl_debug_as_display!(StyleTextTransformParseError<'a>);
1983#[cfg(feature = "parser")]
1984impl_display! { StyleTextTransformParseError<'a>, {
1985    InvalidValue(e) => format!("Invalid text-transform value: \"{}\"", e.0),
1986}}
1987#[cfg(feature = "parser")]
1988impl_from!(InvalidValueErr<'a>, StyleTextTransformParseError::InvalidValue);
1989
1990#[cfg(feature = "parser")]
1991#[derive(Debug, Clone, PartialEq, Eq)]
1992#[repr(C, u8)]
1993pub enum StyleTextTransformParseErrorOwned {
1994    InvalidValue(InvalidValueErrOwned),
1995}
1996
1997#[cfg(feature = "parser")]
1998impl StyleTextTransformParseError<'_> {
1999    #[must_use] pub fn to_contained(&self) -> StyleTextTransformParseErrorOwned {
2000        match self {
2001            Self::InvalidValue(e) => StyleTextTransformParseErrorOwned::InvalidValue(e.to_contained()),
2002        }
2003    }
2004}
2005
2006#[cfg(feature = "parser")]
2007impl StyleTextTransformParseErrorOwned {
2008    #[must_use] pub fn to_shared(&self) -> StyleTextTransformParseError<'_> {
2009        match self {
2010            Self::InvalidValue(e) => StyleTextTransformParseError::InvalidValue(e.to_shared()),
2011        }
2012    }
2013}
2014
2015#[cfg(feature = "parser")]
2016/// # Errors
2017///
2018/// Returns an error if `input` is not a valid CSS `text-transform` value.
2019pub fn parse_style_text_transform(input: &str) -> Result<StyleTextTransform, StyleTextTransformParseError<'_>> {
2020    match input.trim() {
2021        "none" => Ok(StyleTextTransform::None),
2022        "capitalize" => Ok(StyleTextTransform::Capitalize),
2023        "uppercase" => Ok(StyleTextTransform::Uppercase),
2024        "lowercase" => Ok(StyleTextTransform::Lowercase),
2025        "full-width" => Ok(StyleTextTransform::FullWidth),
2026        other => Err(StyleTextTransformParseError::InvalidValue(InvalidValueErr(other))),
2027    }
2028}
2029
2030#[cfg(feature = "parser")]
2031#[derive(Clone, PartialEq, Eq)]
2032pub enum StyleDirectionParseError<'a> {
2033    InvalidValue(InvalidValueErr<'a>),
2034}
2035#[cfg(feature = "parser")]
2036impl_debug_as_display!(StyleDirectionParseError<'a>);
2037#[cfg(feature = "parser")]
2038impl_display! { StyleDirectionParseError<'a>, {
2039    InvalidValue(e) => format!("Invalid direction value: \"{}\"", e.0),
2040}}
2041#[cfg(feature = "parser")]
2042impl_from!(InvalidValueErr<'a>, StyleDirectionParseError::InvalidValue);
2043
2044#[cfg(feature = "parser")]
2045#[derive(Debug, Clone, PartialEq, Eq)]
2046#[repr(C, u8)]
2047pub enum StyleDirectionParseErrorOwned {
2048    InvalidValue(InvalidValueErrOwned),
2049}
2050
2051#[cfg(feature = "parser")]
2052impl StyleDirectionParseError<'_> {
2053    #[must_use] pub fn to_contained(&self) -> StyleDirectionParseErrorOwned {
2054        match self {
2055            Self::InvalidValue(e) => StyleDirectionParseErrorOwned::InvalidValue(e.to_contained()),
2056        }
2057    }
2058}
2059
2060#[cfg(feature = "parser")]
2061impl StyleDirectionParseErrorOwned {
2062    #[must_use] pub fn to_shared(&self) -> StyleDirectionParseError<'_> {
2063        match self {
2064            Self::InvalidValue(e) => StyleDirectionParseError::InvalidValue(e.to_shared()),
2065        }
2066    }
2067}
2068
2069#[cfg(feature = "parser")]
2070/// # Errors
2071///
2072/// Returns an error if `input` is not a valid CSS `direction` value.
2073pub fn parse_style_direction(input: &str) -> Result<StyleDirection, StyleDirectionParseError<'_>> {
2074    match input.trim() {
2075        "ltr" => Ok(StyleDirection::Ltr),
2076        "rtl" => Ok(StyleDirection::Rtl),
2077        other => Err(StyleDirectionParseError::InvalidValue(InvalidValueErr(
2078            other,
2079        ))),
2080    }
2081}
2082
2083#[cfg(feature = "parser")]
2084#[derive(Clone, PartialEq, Eq)]
2085pub enum StyleUserSelectParseError<'a> {
2086    InvalidValue(InvalidValueErr<'a>),
2087}
2088#[cfg(feature = "parser")]
2089impl_debug_as_display!(StyleUserSelectParseError<'a>);
2090#[cfg(feature = "parser")]
2091impl_display! { StyleUserSelectParseError<'a>, {
2092    InvalidValue(e) => format!("Invalid user-select value: \"{}\"", e.0),
2093}}
2094#[cfg(feature = "parser")]
2095impl_from!(InvalidValueErr<'a>, StyleUserSelectParseError::InvalidValue);
2096
2097#[cfg(feature = "parser")]
2098#[derive(Debug, Clone, PartialEq, Eq)]
2099#[repr(C, u8)]
2100pub enum StyleUserSelectParseErrorOwned {
2101    InvalidValue(InvalidValueErrOwned),
2102}
2103
2104#[cfg(feature = "parser")]
2105impl StyleUserSelectParseError<'_> {
2106    #[must_use] pub fn to_contained(&self) -> StyleUserSelectParseErrorOwned {
2107        match self {
2108            Self::InvalidValue(e) => StyleUserSelectParseErrorOwned::InvalidValue(e.to_contained()),
2109        }
2110    }
2111}
2112
2113#[cfg(feature = "parser")]
2114impl StyleUserSelectParseErrorOwned {
2115    #[must_use] pub fn to_shared(&self) -> StyleUserSelectParseError<'_> {
2116        match self {
2117            Self::InvalidValue(e) => StyleUserSelectParseError::InvalidValue(e.to_shared()),
2118        }
2119    }
2120}
2121
2122#[cfg(feature = "parser")]
2123/// # Errors
2124///
2125/// Returns an error if `input` is not a valid CSS `user-select` value.
2126pub fn parse_style_user_select(input: &str) -> Result<StyleUserSelect, StyleUserSelectParseError<'_>> {
2127    match input.trim() {
2128        "auto" => Ok(StyleUserSelect::Auto),
2129        "text" => Ok(StyleUserSelect::Text),
2130        "none" => Ok(StyleUserSelect::None),
2131        "all" => Ok(StyleUserSelect::All),
2132        other => Err(StyleUserSelectParseError::InvalidValue(InvalidValueErr(
2133            other,
2134        ))),
2135    }
2136}
2137
2138#[cfg(feature = "parser")]
2139#[derive(Clone, PartialEq, Eq)]
2140pub enum StyleTextDecorationParseError<'a> {
2141    InvalidValue(InvalidValueErr<'a>),
2142}
2143#[cfg(feature = "parser")]
2144impl_debug_as_display!(StyleTextDecorationParseError<'a>);
2145#[cfg(feature = "parser")]
2146impl_display! { StyleTextDecorationParseError<'a>, {
2147    InvalidValue(e) => format!("Invalid text-decoration value: \"{}\"", e.0),
2148}}
2149#[cfg(feature = "parser")]
2150impl_from!(
2151    InvalidValueErr<'a>,
2152    StyleTextDecorationParseError::InvalidValue
2153);
2154
2155#[cfg(feature = "parser")]
2156#[derive(Debug, Clone, PartialEq, Eq)]
2157#[repr(C, u8)]
2158pub enum StyleTextDecorationParseErrorOwned {
2159    InvalidValue(InvalidValueErrOwned),
2160}
2161
2162#[cfg(feature = "parser")]
2163impl StyleTextDecorationParseError<'_> {
2164    #[must_use] pub fn to_contained(&self) -> StyleTextDecorationParseErrorOwned {
2165        match self {
2166            Self::InvalidValue(e) => {
2167                StyleTextDecorationParseErrorOwned::InvalidValue(e.to_contained())
2168            }
2169        }
2170    }
2171}
2172
2173#[cfg(feature = "parser")]
2174impl StyleTextDecorationParseErrorOwned {
2175    #[must_use] pub fn to_shared(&self) -> StyleTextDecorationParseError<'_> {
2176        match self {
2177            Self::InvalidValue(e) => StyleTextDecorationParseError::InvalidValue(e.to_shared()),
2178        }
2179    }
2180}
2181
2182#[cfg(feature = "parser")]
2183/// # Errors
2184///
2185/// Returns an error if `input` is not a valid CSS `text-decoration` value.
2186pub fn parse_style_text_decoration(
2187    input: &str,
2188) -> Result<StyleTextDecoration, StyleTextDecorationParseError<'_>> {
2189    match input.trim() {
2190        "none" => Ok(StyleTextDecoration::None),
2191        "underline" => Ok(StyleTextDecoration::Underline),
2192        "overline" => Ok(StyleTextDecoration::Overline),
2193        "line-through" => Ok(StyleTextDecoration::LineThrough),
2194        other => Err(StyleTextDecorationParseError::InvalidValue(
2195            InvalidValueErr(other),
2196        )),
2197    }
2198}
2199
2200#[cfg(feature = "parser")]
2201#[derive(Clone, PartialEq, Eq)]
2202pub enum StyleVerticalAlignParseError<'a> {
2203    InvalidValue(InvalidValueErr<'a>),
2204}
2205#[cfg(feature = "parser")]
2206impl_debug_as_display!(StyleVerticalAlignParseError<'a>);
2207#[cfg(feature = "parser")]
2208impl_display! { StyleVerticalAlignParseError<'a>, {
2209    InvalidValue(e) => format!("Invalid vertical-align value: \"{}\"", e.0),
2210}}
2211#[cfg(feature = "parser")]
2212impl_from!(
2213    InvalidValueErr<'a>,
2214    StyleVerticalAlignParseError::InvalidValue
2215);
2216
2217#[cfg(feature = "parser")]
2218#[derive(Debug, Clone, PartialEq, Eq)]
2219#[repr(C, u8)]
2220pub enum StyleVerticalAlignParseErrorOwned {
2221    InvalidValue(InvalidValueErrOwned),
2222}
2223
2224#[cfg(feature = "parser")]
2225impl StyleVerticalAlignParseError<'_> {
2226    #[must_use] pub fn to_contained(&self) -> StyleVerticalAlignParseErrorOwned {
2227        match self {
2228            Self::InvalidValue(e) => {
2229                StyleVerticalAlignParseErrorOwned::InvalidValue(e.to_contained())
2230            }
2231        }
2232    }
2233}
2234
2235#[cfg(feature = "parser")]
2236impl StyleVerticalAlignParseErrorOwned {
2237    #[must_use] pub fn to_shared(&self) -> StyleVerticalAlignParseError<'_> {
2238        match self {
2239            Self::InvalidValue(e) => StyleVerticalAlignParseError::InvalidValue(e.to_shared()),
2240        }
2241    }
2242}
2243
2244#[cfg(feature = "parser")]
2245/// # Errors
2246///
2247/// Returns an error if `input` is not a valid CSS `vertical-align` value.
2248pub fn parse_style_vertical_align(
2249    input: &str,
2250) -> Result<StyleVerticalAlign, StyleVerticalAlignParseError<'_>> {
2251    match input.trim() {
2252        "baseline" => Ok(StyleVerticalAlign::Baseline),
2253        "top" => Ok(StyleVerticalAlign::Top),
2254        "middle" => Ok(StyleVerticalAlign::Middle),
2255        "bottom" => Ok(StyleVerticalAlign::Bottom),
2256        "sub" => Ok(StyleVerticalAlign::Sub),
2257        "super" => Ok(StyleVerticalAlign::Superscript),
2258        "text-top" => Ok(StyleVerticalAlign::TextTop),
2259        "text-bottom" => Ok(StyleVerticalAlign::TextBottom),
2260        other if other.ends_with('%') => {
2261            let num_str = other.trim_end_matches('%').trim();
2262            num_str.parse::<f32>().map_or_else(
2263                |_| Err(StyleVerticalAlignParseError::InvalidValue(InvalidValueErr(other))),
2264                |val| Ok(StyleVerticalAlign::Percentage(PercentageValue::new(val))),
2265            )
2266        }
2267        other => crate::props::basic::pixel::parse_pixel_value(other).map_or_else(
2268            |_| Err(StyleVerticalAlignParseError::InvalidValue(InvalidValueErr(other))),
2269            |pv| Ok(StyleVerticalAlign::Length(pv)),
2270        ),
2271    }
2272}
2273
2274// --- CaretColor ---
2275
2276#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2277#[repr(C)]
2278pub struct CaretColor {
2279    pub inner: ColorU,
2280}
2281
2282impl Default for CaretColor {
2283    fn default() -> Self {
2284        Self {
2285            inner: ColorU::BLACK,
2286        }
2287    }
2288}
2289
2290impl PrintAsCssValue for CaretColor {
2291    fn print_as_css_value(&self) -> String {
2292        self.inner.to_hash()
2293    }
2294}
2295
2296impl FormatAsRustCode for CaretColor {
2297    fn format_as_rust_code(&self, _tabs: usize) -> String {
2298        format!(
2299            "CaretColor {{ inner: {} }}",
2300            crate::codegen::format::format_color_value(&self.inner)
2301        )
2302    }
2303}
2304
2305#[cfg(feature = "parser")]
2306/// # Errors
2307///
2308/// Returns an error if `input` is not a valid CSS `caret-color` value.
2309pub fn parse_caret_color(input: &str) -> Result<CaretColor, CssColorParseError<'_>> {
2310    parse_css_color(input).map(|inner| CaretColor { inner })
2311}
2312
2313// --- CaretAnimationDuration ---
2314
2315#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2316#[repr(C)]
2317pub struct CaretAnimationDuration {
2318    pub inner: CssDuration,
2319}
2320
2321impl Default for CaretAnimationDuration {
2322    fn default() -> Self {
2323        Self {
2324            inner: CssDuration { inner: 500 },
2325        } // Default 500ms blink time
2326    }
2327}
2328
2329impl PrintAsCssValue for CaretAnimationDuration {
2330    fn print_as_css_value(&self) -> String {
2331        self.inner.print_as_css_value()
2332    }
2333}
2334
2335impl FormatAsRustCode for CaretAnimationDuration {
2336    fn format_as_rust_code(&self, _tabs: usize) -> String {
2337        format!(
2338            "CaretAnimationDuration {{ inner: {} }}",
2339            self.inner.format_as_rust_code(0)
2340        )
2341    }
2342}
2343
2344#[cfg(feature = "parser")]
2345/// # Errors
2346///
2347/// Returns an error if `input` is not a valid CSS `caret-animation-duration` value.
2348pub fn parse_caret_animation_duration(
2349    input: &str,
2350) -> Result<CaretAnimationDuration, DurationParseError<'_>> {
2351    use crate::props::basic::parse_duration;
2352
2353    parse_duration(input).map(|inner| CaretAnimationDuration { inner })
2354}
2355
2356// --- CaretWidth ---
2357
2358/// Width of the text cursor (caret) in pixels.
2359/// CSS doesn't have a standard property for this, so we use `-azul-caret-width`.
2360#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2361#[repr(C)]
2362pub struct CaretWidth {
2363    pub inner: PixelValue,
2364}
2365
2366impl Default for CaretWidth {
2367    fn default() -> Self {
2368        Self {
2369            inner: PixelValue::px(2.0), // Default 2px caret width
2370        }
2371    }
2372}
2373
2374impl PrintAsCssValue for CaretWidth {
2375    fn print_as_css_value(&self) -> String {
2376        self.inner.print_as_css_value()
2377    }
2378}
2379
2380impl FormatAsRustCode for CaretWidth {
2381    fn format_as_rust_code(&self, _tabs: usize) -> String {
2382        format!(
2383            "CaretWidth {{ inner: {} }}",
2384            self.inner.format_as_rust_code(0)
2385        )
2386    }
2387}
2388
2389#[cfg(feature = "parser")]
2390/// # Errors
2391///
2392/// Returns an error if `input` is not a valid CSS `caret-width` value.
2393pub fn parse_caret_width(input: &str) -> Result<CaretWidth, CssPixelValueParseError<'_>> {
2394    use crate::props::basic::pixel::parse_pixel_value;
2395
2396    parse_pixel_value(input).map(|inner| CaretWidth { inner })
2397}
2398
2399// --- From implementations for CssProperty ---
2400
2401impl From<StyleUserSelect> for crate::props::property::CssProperty {
2402    fn from(value: StyleUserSelect) -> Self {
2403        use crate::props::property::CssProperty;
2404        Self::user_select(value)
2405    }
2406}
2407
2408impl From<StyleTextDecoration> for crate::props::property::CssProperty {
2409    fn from(value: StyleTextDecoration) -> Self {
2410        use crate::props::property::CssProperty;
2411        Self::text_decoration(value)
2412    }
2413}
2414
2415#[cfg(all(test, feature = "parser"))]
2416mod tests {
2417    use super::*;
2418    use crate::props::basic::{color::ColorU, length::PercentageValue, pixel::PixelValue};
2419
2420    #[test]
2421    fn test_parse_style_text_color() {
2422        assert_eq!(
2423            parse_style_text_color("red").unwrap().inner,
2424            ColorU::new_rgb(255, 0, 0)
2425        );
2426        assert_eq!(
2427            parse_style_text_color("#aabbcc").unwrap().inner,
2428            ColorU::new_rgb(170, 187, 204)
2429        );
2430        assert!(parse_style_text_color("not-a-color").is_err());
2431    }
2432
2433    #[test]
2434    fn test_parse_style_text_align() {
2435        assert_eq!(
2436            parse_style_text_align("left").unwrap(),
2437            StyleTextAlign::Left
2438        );
2439        assert_eq!(
2440            parse_style_text_align("center").unwrap(),
2441            StyleTextAlign::Center
2442        );
2443        assert_eq!(
2444            parse_style_text_align("right").unwrap(),
2445            StyleTextAlign::Right
2446        );
2447        assert_eq!(
2448            parse_style_text_align("justify").unwrap(),
2449            StyleTextAlign::Justify
2450        );
2451        assert_eq!(
2452            parse_style_text_align("start").unwrap(),
2453            StyleTextAlign::Start
2454        );
2455        assert_eq!(parse_style_text_align("end").unwrap(), StyleTextAlign::End);
2456        assert!(parse_style_text_align("middle").is_err());
2457    }
2458
2459    #[test]
2460    fn test_parse_spacing() {
2461        assert_eq!(
2462            parse_style_letter_spacing("2px").unwrap().inner,
2463            PixelValue::px(2.0)
2464        );
2465        assert_eq!(
2466            parse_style_letter_spacing("-0.1em").unwrap().inner,
2467            PixelValue::em(-0.1)
2468        );
2469        assert_eq!(
2470            parse_style_word_spacing("0.5em").unwrap().inner,
2471            PixelValue::em(0.5)
2472        );
2473    }
2474
2475    #[test]
2476    fn test_parse_line_height() {
2477        assert_eq!(
2478            parse_style_line_height("1.5").unwrap().inner,
2479            PercentageValue::new(150.0)
2480        );
2481        assert_eq!(
2482            parse_style_line_height("120%").unwrap().inner,
2483            PercentageValue::new(120.0)
2484        );
2485        // px values stored as negative PercentageValue (convention: negative = absolute px)
2486        assert_eq!(
2487            parse_style_line_height("20px").unwrap().inner,
2488            PercentageValue::new(-20.0 * 100.0)
2489        );
2490    }
2491
2492    #[test]
2493    fn test_parse_tab_size() {
2494        // Unitless number is treated as `em`
2495        assert_eq!(
2496            parse_style_tab_size("4").unwrap().inner,
2497            PixelValue::em(4.0)
2498        );
2499        assert_eq!(
2500            parse_style_tab_size("20px").unwrap().inner,
2501            PixelValue::px(20.0)
2502        );
2503    }
2504
2505    #[test]
2506    fn test_parse_white_space() {
2507        assert_eq!(
2508            parse_style_white_space("normal").unwrap(),
2509            StyleWhiteSpace::Normal
2510        );
2511        assert_eq!(
2512            parse_style_white_space("pre").unwrap(),
2513            StyleWhiteSpace::Pre
2514        );
2515        assert_eq!(
2516            parse_style_white_space("nowrap").unwrap(),
2517            StyleWhiteSpace::Nowrap
2518        );
2519        assert_eq!(
2520            parse_style_white_space("pre-wrap").unwrap(),
2521            StyleWhiteSpace::PreWrap
2522        );
2523    }
2524}
2525
2526// -- StyleUnicodeBidi --
2527
2528/// Represents the `unicode-bidi` CSS property.
2529///
2530/// Controls how bidirectional text is handled within an element.
2531#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2532#[repr(C)]
2533#[derive(Default)]
2534pub enum StyleUnicodeBidi {
2535    /// No additional level of embedding
2536    #[default]
2537    Normal,
2538    /// Open an additional level of embedding
2539    Embed,
2540    /// Isolate the element from surrounding bidirectional text
2541    Isolate,
2542    /// Override the bidirectional algorithm for inline content
2543    BidiOverride,
2544    /// Combine isolation and override
2545    IsolateOverride,
2546    /// Determine paragraph direction from content without bidi algorithm
2547    Plaintext,
2548}
2549impl_option!(
2550    StyleUnicodeBidi,
2551    OptionStyleUnicodeBidi,
2552    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
2553);
2554impl PrintAsCssValue for StyleUnicodeBidi {
2555    fn print_as_css_value(&self) -> String {
2556        String::from(match self {
2557            Self::Normal => "normal",
2558            Self::Embed => "embed",
2559            Self::Isolate => "isolate",
2560            Self::BidiOverride => "bidi-override",
2561            Self::IsolateOverride => "isolate-override",
2562            Self::Plaintext => "plaintext",
2563        })
2564    }
2565}
2566
2567#[cfg(feature = "parser")]
2568#[derive(Clone, PartialEq, Eq)]
2569pub enum StyleUnicodeBidiParseError<'a> {
2570    InvalidValue(InvalidValueErr<'a>),
2571}
2572#[cfg(feature = "parser")]
2573impl_debug_as_display!(StyleUnicodeBidiParseError<'a>);
2574#[cfg(feature = "parser")]
2575impl_display! { StyleUnicodeBidiParseError<'a>, {
2576    InvalidValue(e) => format!("Invalid unicode-bidi value: \"{}\"", e.0),
2577}}
2578#[cfg(feature = "parser")]
2579impl_from!(InvalidValueErr<'a>, StyleUnicodeBidiParseError::InvalidValue);
2580
2581#[cfg(feature = "parser")]
2582#[derive(Debug, Clone, PartialEq, Eq)]
2583#[repr(C, u8)]
2584pub enum StyleUnicodeBidiParseErrorOwned {
2585    InvalidValue(InvalidValueErrOwned),
2586}
2587
2588#[cfg(feature = "parser")]
2589impl StyleUnicodeBidiParseError<'_> {
2590    #[must_use] pub fn to_contained(&self) -> StyleUnicodeBidiParseErrorOwned {
2591        match self {
2592            Self::InvalidValue(e) => StyleUnicodeBidiParseErrorOwned::InvalidValue(e.to_contained()),
2593        }
2594    }
2595}
2596
2597#[cfg(feature = "parser")]
2598impl StyleUnicodeBidiParseErrorOwned {
2599    #[must_use] pub fn to_shared(&self) -> StyleUnicodeBidiParseError<'_> {
2600        match self {
2601            Self::InvalidValue(e) => StyleUnicodeBidiParseError::InvalidValue(e.to_shared()),
2602        }
2603    }
2604}
2605
2606#[cfg(feature = "parser")]
2607/// # Errors
2608///
2609/// Returns an error if `input` is not a valid CSS `unicode-bidi` value.
2610pub fn parse_style_unicode_bidi(input: &str) -> Result<StyleUnicodeBidi, StyleUnicodeBidiParseError<'_>> {
2611    match input.trim() {
2612        "normal" => Ok(StyleUnicodeBidi::Normal),
2613        "embed" => Ok(StyleUnicodeBidi::Embed),
2614        "isolate" => Ok(StyleUnicodeBidi::Isolate),
2615        "bidi-override" => Ok(StyleUnicodeBidi::BidiOverride),
2616        "isolate-override" => Ok(StyleUnicodeBidi::IsolateOverride),
2617        "plaintext" => Ok(StyleUnicodeBidi::Plaintext),
2618        other => Err(StyleUnicodeBidiParseError::InvalidValue(InvalidValueErr(other))),
2619    }
2620}
2621
2622// -- StyleTextBoxTrim --
2623
2624/// Represents the `text-box-trim` CSS property.
2625///
2626/// Controls whether the leading is trimmed at the start/end of a block container.
2627#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2628#[repr(C)]
2629#[derive(Default)]
2630pub enum StyleTextBoxTrim {
2631    /// No trimming
2632    #[default]
2633    None,
2634    /// Trim leading over the first formatted line
2635    TrimStart,
2636    /// Trim leading under the last formatted line
2637    TrimEnd,
2638    /// Trim both start and end
2639    TrimBoth,
2640}
2641impl_option!(
2642    StyleTextBoxTrim,
2643    OptionStyleTextBoxTrim,
2644    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
2645);
2646impl PrintAsCssValue for StyleTextBoxTrim {
2647    fn print_as_css_value(&self) -> String {
2648        String::from(match self {
2649            Self::None => "none",
2650            Self::TrimStart => "trim-start",
2651            Self::TrimEnd => "trim-end",
2652            Self::TrimBoth => "trim-both",
2653        })
2654    }
2655}
2656
2657#[cfg(feature = "parser")]
2658#[derive(Clone, PartialEq, Eq)]
2659pub enum StyleTextBoxTrimParseError<'a> {
2660    InvalidValue(InvalidValueErr<'a>),
2661}
2662#[cfg(feature = "parser")]
2663impl_debug_as_display!(StyleTextBoxTrimParseError<'a>);
2664#[cfg(feature = "parser")]
2665impl_display! { StyleTextBoxTrimParseError<'a>, {
2666    InvalidValue(e) => format!("Invalid text-box-trim value: \"{}\"", e.0),
2667}}
2668#[cfg(feature = "parser")]
2669impl_from!(InvalidValueErr<'a>, StyleTextBoxTrimParseError::InvalidValue);
2670
2671#[cfg(feature = "parser")]
2672#[derive(Debug, Clone, PartialEq, Eq)]
2673#[repr(C, u8)]
2674pub enum StyleTextBoxTrimParseErrorOwned {
2675    InvalidValue(InvalidValueErrOwned),
2676}
2677
2678#[cfg(feature = "parser")]
2679impl StyleTextBoxTrimParseError<'_> {
2680    #[must_use] pub fn to_contained(&self) -> StyleTextBoxTrimParseErrorOwned {
2681        match self {
2682            Self::InvalidValue(e) => StyleTextBoxTrimParseErrorOwned::InvalidValue(e.to_contained()),
2683        }
2684    }
2685}
2686
2687#[cfg(feature = "parser")]
2688impl StyleTextBoxTrimParseErrorOwned {
2689    #[must_use] pub fn to_shared(&self) -> StyleTextBoxTrimParseError<'_> {
2690        match self {
2691            Self::InvalidValue(e) => StyleTextBoxTrimParseError::InvalidValue(e.to_shared()),
2692        }
2693    }
2694}
2695
2696#[cfg(feature = "parser")]
2697/// # Errors
2698///
2699/// Returns an error if `input` is not a valid CSS `text-box-trim` value.
2700pub fn parse_style_text_box_trim(input: &str) -> Result<StyleTextBoxTrim, StyleTextBoxTrimParseError<'_>> {
2701    match input.trim() {
2702        "none" => Ok(StyleTextBoxTrim::None),
2703        "trim-start" => Ok(StyleTextBoxTrim::TrimStart),
2704        "trim-end" => Ok(StyleTextBoxTrim::TrimEnd),
2705        "trim-both" => Ok(StyleTextBoxTrim::TrimBoth),
2706        other => Err(StyleTextBoxTrimParseError::InvalidValue(InvalidValueErr(other))),
2707    }
2708}
2709
2710// -- StyleTextBoxEdge --
2711
2712/// Represents the `text-box-edge` CSS property.
2713///
2714/// Specifies the metrics used for determining the over/under edges of text
2715/// for the purposes of `text-box-trim`.
2716// +spec:writing-modes:daad86 - first value = over edge, second = under edge; single value applies to both (else "text" assumed for missing)
2717#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2718#[repr(C)]
2719#[derive(Default)]
2720pub enum StyleTextBoxEdge {
2721    // +spec:line-height:cc03df - Auto uses line-fit-edge value, interpreting leading (initial) as text
2722    /// Use the line-fit-edge value (initial: text)
2723    #[default]
2724    Auto,
2725    /// Use the text-over / text-under baselines
2726    TextEdge,
2727    /// Use the cap-height baseline
2728    CapHeight,
2729    /// Use the x-height baseline
2730    ExHeight,
2731}
2732impl_option!(
2733    StyleTextBoxEdge,
2734    OptionStyleTextBoxEdge,
2735    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
2736);
2737impl PrintAsCssValue for StyleTextBoxEdge {
2738    fn print_as_css_value(&self) -> String {
2739        String::from(match self {
2740            Self::Auto => "auto",
2741            Self::TextEdge => "text",
2742            Self::CapHeight => "cap",
2743            Self::ExHeight => "ex",
2744        })
2745    }
2746}
2747
2748#[cfg(feature = "parser")]
2749#[derive(Clone, PartialEq, Eq)]
2750pub enum StyleTextBoxEdgeParseError<'a> {
2751    InvalidValue(InvalidValueErr<'a>),
2752}
2753#[cfg(feature = "parser")]
2754impl_debug_as_display!(StyleTextBoxEdgeParseError<'a>);
2755#[cfg(feature = "parser")]
2756impl_display! { StyleTextBoxEdgeParseError<'a>, {
2757    InvalidValue(e) => format!("Invalid text-box-edge value: \"{}\"", e.0),
2758}}
2759#[cfg(feature = "parser")]
2760impl_from!(InvalidValueErr<'a>, StyleTextBoxEdgeParseError::InvalidValue);
2761
2762#[cfg(feature = "parser")]
2763#[derive(Debug, Clone, PartialEq, Eq)]
2764#[repr(C, u8)]
2765pub enum StyleTextBoxEdgeParseErrorOwned {
2766    InvalidValue(InvalidValueErrOwned),
2767}
2768
2769#[cfg(feature = "parser")]
2770impl StyleTextBoxEdgeParseError<'_> {
2771    #[must_use] pub fn to_contained(&self) -> StyleTextBoxEdgeParseErrorOwned {
2772        match self {
2773            Self::InvalidValue(e) => StyleTextBoxEdgeParseErrorOwned::InvalidValue(e.to_contained()),
2774        }
2775    }
2776}
2777
2778#[cfg(feature = "parser")]
2779impl StyleTextBoxEdgeParseErrorOwned {
2780    #[must_use] pub fn to_shared(&self) -> StyleTextBoxEdgeParseError<'_> {
2781        match self {
2782            Self::InvalidValue(e) => StyleTextBoxEdgeParseError::InvalidValue(e.to_shared()),
2783        }
2784    }
2785}
2786
2787#[cfg(feature = "parser")]
2788/// # Errors
2789///
2790/// Returns an error if `input` is not a valid CSS `text-box-edge` value.
2791pub fn parse_style_text_box_edge(input: &str) -> Result<StyleTextBoxEdge, StyleTextBoxEdgeParseError<'_>> {
2792    match input.trim() {
2793        "auto" => Ok(StyleTextBoxEdge::Auto),
2794        "text" => Ok(StyleTextBoxEdge::TextEdge),
2795        "cap" => Ok(StyleTextBoxEdge::CapHeight),
2796        "ex" => Ok(StyleTextBoxEdge::ExHeight),
2797        other => Err(StyleTextBoxEdgeParseError::InvalidValue(InvalidValueErr(other))),
2798    }
2799}
2800
2801// -- StyleDominantBaseline --
2802
2803/// Represents the `dominant-baseline` CSS property.
2804///
2805/// Specifies the dominant baseline used to align inline-level contents.
2806#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2807#[repr(C)]
2808#[derive(Default)]
2809pub enum StyleDominantBaseline {
2810    /// Use the dominant baseline of the parent
2811    #[default]
2812    Auto,
2813    /// Use the text-under baseline
2814    TextBottom,
2815    /// Use the alphabetic baseline
2816    Alphabetic,
2817    /// Use the ideographic baseline
2818    Ideographic,
2819    /// Use the middle baseline
2820    Middle,
2821    /// Use the central baseline
2822    Central,
2823    /// Use the mathematical baseline
2824    Mathematical,
2825    /// Use the hanging baseline
2826    Hanging,
2827    /// Use the text-over baseline
2828    TextTop,
2829}
2830impl_option!(
2831    StyleDominantBaseline,
2832    OptionStyleDominantBaseline,
2833    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
2834);
2835impl PrintAsCssValue for StyleDominantBaseline {
2836    fn print_as_css_value(&self) -> String {
2837        String::from(match self {
2838            Self::Auto => "auto",
2839            Self::TextBottom => "text-bottom",
2840            Self::Alphabetic => "alphabetic",
2841            Self::Ideographic => "ideographic",
2842            Self::Middle => "middle",
2843            Self::Central => "central",
2844            Self::Mathematical => "mathematical",
2845            Self::Hanging => "hanging",
2846            Self::TextTop => "text-top",
2847        })
2848    }
2849}
2850
2851#[cfg(feature = "parser")]
2852#[derive(Clone, PartialEq, Eq)]
2853pub enum StyleDominantBaselineParseError<'a> {
2854    InvalidValue(InvalidValueErr<'a>),
2855}
2856#[cfg(feature = "parser")]
2857impl_debug_as_display!(StyleDominantBaselineParseError<'a>);
2858#[cfg(feature = "parser")]
2859impl_display! { StyleDominantBaselineParseError<'a>, {
2860    InvalidValue(e) => format!("Invalid dominant-baseline value: \"{}\"", e.0),
2861}}
2862#[cfg(feature = "parser")]
2863impl_from!(InvalidValueErr<'a>, StyleDominantBaselineParseError::InvalidValue);
2864
2865#[cfg(feature = "parser")]
2866#[derive(Debug, Clone, PartialEq, Eq)]
2867#[repr(C, u8)]
2868pub enum StyleDominantBaselineParseErrorOwned {
2869    InvalidValue(InvalidValueErrOwned),
2870}
2871
2872#[cfg(feature = "parser")]
2873impl StyleDominantBaselineParseError<'_> {
2874    #[must_use] pub fn to_contained(&self) -> StyleDominantBaselineParseErrorOwned {
2875        match self {
2876            Self::InvalidValue(e) => StyleDominantBaselineParseErrorOwned::InvalidValue(e.to_contained()),
2877        }
2878    }
2879}
2880
2881#[cfg(feature = "parser")]
2882impl StyleDominantBaselineParseErrorOwned {
2883    #[must_use] pub fn to_shared(&self) -> StyleDominantBaselineParseError<'_> {
2884        match self {
2885            Self::InvalidValue(e) => StyleDominantBaselineParseError::InvalidValue(e.to_shared()),
2886        }
2887    }
2888}
2889
2890#[cfg(feature = "parser")]
2891/// # Errors
2892///
2893/// Returns an error if `input` is not a valid CSS `dominant-baseline` value.
2894pub fn parse_style_dominant_baseline(input: &str) -> Result<StyleDominantBaseline, StyleDominantBaselineParseError<'_>> {
2895    match input.trim() {
2896        "auto" => Ok(StyleDominantBaseline::Auto),
2897        "text-bottom" => Ok(StyleDominantBaseline::TextBottom),
2898        "alphabetic" => Ok(StyleDominantBaseline::Alphabetic),
2899        "ideographic" => Ok(StyleDominantBaseline::Ideographic),
2900        "middle" => Ok(StyleDominantBaseline::Middle),
2901        "central" => Ok(StyleDominantBaseline::Central),
2902        "mathematical" => Ok(StyleDominantBaseline::Mathematical),
2903        "hanging" => Ok(StyleDominantBaseline::Hanging),
2904        "text-top" => Ok(StyleDominantBaseline::TextTop),
2905        other => Err(StyleDominantBaselineParseError::InvalidValue(InvalidValueErr(other))),
2906    }
2907}
2908
2909// -- StyleAlignmentBaseline --
2910
2911// +spec:display-property:c90924 - alignment-baseline property: values, initial value, and applies-to per CSS Inline 3 §4.2.2
2912// +spec:font-metrics:fa4489 - alignment-baseline property: specifies box's alignment baseline used before post-alignment shift
2913// +spec:inline-block:939f05 - alignment-baseline property definition with all spec values (baseline, text-bottom, alphabetic, ideographic, middle, central, mathematical, text-top)
2914/// Represents the `alignment-baseline` CSS property.
2915///
2916/// Specifies which baseline of the element is aligned with the dominant baseline.
2917// +spec:writing-modes:cc8e70 - alignment-baseline values for inline baseline alignment
2918#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2919#[repr(C)]
2920#[derive(Default)]
2921pub enum StyleAlignmentBaseline {
2922    /// Use the dominant baseline of the parent
2923    #[default]
2924    Baseline,
2925    /// Align to the text-under baseline
2926    TextBottom,
2927    /// Align to the alphabetic baseline
2928    Alphabetic,
2929    /// Align to the ideographic baseline
2930    Ideographic,
2931    /// Align to the middle baseline
2932    Middle,
2933    /// Align to the central baseline
2934    Central,
2935    /// Align to the mathematical baseline
2936    Mathematical,
2937    /// Align to the text-over baseline
2938    TextTop,
2939}
2940impl_option!(
2941    StyleAlignmentBaseline,
2942    OptionStyleAlignmentBaseline,
2943    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
2944);
2945impl PrintAsCssValue for StyleAlignmentBaseline {
2946    fn print_as_css_value(&self) -> String {
2947        String::from(match self {
2948            Self::Baseline => "baseline",
2949            Self::TextBottom => "text-bottom",
2950            Self::Alphabetic => "alphabetic",
2951            Self::Ideographic => "ideographic",
2952            Self::Middle => "middle",
2953            Self::Central => "central",
2954            Self::Mathematical => "mathematical",
2955            Self::TextTop => "text-top",
2956        })
2957    }
2958}
2959
2960#[cfg(feature = "parser")]
2961#[derive(Clone, PartialEq, Eq)]
2962pub enum StyleAlignmentBaselineParseError<'a> {
2963    InvalidValue(InvalidValueErr<'a>),
2964}
2965#[cfg(feature = "parser")]
2966impl_debug_as_display!(StyleAlignmentBaselineParseError<'a>);
2967#[cfg(feature = "parser")]
2968impl_display! { StyleAlignmentBaselineParseError<'a>, {
2969    InvalidValue(e) => format!("Invalid alignment-baseline value: \"{}\"", e.0),
2970}}
2971#[cfg(feature = "parser")]
2972impl_from!(InvalidValueErr<'a>, StyleAlignmentBaselineParseError::InvalidValue);
2973
2974#[cfg(feature = "parser")]
2975#[derive(Debug, Clone, PartialEq, Eq)]
2976#[repr(C, u8)]
2977pub enum StyleAlignmentBaselineParseErrorOwned {
2978    InvalidValue(InvalidValueErrOwned),
2979}
2980
2981#[cfg(feature = "parser")]
2982impl StyleAlignmentBaselineParseError<'_> {
2983    #[must_use] pub fn to_contained(&self) -> StyleAlignmentBaselineParseErrorOwned {
2984        match self {
2985            Self::InvalidValue(e) => StyleAlignmentBaselineParseErrorOwned::InvalidValue(e.to_contained()),
2986        }
2987    }
2988}
2989
2990#[cfg(feature = "parser")]
2991impl StyleAlignmentBaselineParseErrorOwned {
2992    #[must_use] pub fn to_shared(&self) -> StyleAlignmentBaselineParseError<'_> {
2993        match self {
2994            Self::InvalidValue(e) => StyleAlignmentBaselineParseError::InvalidValue(e.to_shared()),
2995        }
2996    }
2997}
2998
2999#[cfg(feature = "parser")]
3000/// # Errors
3001///
3002/// Returns an error if `input` is not a valid CSS `alignment-baseline` value.
3003pub fn parse_style_alignment_baseline(input: &str) -> Result<StyleAlignmentBaseline, StyleAlignmentBaselineParseError<'_>> {
3004    match input.trim() {
3005        "baseline" => Ok(StyleAlignmentBaseline::Baseline),
3006        "text-bottom" => Ok(StyleAlignmentBaseline::TextBottom),
3007        "alphabetic" => Ok(StyleAlignmentBaseline::Alphabetic),
3008        "ideographic" => Ok(StyleAlignmentBaseline::Ideographic),
3009        "middle" => Ok(StyleAlignmentBaseline::Middle),
3010        "central" => Ok(StyleAlignmentBaseline::Central),
3011        "mathematical" => Ok(StyleAlignmentBaseline::Mathematical),
3012        "text-top" => Ok(StyleAlignmentBaseline::TextTop),
3013        other => Err(StyleAlignmentBaselineParseError::InvalidValue(InvalidValueErr(other))),
3014    }
3015}
3016
3017// -- StyleBaselineSource --
3018
3019// +spec:inline-block:939f05 - baseline-source longhand: auto | first | last (auto = last baseline for inline-block / IFC roots, first baseline otherwise)
3020/// Represents the `baseline-source` CSS property.
3021///
3022/// Selects which of the box's baselines is used as its baseline in the parent's
3023/// baseline alignment (CSS Inline Layout Module Level 3 §5.2).
3024#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3025#[repr(C)]
3026#[derive(Default)]
3027pub enum StyleBaselineSource {
3028    /// `auto`: last baseline for inline-block / IFC roots, first baseline otherwise.
3029    #[default]
3030    Auto,
3031    /// `first`: use the first baseline set.
3032    First,
3033    /// `last`: use the last baseline set.
3034    Last,
3035}
3036impl_option!(
3037    StyleBaselineSource,
3038    OptionStyleBaselineSource,
3039    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
3040);
3041impl PrintAsCssValue for StyleBaselineSource {
3042    fn print_as_css_value(&self) -> String {
3043        String::from(match self {
3044            Self::Auto => "auto",
3045            Self::First => "first",
3046            Self::Last => "last",
3047        })
3048    }
3049}
3050
3051#[cfg(feature = "parser")]
3052#[derive(Clone, PartialEq, Eq)]
3053pub enum StyleBaselineSourceParseError<'a> {
3054    InvalidValue(InvalidValueErr<'a>),
3055}
3056#[cfg(feature = "parser")]
3057impl_debug_as_display!(StyleBaselineSourceParseError<'a>);
3058#[cfg(feature = "parser")]
3059impl_display! { StyleBaselineSourceParseError<'a>, {
3060    InvalidValue(e) => format!("Invalid baseline-source value: \"{}\"", e.0),
3061}}
3062#[cfg(feature = "parser")]
3063impl_from!(InvalidValueErr<'a>, StyleBaselineSourceParseError::InvalidValue);
3064
3065#[cfg(feature = "parser")]
3066#[derive(Debug, Clone, PartialEq, Eq)]
3067#[repr(C, u8)]
3068pub enum StyleBaselineSourceParseErrorOwned {
3069    InvalidValue(InvalidValueErrOwned),
3070}
3071
3072#[cfg(feature = "parser")]
3073impl StyleBaselineSourceParseError<'_> {
3074    #[must_use] pub fn to_contained(&self) -> StyleBaselineSourceParseErrorOwned {
3075        match self {
3076            Self::InvalidValue(e) => StyleBaselineSourceParseErrorOwned::InvalidValue(e.to_contained()),
3077        }
3078    }
3079}
3080
3081#[cfg(feature = "parser")]
3082impl StyleBaselineSourceParseErrorOwned {
3083    #[must_use] pub fn to_shared(&self) -> StyleBaselineSourceParseError<'_> {
3084        match self {
3085            Self::InvalidValue(e) => StyleBaselineSourceParseError::InvalidValue(e.to_shared()),
3086        }
3087    }
3088}
3089
3090#[cfg(feature = "parser")]
3091/// # Errors
3092///
3093/// Returns an error if `input` is not a valid CSS `baseline-source` value.
3094pub fn parse_style_baseline_source(input: &str) -> Result<StyleBaselineSource, StyleBaselineSourceParseError<'_>> {
3095    match input.trim() {
3096        "auto" => Ok(StyleBaselineSource::Auto),
3097        "first" => Ok(StyleBaselineSource::First),
3098        "last" => Ok(StyleBaselineSource::Last),
3099        other => Err(StyleBaselineSourceParseError::InvalidValue(InvalidValueErr(other))),
3100    }
3101}
3102
3103// -- StyleLineFitEdge --
3104
3105// +spec:line-height:cc03df - line-fit-edge selects the over/under metrics that size a line box; initial `leading` uses the line-height leading model
3106// +spec:box-model:0e75c1 - with line-fit-edge:leading (initial), margin/border/padding do not contribute to inline layout bounds
3107/// Represents the `line-fit-edge` CSS property.
3108///
3109/// Selects which font metrics determine the over/under edges used when fitting an
3110/// inline box into its line box (CSS Inline Layout Module Level 3 §5). `Auto` on
3111/// `text-box-edge` defers to this value.
3112#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3113#[repr(C)]
3114#[derive(Default)]
3115pub enum StyleLineFitEdge {
3116    /// `leading` (initial): size the line box using the line-height leading model.
3117    #[default]
3118    Leading,
3119    /// `text`: use the text-over / text-under baselines.
3120    Text,
3121    /// `cap`: use the cap-height baseline for the over edge.
3122    Cap,
3123    /// `ex`: use the x-height baseline for the over edge.
3124    Ex,
3125    /// `ideographic`: use the ideographic-em baseline.
3126    Ideographic,
3127    /// `ideographic-ink`: use the ideographic-ink baseline.
3128    IdeographicInk,
3129    /// `alphabetic`: use the alphabetic baseline for the under edge.
3130    Alphabetic,
3131}
3132impl_option!(
3133    StyleLineFitEdge,
3134    OptionStyleLineFitEdge,
3135    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
3136);
3137impl PrintAsCssValue for StyleLineFitEdge {
3138    fn print_as_css_value(&self) -> String {
3139        String::from(match self {
3140            Self::Leading => "leading",
3141            Self::Text => "text",
3142            Self::Cap => "cap",
3143            Self::Ex => "ex",
3144            Self::Ideographic => "ideographic",
3145            Self::IdeographicInk => "ideographic-ink",
3146            Self::Alphabetic => "alphabetic",
3147        })
3148    }
3149}
3150
3151#[cfg(feature = "parser")]
3152#[derive(Clone, PartialEq, Eq)]
3153pub enum StyleLineFitEdgeParseError<'a> {
3154    InvalidValue(InvalidValueErr<'a>),
3155}
3156#[cfg(feature = "parser")]
3157impl_debug_as_display!(StyleLineFitEdgeParseError<'a>);
3158#[cfg(feature = "parser")]
3159impl_display! { StyleLineFitEdgeParseError<'a>, {
3160    InvalidValue(e) => format!("Invalid line-fit-edge value: \"{}\"", e.0),
3161}}
3162#[cfg(feature = "parser")]
3163impl_from!(InvalidValueErr<'a>, StyleLineFitEdgeParseError::InvalidValue);
3164
3165#[cfg(feature = "parser")]
3166#[derive(Debug, Clone, PartialEq, Eq)]
3167#[repr(C, u8)]
3168pub enum StyleLineFitEdgeParseErrorOwned {
3169    InvalidValue(InvalidValueErrOwned),
3170}
3171
3172#[cfg(feature = "parser")]
3173impl StyleLineFitEdgeParseError<'_> {
3174    #[must_use] pub fn to_contained(&self) -> StyleLineFitEdgeParseErrorOwned {
3175        match self {
3176            Self::InvalidValue(e) => StyleLineFitEdgeParseErrorOwned::InvalidValue(e.to_contained()),
3177        }
3178    }
3179}
3180
3181#[cfg(feature = "parser")]
3182impl StyleLineFitEdgeParseErrorOwned {
3183    #[must_use] pub fn to_shared(&self) -> StyleLineFitEdgeParseError<'_> {
3184        match self {
3185            Self::InvalidValue(e) => StyleLineFitEdgeParseError::InvalidValue(e.to_shared()),
3186        }
3187    }
3188}
3189
3190#[cfg(feature = "parser")]
3191/// # Errors
3192///
3193/// Returns an error if `input` is not a valid CSS `line-fit-edge` value.
3194pub fn parse_style_line_fit_edge(input: &str) -> Result<StyleLineFitEdge, StyleLineFitEdgeParseError<'_>> {
3195    match input.trim() {
3196        "leading" => Ok(StyleLineFitEdge::Leading),
3197        "text" => Ok(StyleLineFitEdge::Text),
3198        "cap" => Ok(StyleLineFitEdge::Cap),
3199        "ex" => Ok(StyleLineFitEdge::Ex),
3200        "ideographic" => Ok(StyleLineFitEdge::Ideographic),
3201        "ideographic-ink" => Ok(StyleLineFitEdge::IdeographicInk),
3202        "alphabetic" => Ok(StyleLineFitEdge::Alphabetic),
3203        other => Err(StyleLineFitEdgeParseError::InvalidValue(InvalidValueErr(other))),
3204    }
3205}
3206
3207// -- StyleInitialLetterAlign --
3208
3209/// Represents the `initial-letter-align` CSS property.
3210///
3211/// Specifies the alignment points used to align an initial letter.
3212#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3213#[repr(C)]
3214#[derive(Default)]
3215pub enum StyleInitialLetterAlign {
3216    /// Automatically determine alignment based on script
3217    #[default]
3218    Auto,
3219    /// Align to the alphabetic baseline
3220    Alphabetic,
3221    /// Align to the hanging baseline
3222    Hanging,
3223    /// Align to the ideographic baseline
3224    Ideographic,
3225}
3226impl_option!(
3227    StyleInitialLetterAlign,
3228    OptionStyleInitialLetterAlign,
3229    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
3230);
3231impl PrintAsCssValue for StyleInitialLetterAlign {
3232    fn print_as_css_value(&self) -> String {
3233        String::from(match self {
3234            Self::Auto => "auto",
3235            Self::Alphabetic => "alphabetic",
3236            Self::Hanging => "hanging",
3237            Self::Ideographic => "ideographic",
3238        })
3239    }
3240}
3241
3242#[cfg(feature = "parser")]
3243#[derive(Clone, PartialEq, Eq)]
3244pub enum StyleInitialLetterAlignParseError<'a> {
3245    InvalidValue(InvalidValueErr<'a>),
3246}
3247#[cfg(feature = "parser")]
3248impl_debug_as_display!(StyleInitialLetterAlignParseError<'a>);
3249#[cfg(feature = "parser")]
3250impl_display! { StyleInitialLetterAlignParseError<'a>, {
3251    InvalidValue(e) => format!("Invalid initial-letter-align value: \"{}\"", e.0),
3252}}
3253#[cfg(feature = "parser")]
3254impl_from!(InvalidValueErr<'a>, StyleInitialLetterAlignParseError::InvalidValue);
3255
3256#[cfg(feature = "parser")]
3257#[derive(Debug, Clone, PartialEq, Eq)]
3258#[repr(C, u8)]
3259pub enum StyleInitialLetterAlignParseErrorOwned {
3260    InvalidValue(InvalidValueErrOwned),
3261}
3262
3263#[cfg(feature = "parser")]
3264impl StyleInitialLetterAlignParseError<'_> {
3265    #[must_use] pub fn to_contained(&self) -> StyleInitialLetterAlignParseErrorOwned {
3266        match self {
3267            Self::InvalidValue(e) => StyleInitialLetterAlignParseErrorOwned::InvalidValue(e.to_contained()),
3268        }
3269    }
3270}
3271
3272#[cfg(feature = "parser")]
3273impl StyleInitialLetterAlignParseErrorOwned {
3274    #[must_use] pub fn to_shared(&self) -> StyleInitialLetterAlignParseError<'_> {
3275        match self {
3276            Self::InvalidValue(e) => StyleInitialLetterAlignParseError::InvalidValue(e.to_shared()),
3277        }
3278    }
3279}
3280
3281#[cfg(feature = "parser")]
3282/// # Errors
3283///
3284/// Returns an error if `input` is not a valid CSS `initial-letter-align` value.
3285pub fn parse_style_initial_letter_align(input: &str) -> Result<StyleInitialLetterAlign, StyleInitialLetterAlignParseError<'_>> {
3286    match input.trim() {
3287        "auto" => Ok(StyleInitialLetterAlign::Auto),
3288        "alphabetic" => Ok(StyleInitialLetterAlign::Alphabetic),
3289        "hanging" => Ok(StyleInitialLetterAlign::Hanging),
3290        "ideographic" => Ok(StyleInitialLetterAlign::Ideographic),
3291        other => Err(StyleInitialLetterAlignParseError::InvalidValue(InvalidValueErr(other))),
3292    }
3293}
3294
3295// -- StyleInitialLetterWrap --
3296
3297/// Represents the `initial-letter-wrap` CSS property.
3298///
3299/// Specifies how text adjacent to an initial letter wraps.
3300#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3301#[repr(C)]
3302#[derive(Default)]
3303pub enum StyleInitialLetterWrap {
3304    /// No special wrapping around the initial letter
3305    #[default]
3306    None,
3307    /// Wrap only the first line adjacent to the initial letter
3308    First,
3309    /// Wrap all lines adjacent to the initial letter
3310    All,
3311    /// Wrap using a grid-based layout
3312    Grid,
3313}
3314impl_option!(
3315    StyleInitialLetterWrap,
3316    OptionStyleInitialLetterWrap,
3317    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
3318);
3319impl PrintAsCssValue for StyleInitialLetterWrap {
3320    fn print_as_css_value(&self) -> String {
3321        String::from(match self {
3322            Self::None => "none",
3323            Self::First => "first",
3324            Self::All => "all",
3325            Self::Grid => "grid",
3326        })
3327    }
3328}
3329
3330#[cfg(feature = "parser")]
3331#[derive(Clone, PartialEq, Eq)]
3332pub enum StyleInitialLetterWrapParseError<'a> {
3333    InvalidValue(InvalidValueErr<'a>),
3334}
3335#[cfg(feature = "parser")]
3336impl_debug_as_display!(StyleInitialLetterWrapParseError<'a>);
3337#[cfg(feature = "parser")]
3338impl_display! { StyleInitialLetterWrapParseError<'a>, {
3339    InvalidValue(e) => format!("Invalid initial-letter-wrap value: \"{}\"", e.0),
3340}}
3341#[cfg(feature = "parser")]
3342impl_from!(InvalidValueErr<'a>, StyleInitialLetterWrapParseError::InvalidValue);
3343
3344#[cfg(feature = "parser")]
3345#[derive(Debug, Clone, PartialEq, Eq)]
3346#[repr(C, u8)]
3347pub enum StyleInitialLetterWrapParseErrorOwned {
3348    InvalidValue(InvalidValueErrOwned),
3349}
3350
3351#[cfg(feature = "parser")]
3352impl StyleInitialLetterWrapParseError<'_> {
3353    #[must_use] pub fn to_contained(&self) -> StyleInitialLetterWrapParseErrorOwned {
3354        match self {
3355            Self::InvalidValue(e) => StyleInitialLetterWrapParseErrorOwned::InvalidValue(e.to_contained()),
3356        }
3357    }
3358}
3359
3360#[cfg(feature = "parser")]
3361impl StyleInitialLetterWrapParseErrorOwned {
3362    #[must_use] pub fn to_shared(&self) -> StyleInitialLetterWrapParseError<'_> {
3363        match self {
3364            Self::InvalidValue(e) => StyleInitialLetterWrapParseError::InvalidValue(e.to_shared()),
3365        }
3366    }
3367}
3368
3369#[cfg(feature = "parser")]
3370/// # Errors
3371///
3372/// Returns an error if `input` is not a valid CSS `initial-letter-wrap` value.
3373pub fn parse_style_initial_letter_wrap(input: &str) -> Result<StyleInitialLetterWrap, StyleInitialLetterWrapParseError<'_>> {
3374    match input.trim() {
3375        "none" => Ok(StyleInitialLetterWrap::None),
3376        "first" => Ok(StyleInitialLetterWrap::First),
3377        "all" => Ok(StyleInitialLetterWrap::All),
3378        "grid" => Ok(StyleInitialLetterWrap::Grid),
3379        other => Err(StyleInitialLetterWrapParseError::InvalidValue(InvalidValueErr(other))),
3380    }
3381}
3382
3383#[cfg(test)]
3384#[allow(clippy::float_cmp, clippy::too_many_lines, clippy::cast_precision_loss)]
3385mod autotest_generated {
3386    use super::*;
3387    use crate::props::basic::length::SizeMetric;
3388
3389    const OPAQUE_BLACK: ColorU = ColorU { r: 0, g: 0, b: 0, a: 255 };
3390    const OPAQUE_WHITE: ColorU = ColorU { r: 255, g: 255, b: 255, a: 255 };
3391    const TRANSPARENT_BLACK: ColorU = ColorU { r: 0, g: 0, b: 0, a: 0 };
3392
3393    /// `FloatValue` encodes an f32 as `isize` scaled by this factor.
3394    const SCALE: isize = 1000;
3395
3396    // =====================================================================
3397    // StyleTextIndent — constructors + numeric edges
3398    // =====================================================================
3399
3400    #[test]
3401    fn text_indent_zero_is_the_neutral_element() {
3402        let z = StyleTextIndent::zero();
3403        assert_eq!(z, StyleTextIndent::default());
3404        assert_eq!(z.inner.metric, SizeMetric::Px);
3405        assert_eq!(z.inner.number.get(), 0.0);
3406        assert!(!z.each_line);
3407        assert!(!z.hanging);
3408        assert_eq!(z.print_as_css_value(), "0px");
3409    }
3410
3411    #[test]
3412    fn text_indent_const_ctors_pin_metric_and_value() {
3413        // (constructed value, expected metric) for 0 / positive / negative.
3414        for v in [0_isize, 1, -1, 42, -42] {
3415            let cases = [
3416                (StyleTextIndent::const_px(v), SizeMetric::Px),
3417                (StyleTextIndent::const_em(v), SizeMetric::Em),
3418                (StyleTextIndent::const_pt(v), SizeMetric::Pt),
3419                (StyleTextIndent::const_percent(v), SizeMetric::Percent),
3420                (StyleTextIndent::const_in(v), SizeMetric::In),
3421                (StyleTextIndent::const_cm(v), SizeMetric::Cm),
3422                (StyleTextIndent::const_mm(v), SizeMetric::Mm),
3423            ];
3424            for (got, metric) in cases {
3425                assert_eq!(got.inner.metric, metric, "metric for {v}");
3426                assert_eq!(got.inner.number.get(), v as f32, "value for {v} {metric:?}");
3427                // The keyword flags are never set by the numeric constructors.
3428                assert!(!got.each_line && !got.hanging);
3429            }
3430        }
3431    }
3432
3433    #[test]
3434    fn text_indent_const_ctors_hold_the_whole_encodable_isize_range() {
3435        // The isize encoding is `value * 1000`, so the representable input range is
3436        // isize::MIN/1000 ..= isize::MAX/1000. Pin the scale, then both ends of it.
3437        assert_eq!(StyleTextIndent::const_px(1).inner.number.number(), SCALE);
3438
3439        let max = isize::MAX / SCALE;
3440        let min = isize::MIN / SCALE;
3441        assert_eq!(StyleTextIndent::const_px(max).inner.number.number(), max * SCALE);
3442        assert_eq!(StyleTextIndent::const_px(min).inner.number.number(), min * SCALE);
3443        assert_eq!(
3444            StyleTextIndent::const_from_metric(SizeMetric::Em, max).inner.number.number(),
3445            max * SCALE
3446        );
3447        // NOTE: one step past those bounds (e.g. `const_px(isize::MAX)`) overflows the
3448        // `value * 1000` multiply and panics in debug. See the report — not asserted here
3449        // because the behaviour differs between debug (panic) and release (wrap).
3450    }
3451
3452    #[test]
3453    fn text_indent_float_ctors_saturate_on_nan_and_infinity() {
3454        // f32 -> isize is a saturating `as` cast: NaN -> 0, +inf -> MAX, -inf -> MIN.
3455        assert_eq!(StyleTextIndent::px(f32::NAN).inner.number.get(), 0.0);
3456        assert_eq!(StyleTextIndent::em(f32::NAN).inner.number.get(), 0.0);
3457
3458        assert_eq!(StyleTextIndent::px(f32::INFINITY).inner.number.number(), isize::MAX);
3459        assert_eq!(StyleTextIndent::px(f32::NEG_INFINITY).inner.number.number(), isize::MIN);
3460        assert_eq!(StyleTextIndent::pt(f32::MAX).inner.number.number(), isize::MAX);
3461        assert_eq!(StyleTextIndent::pt(-f32::MAX).inner.number.number(), isize::MIN);
3462
3463        // Sub-precision magnitudes collapse to zero rather than trapping.
3464        assert_eq!(StyleTextIndent::percent(f32::MIN_POSITIVE).inner.number.number(), 0);
3465        assert_eq!(StyleTextIndent::px(-0.0).inner.number.number(), 0);
3466
3467        // Every saturated result is still a finite, readable f32.
3468        for v in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, f32::MAX, -f32::MAX] {
3469            assert!(StyleTextIndent::px(v).inner.number.get().is_finite());
3470        }
3471    }
3472
3473    #[test]
3474    fn text_indent_from_metric_agrees_with_the_typed_ctors() {
3475        assert_eq!(StyleTextIndent::from_metric(SizeMetric::Px, 1.5), StyleTextIndent::px(1.5));
3476        assert_eq!(StyleTextIndent::from_metric(SizeMetric::Em, -2.5), StyleTextIndent::em(-2.5));
3477        assert_eq!(StyleTextIndent::from_metric(SizeMetric::Pt, 0.0), StyleTextIndent::pt(0.0));
3478        assert_eq!(
3479            StyleTextIndent::from_metric(SizeMetric::Percent, 50.0),
3480            StyleTextIndent::percent(50.0)
3481        );
3482        assert_eq!(StyleTextIndent::const_from_metric(SizeMetric::Cm, 3), StyleTextIndent::const_cm(3));
3483        assert_eq!(StyleTextIndent::const_from_metric(SizeMetric::Mm, -3), StyleTextIndent::const_mm(-3));
3484
3485        // A metric with no typed ctor still round-trips through from_metric.
3486        let vw = StyleTextIndent::from_metric(SizeMetric::Vw, 10.0);
3487        assert_eq!(vw.inner.metric, SizeMetric::Vw);
3488        assert_eq!(vw.inner.number.get(), 10.0);
3489    }
3490
3491    #[test]
3492    fn text_indent_interpolate_endpoints_and_extrapolation() {
3493        let a = StyleTextIndent::px(0.0);
3494        let b = StyleTextIndent::px(100.0);
3495
3496        assert_eq!(a.interpolate(&b, 0.0).inner.number.get(), 0.0);
3497        assert_eq!(a.interpolate(&b, 1.0).inner.number.get(), 100.0);
3498        assert_eq!(a.interpolate(&b, 0.5).inner.number.get(), 50.0);
3499        // t outside [0,1] extrapolates rather than clamping.
3500        assert_eq!(a.interpolate(&b, -1.0).inner.number.get(), -100.0);
3501        assert_eq!(a.interpolate(&b, 2.0).inner.number.get(), 200.0);
3502        // Interpolating a value with itself is the identity for any finite t.
3503        assert_eq!(b.interpolate(&b, 0.25), b);
3504    }
3505
3506    #[test]
3507    fn text_indent_interpolate_with_nonfinite_t_is_defined() {
3508        let a = StyleTextIndent::px(0.0);
3509        let b = StyleTextIndent::px(100.0);
3510
3511        // NaN propagates into the f32 -> isize cast, which saturates NaN to 0.
3512        assert_eq!(a.interpolate(&b, f32::NAN).inner.number.get(), 0.0);
3513        // +/-inf saturate to the isize bounds instead of panicking.
3514        assert_eq!(a.interpolate(&b, f32::INFINITY).inner.number.number(), isize::MAX);
3515        assert_eq!(a.interpolate(&b, f32::NEG_INFINITY).inner.number.number(), isize::MIN);
3516        // 0 * inf is NaN, so interpolating equal endpoints by inf collapses to zero.
3517        assert_eq!(b.interpolate(&b, f32::INFINITY).inner.number.get(), 0.0);
3518
3519        for t in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, f32::MAX] {
3520            assert!(a.interpolate(&b, t).inner.number.get().is_finite());
3521        }
3522    }
3523
3524    #[test]
3525    fn text_indent_interpolate_keeps_self_flags_and_normalizes_mixed_metrics() {
3526        let a = StyleTextIndent { each_line: true, hanging: true, ..StyleTextIndent::px(0.0) };
3527        let b = StyleTextIndent { each_line: false, hanging: false, ..StyleTextIndent::px(10.0) };
3528
3529        // Flags are taken from `self`, never blended.
3530        let mid = a.interpolate(&b, 0.5);
3531        assert!(mid.each_line && mid.hanging);
3532        assert!(!b.interpolate(&a, 0.5).each_line);
3533
3534        // Mismatched metrics are resolved into px.
3535        let mixed = StyleTextIndent::px(10.0).interpolate(&StyleTextIndent::em(2.0), 0.5);
3536        assert_eq!(mixed.inner.metric, SizeMetric::Px);
3537        assert!(mixed.inner.number.get().is_finite());
3538    }
3539
3540    // =====================================================================
3541    // StyleTextColor::interpolate
3542    // =====================================================================
3543
3544    #[test]
3545    fn text_color_interpolate_endpoints_are_exact() {
3546        let a = StyleTextColor { inner: OPAQUE_BLACK };
3547        let b = StyleTextColor { inner: OPAQUE_WHITE };
3548
3549        assert_eq!(a.interpolate(&b, 0.0), a);
3550        assert_eq!(a.interpolate(&b, 1.0), b);
3551        // 0 + 255*0.5 = 127.5, rounded half-away-from-zero.
3552        assert_eq!(a.interpolate(&b, 0.5).inner, ColorU { r: 128, g: 128, b: 128, a: 255 });
3553        assert_eq!(a.print_as_css_value(), "#000000ff");
3554    }
3555
3556    #[test]
3557    fn text_color_interpolate_saturates_out_of_range_t() {
3558        let a = StyleTextColor { inner: OPAQUE_BLACK };
3559        let b = StyleTextColor { inner: OPAQUE_WHITE };
3560
3561        // 0 + 255*2 = 510 -> clamped to 255 by the saturating u8 cast (no wrap to 254).
3562        assert_eq!(a.interpolate(&b, 2.0).inner, OPAQUE_WHITE);
3563        // 0 + 255*-1 = -255 -> clamped to 0 (no wrap to 1).
3564        assert_eq!(a.interpolate(&b, -1.0).inner, OPAQUE_BLACK);
3565    }
3566
3567    #[test]
3568    fn text_color_interpolate_with_nonfinite_t_is_defined() {
3569        let a = StyleTextColor { inner: OPAQUE_BLACK };
3570        let b = StyleTextColor { inner: OPAQUE_WHITE };
3571
3572        // NaN saturates to 0 in every channel — including alpha, so the result is
3573        // transparent black rather than a panic or garbage.
3574        assert_eq!(a.interpolate(&b, f32::NAN).inner, TRANSPARENT_BLACK);
3575
3576        // With t = +inf the changing channels saturate to 255, but alpha is *equal* in
3577        // both endpoints, so it computes 255 + (0 * inf) = NaN and saturates to 0.
3578        // A fully-opaque pair therefore interpolates to a fully-transparent colour.
3579        assert_eq!(
3580            a.interpolate(&b, f32::INFINITY).inner,
3581            ColorU { r: 255, g: 255, b: 255, a: 0 }
3582        );
3583        assert_eq!(
3584            a.interpolate(&b, f32::NEG_INFINITY).inner,
3585            ColorU { r: 0, g: 0, b: 0, a: 0 }
3586        );
3587    }
3588
3589    // =====================================================================
3590    // StyleHangingPunctuation::is_enabled (predicate invariants)
3591    // =====================================================================
3592
3593    #[test]
3594    fn hanging_punctuation_is_enabled_matches_its_flags_exhaustively() {
3595        assert!(!StyleHangingPunctuation::default().is_enabled());
3596
3597        for bits in 0_u8..16 {
3598            let hp = StyleHangingPunctuation {
3599                first: bits & 1 != 0,
3600                force_end: bits & 2 != 0,
3601                allow_end: bits & 4 != 0,
3602                last: bits & 8 != 0,
3603            };
3604            assert_eq!(hp.is_enabled(), bits != 0, "bits={bits}");
3605            // is_enabled() is exactly the "prints as something other than none" predicate.
3606            assert_eq!(hp.print_as_css_value() == "none", !hp.is_enabled(), "bits={bits}");
3607        }
3608    }
3609
3610    #[test]
3611    fn hanging_punctuation_prints_flags_in_spec_order() {
3612        let all = StyleHangingPunctuation { first: true, force_end: true, allow_end: true, last: true };
3613        assert_eq!(all.print_as_css_value(), "first force-end allow-end last");
3614        assert_eq!(
3615            StyleHangingPunctuation { last: true, ..Default::default() }.print_as_css_value(),
3616            "last"
3617        );
3618    }
3619
3620    // =====================================================================
3621    // Parser-gated tests
3622    // =====================================================================
3623
3624    #[cfg(feature = "parser")]
3625    mod parser {
3626        use super::super::*;
3627        use crate::props::basic::length::SizeMetric;
3628
3629        const GARBAGE: &[&str] = &[
3630            "",
3631            "   ",
3632            "\t\n",
3633            "!!!",
3634            "\0\0",
3635            "0",
3636            "-0",
3637            "9223372036854775807",
3638            "1e400",
3639            "NaN",
3640            "inf",
3641            "\u{1F600}",
3642            "e\u{0301}",
3643            "\u{202E}left",
3644            "left;garbage",
3645            "left garbage",
3646        ];
3647
3648        /// Every keyword parser must reject junk, and accept its own printed form.
3649        macro_rules! assert_keyword_round_trip {
3650            ($parse:ident, $variants:expr) => {{
3651                for v in $variants {
3652                    let printed = v.print_as_css_value();
3653                    assert_eq!($parse(&printed).as_ref(), Ok(&v), "round-trip of {printed:?}");
3654                    // Surrounding whitespace is trimmed, not rejected.
3655                    assert_eq!($parse(&format!("  {printed}  ")).as_ref(), Ok(&v));
3656                }
3657                for g in GARBAGE.iter().copied() {
3658                    assert!($parse(g).is_err(), "{} accepted garbage {g:?}", stringify!($parse));
3659                }
3660            }};
3661        }
3662
3663        #[test]
3664        fn keyword_parsers_round_trip_every_variant_and_reject_garbage() {
3665            type TA = StyleTextAlign;
3666            assert_keyword_round_trip!(
3667                parse_style_text_align,
3668                [TA::Left, TA::Center, TA::Right, TA::Justify, TA::Start, TA::End]
3669            );
3670            type WS = StyleWhiteSpace;
3671            assert_keyword_round_trip!(
3672                parse_style_white_space,
3673                [WS::Normal, WS::Pre, WS::Nowrap, WS::PreWrap, WS::PreLine, WS::BreakSpaces]
3674            );
3675            type H = StyleHyphens;
3676            assert_keyword_round_trip!(parse_style_hyphens, [H::None, H::Manual, H::Auto]);
3677            type LB = StyleLineBreak;
3678            assert_keyword_round_trip!(
3679                parse_style_line_break,
3680                [LB::Auto, LB::Loose, LB::Normal, LB::Strict, LB::Anywhere]
3681            );
3682            type WB = StyleWordBreak;
3683            assert_keyword_round_trip!(
3684                parse_style_word_break,
3685                [WB::Normal, WB::BreakAll, WB::KeepAll, WB::BreakWord]
3686            );
3687            type OW = StyleOverflowWrap;
3688            assert_keyword_round_trip!(
3689                parse_style_overflow_wrap,
3690                [OW::Normal, OW::Anywhere, OW::BreakWord]
3691            );
3692            type Tal = StyleTextAlignLast;
3693            assert_keyword_round_trip!(
3694                parse_style_text_align_last,
3695                [Tal::Auto, Tal::Start, Tal::End, Tal::Left, Tal::Right, Tal::Center, Tal::Justify]
3696            );
3697            type TT = StyleTextTransform;
3698            assert_keyword_round_trip!(
3699                parse_style_text_transform,
3700                [TT::None, TT::Capitalize, TT::Uppercase, TT::Lowercase, TT::FullWidth]
3701            );
3702            type D = StyleDirection;
3703            assert_keyword_round_trip!(parse_style_direction, [D::Ltr, D::Rtl]);
3704            type US = StyleUserSelect;
3705            assert_keyword_round_trip!(
3706                parse_style_user_select,
3707                [US::Auto, US::Text, US::None, US::All]
3708            );
3709            type TD = StyleTextDecoration;
3710            assert_keyword_round_trip!(
3711                parse_style_text_decoration,
3712                [TD::None, TD::Underline, TD::Overline, TD::LineThrough]
3713            );
3714            type UB = StyleUnicodeBidi;
3715            assert_keyword_round_trip!(
3716                parse_style_unicode_bidi,
3717                [UB::Normal, UB::Embed, UB::Isolate, UB::BidiOverride, UB::IsolateOverride, UB::Plaintext]
3718            );
3719            type Tbt = StyleTextBoxTrim;
3720            assert_keyword_round_trip!(
3721                parse_style_text_box_trim,
3722                [Tbt::None, Tbt::TrimStart, Tbt::TrimEnd, Tbt::TrimBoth]
3723            );
3724            type Tbe = StyleTextBoxEdge;
3725            assert_keyword_round_trip!(
3726                parse_style_text_box_edge,
3727                [Tbe::Auto, Tbe::TextEdge, Tbe::CapHeight, Tbe::ExHeight]
3728            );
3729            type DB = StyleDominantBaseline;
3730            assert_keyword_round_trip!(
3731                parse_style_dominant_baseline,
3732                [
3733                    DB::Auto, DB::TextBottom, DB::Alphabetic, DB::Ideographic, DB::Middle,
3734                    DB::Central, DB::Mathematical, DB::Hanging, DB::TextTop
3735                ]
3736            );
3737            type AB = StyleAlignmentBaseline;
3738            assert_keyword_round_trip!(
3739                parse_style_alignment_baseline,
3740                [
3741                    AB::Baseline, AB::TextBottom, AB::Alphabetic, AB::Ideographic, AB::Middle,
3742                    AB::Central, AB::Mathematical, AB::TextTop
3743                ]
3744            );
3745            type Bs = StyleBaselineSource;
3746            assert_keyword_round_trip!(
3747                parse_style_baseline_source,
3748                [Bs::Auto, Bs::First, Bs::Last]
3749            );
3750            type Lfe = StyleLineFitEdge;
3751            assert_keyword_round_trip!(
3752                parse_style_line_fit_edge,
3753                [
3754                    Lfe::Leading, Lfe::Text, Lfe::Cap, Lfe::Ex, Lfe::Ideographic,
3755                    Lfe::IdeographicInk, Lfe::Alphabetic
3756                ]
3757            );
3758            type Ila = StyleInitialLetterAlign;
3759            assert_keyword_round_trip!(
3760                parse_style_initial_letter_align,
3761                [Ila::Auto, Ila::Alphabetic, Ila::Hanging, Ila::Ideographic]
3762            );
3763            type Ilw = StyleInitialLetterWrap;
3764            assert_keyword_round_trip!(
3765                parse_style_initial_letter_wrap,
3766                [Ilw::None, Ilw::First, Ilw::All, Ilw::Grid]
3767            );
3768        }
3769
3770        #[test]
3771        fn keyword_parsers_are_case_sensitive() {
3772            // BUG: CSS keywords are ASCII case-insensitive (CSS Syntax 3 §3.1), but every
3773            // `match input.trim()` parser in this file compares exactly, so `text-align: LEFT`
3774            // is rejected. `hanging-punctuation` / `text-combine-upright` *do* fold case, so
3775            // the file is internally inconsistent too. Pinned as-is; see the report.
3776            assert!(parse_style_text_align("LEFT").is_err());
3777            assert!(parse_style_text_align("Left").is_err());
3778            assert!(parse_style_white_space("Normal").is_err());
3779            assert!(parse_style_direction("LTR").is_err());
3780            // ...whereas these two fold case as the spec requires:
3781            assert!(parse_style_hanging_punctuation("FIRST").is_ok());
3782            assert!(parse_style_text_combine_upright("NONE").is_ok());
3783        }
3784
3785        #[test]
3786        fn extremely_long_and_deeply_nested_input_terminates_with_err() {
3787            let long = "a".repeat(1_000_000);
3788            assert!(parse_style_text_align(&long).is_err());
3789            assert!(parse_style_white_space(&long).is_err());
3790            assert!(parse_style_text_color(&long).is_err());
3791            assert!(parse_style_letter_spacing(&long).is_err());
3792            assert!(parse_style_word_spacing(&long).is_err());
3793            assert!(parse_style_tab_size(&long).is_err());
3794            assert!(parse_style_line_height(&long).is_err());
3795            assert!(parse_style_text_indent(&long).is_err());
3796            assert!(parse_style_hanging_punctuation(&long).is_err());
3797            assert!(parse_style_initial_letter(&long).is_err());
3798            assert!(parse_style_line_clamp(&long).is_err());
3799            assert!(parse_style_vertical_align(&long).is_err());
3800
3801            // A 1000-digit integer overflows every numeric target -> Err, never a wrap.
3802            let huge_number = "9".repeat(1000);
3803            assert!(parse_style_line_clamp(&huge_number).is_err());
3804            assert!(parse_style_initial_letter(&huge_number).is_err());
3805
3806            // No parser here recurses, so nesting cannot blow the stack.
3807            let nested = format!("{}{}", "(".repeat(10_000), ")".repeat(10_000));
3808            assert!(parse_style_text_align(&nested).is_err());
3809            assert!(parse_style_letter_spacing(&nested).is_err());
3810            assert!(parse_style_text_indent(&nested).is_err());
3811            assert!(parse_style_line_height(&nested).is_err());
3812        }
3813
3814        // -- pixel-valued properties -------------------------------------------
3815
3816        #[test]
3817        fn spacing_and_tab_size_round_trip_through_their_printed_form() {
3818            for pv in [
3819                PixelValue::px(2.0),
3820                PixelValue::px(-3.0),
3821                PixelValue::em(0.5),
3822                PixelValue::pt(12.0),
3823                PixelValue::percent(10.0),
3824                PixelValue::zero(),
3825            ] {
3826                let ls = StyleLetterSpacing { inner: pv };
3827                assert_eq!(parse_style_letter_spacing(&ls.print_as_css_value()).unwrap(), ls);
3828                let ws = StyleWordSpacing { inner: pv };
3829                assert_eq!(parse_style_word_spacing(&ws.print_as_css_value()).unwrap(), ws);
3830            }
3831            // tab-size: unitless numbers mean `em`, lengths keep their unit.
3832            let ts = StyleTabSize::default();
3833            assert_eq!(ts.inner, PixelValue::em(8.0));
3834            assert_eq!(parse_style_tab_size(&ts.print_as_css_value()).unwrap(), ts);
3835            assert_eq!(parse_style_tab_size("4").unwrap().inner, PixelValue::em(4.0));
3836            assert_eq!(parse_style_tab_size("20px").unwrap().inner, PixelValue::px(20.0));
3837        }
3838
3839        #[test]
3840        fn pixel_parsers_reject_empty_and_unit_only_input() {
3841            for bad in ["", "   ", "\t\n", "px", "em", "abc", "px px", "10pxx", "\u{1F600}"] {
3842                assert!(parse_style_letter_spacing(bad).is_err(), "letter-spacing {bad:?}");
3843                assert!(parse_style_word_spacing(bad).is_err(), "word-spacing {bad:?}");
3844            }
3845            // BUG: the shared pixel parser trims *after* stripping the unit suffix, so a space
3846            // between the number and its unit is accepted even though CSS forbids it.
3847            assert_eq!(parse_style_letter_spacing("10 px").unwrap().inner, PixelValue::px(10.0));
3848        }
3849
3850        #[test]
3851        fn pixel_parsers_accept_float_keywords_and_saturate_instead_of_panicking() {
3852            // BUG: `f32::from_str` accepts "NaN"/"inf"/"1e400", so these are *not* rejected
3853            // as CSS lengths. They cannot panic (the isize cast saturates), but they should
3854            // be Err. Pinned as-is; see the report.
3855            assert_eq!(parse_style_letter_spacing("NaN").unwrap().inner.number.number(), 0);
3856            assert_eq!(
3857                parse_style_letter_spacing("inf").unwrap().inner.number.number(),
3858                isize::MAX
3859            );
3860            assert_eq!(
3861                parse_style_letter_spacing("-inf").unwrap().inner.number.number(),
3862                isize::MIN
3863            );
3864            assert_eq!(
3865                parse_style_word_spacing("1e400px").unwrap().inner.number.number(),
3866                isize::MAX
3867            );
3868            // Same story via the tab-size unitless branch.
3869            assert_eq!(parse_style_tab_size("NaN").unwrap().inner, PixelValue::em(0.0));
3870            assert!(parse_style_tab_size("inf").unwrap().inner.number.get().is_finite());
3871
3872            // Boundary numeric strings that *are* legal still parse to the right value.
3873            assert_eq!(parse_style_letter_spacing("0").unwrap().inner, PixelValue::px(0.0));
3874            assert_eq!(parse_style_letter_spacing("-0").unwrap().inner.number.number(), 0);
3875            assert!(parse_style_letter_spacing("9223372036854775807px").unwrap().inner.number.get().is_finite());
3876        }
3877
3878        // -- text-indent --------------------------------------------------------
3879
3880        #[test]
3881        fn text_indent_round_trips_length_and_keywords() {
3882            for pv in [PixelValue::px(10.0), PixelValue::em(-2.0), PixelValue::percent(50.0)] {
3883                for (each_line, hanging) in [(false, false), (true, false), (false, true), (true, true)] {
3884                    let ti = StyleTextIndent { inner: pv, each_line, hanging };
3885                    let printed = ti.print_as_css_value();
3886                    assert_eq!(parse_style_text_indent(&printed).unwrap(), ti, "{printed:?}");
3887                }
3888            }
3889            assert!(parse_style_text_indent("10px hanging").unwrap().hanging);
3890            assert!(parse_style_text_indent("each-line 10px").unwrap().each_line);
3891        }
3892
3893        #[test]
3894        fn text_indent_defaults_missing_length_to_zero_and_keeps_the_last_one() {
3895            // BUG: `text-indent` requires a <length-percentage>; these should all be Err.
3896            // Instead an absent length silently defaults to 0px, so empty/whitespace/keyword-
3897            // only input parses Ok. Pinned as-is; see the report.
3898            assert_eq!(parse_style_text_indent("").unwrap(), StyleTextIndent::zero());
3899            assert_eq!(parse_style_text_indent("   ").unwrap(), StyleTextIndent::zero());
3900            assert_eq!(
3901                parse_style_text_indent("hanging").unwrap(),
3902                StyleTextIndent { hanging: true, ..StyleTextIndent::zero() }
3903            );
3904            // BUG: repeated lengths are not rejected — the last token silently wins.
3905            assert_eq!(parse_style_text_indent("10px 20px").unwrap().inner, PixelValue::px(20.0));
3906
3907            // Junk in the length slot is still rejected.
3908            assert!(parse_style_text_indent("garbage").is_err());
3909            assert!(parse_style_text_indent("hanging garbage").is_err());
3910        }
3911
3912        // -- initial-letter -----------------------------------------------------
3913
3914        #[test]
3915        fn initial_letter_parses_size_and_optional_sink() {
3916            assert_eq!(
3917                parse_style_initial_letter("3").unwrap(),
3918                StyleInitialLetter { size: 3, sink: crate::corety::OptionU32::None }
3919            );
3920            let with_sink = StyleInitialLetter { size: 3, sink: crate::corety::OptionU32::Some(2) };
3921            assert_eq!(parse_style_initial_letter("3 2").unwrap(), with_sink);
3922            // Printed form round-trips.
3923            assert_eq!(parse_style_initial_letter(&with_sink.print_as_css_value()).unwrap(), with_sink);
3924        }
3925
3926        #[test]
3927        fn initial_letter_rejects_zero_negative_and_overflowing_sizes() {
3928            assert!(parse_style_initial_letter("0").is_err(), "size 0 must be rejected");
3929            assert!(parse_style_initial_letter("-1").is_err());
3930            assert!(parse_style_initial_letter("1.5").is_err());
3931            assert!(parse_style_initial_letter("4294967296").is_err(), "u32::MAX + 1");
3932            assert!(parse_style_initial_letter("3 -1").is_err(), "negative sink");
3933            assert!(parse_style_initial_letter("3 x").is_err());
3934            assert!(parse_style_initial_letter("").is_err());
3935            assert!(parse_style_initial_letter("   ").is_err());
3936            // u32::MAX itself is in range.
3937            assert_eq!(parse_style_initial_letter("4294967295").unwrap().size, u32::MAX);
3938            // BUG: a third component should be a parse error, but it is silently dropped.
3939            assert_eq!(parse_style_initial_letter("3 2 9").unwrap(), StyleInitialLetter {
3940                size: 3,
3941                sink: crate::corety::OptionU32::Some(2),
3942            });
3943        }
3944
3945        // -- line-clamp ---------------------------------------------------------
3946
3947        #[test]
3948        fn line_clamp_rejects_zero_and_out_of_range_values() {
3949            assert_eq!(parse_style_line_clamp("3").unwrap(), StyleLineClamp { max_lines: 3 });
3950            assert_eq!(parse_style_line_clamp("  7  ").unwrap().max_lines, 7);
3951            assert_eq!(
3952                parse_style_line_clamp("0").unwrap_err(),
3953                StyleLineClampParseError::ZeroValue
3954            );
3955            assert!(parse_style_line_clamp("-1").is_err());
3956            assert!(parse_style_line_clamp("1.0").is_err());
3957            assert!(parse_style_line_clamp("").is_err());
3958            assert!(parse_style_line_clamp("   ").is_err());
3959            assert!(parse_style_line_clamp("\u{1F600}").is_err());
3960            // Saturating/wrapping never happens: an out-of-range integer is an error.
3961            assert!(parse_style_line_clamp("99999999999999999999999").is_err());
3962            let max = usize::MAX.to_string();
3963            assert_eq!(parse_style_line_clamp(&max).unwrap().max_lines, usize::MAX);
3964            // Printed form round-trips.
3965            let lc = StyleLineClamp { max_lines: 42 };
3966            assert_eq!(parse_style_line_clamp(&lc.print_as_css_value()).unwrap(), lc);
3967        }
3968
3969        // -- hanging-punctuation ------------------------------------------------
3970
3971        #[test]
3972        fn hanging_punctuation_round_trips_and_enforces_mutual_exclusion() {
3973            for bits in 0_u8..16 {
3974                let hp = StyleHangingPunctuation {
3975                    first: bits & 1 != 0,
3976                    force_end: bits & 2 != 0,
3977                    allow_end: bits & 4 != 0,
3978                    last: bits & 8 != 0,
3979                };
3980                let printed = hp.print_as_css_value();
3981                if hp.force_end && hp.allow_end {
3982                    // `force-end` and `allow-end` are mutually exclusive per CSS Text 3 §8.
3983                    assert!(parse_style_hanging_punctuation(&printed).is_err(), "{printed:?}");
3984                } else {
3985                    assert_eq!(parse_style_hanging_punctuation(&printed).unwrap(), hp, "{printed:?}");
3986                }
3987            }
3988            assert!(parse_style_hanging_punctuation("first bogus").is_err());
3989            assert!(parse_style_hanging_punctuation("\u{1F600}").is_err());
3990            assert!(parse_style_hanging_punctuation("none first").is_err());
3991        }
3992
3993        #[test]
3994        fn hanging_punctuation_accepts_empty_input_as_none() {
3995            // BUG: empty / whitespace-only input has no tokens, so the loop body never runs
3996            // and the parser returns Ok(none) instead of Err. Pinned as-is; see the report.
3997            assert_eq!(
3998                parse_style_hanging_punctuation("").unwrap(),
3999                StyleHangingPunctuation::default()
4000            );
4001            assert_eq!(
4002                parse_style_hanging_punctuation("   ").unwrap(),
4003                StyleHangingPunctuation::default()
4004            );
4005            // Duplicate keywords are also accepted (idempotent flag set).
4006            assert!(parse_style_hanging_punctuation("first first").unwrap().first);
4007        }
4008
4009        // -- text-combine-upright -----------------------------------------------
4010
4011        #[test]
4012        fn text_combine_upright_bounds_the_digits_operand() {
4013            assert_eq!(parse_style_text_combine_upright("none").unwrap(), StyleTextCombineUpright::None);
4014            assert_eq!(parse_style_text_combine_upright("all").unwrap(), StyleTextCombineUpright::All);
4015            for n in 2_u8..=4 {
4016                let v = StyleTextCombineUpright::Digits(n);
4017                assert_eq!(parse_style_text_combine_upright(&v.print_as_css_value()).unwrap(), v);
4018            }
4019            // Outside the spec'd 2..=4 range -> Err, not a silent clamp or wrap.
4020            for bad in ["digits 0", "digits 1", "digits 5", "digits 255", "digits 256", "digits -1"] {
4021                assert!(parse_style_text_combine_upright(bad).is_err(), "{bad:?} accepted");
4022            }
4023            assert!(parse_style_text_combine_upright("").is_err());
4024            assert!(parse_style_text_combine_upright("bogus").is_err());
4025        }
4026
4027        #[test]
4028        fn text_combine_upright_accepts_garbage_after_the_digits_prefix() {
4029            // BUG: the `digits` branch is chosen by `starts_with("digits")` with no word
4030            // boundary, and any token count != 2 falls back to `digits 2`. So junk that
4031            // merely starts with "digits" parses Ok. Pinned as-is; see the report.
4032            assert_eq!(
4033                parse_style_text_combine_upright("digits").unwrap(),
4034                StyleTextCombineUpright::Digits(2)
4035            );
4036            assert_eq!(
4037                parse_style_text_combine_upright("digitsgarbage").unwrap(),
4038                StyleTextCombineUpright::Digits(2)
4039            );
4040            assert_eq!(
4041                parse_style_text_combine_upright("digits 2 3").unwrap(),
4042                StyleTextCombineUpright::Digits(2)
4043            );
4044        }
4045
4046        // -- line-height --------------------------------------------------------
4047
4048        #[test]
4049        fn line_height_parses_numbers_percentages_and_px() {
4050            assert_eq!(parse_style_line_height("1.5").unwrap().inner, PercentageValue::new(150.0));
4051            assert_eq!(parse_style_line_height("120%").unwrap().inner, PercentageValue::new(120.0));
4052            // px lengths are encoded as a *negative* percentage (documented convention).
4053            assert_eq!(parse_style_line_height("20px").unwrap().inner, PercentageValue::new(-2000.0));
4054            assert!(parse_style_line_height("").is_err());
4055            assert!(parse_style_line_height("   ").is_err());
4056            assert!(parse_style_line_height("abc").is_err());
4057            assert!(parse_style_line_height("\u{1F600}").is_err());
4058            // Printed form round-trips as a value.
4059            let lh = StyleLineHeight::default();
4060            assert_eq!(parse_style_line_height(&lh.print_as_css_value()).unwrap(), lh);
4061        }
4062
4063        #[test]
4064        fn line_height_negative_numbers_alias_absolute_px_lengths() {
4065            // BUG: negative values are the internal marker for "absolute px", but the number
4066            // branch happily parses a negative <number>, so `line-height: -1` and
4067            // `line-height: 1px` produce the *same* value and are indistinguishable
4068            // downstream. A negative line-height is invalid CSS and should be Err.
4069            assert_eq!(
4070                parse_style_line_height("-1").unwrap(),
4071                parse_style_line_height("1px").unwrap()
4072            );
4073            assert_eq!(
4074                parse_style_line_height("-100%").unwrap(),
4075                parse_style_line_height("1px").unwrap()
4076            );
4077        }
4078
4079        #[test]
4080        fn line_height_rejects_em_and_other_length_units() {
4081            // BUG: `line-height: 1.5em` (and rem/pt/...) is valid CSS but only Px survives
4082            // the length branch, so every other unit is rejected. Pinned as-is.
4083            assert!(parse_style_line_height("1.5em").is_err());
4084            assert!(parse_style_line_height("12pt").is_err());
4085            assert!(parse_style_line_height("2rem").is_err());
4086        }
4087
4088        #[test]
4089        fn line_height_rejects_non_ascii_numerals_without_panicking() {
4090            // `char::is_numeric()` is true for U+FF15 FULLWIDTH DIGIT FIVE, so
4091            // parse_percentage_value sets split_pos = idx + 1 = 1 and then slices
4092            // `input[1..]` — a byte index inside a 3-byte char -> panic.
4093            // Any of these is a CSS-reachable crash:
4094            assert!(parse_style_line_height("\u{FF15}").is_err()); // fullwidth 5
4095            assert!(parse_style_line_height("\u{0665}").is_err()); // arabic-indic 5
4096            assert!(parse_style_line_height("1\u{00B2}").is_err()); // superscript 2
4097        }
4098
4099        // -- vertical-align -----------------------------------------------------
4100
4101        #[test]
4102        fn vertical_align_round_trips_keywords_percentages_and_lengths() {
4103            type VA = StyleVerticalAlign;
4104            for v in [
4105                VA::Baseline, VA::Top, VA::Middle, VA::Bottom, VA::Sub, VA::Superscript,
4106                VA::TextTop, VA::TextBottom,
4107                VA::Percentage(PercentageValue::new(50.0)),
4108                VA::Percentage(PercentageValue::new(-25.0)),
4109                VA::Length(PixelValue::px(12.0)),
4110                VA::Length(PixelValue::em(1.5)),
4111            ] {
4112                let printed = v.print_as_css_value();
4113                assert_eq!(parse_style_vertical_align(&printed).unwrap(), v, "{printed:?}");
4114            }
4115            assert!(parse_style_vertical_align("").is_err());
4116            assert!(parse_style_vertical_align("%").is_err());
4117            assert!(parse_style_vertical_align("bogus%").is_err());
4118            assert!(parse_style_vertical_align("\u{1F600}").is_err());
4119        }
4120
4121        // -- caret-* helpers ----------------------------------------------------
4122
4123        #[test]
4124        fn caret_parsers_reject_garbage_and_accept_minimal_input() {
4125            assert_eq!(parse_caret_color("red").unwrap().inner, parse_style_text_color("red").unwrap().inner);
4126            assert!(parse_caret_color("").is_err());
4127            assert!(parse_caret_color("not-a-color").is_err());
4128            assert_eq!(parse_caret_width("2px").unwrap().inner, PixelValue::px(2.0));
4129            assert!(parse_caret_width("").is_err());
4130            assert!(parse_caret_animation_duration("bogus").is_err());
4131            assert!(parse_caret_animation_duration("500ms").is_ok());
4132        }
4133
4134        // -- error type getters: to_contained / to_shared ------------------------
4135
4136        /// Owned<->shared conversion must be lossless for every error family here.
4137        macro_rules! assert_error_round_trip {
4138            ($parse:ident, $($bad:expr),+ $(,)?) => {{
4139                $(
4140                    let e = $parse($bad).expect_err(concat!(stringify!($parse), " accepted ", $bad));
4141                    assert_eq!(e.to_contained().to_shared(), e, "{:?} via {}", $bad, stringify!($parse));
4142                )+
4143            }};
4144        }
4145
4146        #[test]
4147        fn invalid_value_errors_round_trip_through_their_owned_form() {
4148            assert_error_round_trip!(parse_style_text_align, "", "middle", "\u{1F600}");
4149            assert_error_round_trip!(parse_style_white_space, "", "wrap");
4150            assert_error_round_trip!(parse_style_hyphens, "", "always");
4151            assert_error_round_trip!(parse_style_line_break, "", "tight");
4152            assert_error_round_trip!(parse_style_word_break, "", "break");
4153            assert_error_round_trip!(parse_style_overflow_wrap, "", "wrap");
4154            assert_error_round_trip!(parse_style_text_align_last, "", "middle");
4155            assert_error_round_trip!(parse_style_text_transform, "", "smallcaps");
4156            assert_error_round_trip!(parse_style_direction, "", "sideways");
4157            assert_error_round_trip!(parse_style_user_select, "", "some");
4158            assert_error_round_trip!(parse_style_text_decoration, "", "blink");
4159            assert_error_round_trip!(parse_style_vertical_align, "", "bogus");
4160            assert_error_round_trip!(parse_style_unicode_bidi, "", "override");
4161            assert_error_round_trip!(parse_style_text_box_trim, "", "trim");
4162            assert_error_round_trip!(parse_style_text_box_edge, "", "edge");
4163            assert_error_round_trip!(parse_style_dominant_baseline, "", "bogus");
4164            assert_error_round_trip!(parse_style_alignment_baseline, "", "bogus");
4165            assert_error_round_trip!(parse_style_baseline_source, "", "bogus");
4166            assert_error_round_trip!(parse_style_line_fit_edge, "", "bogus");
4167            assert_error_round_trip!(parse_style_initial_letter_align, "", "bogus");
4168            assert_error_round_trip!(parse_style_initial_letter_wrap, "", "bogus");
4169        }
4170
4171        #[test]
4172        fn pixel_and_numeric_errors_round_trip_through_their_owned_form() {
4173            // EmptyString / ValueParseErr / NoValueGiven / InvalidPixelValue variants.
4174            assert_error_round_trip!(parse_style_letter_spacing, "", "abcpx", "px", "zz");
4175            assert_error_round_trip!(parse_style_word_spacing, "", "abcem", "em", "zz");
4176            assert_error_round_trip!(parse_style_text_indent, "abcpx", "zz");
4177            assert_error_round_trip!(parse_style_tab_size, "", "abcpx", "zz");
4178            assert_error_round_trip!(parse_style_line_height, "", "abc", "1.5em");
4179            assert_error_round_trip!(parse_style_initial_letter, "", "x", "0", "3 x");
4180            assert_error_round_trip!(parse_style_line_clamp, "", "x", "0");
4181            assert_error_round_trip!(parse_style_hanging_punctuation, "bogus", "force-end allow-end");
4182            assert_error_round_trip!(parse_style_text_combine_upright, "bogus", "digits 9");
4183        }
4184
4185        #[test]
4186        fn text_color_error_round_trips_and_preserves_its_message() {
4187            let e = parse_style_text_color("not-a-color").unwrap_err();
4188            let owned = e.to_contained();
4189            let round_tripped = owned.to_shared();
4190            assert_eq!(format!("{e}"), format!("{round_tripped}"));
4191            assert!(parse_style_text_color("").is_err());
4192            assert!(parse_style_text_color("#gggggg").is_err());
4193            assert!(parse_style_text_color("\u{1F600}").is_err());
4194            // Positive control.
4195            assert_eq!(parse_style_text_color("#aabbcc").unwrap().inner.to_hash(), "#aabbccff");
4196        }
4197
4198        #[test]
4199        fn error_types_survive_a_default_ish_extreme_instance() {
4200            // to_contained/to_shared must not panic on empty or huge payloads.
4201            let long = "z".repeat(100_000);
4202            let e = parse_style_text_align(&long).unwrap_err();
4203            assert_eq!(e.to_contained().to_shared(), e);
4204            let e = parse_style_line_clamp(&long).unwrap_err();
4205            assert_eq!(e.to_contained().to_shared(), e);
4206            let e = parse_style_hanging_punctuation(&long).unwrap_err();
4207            assert_eq!(e.to_contained().to_shared(), e);
4208            // Empty payload.
4209            let e = parse_style_letter_spacing("").unwrap_err();
4210            assert_eq!(e.to_contained().to_shared(), e);
4211        }
4212
4213        // -- metric coverage ----------------------------------------------------
4214
4215        #[test]
4216        fn letter_spacing_accepts_every_size_metric_it_prints() {
4217            for (unit, metric) in [
4218                ("px", SizeMetric::Px),
4219                ("pt", SizeMetric::Pt),
4220                ("em", SizeMetric::Em),
4221                ("rem", SizeMetric::Rem),
4222                ("in", SizeMetric::In),
4223                ("cm", SizeMetric::Cm),
4224                ("mm", SizeMetric::Mm),
4225                ("%", SizeMetric::Percent),
4226                ("vw", SizeMetric::Vw),
4227                ("vh", SizeMetric::Vh),
4228                ("vmax", SizeMetric::Vmax),
4229                // FIXED: `vmin` used to be unreachable — the suffix table tried "in"
4230                // before "vmin", so "1vmin" was stripped to "1vm" and failed to parse,
4231                // making every parse_pixel_value-backed property (letter-spacing,
4232                // word-spacing, text-indent, tab-size, vertical-align) reject a valid
4233                // CSS unit. The table now puts "vmin" ahead of "in".
4234                ("vmin", SizeMetric::Vmin),
4235            ] {
4236                let parsed = parse_style_letter_spacing(&format!("1{unit}")).unwrap();
4237                assert_eq!(parsed.inner.metric, metric, "unit {unit}");
4238                assert_eq!(parsed.inner.number.get(), 1.0);
4239            }
4240        }
4241    }
4242}