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