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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* 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/. */

//! Specified types for CSS values that are related to motion path.

use crate::parser::{Parse, ParserContext};
use crate::values::computed::motion::OffsetRotate as ComputedOffsetRotate;
use crate::values::computed::{Context, ToComputedValue};
use crate::values::generics::motion as generics;
use crate::values::specified::basic_shape::BasicShape;
use crate::values::specified::position::{HorizontalPosition, VerticalPosition};
use crate::values::specified::url::SpecifiedUrl;
use crate::values::specified::{Angle, Position};
use crate::Zero;
use cssparser::Parser;
use style_traits::{ParseError, StyleParseErrorKind};

/// The specified value of ray() function.
pub type RayFunction = generics::GenericRayFunction<Angle, Position>;

/// The specified value of <offset-path>.
pub type OffsetPathFunction =
    generics::GenericOffsetPathFunction<BasicShape, RayFunction, SpecifiedUrl>;

/// The specified value of `offset-path`.
pub type OffsetPath = generics::GenericOffsetPath<OffsetPathFunction>;

/// The specified value of `offset-position`.
pub type OffsetPosition = generics::GenericOffsetPosition<HorizontalPosition, VerticalPosition>;

/// The <coord-box> value, which defines the box that the <offset-path> sizes into.
/// https://drafts.fxtf.org/motion-1/#valdef-offset-path-coord-box
///
/// <coord-box> = content-box | padding-box | border-box | fill-box | stroke-box | view-box
/// https://drafts.csswg.org/css-box-4/#typedef-coord-box
#[allow(missing_docs)]
#[derive(
    Animate,
    Clone,
    ComputeSquaredDistance,
    Copy,
    Debug,
    Deserialize,
    MallocSizeOf,
    Parse,
    PartialEq,
    Serialize,
    SpecifiedValueInfo,
    ToAnimatedValue,
    ToComputedValue,
    ToCss,
    ToResolvedValue,
    ToShmem,
)]
#[repr(u8)]
pub enum CoordBox {
    ContentBox,
    PaddingBox,
    BorderBox,
    FillBox,
    StrokeBox,
    ViewBox,
}

impl CoordBox {
    /// Returns true if it is default value, border-box.
    #[inline]
    pub fn is_default(&self) -> bool {
        matches!(*self, Self::BorderBox)
    }
}

impl Parse for RayFunction {
    fn parse<'i, 't>(
        context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Self, ParseError<'i>> {
        input.expect_function_matching("ray")?;
        input.parse_nested_block(|i| Self::parse_function_arguments(context, i))
    }
}

impl RayFunction {
    /// Parse the inner arguments of a `ray` function.
    fn parse_function_arguments<'i, 't>(
        context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Self, ParseError<'i>> {
        use crate::values::specified::PositionOrAuto;

        let mut angle = None;
        let mut size = None;
        let mut contain = false;
        let mut position = None;
        loop {
            if angle.is_none() {
                angle = input.try_parse(|i| Angle::parse(context, i)).ok();
            }

            if size.is_none() {
                size = input.try_parse(generics::RaySize::parse).ok();
                if size.is_some() {
                    continue;
                }
            }

            if !contain {
                contain = input
                    .try_parse(|i| i.expect_ident_matching("contain"))
                    .is_ok();
                if contain {
                    continue;
                }
            }

            if position.is_none() {
                if input.try_parse(|i| i.expect_ident_matching("at")).is_ok() {
                    let pos = Position::parse(context, input)?;
                    position = Some(PositionOrAuto::Position(pos));
                }

                if position.is_some() {
                    continue;
                }
            }
            break;
        }

        if angle.is_none() {
            return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
        }

        Ok(RayFunction {
            angle: angle.unwrap(),
            // If no <ray-size> is specified it defaults to closest-side.
            size: size.unwrap_or(generics::RaySize::ClosestSide),
            contain,
            position: position.unwrap_or(PositionOrAuto::auto()),
        })
    }
}

impl Parse for OffsetPathFunction {
    fn parse<'i, 't>(
        context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Self, ParseError<'i>> {
        use crate::values::specified::basic_shape::{AllowedBasicShapes, ShapeType};

        // <offset-path> = <ray()> | <url> | <basic-shape>
        // https://drafts.fxtf.org/motion-1/#typedef-offset-path
        if let Ok(ray) = input.try_parse(|i| RayFunction::parse(context, i)) {
            return Ok(OffsetPathFunction::Ray(ray));
        }

        if static_prefs::pref!("layout.css.motion-path-url.enabled") {
            if let Ok(url) = input.try_parse(|i| SpecifiedUrl::parse(context, i)) {
                return Ok(OffsetPathFunction::Url(url));
            }
        }

        BasicShape::parse(context, input, AllowedBasicShapes::ALL, ShapeType::Outline)
            .map(OffsetPathFunction::Shape)
    }
}

