Skip to main content

style/properties/
computed_value_flags.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//! Misc information about a given computed style.
6
7/// Misc information about a given computed style.
8///
9/// All flags are currently inherited for text, pseudo elements, and
10/// anonymous boxes, see StyleBuilder::for_inheritance and its callsites.
11/// If we ever want to add some flags that shouldn't inherit for them,
12/// we might want to add a function to handle this.
13#[repr(C)]
14#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15#[cfg_attr(feature = "servo", derive(crate::derives::MallocSizeOf))]
16pub struct ComputedValueFlags(u32);
17
18bitflags! {
19    impl ComputedValueFlags: u32 {
20        /// Whether the style or any of the ancestors has a text-decoration-line
21        /// property that should get propagated to descendants.
22        ///
23        /// text-decoration-line is a reset property, but gets propagated in the
24        /// frame/box tree.
25        const HAS_TEXT_DECORATION_LINES = 1 << 0;
26
27        /// Whether line break inside should be suppressed.
28        ///
29        /// If this flag is set, the line should not be broken inside,
30        /// which means inlines act as if nowrap is set, <br> element is
31        /// suppressed, and blocks are inlinized.
32        ///
33        /// This bit is propagated to all children of line participants.
34        /// It is currently used by ruby to make its content unbreakable.
35        const SHOULD_SUPPRESS_LINEBREAK = 1 << 1;
36
37        /// A flag used to mark text that that has text-combine-upright.
38        ///
39        /// This is used from Gecko's layout engine.
40        const IS_TEXT_COMBINED = 1 << 2;
41
42        /// A flag used to mark styles under a relevant link that is also
43        /// visited.
44        const IS_RELEVANT_LINK_VISITED = 1 << 3;
45
46        /// A flag used to mark styles which are a ::first-line or under one.
47        const IS_IN_FIRST_LINE_SUBTREE = 1 << 4;
48
49        /// A flag used to mark styles which have contain:style or under one.
50        const SELF_OR_ANCESTOR_HAS_CONTAIN_STYLE = 1 << 5;
51
52        /// Whether this style's `display` or `content`property depends on our parent style.
53        ///
54        /// This is important because it may affect our optimizations to avoid
55        /// computing the style of pseudo-elements, given whether the
56        /// pseudo-element is generated depends on the `display` (or `content`) value.
57        const DISPLAY_OR_CONTENT_DEPEND_ON_INHERITED_STYLE = 1 << 6;
58
59        /// Whether the child explicitly inherits any reset property.
60        const INHERITS_RESET_STYLE = 1 << 7;
61
62        /// Whether any value on our style is font-metric-dependent on our
63        /// primary font.
64        const DEPENDS_ON_SELF_FONT_METRICS = 1 << 8;
65
66        /// Whether any value on our style is font-metric-dependent on the
67        /// primary font of our parent.
68        const DEPENDS_ON_INHERITED_FONT_METRICS = 1 << 9;
69
70        /// Whether this style is the style of the document element.
71        const IS_ROOT_ELEMENT_STYLE = 1 << 10;
72
73        /// Whether this element is inside an `opacity: 0` subtree.
74        const IS_IN_OPACITY_ZERO_SUBTREE = 1 << 11;
75
76        /// Whether there are author-specified rules for border-* properties
77        /// (except border-image-*), background-color, or background-image.
78        ///
79        /// TODO(emilio): Maybe do include border-image, see:
80        ///
81        /// https://github.com/w3c/csswg-drafts/issues/4777#issuecomment-604424845
82        const HAS_AUTHOR_SPECIFIED_BORDER_BACKGROUND = 1 << 12;
83
84        /// Whether the style depends on viewport units.
85        const USES_VIEWPORT_UNITS = 1 << 13;
86
87        /// Whether the style depends on viewport units on container queries.
88        ///
89        /// This needs to be a separate flag from `USES_VIEWPORT_UNITS` because
90        /// it causes us to re-match the style (rather than re-cascascading it,
91        /// which is enough for other uses of viewport units).
92        const USES_VIEWPORT_UNITS_ON_CONTAINER_QUERIES = 1 << 14;
93
94        /// A flag used to mark styles which have `container-type` of `size` or
95        /// `inline-size`, or under one.
96        const SELF_OR_ANCESTOR_HAS_SIZE_CONTAINER_TYPE = 1 << 15;
97
98        /// Whether the style uses container query units, in which case the style depends on the
99        /// container's size and we can't reuse it across cousins (without double-checking the
100        /// container at least).
101        const USES_CONTAINER_UNITS = 1 << 16;
102
103        /// Whether there are author-specific rules for text `color`.
104        const HAS_AUTHOR_SPECIFIED_TEXT_COLOR = 1 << 17;
105
106        /// Whether this style considered a scope style rule.
107        const CONSIDERED_NONTRIVIAL_SCOPED_STYLE = 1 << 18;
108
109        /// Whether this style is that of a `display: contents` element that is either a direct
110        /// child of an item container or another `display: contents` element, the style of which
111        /// has this flag set, marked in order to cascade beyond them to the descendants of the
112        /// the item container that do generate a box.
113        const DISPLAY_CONTENTS_IN_ITEM_CONTAINER = 1 << 19;
114
115        /// Whether there are author-specific rules for `text-shadow`.
116        const HAS_AUTHOR_SPECIFIED_TEXT_SHADOW = 1 << 20;
117
118        /// Whether this style depends on container style query.
119        const DEPENDS_ON_CONTAINER_STYLE_QUERY = 1 << 21;
120
121        /// Whether this style is in an appearance: base subtree
122        const IS_IN_APPEARANCE_BASE_SUBTREE = 1 << 22;
123
124        /// Whether grid-auto-flow is author-specified.
125        const HAS_AUTHOR_SPECIFIED_GRID_AUTO_FLOW = 1 << 23;
126
127        /// Whether this style has used font relative units. Note that this is different than the
128        /// FONT_METRICS bits, which don't include rem / em etc.
129        const USES_FONT_RELATIVE_UNITS = 1 << 24;
130
131        /// Whether this style depends on font relative units in container queries.
132        const USES_FONT_RELATIVE_UNITS_ON_CONTAINER_QUERIES = 1 << 25;
133    }
134}
135
136impl Default for ComputedValueFlags {
137    #[inline]
138    fn default() -> Self {
139        Self::empty()
140    }
141}
142
143impl ComputedValueFlags {
144    /// Flags that are unconditionally propagated to descendants.
145    #[inline]
146    fn inherited_flags() -> Self {
147        Self::IS_RELEVANT_LINK_VISITED
148            | Self::IS_IN_FIRST_LINE_SUBTREE
149            | Self::HAS_TEXT_DECORATION_LINES
150            | Self::IS_IN_OPACITY_ZERO_SUBTREE
151            | Self::SELF_OR_ANCESTOR_HAS_CONTAIN_STYLE
152            | Self::SELF_OR_ANCESTOR_HAS_SIZE_CONTAINER_TYPE
153            | Self::IS_IN_APPEARANCE_BASE_SUBTREE
154    }
155
156    /// Flags that may be propagated to descendants.
157    #[inline]
158    fn maybe_inherited_flags() -> Self {
159        Self::inherited_flags()
160            | Self::SHOULD_SUPPRESS_LINEBREAK
161            | Self::DISPLAY_CONTENTS_IN_ITEM_CONTAINER
162    }
163
164    /// Flags that are an input to the cascade.
165    #[inline]
166    fn cascade_input_flags() -> Self {
167        Self::USES_VIEWPORT_UNITS_ON_CONTAINER_QUERIES
168            | Self::CONSIDERED_NONTRIVIAL_SCOPED_STYLE
169            | Self::DEPENDS_ON_CONTAINER_STYLE_QUERY
170            | Self::USES_FONT_RELATIVE_UNITS_ON_CONTAINER_QUERIES
171    }
172
173    /// Returns the flags that are always propagated to descendants.
174    ///
175    /// See StyleAdjuster::set_bits and StyleBuilder.
176    #[inline]
177    pub fn inherited(self) -> Self {
178        self & Self::inherited_flags()
179    }
180
181    /// Flags that are conditionally propagated to descendants, just to handle
182    /// properly style invalidation.
183    #[inline]
184    pub fn maybe_inherited(self) -> Self {
185        self & Self::maybe_inherited_flags()
186    }
187
188    /// Flags that are an input to the cascade.
189    #[inline]
190    pub fn for_cascade_inputs(self) -> Self {
191        self & Self::cascade_input_flags()
192    }
193}