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
/* 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/. */

//! Resolved values. These are almost always computed values, but in some cases
//! there are used values.

#[cfg(feature = "gecko")]
use crate::media_queries::Device;
use crate::properties::ComputedValues;
use crate::ArcSlice;
use servo_arc::Arc;
use smallvec::SmallVec;

mod color;
mod counters;

use crate::values::computed;

/// Element-specific information needed to resolve property values.
#[cfg(feature = "gecko")]
pub struct ResolvedElementInfo<'a> {
    /// Element we're resolving line-height against.
    pub element: crate::gecko::wrapper::GeckoElement<'a>,
}

/// Information needed to resolve a given value.
pub struct Context<'a> {
    /// The style we're resolving for. This is useful to resolve currentColor.
    pub style: &'a ComputedValues,
    /// The device / document we're resolving style for. Useful to do font metrics stuff needed for
    /// line-height.
    #[cfg(feature = "gecko")]
    pub device: &'a Device,
    /// The element-specific information to resolve the value.
    #[cfg(feature = "gecko")]
    pub element_info: ResolvedElementInfo<'a>,
}

/// A trait to represent the conversion between resolved and resolved values.
///
/// This trait is derivable with `#[derive(ToResolvedValue)]`.
///
/// The deriving code assumes that if the type isn't generic, then the trait can
/// be implemented as simple move. This means that a manual implementation with
/// `ResolvedValue = Self` is bogus if it returns anything else than a clone.
pub trait ToResolvedValue {
    /// The resolved value type we're going to be converted to.
    type ResolvedValue;

    /// Convert a resolved value to a resolved value.
    fn to_resolved_value(self, context: &Context) -> Self::ResolvedValue;

    /// Convert a resolved value to resolved value form.
    fn from_resolved_value(resolved: Self::ResolvedValue) -> Self;
}

macro_rules! trivial_to_resolved_value {
    ($ty:ty) => {
        impl $crate::values::resolved::ToResolvedValue for $ty {
            type ResolvedValue = Self;

            #[inline]
            fn to_resolved_value(self, _: &Context) -> Self {
                self
            }

            #[inline]
            fn from_resolved_value(resolved: Self::ResolvedValue) -> Self {
                resolved
            }
        }
    };
}

trivial_to_resolved_value!(());
trivial_to_resolved_value!(bool);
trivial_to_resolved_value!(f32);
trivial_to_resolved_value!(u8);
trivial_to_resolved_value!(i8);
trivial_to_resolved_value!(u16);
trivial_to_resolved_value!(i16);
trivial_to_resolved_value!(u32);
trivial_to_resolved_value!(i32);
trivial_to_resolved_value!(usize);
trivial_to_resolved_value!(String);
trivial_to_resolved_value!(Box<str>);
trivial_to_resolved_value!(crate::OwnedStr);
trivial_to_resolved_value!(crate::color::AbsoluteColor);
trivial_to_resolved_value!(crate::values::generics::color::ColorMixFlags);
trivial_to_resolved_value!(crate::Atom);
trivial_to_resolved_value!(crate::values::AtomIdent);
trivial_to_resolved_value!(app_units::Au);
trivial_to_resolved_value!(computed::url::ComputedUrl);
#[cfg(feature = "gecko")]
trivial_to_resolved_value!(computed::url::ComputedImageUrl);
#[cfg(feature = "servo")]
trivial_to_resolved_value!(crate::Namespace);
#[cfg(feature = "servo")]
trivial_to_resolved_value!(crate::Prefix);
trivial_to_resolved_value!(computed::LengthPercentage);
trivial_to_resolved_value!(style_traits::values::specified::AllowedNumericType);
trivial_to_resolved_value!(computed::TimingFunction);

impl<A, B> ToResolvedValue for (A, B)
where
    A: ToResolvedValue,
    B: ToResolvedValue,
{
    type ResolvedValue = (
        <A as ToResolvedValue>::ResolvedValue,
        <B as ToResolvedValue>::ResolvedValue,
    );

    #[inline]
    fn to_resolved_value(self, context: &Context) -> Self::ResolvedValue {
        (
            self.0.to_resolved_value(context),
            self.1.to_resolved_value(context),
        )
    }

    #[inline]
    fn from_resolved_value(resolved: Self::ResolvedValue) -> Self {
        (
            A::from_resolved_value(resolved.0),
            B::from_resolved_value(resolved.1),
        )
    }
}

