Skip to content

Commit a397590

Browse files
heycamemilio
authored andcommitted
style: Refactor RestyleHint to be a struct.
Later PRs will add additional data to it that is not so easy to represent using bitflags.
1 parent c13be5c commit a397590

File tree

9 files changed

+294
-111
lines changed

9 files changed

+294
-111
lines changed

components/layout_thread/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ impl LayoutThread {
11001100
let el = node.as_element().unwrap();
11011101
if let Some(mut d) = element.mutate_data() {
11021102
if d.has_styles() {
1103-
d.ensure_restyle().hint.insert(&StoredRestyleHint::subtree());
1103+
d.ensure_restyle().hint.insert(StoredRestyleHint::subtree());
11041104
}
11051105
}
11061106
if let Some(p) = el.parent_element() {
@@ -1136,7 +1136,7 @@ impl LayoutThread {
11361136
if needs_dirtying {
11371137
if let Some(mut d) = element.mutate_data() {
11381138
if d.has_styles() {
1139-
d.ensure_restyle().hint.insert(&StoredRestyleHint::subtree());
1139+
d.ensure_restyle().hint.insert(StoredRestyleHint::subtree());
11401140
}
11411141
}
11421142
}
@@ -1184,7 +1184,7 @@ impl LayoutThread {
11841184
let mut restyle_data = style_data.ensure_restyle();
11851185

11861186
// Stash the data on the element for processing by the style system.
1187-
restyle_data.hint.insert(&restyle.hint.into());
1187+
restyle_data.hint.insert(restyle.hint.into());
11881188
restyle_data.damage = restyle.damage;
11891189
debug!("Noting restyle for {:?}: {:?}", el, restyle_data);
11901190
}

components/script/dom/document.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ use std::rc::Rc;
131131
use std::time::{Duration, Instant};
132132
use style::attr::AttrValue;
133133
use style::context::{QuirksMode, ReflowGoal};
134-
use style::restyle_hints::{RestyleHint, RESTYLE_SELF, RESTYLE_STYLE_ATTRIBUTE};
134+
use style::restyle_hints::{RestyleHint, RestyleReplacements, RESTYLE_STYLE_ATTRIBUTE};
135135
use style::selector_parser::{RestyleDamage, Snapshot};
136136
use style::shared_lock::SharedRwLock as StyleSharedRwLock;
137137
use style::str::{HTML_SPACE_CHARACTERS, split_html_space_chars, str_join};
@@ -2361,14 +2361,14 @@ impl Document {
23612361
entry.snapshot = Some(Snapshot::new(el.html_element_in_html_document()));
23622362
}
23632363
if attr.local_name() == &local_name!("style") {
2364-
entry.hint |= RESTYLE_STYLE_ATTRIBUTE;
2364+
entry.hint.insert(RestyleHint::for_replacements(RESTYLE_STYLE_ATTRIBUTE));
23652365
}
23662366

23672367
// FIXME(emilio): This should become something like
23682368
// element.is_attribute_mapped(attr.local_name()).
23692369
if attr.local_name() == &local_name!("width") ||
23702370
attr.local_name() == &local_name!("height") {
2371-
entry.hint |= RESTYLE_SELF;
2371+
entry.hint.insert(RestyleHint::for_self());
23722372
}
23732373

23742374
let mut snapshot = entry.snapshot.as_mut().unwrap();

components/script/dom/element.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ use style::context::{QuirksMode, ReflowGoal};
102102
use style::element_state::*;
103103
use style::properties::{Importance, PropertyDeclaration, PropertyDeclarationBlock, parse_style_attribute};
104104
use style::properties::longhands::{self, background_image, border_spacing, font_family, font_size, overflow_x};
105-
use style::restyle_hints::RESTYLE_SELF;
105+
use style::restyle_hints::RestyleHint;
106106
use style::rule_tree::CascadeLevel;
107107
use style::selector_parser::{NonTSPseudoClass, PseudoElement, RestyleDamage, SelectorImpl, SelectorParser};
108108
use style::shared_lock::{SharedRwLock, Locked};
@@ -245,7 +245,7 @@ impl Element {
245245

246246
// FIXME(bholley): I think we should probably only do this for
247247
// NodeStyleDamaged, but I'm preserving existing behavior.
248-
restyle.hint |= RESTYLE_SELF;
248+
restyle.hint.insert(RestyleHint::for_self());
249249

250250
if damage == NodeDamage::OtherNodeDamage {
251251
restyle.damage = RestyleDamage::rebuild_and_reflow();

components/style/data.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use context::SharedStyleContext;
1010
use dom::TElement;
1111
use properties::ComputedValues;
1212
use properties::longhands::display::computed_value as display;
13-
use restyle_hints::{RESTYLE_DESCENDANTS, RESTYLE_LATER_SIBLINGS, RESTYLE_SELF, RestyleHint};
13+
use restyle_hints::{RestyleReplacements, RestyleHint};
1414
use rule_tree::StrongRuleNode;
1515
use selector_parser::{EAGER_PSEUDO_COUNT, PseudoElement, RestyleDamage};
1616
use shared_lock::StylesheetGuards;
@@ -198,21 +198,18 @@ impl StoredRestyleHint {
198198
// In the middle of an animation only restyle, we don't need to
199199
// propagate any restyle hints, and we need to remove ourselves.
200200
if traversal_flags.for_animation_only() {
201-
self.0.remove(RestyleHint::for_animations());
201+
self.0.remove_animation_hints();
202202
return Self::empty();
203203
}
204204

205-
debug_assert!(!self.0.intersects(RestyleHint::for_animations()),
205+
debug_assert!(!self.0.has_animation_hint(),
206206
"There should not be any animation restyle hints \
207207
during normal traversal");
208208

209209
// Else we should clear ourselves, and return the propagated hint.
210-
let hint = mem::replace(&mut self.0, RestyleHint::empty());
211-
StoredRestyleHint(if hint.contains(RESTYLE_DESCENDANTS) {
212-
RESTYLE_SELF | RESTYLE_DESCENDANTS
213-
} else {
214-
RestyleHint::empty()
215-
})
210+
let new_hint = mem::replace(&mut self.0, RestyleHint::empty())
211+
.propagate_for_non_animation_restyle();
212+
StoredRestyleHint(new_hint)
216213
}
217214

218215
/// Creates an empty `StoredRestyleHint`.
@@ -223,25 +220,25 @@ impl StoredRestyleHint {
223220
/// Creates a restyle hint that forces the whole subtree to be restyled,
224221
/// including the element.
225222
pub fn subtree() -> Self {
226-
StoredRestyleHint(RESTYLE_SELF | RESTYLE_DESCENDANTS)
223+
StoredRestyleHint(RestyleHint::subtree())
227224
}
228225

229226
/// Creates a restyle hint that forces the element and all its later
230227
/// siblings to have their whole subtrees restyled, including the elements
231228
/// themselves.
232229
pub fn subtree_and_later_siblings() -> Self {
233-
StoredRestyleHint(RESTYLE_SELF | RESTYLE_DESCENDANTS | RESTYLE_LATER_SIBLINGS)
230+
StoredRestyleHint(RestyleHint::subtree_and_later_siblings())
234231
}
235232

236233
/// Returns true if the hint indicates that our style may be invalidated.
237234
pub fn has_self_invalidations(&self) -> bool {
238-
self.0.intersects(RestyleHint::for_self())
235+
self.0.affects_self()
239236
}
240237

241238
/// Returns true if the hint indicates that our sibling's style may be
242239
/// invalidated.
243240
pub fn has_sibling_invalidations(&self) -> bool {
244-
self.0.intersects(RESTYLE_LATER_SIBLINGS)
241+
self.0.affects_later_siblings()
245242
}
246243

247244
/// Whether the restyle hint is empty (nothing requires to be restyled).
@@ -250,13 +247,18 @@ impl StoredRestyleHint {
250247
}
251248

252249
/// Insert another restyle hint, effectively resulting in the union of both.
253-
pub fn insert(&mut self, other: &Self) {
254-
self.0 |= other.0
250+
pub fn insert(&mut self, other: Self) {
251+
self.0.insert(other.0)
252+
}
253+
254+
/// Insert another restyle hint, effectively resulting in the union of both.
255+
pub fn insert_from(&mut self, other: &Self) {
256+
self.0.insert_from(&other.0)
255257
}
256258

257259
/// Returns true if the hint has animation-only restyle.
258260
pub fn has_animation_hint(&self) -> bool {
259-
self.0.intersects(RestyleHint::for_animations())
261+
self.0.has_animation_hint()
260262
}
261263
}
262264

@@ -356,7 +358,7 @@ pub enum RestyleKind {
356358
MatchAndCascade,
357359
/// We need to recascade with some replacement rule, such as the style
358360
/// attribute, or animation rules.
359-
CascadeWithReplacements(RestyleHint),
361+
CascadeWithReplacements(RestyleReplacements),
360362
/// We only need to recascade, for example, because only inherited
361363
/// properties in the parent changed.
362364
CascadeOnly,
@@ -381,7 +383,7 @@ impl ElementData {
381383
context.traversal_flags);
382384

383385
let mut hint = match self.get_restyle() {
384-
Some(r) => r.hint.0,
386+
Some(r) => r.hint.0.clone(),
385387
None => RestyleHint::empty(),
386388
};
387389

@@ -393,7 +395,7 @@ impl ElementData {
393395
element.implemented_pseudo_element());
394396

395397
if element.has_snapshot() && !element.handled_snapshot() {
396-
hint |= context.stylist.compute_restyle_hint(&element, context.snapshot_map);
398+
hint.insert(context.stylist.compute_restyle_hint(&element, context.snapshot_map));
397399
unsafe { element.set_handled_snapshot() }
398400
debug_assert!(element.handled_snapshot());
399401
}
@@ -402,8 +404,7 @@ impl ElementData {
402404

403405
// If the hint includes a directive for later siblings, strip it out and
404406
// notify the caller to modify the base hint for future siblings.
405-
let later_siblings = hint.contains(RESTYLE_LATER_SIBLINGS);
406-
hint.remove(RESTYLE_LATER_SIBLINGS);
407+
let later_siblings = hint.remove_later_siblings_hint();
407408

408409
// Insert the hint, overriding the previous hint. This effectively takes
409410
// care of removing the later siblings restyle hint.
@@ -445,13 +446,13 @@ impl ElementData {
445446
debug_assert!(self.restyle.is_some());
446447
let restyle_data = self.restyle.as_ref().unwrap();
447448

448-
let hint = restyle_data.hint.0;
449-
if hint.contains(RESTYLE_SELF) {
449+
let hint = &restyle_data.hint.0;
450+
if hint.match_self() {
450451
return RestyleKind::MatchAndCascade;
451452
}
452453

453454
if !hint.is_empty() {
454-
return RestyleKind::CascadeWithReplacements(hint);
455+
return RestyleKind::CascadeWithReplacements(hint.replacements);
455456
}
456457

457458
debug_assert!(restyle_data.recascade,

components/style/matching.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use font_metrics::FontMetricsProvider;
1919
use log::LogLevel::Trace;
2020
use properties::{CascadeFlags, ComputedValues, SKIP_ROOT_AND_ITEM_BASED_DISPLAY_FIXUP, cascade};
2121
use properties::longhands::display::computed_value as display;
22-
use restyle_hints::{RESTYLE_CSS_ANIMATIONS, RESTYLE_CSS_TRANSITIONS, RestyleHint};
22+
use restyle_hints::{RESTYLE_CSS_ANIMATIONS, RESTYLE_CSS_TRANSITIONS, RestyleReplacements};
2323
use restyle_hints::{RESTYLE_STYLE_ATTRIBUTE, RESTYLE_SMIL};
2424
use rule_tree::{CascadeLevel, RuleTree, StrongRuleNode};
2525
use selector_parser::{PseudoElement, RestyleDamage, SelectorImpl};
@@ -1327,7 +1327,7 @@ pub trait MatchMethods : TElement {
13271327
/// the rule tree. Returns RulesChanged which indicates whether the rule nodes changed
13281328
/// and whether the important rules changed.
13291329
fn replace_rules(&self,
1330-
hint: RestyleHint,
1330+
replacements: RestyleReplacements,
13311331
context: &StyleContext<Self>,
13321332
data: &mut AtomicRefMut<ElementData>)
13331333
-> RulesChanged {
@@ -1359,10 +1359,10 @@ pub trait MatchMethods : TElement {
13591359
//
13601360
// Non-animation restyle hints will be processed in a subsequent
13611361
// normal traversal.
1362-
if hint.intersects(RestyleHint::for_animations()) {
1362+
if replacements.intersects(RestyleReplacements::for_animations()) {
13631363
debug_assert!(context.shared.traversal_flags.for_animation_only());
13641364

1365-
if hint.contains(RESTYLE_SMIL) {
1365+
if replacements.contains(RESTYLE_SMIL) {
13661366
replace_rule_node(CascadeLevel::SMILOverride,
13671367
self.get_smil_override(),
13681368
primary_rules);
@@ -1378,16 +1378,16 @@ pub trait MatchMethods : TElement {
13781378

13791379
// Apply Transition rules and Animation rules if the corresponding restyle hint
13801380
// is contained.
1381-
if hint.contains(RESTYLE_CSS_TRANSITIONS) {
1381+
if replacements.contains(RESTYLE_CSS_TRANSITIONS) {
13821382
replace_rule_node_for_animation(CascadeLevel::Transitions,
13831383
primary_rules);
13841384
}
13851385

1386-
if hint.contains(RESTYLE_CSS_ANIMATIONS) {
1386+
if replacements.contains(RESTYLE_CSS_ANIMATIONS) {
13871387
replace_rule_node_for_animation(CascadeLevel::Animations,
13881388
primary_rules);
13891389
}
1390-
} else if hint.contains(RESTYLE_STYLE_ATTRIBUTE) {
1390+
} else if replacements.contains(RESTYLE_STYLE_ATTRIBUTE) {
13911391
let style_attribute = self.style_attribute();
13921392
replace_rule_node(CascadeLevel::StyleAttributeNormal,
13931393
style_attribute,

0 commit comments

Comments
 (0)