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)]
38#[typed(todo_derive_fields)]
39pub struct ComputedJustifyItems {
40 /// The specified value for the property. Can contain the bare `legacy`
41 /// keyword.
42 #[css(skip)]
43 pub specified: specified::JustifyItems,
44 /// The computed value for the property. Cannot contain the bare `legacy`
45 /// keyword, but note that it could contain it in combination with other
46 /// keywords like `left`, `right` or `center`.
47 pub computed: specified::JustifyItems,
48}
49
50pub use self::ComputedJustifyItems as JustifyItems;
51
52impl JustifyItems {
53 /// Returns the `legacy` value.
54 pub fn legacy() -> Self {
55 Self {
56 specified: specified::JustifyItems::legacy(),
57 computed: specified::JustifyItems::normal(),
58 }
59 }
60}
61
62impl ToComputedValue for specified::JustifyItems {
63 type ComputedValue = JustifyItems;
64
65 /// <https://drafts.csswg.org/css-align/#valdef-justify-items-legacy>
66 fn to_computed_value(&self, _context: &Context) -> JustifyItems {
67 use crate::values::specified::align;
68 let specified = *self;
69 let computed = if (self.0).0 != align::AlignFlags::LEGACY {
70 *self
71 } else {
72 // If the inherited value of `justify-items` includes the
73 // `legacy` keyword, `legacy` computes to the inherited value, but
74 // we assume it computes to `normal`, and handle that special-case
75 // in StyleAdjuster.
76 Self::normal()
77 };
78
79 JustifyItems {
80 specified,
81 computed,
82 }
83 }
84
85 #[inline]
86 fn from_computed_value(computed: &JustifyItems) -> Self {
87 computed.specified
88 }
89}