summaryrefslogtreecommitdiffstats
path: root/servo/components/style/properties/shorthands/text.mako.rs
blob: 5b071be2c4b89c752053938f69b92fb11769f9ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

<%namespace name="helpers" file="/helpers.mako.rs" />

<%helpers:shorthand name="text-decoration"
                    engines="gecko servo-2013 servo-2020"
                    flags="SHORTHAND_IN_GETCS"
                    sub_properties="text-decoration-line
                    ${' text-decoration-style text-decoration-color text-decoration-thickness' if engine == 'gecko' else ''}"
                    spec="https://drafts.csswg.org/css-text-decor/#propdef-text-decoration">
    % if engine == "gecko":
        use crate::values::specified;
        use crate::properties::longhands::{text_decoration_style, text_decoration_color, text_decoration_thickness};
    % endif
    use crate::properties::longhands::text_decoration_line;

    pub fn parse_value<'i, 't>(
        context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Longhands, ParseError<'i>> {
        % if engine == "gecko":
            let (mut line, mut style, mut color, mut thickness, mut any) = (None, None, None, None, false);
        % else:
            let (mut line, mut any) = (None, false);
        % endif

        loop {
            macro_rules! parse_component {
                ($value:ident, $module:ident) => (
                    if $value.is_none() {
                        if let Ok(value) = input.try_parse(|input| $module::parse(context, input)) {
                            $value = Some(value);
                            any = true;
                            continue;
                        }
                    }
                )
            }

            parse_component!(line, text_decoration_line);

            % if engine == "gecko":
                parse_component!(style, text_decoration_style);
                parse_component!(color, text_decoration_color);
                parse_component!(thickness, text_decoration_thickness);
            % endif

            break;
        }

        if !any {
            return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
        }

        Ok(expanded! {
            text_decoration_line: unwrap_or_initial!(text_decoration_line, line),

            % if engine == "gecko":
                text_decoration_style: unwrap_or_initial!(text_decoration_style, style),
                text_decoration_color: unwrap_or_initial!(text_decoration_color, color),
                text_decoration_thickness: unwrap_or_initial!(text_decoration_thickness, thickness),
            % endif
        })
    }

    impl<'a> ToCss for LonghandsToSerialize<'a>  {
        #[allow(unused)]
        fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write {
            use crate::values::specified::TextDecorationLine;

            let (is_solid_style, is_current_color, is_auto_thickness) =
            (
            % if engine == "gecko":
                *self.text_decoration_style == text_decoration_style::SpecifiedValue::Solid,
                *self.text_decoration_color == specified::Color::CurrentColor,
                self.text_decoration_thickness.is_auto()
            % else:
                true, true, true
            % endif
            );

            let mut has_value = false;
            let is_none = *self.text_decoration_line == TextDecorationLine::none();
            if (is_solid_style && is_current_color && is_auto_thickness) || !is_none {
                self.text_decoration_line.to_css(dest)?;
                has_value = true;
            }

            if !is_auto_thickness {
                if has_value {
                    dest.write_char(' ')?;
                }
                self.text_decoration_thickness.to_css(dest)?;
                has_value = true;
            }

            % if engine == "gecko":
            if !is_solid_style {
                if has_value {
                    dest.write_char(' ')?;
                }
                self.text_decoration_style.to_css(dest)?;
                has_value = true;
            }

            if !is_current_color {
                if has_value {
                    dest.write_char(' ')?;
                }
                self.text_decoration_color.to_css(dest)?;
                has_value = true;
            }
            % endif

            Ok(())
        }
    }
</%helpers:shorthand>