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 depends on root font metrics in container queries.
128        const DEPENDS_ON_FONT_METRICS_IN_CONTAINER_QUERY = 1 << 24;
129    }
130}
131
132impl Default for ComputedValueFlags {
133    #[inline]
134    fn default() -> Self {
135        Self::empty()
136    }
137}
138
139impl ComputedValueFlags {
140    /// Flags that are unconditionally propagated to descendants.
141    #[inline]
142    fn inherited_flags() -> Self {
143        Self::IS_RELEVANT_LINK_VISITED
144            | Self::IS_IN_FIRST_LINE_SUBTREE
145            | Self::HAS_TEXT_DECORATION_LINES
146            | Self::IS_IN_OPACITY_ZERO_SUBTREE
147            | Self::SELF_OR_ANCESTOR_HAS_CONTAIN_STYLE
148            | Self::SELF_OR_ANCESTOR_HAS_SIZE_CONTAINER_TYPE
149            | Self::IS_IN_APPEARANCE_BASE_SUBTREE
150    }
151
152    /// Flags that may be propagated to descendants.
153    #[inline]
154    fn maybe_inherited_flags() -> Self {
155        Self::inherited_flags()
156            | Self::SHOULD_SUPPRESS_LINEBREAK
157            | Self::DISPLAY_CONTENTS_IN_ITEM_CONTAINER
158    }
159
160    /// Flags that are an input to the cascade.
161    #[inline]
162    fn cascade_input_flags() -> Self {
163        Self::USES_VIEWPORT_UNITS_ON_CONTAINER_QUERIES
164            | Self::CONSIDERED_NONTRIVIAL_SCOPED_STYLE
165            | Self::DEPENDS_ON_CONTAINER_STYLE_QUERY
166            | Self::DEPENDS_ON_FONT_METRICS_IN_CONTAINER_QUERY
167    }
168
169    /// Returns the flags that are always propagated to descendants.
170    ///
171    /// See StyleAdjuster::set_bits and StyleBuilder.
172    #[inline]
173    pub fn inherited(self) -> Self {
174        self & Self::inherited_flags()
175    }
176
177    /// Flags that are conditionally propagated to descendants, just to handle
178    /// properly style invalidation.
179    #[inline]
180    pub fn maybe_inherited(self) -> Self {
181        self & Self::maybe_inherited_flags()
182    }
183
184    /// Flags that are an input to the cascade.
185    #[inline]
186    pub fn for_cascade_inputs(self) -> Self {
187        self & Self::cascade_input_flags()
188    }
189}