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 if this element is dependent on a style query.
57 const RESTYLE_IF_AFFECTED_BY_STYLE_QUERIES = 1 << 11;
58
59 /// Match self or descendants if dependent on a named style query.
60 const RESTYLE_IF_AFFECTED_BY_NAMED_STYLE_CONTAINER = 1 << 12;
61
62 /// Do a selector match of the element if it depends on an ancestor's font.
63 const RESTYLE_IF_AFFECTED_BY_ANCESTOR_FONT = 1 << 13;
64 }
65}
66
67impl RestyleHint {
68 /// Creates a new `RestyleHint` indicating that the current element and all
69 /// its descendants must be fully restyled.
70 #[inline]
71 pub fn restyle_subtree() -> Self {
72 RestyleHint::RESTYLE_SELF | RestyleHint::RESTYLE_DESCENDANTS
73 }
74
75 /// Creates a new `RestyleHint` indicating that the current element and all
76 /// its descendants must be recascaded.
77 #[inline]
78 pub fn recascade_subtree() -> Self {
79 RestyleHint::RECASCADE_SELF | RestyleHint::RECASCADE_DESCENDANTS
80 }
81
82 /// Returns whether this hint invalidates the element and all its
83 /// descendants.
84 #[inline]
85 pub fn contains_subtree(&self) -> bool {
86 self.contains(Self::restyle_subtree())
87 }
88
89 /// Returns whether we'll recascade all of the descendants.
90 #[inline]
91 pub fn will_recascade_subtree(&self) -> bool {
92 self.contains_subtree() || self.contains(Self::recascade_subtree())
93 }
94
95 /// Returns whether we need to restyle this element.
96 pub fn has_non_animation_invalidations(&self) -> bool {
97 !(*self & !Self::for_animations()).is_empty()
98 }
99
100 /// Propagates this restyle hint to a child element.
101 pub fn propagate(&mut self, traversal_flags: &TraversalFlags) -> Self {
102 use std::mem;
103
104 // In the middle of an animation only restyle, we don't need to
105 // propagate any restyle hints, and we need to remove ourselves.
106 if traversal_flags.for_animation_only() {
107 self.remove_animation_hints();
108 return Self::empty();
109 }
110
111 debug_assert!(
112 !self.has_animation_hint(),
113 "There should not be any animation restyle hints \
114 during normal traversal"
115 );
116
117 // Else we should clear ourselves, and return the propagated hint.
118 mem::replace(self, Self::empty()).propagate_for_non_animation_restyle()
119 }
120
121 /// Returns a new `RestyleHint` appropriate for children of the current element.
122 fn propagate_for_non_animation_restyle(&self) -> Self {
123 if self.contains(RestyleHint::RESTYLE_DESCENDANTS) {
124 return Self::restyle_subtree();
125 }
126 let mut result = Self::empty();
127 if self.contains(Self::RESTYLE_PSEUDOS) {
128 result |= Self::RESTYLE_SELF_IF_PSEUDO;
129 }
130 if self.contains(Self::RECASCADE_DESCENDANTS) {
131 result |= Self::recascade_subtree();
132 }
133 if self.contains(Self::RESTYLE_IF_AFFECTED_BY_ANCESTOR_FONT) {
134 result |= Self::RESTYLE_IF_AFFECTED_BY_ANCESTOR_FONT;
135 }
136 if self.contains(Self::RESTYLE_IF_AFFECTED_BY_NAMED_STYLE_CONTAINER) {
137 // We may need to restyle further down the tree if rules are
138 // declared for a named container.
139 // e.g @container my-name {#b {...}}
140 // and <div id=a> <div> <div id=b> </div> </div> </div>
141 // If a toggles `container-name: my-name` the rules for #b
142 // also invalidate. This is why we need one hint for unnamed
143 // container and one for named containers.
144 result |= Self::RESTYLE_IF_AFFECTED_BY_NAMED_STYLE_CONTAINER;
145 }
146 result
147 }
148
149 /// Returns a hint that contains all the replacement hints.
150 pub fn replacements() -> Self {
151 RestyleHint::RESTYLE_STYLE_ATTRIBUTE | Self::for_animations()
152 }
153
154 /// The replacements for the animation cascade levels.
155 #[inline]
156 pub fn for_animations() -> Self {
157 RestyleHint::RESTYLE_SMIL
158 | RestyleHint::RESTYLE_CSS_ANIMATIONS
159 | RestyleHint::RESTYLE_CSS_TRANSITIONS
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(&self) -> bool {
166 self.intersects(Self::for_animations())
167 }
168
169 /// Returns whether the hint specifies that an animation cascade level must
170 /// be replaced.
171 #[inline]
172 pub fn has_animation_hint_or_recascade(&self) -> bool {
173 self.intersects(
174 Self::for_animations()
175 | Self::RECASCADE_SELF
176 | Self::RECASCADE_SELF_IF_INHERIT_RESET_STYLE,
177 )
178 }
179
180 /// Returns whether the hint specifies some restyle work other than an
181 /// animation cascade level replacement.
182 #[inline]
183 pub fn has_non_animation_hint(&self) -> bool {
184 !(*self & !Self::for_animations()).is_empty()
185 }
186
187 /// Returns whether the hint specifies that some cascade levels must be
188 /// replaced.
189 #[inline]
190 pub fn has_replacements(&self) -> bool {
191 self.intersects(Self::replacements())
192 }
193
194 /// Removes all of the animation-related hints.
195 #[inline]
196 pub fn remove_animation_hints(&mut self) {
197 self.remove(Self::for_animations());
198
199 // While RECASCADE_SELF is not animation-specific, we only ever add and process it during
200 // traversal. If we are here, removing animation hints, then we are in an animation-only
201 // traversal, and we know that any RECASCADE_SELF flag must have been set due to changes in
202 // inherited values after restyling for animations, and thus we want to remove it so that
203 // we don't later try to restyle the element during a normal restyle.
204 // (We could have separate RECASCADE_SELF_NORMAL and RECASCADE_SELF_ANIMATIONS flags to
205 // make it clear, but this isn't currently necessary.)
206 self.remove(Self::RECASCADE_SELF | Self::RECASCADE_SELF_IF_INHERIT_RESET_STYLE);
207 }
208}
209
210impl Default for RestyleHint {
211 fn default() -> Self {
212 Self::empty()
213 }
214}
215
216#[cfg(feature = "servo")]
217malloc_size_of::malloc_size_of_is_0!(RestyleHint);