style/values/computed/
align.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Values for CSS Box Alignment properties
6//!
7//! https://drafts.csswg.org/css-align/
8
9use crate::derives::*;
10use crate::values::computed::{Context, ToComputedValue};
11use crate::values::specified;
12
13pub use super::specified::{ContentDistribution, ItemPlacement, SelfAlignment};
14
15/// The computed value for the `justify-items` property.
16///
17/// Need to carry around both the specified and computed value to handle the
18/// special legacy keyword without destroying style sharing.
19///
20/// In particular, `justify-items` is a reset property, so we ought to be able
21/// to share its computed representation across elements as long as they match
22/// the same rules. Except that it's not true if the specified value for
23/// `justify-items` is `legacy` and the computed value of the parent has the
24/// `legacy` modifier.
25///
26/// So instead of computing `legacy` "normally" looking at get_parent_position(),
27/// marking it as uncacheable, we carry the specified value around and handle
28/// the special case in `StyleAdjuster` instead, only when the result of the
29/// computation would vary.
30///
31/// Note that we also need to special-case this property in matching.rs, in
32/// order to properly handle changes to the legacy keyword... This all kinda
33/// sucks :(.
34///
35/// See the discussion in https://bugzil.la/1384542.
36#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, ToCss, ToResolvedValue, ToTyped)]
37#[repr(C)]
38pub struct ComputedJustifyItems {
39    /// The specified value for the property. Can contain the bare `legacy`
40    /// keyword.
41    #[css(skip)]
42    pub specified: specified::JustifyItems,
43    /// The computed value for the property. Cannot contain the bare `legacy`
44    /// keyword, but note that it could contain it in combination with other
45    /// keywords like `left`, `right` or `center`.
46    pub computed: specified::JustifyItems,
47}
48
49pub use self::ComputedJustifyItems as JustifyItems;
50
51impl JustifyItems {
52    /// Returns the `legacy` value.
53    pub fn legacy() -> Self {
54        Self {
55            specified: specified::JustifyItems::legacy(),
56            computed: specified::JustifyItems::normal(),
57        }
58    }
59}
60
61impl ToComputedValue for specified::JustifyItems {
62    type ComputedValue = JustifyItems;
63
64    /// <https://drafts.csswg.org/css-align/#valdef-justify-items-legacy>
65    fn to_computed_value(&self, _context: &Context) -> JustifyItems {
66        use crate::values::specified::align;
67        let specified = *self;
68        let computed = if (self.0).0 != align::AlignFlags::LEGACY {
69            *self
70        } else {
71            // If the inherited value of `justify-items` includes the
72            // `legacy` keyword, `legacy` computes to the inherited value, but
73            // we assume it computes to `normal`, and handle that special-case
74            // in StyleAdjuster.
75            Self::normal()
76        };
77
78        JustifyItems {
79            specified,
80            computed,
81        }
82    }
83
84    #[inline]
85    fn from_computed_value(computed: &JustifyItems) -> Self {
86        computed.specified
87    }
88}