style/invalidation/element/restyle_hints.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//! Restyle hints: an optimization to avoid unnecessarily matching selectors.
6
7use crate::traversal_flags::TraversalFlags;
8
9bitflags! {
10 /// The kind of restyle we need to do for a given element.
11 #[repr(C)]
12 #[derive(Clone, Copy, Debug)]
13 pub struct RestyleHint: u16 {
14 /// Do a selector match of the element.
15 const RESTYLE_SELF = 1 << 0;
16
17 /// Do a selector match of the element's pseudo-elements. Always to be combined with
18 /// RESTYLE_SELF.
19 const RESTYLE_PSEUDOS = 1 << 1;
20
21 /// Do a selector match if the element is a pseudo-element.
22 const RESTYLE_SELF_IF_PSEUDO = 1 << 2;
23
24 /// Do a selector match of the element's descendants.
25 const RESTYLE_DESCENDANTS = 1 << 3;
26
27 /// Recascade the current element.
28 const RECASCADE_SELF = 1 << 4;
29
30 /// Recascade the current element if it inherits any reset style.
31 const RECASCADE_SELF_IF_INHERIT_RESET_STYLE = 1 << 5;
32
33 /// Recascade all descendant elements.
34 const RECASCADE_DESCENDANTS = 1 << 6;
35
36 /// Replace the style data coming from CSS transitions without updating
37 /// any other style data. This hint is only processed in animation-only
38 /// traversal which is prior to normal traversal.
39 const RESTYLE_CSS_TRANSITIONS = 1 << 7;
40
41 /// Replace the style data coming from CSS animations without updating
42 /// any other style data. This hint is only processed in animation-only
43 /// traversal which is prior to normal traversal.
44 const RESTYLE_CSS_ANIMATIONS = 1 << 8;
45
46 /// Don't re-run selector-matching on the element, only the style
47 /// attribute has changed, and this change didn't have any other
48 /// dependencies.
49 const RESTYLE_STYLE_ATTRIBUTE = 1 << 9;
50
51 /// Replace the style data coming from SMIL animations without updating
52 /// any other style data. This hint is only processed in animation-only
53 /// traversal which is prior to normal traversal.
54 const RESTYLE_SMIL = 1 << 10;
55
56 /// Match self or a descendant if it is dependent on a style query.
57 const RESTYLE_IF_AFFECTED_BY_STYLE_QUERIES = 1 << 11;
58 }
59}
60
61impl RestyleHint {
62 /// Creates a new `RestyleHint` indicating that the current element and all
63 /// its descendants must be fully restyled.
64 #[inline]
65 pub fn restyle_subtree() -> Self {
66 RestyleHint::RESTYLE_SELF | RestyleHint::RESTYLE_DESCENDANTS
67 }
68
69 /// Creates a new `RestyleHint` indicating that the current element and all
70 /// its descendants must be recascaded.
71 #[inline]
72 pub fn recascade_subtree() -> Self {
73 RestyleHint::RECASCADE_SELF | RestyleHint::RECASCADE_DESCENDANTS
74 }
75
76 /// Returns whether this hint invalidates the element and all its
77 /// descendants.
78 #[inline]
79 pub fn contains_subtree(&self) -> bool {
80 self.contains(Self::restyle_subtree())
81 }
82
83 /// Returns whether we'll recascade all of the descendants.
84 #[inline]
85 pub fn will_recascade_subtree(&self) -> bool {
86 self.contains_subtree() || self.contains(Self::recascade_subtree())
87 }
88
89 /// Returns whether we need to restyle this element.
90 pub fn has_non_animation_invalidations(&self) -> bool {
91 !(*self & !Self::for_animations()).is_empty()
92 }
93
94 /// Propagates this restyle hint to a child element.
95 pub fn propagate(&mut self, traversal_flags: &TraversalFlags) -> Self {
96 use std::mem;
97
98 // In the middle of an animation only restyle, we don't need to
99 // propagate any restyle hints, and we need to remove ourselves.
100 if traversal_flags.for_animation_only() {
101 self.remove_animation_hints();
102 return Self::empty();
103 }
104
105 debug_assert!(
106 !self.has_animation_hint(),
107 "There should not be any animation restyle hints \
108 during normal traversal"
109 );
110
111 // Else we should clear ourselves, and return the propagated hint.
112 mem::replace(self, Self::empty()).propagate_for_non_animation_restyle()
113 }
114
115 /// Returns a new `RestyleHint` appropriate for children of the current element.
116 fn propagate_for_non_animation_restyle(&self) -> Self {
117 if self.contains(RestyleHint::RESTYLE_DESCENDANTS) {
118 return Self::restyle_subtree();
119 }
120 let mut result = Self::empty();
121 if self.contains(RestyleHint::RESTYLE_PSEUDOS) {
122 result |= Self::RESTYLE_SELF_IF_PSEUDO;
123 }
124 if self.contains(RestyleHint::RECASCADE_DESCENDANTS) {
125 result |= Self::recascade_subtree();
126 }
127 if self.contains(RestyleHint::RESTYLE_IF_AFFECTED_BY_STYLE_QUERIES) {
128 // We may need to restyle further down the tree if rules are
129 // declared for a named container.
130 // e.g @container my-name {#b {...}}
131 // and <div id=a> <div> <div id=b> </div> </div> </div>
132 // If a toggles `container-name: my-name` the rules for #b
133 // also invalidate.
134 // TODO (bug 2024928): We can be more discerning if we know
135 // a container is named and we could avoid always propagating.
136 result |= RestyleHint::RESTYLE_IF_AFFECTED_BY_STYLE_QUERIES;
137 }
138
139 result
140 }
141
142 /// Returns a hint that contains all the replacement hints.
143 pub fn replacements() -> Self {
144 RestyleHint::RESTYLE_STYLE_ATTRIBUTE | Self::for_animations()
145 }
146
147 /// The replacements for the animation cascade levels.
148 #[inline]
149 pub fn for_animations() -> Self {
150 RestyleHint::RESTYLE_SMIL
151 | RestyleHint::RESTYLE_CSS_ANIMATIONS
152 | RestyleHint::RESTYLE_CSS_TRANSITIONS
153 }
154
155 /// Returns whether the hint specifies that an animation cascade level must
156 /// be replaced.
157 #[inline]
158 pub fn has_animation_hint(&self) -> bool {
159 self.intersects(Self::for_animations())
160 }
161
162 /// Returns whether the hint specifies that an animation cascade level must
163 /// be replaced.
164 #[inline]
165 pub fn has_animation_hint_or_recascade(&self) -> bool {
166 self.intersects(
167 Self::for_animations()
168 | Self::RECASCADE_SELF
169 | Self::RECASCADE_SELF_IF_INHERIT_RESET_STYLE,
170 )
171 }
172
173 /// Returns whether the hint specifies some restyle work other than an
174 /// animation cascade level replacement.
175 #[inline]
176 pub fn has_non_animation_hint(&self) -> bool {
177 !(*self & !Self::for_animations()).is_empty()
178 }
179
180 /// Returns whether the hint specifies that some cascade levels must be
181 /// replaced.
182 #[inline]
183 pub fn has_replacements(&self) -> bool {
184 self.intersects(Self::replacements())
185 }
186
187 /// Removes all of the animation-related hints.
188 #[inline]
189 pub fn remove_animation_hints(&mut self) {
190 self.remove(Self::for_animations());
191
192 // While RECASCADE_SELF is not animation-specific, we only ever add and process it during
193 // traversal. If we are here, removing animation hints, then we are in an animation-only
194 // traversal, and we know that any RECASCADE_SELF flag must have been set due to changes in
195 // inherited values after restyling for animations, and thus we want to remove it so that
196 // we don't later try to restyle the element during a normal restyle.
197 // (We could have separate RECASCADE_SELF_NORMAL and RECASCADE_SELF_ANIMATIONS flags to
198 // make it clear, but this isn't currently necessary.)
199 self.remove(Self::RECASCADE_SELF | Self::RECASCADE_SELF_IF_INHERIT_RESET_STYLE);
200 }
201}
202
203impl Default for RestyleHint {
204 fn default() -> Self {
205 Self::empty()
206 }
207}
208
209#[cfg(feature = "servo")]
210malloc_size_of::malloc_size_of_is_0!(RestyleHint);