impl Parse for OffsetPath {
    fn parse<'i, 't>(
        context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Self, ParseError<'i>> {
        // Parse none.
        if input.try_parse(|i| i.expect_ident_matching("none")).is_ok() {
            return Ok(OffsetPath::none());
        }

        let mut path = None;
        let mut coord_box = None;
        loop {
            if path.is_none() {
                path = input
                    .try_parse(|i| OffsetPathFunction::parse(context, i))
                    .ok();
            }

            if coord_box.is_none() {
                coord_box = input.try_parse(CoordBox::parse).ok();
                if coord_box.is_some() {
                    continue;
                }
            }
            break;
        }

        if let Some(p) = path {
            return Ok(OffsetPath::OffsetPath {
                path: Box::new(p),
                coord_box: coord_box.unwrap_or(CoordBox::BorderBox),
            });
        }

        match coord_box {
            Some(c) => Ok(OffsetPath::CoordBox(c)),
            None => Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)),
        }
    }
}

/// The direction of offset-rotate.
#[derive(
    Clone, Copy, Debug, MallocSizeOf, Parse, PartialEq, SpecifiedValueInfo, ToCss, ToShmem,
)]
#[repr(u8)]
pub enum OffsetRotateDirection {
    /// Unspecified direction keyword.
    #[css(skip)]
    None,
    /// 0deg offset (face forward).
    Auto,
    /// 180deg offset (face backward).
    Reverse,
}

impl OffsetRotateDirection {
    /// Returns true if it is none (i.e. the keyword is not specified).
    #[inline]
    fn is_none(&self) -> bool {
        *self == OffsetRotateDirection::None
    }
}

#[inline]
fn direction_specified_and_angle_is_zero(direction: &OffsetRotateDirection, angle: &Angle) -> bool {
    !direction.is_none() && angle.is_zero()
}

/// The specified offset-rotate.
/// The syntax is: "[ auto | reverse ] || <angle>"
///
/// https://drafts.fxtf.org/motion-1/#offset-rotate-property
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToCss, ToShmem)]
pub struct OffsetRotate {
    /// [auto | reverse].
    #[css(skip_if = "OffsetRotateDirection::is_none")]
    direction: OffsetRotateDirection,
    /// <angle>.
    /// If direction is None, this is a fixed angle which indicates a
    /// constant clockwise rotation transformation applied to it by this
    /// specified rotation angle. Otherwise, the angle will be added to
    /// the angle of the direction in layout.
    #[css(contextual_skip_if = "direction_specified_and_angle_is_zero")]
    angle: Angle,
}

impl OffsetRotate {
    /// Returns the initial value, auto.
    #[inline]
    pub fn auto() -> Self {
        OffsetRotate {
            direction: OffsetRotateDirection::Auto,
            angle: Angle::zero(),
        }
    }

    /// Returns true if self is auto 0deg.
    #[inline]
    pub fn is_auto(&self) -> bool {
        self.direction == OffsetRotateDirection::Auto && self.angle.is_zero()
    }
}

impl Parse for OffsetRotate {
    fn parse<'i, 't>(
        context: &ParserContext,
        input: &mut Parser<'i, 't>,
    ) -> Result<Self, ParseError<'i>> {
        let location = input.current_source_location();
        let mut direction = input.try_parse(OffsetRotateDirection::parse);
        let angle = input.try_parse(|i| Angle::parse(context, i));
        if direction.is_err() {
            // The direction and angle could be any order, so give it a change to parse
            // direction again.
            direction = input.try_parse(OffsetRotateDirection::parse);
        }

        if direction.is_err() && angle.is_err() {
            return Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError));
        }

        Ok(OffsetRotate {
            direction: direction.unwrap_or(OffsetRotateDirection::None),
            angle: angle.unwrap_or(Zero::zero()),
        })
    }
}

impl ToComputedValue for OffsetRotate {
    type ComputedValue = ComputedOffsetRotate;

    #[inline]
    fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
        use crate::values::computed::Angle as ComputedAngle;

        ComputedOffsetRotate {
            auto: !self.direction.is_none(),
            angle: if self.direction == OffsetRotateDirection::Reverse {
                // The computed value should always convert "reverse" into "auto".
                // e.g. "reverse calc(20deg + 10deg)" => "auto 210deg"
                self.angle.to_computed_value(context) + ComputedAngle::from_degrees(180.0)
            } else {
                self.angle.to_computed_value(context)
            },
        }
    }

    #[inline]
    fn from_computed_value(computed: &Self::ComputedValue) -> Self {
        OffsetRotate {
            direction: if computed.auto {
                OffsetRotateDirection::Auto
            } else {
                OffsetRotateDirection::None
            },
            angle: ToComputedValue::from_computed_value(&computed.angle),
        }
    }
}