impl<T> ToResolvedValue for Option<T>
where
    T: ToResolvedValue,
{
    type ResolvedValue = Option<<T as ToResolvedValue>::ResolvedValue>;

    #[inline]
    fn to_resolved_value(self, context: &Context) -> Self::ResolvedValue {
        self.map(|item| item.to_resolved_value(context))
    }

    #[inline]
    fn from_resolved_value(resolved: Self::ResolvedValue) -> Self {
        resolved.map(T::from_resolved_value)
    }
}

impl<T> ToResolvedValue for SmallVec<[T; 1]>
where
    T: ToResolvedValue,
{
    type ResolvedValue = SmallVec<[<T as ToResolvedValue>::ResolvedValue; 1]>;

    #[inline]
    fn to_resolved_value(self, context: &Context) -> Self::ResolvedValue {
        self.into_iter()
            .map(|item| item.to_resolved_value(context))
            .collect()
    }

    #[inline]
    fn from_resolved_value(resolved: Self::ResolvedValue) -> Self {
        resolved.into_iter().map(T::from_resolved_value).collect()
    }
}

impl<T> ToResolvedValue for Vec<T>
where
    T: ToResolvedValue,
{
    type ResolvedValue = Vec<<T as ToResolvedValue>::ResolvedValue>;

    #[inline]
    fn to_resolved_value(self, context: &Context) -> Self::ResolvedValue {
        self.into_iter()
            .map(|item| item.to_resolved_value(context))
            .collect()
    }

    #[inline]
    fn from_resolved_value(resolved: Self::ResolvedValue) -> Self {
        resolved.into_iter().map(T::from_resolved_value).collect()
    }
}

impl<T> ToResolvedValue for Box<T>
where
    T: ToResolvedValue,
{
    type ResolvedValue = Box<<T as ToResolvedValue>::ResolvedValue>;

    #[inline]
    fn to_resolved_value(self, context: &Context) -> Self::ResolvedValue {
        Box::new(T::to_resolved_value(*self, context))
    }

    #[inline]
    fn from_resolved_value(resolved: Self::ResolvedValue) -> Self {
        Box::new(T::from_resolved_value(*resolved))
    }
}

impl<T> ToResolvedValue for Box<[T]>
where
    T: ToResolvedValue,
{
    type ResolvedValue = Box<[<T as ToResolvedValue>::ResolvedValue]>;

    #[inline]
    fn to_resolved_value(self, context: &Context) -> Self::ResolvedValue {
        Vec::from(self)
            .to_resolved_value(context)
            .into_boxed_slice()
    }

    #[inline]
    fn from_resolved_value(resolved: Self::ResolvedValue) -> Self {
        Vec::from_resolved_value(Vec::from(resolved)).into_boxed_slice()
    }
}

impl<T> ToResolvedValue for crate::OwnedSlice<T>
where
    T: ToResolvedValue,
{
    type ResolvedValue = crate::OwnedSlice<<T as ToResolvedValue>::ResolvedValue>;

    #[inline]
    fn to_resolved_value(self, context: &Context) -> Self::ResolvedValue {
        self.into_box().to_resolved_value(context).into()
    }

    #[inline]
    fn from_resolved_value(resolved: Self::ResolvedValue) -> Self {
        Self::from(Box::from_resolved_value(resolved.into_box()))
    }
}

// NOTE(emilio): This is implementable more generically, but it's unlikely what
// you want there, as it forces you to have an extra allocation.
//
// We could do that if needed, ideally with specialization for the case where
// ResolvedValue = T. But we don't need it for now.
impl<T> ToResolvedValue for Arc<T>
where
    T: ToResolvedValue<ResolvedValue = T>,
{
    type ResolvedValue = Self;

    #[inline]
    fn to_resolved_value(self, _: &Context) -> Self {
        self
    }

    #[inline]
    fn from_resolved_value(resolved: Self) -> Self {
        resolved
    }
}

// Same caveat as above applies.
impl<T> ToResolvedValue for ArcSlice<T>
where
    T: ToResolvedValue<ResolvedValue = T>,
{
    type ResolvedValue = Self;

    #[inline]
    fn to_resolved_value(self, _: &Context) -> Self {
        self
    }

    #[inline]
    fn from_resolved_value(resolved: Self) -> Self {
        resolved
    }
}