style: Rewrite restyling to split between resolving styles and handling changes.

MozReview-Commit-ID: 4BzjbLbFebF
This commit is contained in:
Emilio Cobos Álvarez 2017-07-12 08:40:23 +02:00
parent 0ad2d39c30
commit c6d5dbbb01
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
8 changed files with 485 additions and 1500 deletions

View file

@ -7,7 +7,6 @@
#[cfg(feature = "servo")] use animation::Animation;
use animation::PropertyAnimation;
use app_units::Au;
use arrayvec::ArrayVec;
use bloom::StyleBloom;
use cache::LRUCache;
use data::{EagerPseudoStyles, ElementData};
@ -19,8 +18,8 @@ use font_metrics::FontMetricsProvider;
#[cfg(feature = "servo")] use parking_lot::RwLock;
use properties::ComputedValues;
use rule_tree::StrongRuleNode;
use selector_parser::{EAGER_PSEUDO_COUNT, PseudoElement, SnapshotMap};
use selectors::matching::{ElementSelectorFlags, VisitedHandlingMode};
use selector_parser::{EAGER_PSEUDO_COUNT, SnapshotMap};
use selectors::matching::ElementSelectorFlags;
use shared_lock::StylesheetGuards;
use sharing::{ValidationData, StyleSharingCandidateCache};
use std::fmt;
@ -162,36 +161,17 @@ impl<'a> SharedStyleContext<'a> {
/// within the `CurrentElementInfo`. At the end of the cascade, they are folded
/// down into the main `ComputedValues` to reduce memory usage per element while
/// still remaining accessible.
#[derive(Clone)]
#[derive(Clone, Default)]
pub struct CascadeInputs {
/// The rule node representing the ordered list of rules matched for this
/// node.
rules: Option<StrongRuleNode>,
pub rules: Option<StrongRuleNode>,
/// The rule node representing the ordered list of rules matched for this
/// node if visited, only computed if there's a relevant link for this
/// element. A element's "relevant link" is the element being matched if it
/// is a link or the nearest ancestor link.
visited_rules: Option<StrongRuleNode>,
/// The element's computed values if visited, only computed if there's a
/// relevant link for this element. A element's "relevant link" is the
/// element being matched if it is a link or the nearest ancestor link.
///
/// We also store a reference to this inside the regular ComputedValues to
/// avoid refactoring all APIs to become aware of multiple ComputedValues
/// objects.
visited_values: Option<Arc<ComputedValues>>,
}
impl Default for CascadeInputs {
fn default() -> Self {
CascadeInputs {
rules: None,
visited_rules: None,
visited_values: None,
}
}
pub visited_rules: Option<StrongRuleNode>,
}
impl CascadeInputs {
@ -200,125 +180,8 @@ impl CascadeInputs {
CascadeInputs {
rules: style.rules.clone(),
visited_rules: style.get_visited_style().and_then(|v| v.rules.clone()),
// Values will be re-cascaded if necessary, so this can be None.
visited_values: None,
}
}
/// Whether there are any rules. Rules will be present after unvisited
/// matching or pulled from a previous cascade if no matching is expected.
pub fn has_rules(&self) -> bool {
self.rules.is_some()
}
/// Gets a mutable reference to the rule node, if any.
pub fn get_rules_mut(&mut self) -> Option<&mut StrongRuleNode> {
self.rules.as_mut()
}
/// Gets a reference to the rule node, if any.
pub fn get_rules(&self) -> Option<&StrongRuleNode> {
self.rules.as_ref()
}
/// Gets a reference to the rule node. Panic if the element does not have
/// rule node.
pub fn rules(&self) -> &StrongRuleNode {
self.rules.as_ref().unwrap()
}
/// Sets the rule node depending on visited mode.
/// Returns whether the rules changed.
pub fn set_rules(&mut self,
visited_handling: VisitedHandlingMode,
rules: StrongRuleNode)
-> bool {
match visited_handling {
VisitedHandlingMode::AllLinksVisitedAndUnvisited => {
unreachable!("We should never try to selector match with \
AllLinksVisitedAndUnvisited");
},
VisitedHandlingMode::AllLinksUnvisited => self.set_unvisited_rules(rules),
VisitedHandlingMode::RelevantLinkVisited => self.set_visited_rules(rules),
}
}
/// Sets the unvisited rule node, and returns whether it changed.
fn set_unvisited_rules(&mut self, rules: StrongRuleNode) -> bool {
if let Some(ref old_rules) = self.rules {
if *old_rules == rules {
return false
}
}
self.rules = Some(rules);
true
}
/// Whether there are any visited rules. Visited rules will be present
/// after visited matching or pulled from a previous cascade (assuming there
/// was a relevant link at the time) if no matching is expected.
pub fn has_visited_rules(&self) -> bool {
self.visited_rules.is_some()
}
/// Gets a reference to the visited rule node, if any.
pub fn get_visited_rules(&self) -> Option<&StrongRuleNode> {
self.visited_rules.as_ref()
}
/// Gets a mutable reference to the visited rule node, if any.
pub fn get_visited_rules_mut(&mut self) -> Option<&mut StrongRuleNode> {
self.visited_rules.as_mut()
}
/// Gets a reference to the visited rule node. Panic if the element does not
/// have visited rule node.
pub fn visited_rules(&self) -> &StrongRuleNode {
self.visited_rules.as_ref().unwrap()
}
/// Sets the visited rule node, and returns whether it changed.
fn set_visited_rules(&mut self, rules: StrongRuleNode) -> bool {
if let Some(ref old_rules) = self.visited_rules {
if *old_rules == rules {
return false
}
}
self.visited_rules = Some(rules);
true
}
/// Takes the visited rule node.
pub fn take_visited_rules(&mut self) -> Option<StrongRuleNode> {
self.visited_rules.take()
}
/// Whether there are any visited values.
pub fn has_visited_values(&self) -> bool {
self.visited_values.is_some()
}
/// Gets a reference to the visited computed values. Panic if the element
/// does not have visited computed values.
pub fn visited_values(&self) -> &Arc<ComputedValues> {
self.visited_values.as_ref().unwrap()
}
/// Sets the visited computed values.
pub fn set_visited_values(&mut self, values: Arc<ComputedValues>) {
self.visited_values = Some(values);
}
/// Take the visited computed values.
pub fn take_visited_values(&mut self) -> Option<Arc<ComputedValues>> {
self.visited_values.take()
}
/// Clone the visited computed values Arc. Used to store a reference to the
/// visited values inside the regular values.
pub fn clone_visited_values(&self) -> Option<Arc<ComputedValues>> {
self.visited_values.clone()
}
}
// We manually implement Debug for CascadeInputs so that we can avoid the
@ -363,160 +226,9 @@ impl EagerPseudoCascadeInputs {
}))
}
/// Returns whether there are any pseudo inputs.
pub fn is_empty(&self) -> bool {
self.0.is_none()
}
/// Returns a reference to the inputs for a given eager pseudo, if they exist.
pub fn get(&self, pseudo: &PseudoElement) -> Option<&CascadeInputs> {
debug_assert!(pseudo.is_eager());
self.0.as_ref().and_then(|p| p[pseudo.eager_index()].as_ref())
}
/// Returns a mutable reference to the inputs for a given eager pseudo, if they exist.
pub fn get_mut(&mut self, pseudo: &PseudoElement) -> Option<&mut CascadeInputs> {
debug_assert!(pseudo.is_eager());
self.0.as_mut().and_then(|p| p[pseudo.eager_index()].as_mut())
}
/// Returns true if the EagerPseudoCascadeInputs has a inputs for |pseudo|.
pub fn has(&self, pseudo: &PseudoElement) -> bool {
self.get(pseudo).is_some()
}
/// Inserts a pseudo-element. The pseudo-element must not already exist.
pub fn insert(&mut self, pseudo: &PseudoElement, inputs: CascadeInputs) {
debug_assert!(!self.has(pseudo));
if self.0.is_none() {
self.0 = Some(Default::default());
}
self.0.as_mut().unwrap()[pseudo.eager_index()] = Some(inputs);
}
/// Removes a pseudo-element inputs if they exist, and returns it.
pub fn take(&mut self, pseudo: &PseudoElement) -> Option<CascadeInputs> {
let result = match self.0.as_mut() {
None => return None,
Some(arr) => arr[pseudo.eager_index()].take(),
};
let empty = self.0.as_ref().unwrap().iter().all(|x| x.is_none());
if empty {
self.0 = None;
}
result
}
/// Returns a list of the pseudo-elements.
pub fn keys(&self) -> ArrayVec<[PseudoElement; EAGER_PSEUDO_COUNT]> {
let mut v = ArrayVec::new();
if let Some(ref arr) = self.0 {
for i in 0..EAGER_PSEUDO_COUNT {
if arr[i].is_some() {
v.push(PseudoElement::from_eager_index(i));
}
}
}
v
}
/// Adds the unvisited rule node for a given pseudo-element, which may or
/// may not exist.
///
/// Returns true if the pseudo-element is new.
fn add_unvisited_rules(&mut self,
pseudo: &PseudoElement,
rules: StrongRuleNode)
-> bool {
if let Some(mut inputs) = self.get_mut(pseudo) {
inputs.set_unvisited_rules(rules);
return false
}
let mut inputs = CascadeInputs::default();
inputs.set_unvisited_rules(rules);
self.insert(pseudo, inputs);
true
}
/// Remove the unvisited rule node for a given pseudo-element, which may or
/// may not exist. Since removing the rule node implies we don't need any
/// other data for the pseudo, take the entire pseudo if found.
///
/// Returns true if the pseudo-element was removed.
fn remove_unvisited_rules(&mut self, pseudo: &PseudoElement) -> bool {
self.take(pseudo).is_some()
}
/// Adds the visited rule node for a given pseudo-element. It is assumed to
/// already exist because unvisited inputs should have been added first.
///
/// Returns true if the pseudo-element is new. (Always false, but returns a
/// bool for parity with `add_unvisited_rules`.)
fn add_visited_rules(&mut self,
pseudo: &PseudoElement,
rules: StrongRuleNode)
-> bool {
debug_assert!(self.has(pseudo));
let mut inputs = self.get_mut(pseudo).unwrap();
inputs.set_visited_rules(rules);
false
}
/// Remove the visited rule node for a given pseudo-element, which may or
/// may not exist.
///
/// Returns true if the psuedo-element was removed. (Always false, but
/// returns a bool for parity with `remove_unvisited_rules`.)
fn remove_visited_rules(&mut self, pseudo: &PseudoElement) -> bool {
if let Some(mut inputs) = self.get_mut(pseudo) {
inputs.take_visited_rules();
}
false
}
/// Adds a rule node for a given pseudo-element, which may or may not exist.
/// The type of rule node depends on the visited mode.
///
/// Returns true if the pseudo-element is new.
pub fn add_rules(&mut self,
pseudo: &PseudoElement,
visited_handling: VisitedHandlingMode,
rules: StrongRuleNode)
-> bool {
match visited_handling {
VisitedHandlingMode::AllLinksVisitedAndUnvisited => {
unreachable!("We should never try to selector match with \
AllLinksVisitedAndUnvisited");
},
VisitedHandlingMode::AllLinksUnvisited => {
self.add_unvisited_rules(&pseudo, rules)
},
VisitedHandlingMode::RelevantLinkVisited => {
self.add_visited_rules(&pseudo, rules)
},
}
}
/// Removes a rule node for a given pseudo-element, which may or may not
/// exist. The type of rule node depends on the visited mode.
///
/// Returns true if the psuedo-element was removed.
pub fn remove_rules(&mut self,
pseudo: &PseudoElement,
visited_handling: VisitedHandlingMode)
-> bool {
match visited_handling {
VisitedHandlingMode::AllLinksVisitedAndUnvisited => {
unreachable!("We should never try to selector match with \
AllLinksVisitedAndUnvisited");
},
VisitedHandlingMode::AllLinksUnvisited => {
self.remove_unvisited_rules(&pseudo)
},
VisitedHandlingMode::RelevantLinkVisited => {
self.remove_visited_rules(&pseudo)
},
}
/// Returns the list of rules, if they exist.
pub fn into_array(self) -> Option<[Option<CascadeInputs>; EAGER_PSEUDO_COUNT]> {
self.0
}
}
@ -530,56 +242,20 @@ impl EagerPseudoCascadeInputs {
#[derive(Clone, Debug)]
pub struct ElementCascadeInputs {
/// The element's cascade inputs.
pub primary: Option<CascadeInputs>,
pub primary: CascadeInputs,
/// A list of the inputs for the element's eagerly-cascaded pseudo-elements.
pub pseudos: EagerPseudoCascadeInputs,
}
impl Default for ElementCascadeInputs {
/// Construct an empty `ElementCascadeInputs`.
fn default() -> Self {
ElementCascadeInputs {
primary: None,
pseudos: EagerPseudoCascadeInputs(None),
}
}
}
impl ElementCascadeInputs {
/// Construct inputs from previous cascade results, if any.
pub fn new_from_element_data(data: &ElementData) -> Self {
if !data.has_styles() {
return ElementCascadeInputs::default()
}
debug_assert!(data.has_styles());
ElementCascadeInputs {
primary: Some(CascadeInputs::new_from_style(data.styles.primary())),
primary: CascadeInputs::new_from_style(data.styles.primary()),
pseudos: EagerPseudoCascadeInputs::new_from_style(&data.styles.pseudos),
}
}
/// Returns whether we have primary inputs.
pub fn has_primary(&self) -> bool {
self.primary.is_some()
}
/// Gets the primary inputs. Panic if unavailable.
pub fn primary(&self) -> &CascadeInputs {
self.primary.as_ref().unwrap()
}
/// Gets the mutable primary inputs. Panic if unavailable.
pub fn primary_mut(&mut self) -> &mut CascadeInputs {
self.primary.as_mut().unwrap()
}
/// Ensure primary inputs exist and create them if they do not.
/// Returns a mutable reference to the primary inputs.
pub fn ensure_primary(&mut self) -> &mut CascadeInputs {
if self.primary.is_none() {
self.primary = Some(CascadeInputs::default());
}
self.primary.as_mut().unwrap()
}
}
/// Information about the current element being processed. We group this
@ -598,11 +274,6 @@ pub struct CurrentElementInfo {
/// A Vec of possibly expired animations. Used only by Servo.
#[allow(dead_code)]
pub possibly_expired_animations: Vec<PropertyAnimation>,
/// Temporary storage for various intermediate inputs that are eventually
/// used by by the cascade. At the end of the cascade, they are folded down
/// into the main `ComputedValues` to reduce memory usage per element while
/// still remaining accessible.
pub cascade_inputs: ElementCascadeInputs,
}
/// Statistics gathered during the traversal. We gather statistics on each
@ -906,7 +577,6 @@ impl<E: TElement> ThreadLocalStyleContext<E> {
is_initial_style: !data.has_styles(),
validation_data: ValidationData::default(),
possibly_expired_animations: Vec::new(),
cascade_inputs: ElementCascadeInputs::default(),
});
}
@ -951,24 +621,6 @@ pub struct StyleContext<'a, E: TElement + 'a> {
pub thread_local: &'a mut ThreadLocalStyleContext<E>,
}
impl<'a, E: TElement + 'a> StyleContext<'a, E> {
/// Returns a reference to the cascade inputs. Panics if there is no
/// `CurrentElementInfo`.
pub fn cascade_inputs(&self) -> &ElementCascadeInputs {
&self.thread_local.current_element_info
.as_ref().unwrap()
.cascade_inputs
}
/// Returns a mutable reference to the cascade inputs. Panics if there is
/// no `CurrentElementInfo`.
pub fn cascade_inputs_mut(&mut self) -> &mut ElementCascadeInputs {
&mut self.thread_local.current_element_info
.as_mut().unwrap()
.cascade_inputs
}
}
/// Why we're doing reflow.
#[derive(PartialEq, Copy, Clone, Debug)]
pub enum ReflowGoal {

File diff suppressed because it is too large Load diff

View file

@ -2576,11 +2576,11 @@ pub fn cascade(device: &Device,
flags: CascadeFlags,
quirks_mode: QuirksMode)
-> ComputedValues {
debug_assert_eq!(parent_style.is_some(), layout_parent_style.is_some());
debug_assert!(layout_parent_style.is_none() || parent_style.is_some());
let (inherited_style, layout_parent_style) = match parent_style {
Some(parent_style) => {
(parent_style,
layout_parent_style.unwrap())
layout_parent_style.unwrap_or(parent_style))
},
None => {
(device.default_computed_values(),

View file

@ -75,7 +75,7 @@ use dom::{TElement, SendElement};
use matching::{ChildCascadeRequirement, MatchMethods};
use properties::ComputedValues;
use selector_parser::RestyleDamage;
use selectors::matching::{ElementSelectorFlags, VisitedHandlingMode, StyleRelations};
use selectors::matching::{ElementSelectorFlags, VisitedHandlingMode};
use smallvec::SmallVec;
use std::mem;
use std::ops::Deref;
@ -494,11 +494,8 @@ impl<E: TElement> StyleSharingCandidateCache<E> {
pub fn insert_if_possible(&mut self,
element: &E,
style: &ComputedValues,
relations: StyleRelations,
mut validation_data: ValidationData,
validation_data: ValidationData,
dom_depth: usize) {
use selectors::matching::AFFECTED_BY_PRESENTATIONAL_HINTS;
let parent = match element.traversal_parent() {
Some(element) => element,
None => {
@ -525,13 +522,6 @@ impl<E: TElement> StyleSharingCandidateCache<E> {
return;
}
// Take advantage of the information we've learned during
// selector-matching.
if !relations.intersects(AFFECTED_BY_PRESENTATIONAL_HINTS) {
debug_assert!(validation_data.pres_hints.as_ref().map_or(true, |v| v.is_empty()));
validation_data.pres_hints = Some(SmallVec::new());
}
debug!("Inserting into cache: {:?} with parent {:?}", element, parent);
if self.dom_depth != dom_depth {

View file

@ -6,7 +6,7 @@
use applicable_declarations::ApplicableDeclarationList;
use cascade_info::CascadeInfo;
use context::StyleContext;
use context::{CascadeInputs, ElementCascadeInputs, StyleContext};
use data::{ElementStyles, EagerPseudoStyles};
use dom::TElement;
use log::LogLevel::Trace;
@ -42,11 +42,37 @@ struct MatchingResults {
pub struct PrimaryStyle {
/// The style per se.
pub style: Arc<ComputedValues>,
}
/// Whether a relevant link was found while computing this style.
///
/// FIXME(emilio): Slightly out of place?
pub relevant_link_found: bool,
fn with_default_parent_styles<E, F, R>(element: E, f: F) -> R
where
E: TElement,
F: FnOnce(Option<&ComputedValues>, Option<&ComputedValues>) -> R,
{
let parent_el = element.inheritance_parent();
let parent_data = parent_el.as_ref().and_then(|e| e.borrow_data());
let parent_style = parent_data.as_ref().map(|d| {
// Sometimes Gecko eagerly styles things without processing
// pending restyles first. In general we'd like to avoid this,
// but there can be good reasons (for example, needing to
// construct a frame for some small piece of newly-added
// content in order to do something specific with that frame,
// but not wanting to flush all of layout).
debug_assert!(cfg!(feature = "gecko") ||
parent_el.unwrap().has_current_styles(d));
d.styles.primary()
});
let mut layout_parent_el = parent_el.clone();
let layout_parent_data;
let mut layout_parent_style = parent_style;
if parent_style.map_or(false, |s| s.is_display_contents()) {
layout_parent_el = Some(layout_parent_el.unwrap().layout_parent());
layout_parent_data = layout_parent_el.as_ref().unwrap().borrow_data().unwrap();
layout_parent_style = Some(layout_parent_data.styles.primary());
}
f(parent_style.map(|s| &**s), layout_parent_style.map(|s| &**s))
}
impl<'a, 'ctx, 'le, E> StyleResolverForElement<'a, 'ctx, 'le, E>
@ -95,7 +121,7 @@ where
if should_compute_visited_style {
visited_style = Some(self.cascade_style(
visited_rules.as_ref().unwrap_or(&primary_results.rule_node),
visited_rules.as_ref(),
/* style_if_visited = */ None,
parent_style,
layout_parent_style,
@ -105,7 +131,7 @@ where
}
let style = self.cascade_style(
&primary_results.rule_node,
Some(&primary_results.rule_node),
visited_style,
parent_style,
layout_parent_style,
@ -113,7 +139,7 @@ where
/* pseudo = */ None,
);
PrimaryStyle { style, relevant_link_found, }
PrimaryStyle { style, }
}
@ -137,7 +163,7 @@ where
}
}
{
if self.element.implemented_pseudo_element().is_none() {
let layout_parent_style_for_pseudo =
if primary_style.style.is_display_contents() {
layout_parent_style
@ -163,6 +189,115 @@ where
}
}
/// Resolve an element's styles with the default inheritance parent/layout
/// parents.
pub fn resolve_style_with_default_parents(&mut self) -> ElementStyles {
with_default_parent_styles(self.element, |parent_style, layout_parent_style| {
self.resolve_style(parent_style, layout_parent_style)
})
}
/// Cascade a set of rules, using the default parent for inheritance.
pub fn cascade_style_and_visited_with_default_parents(
&mut self,
inputs: CascadeInputs,
) -> Arc<ComputedValues> {
with_default_parent_styles(self.element, |parent_style, layout_parent_style| {
self.cascade_style_and_visited(
inputs,
parent_style,
layout_parent_style,
/* pseudo = */ None
)
})
}
fn cascade_style_and_visited(
&mut self,
inputs: CascadeInputs,
parent_style: Option<&ComputedValues>,
layout_parent_style: Option<&ComputedValues>,
pseudo: Option<&PseudoElement>,
) -> Arc<ComputedValues> {
let mut style_if_visited = None;
if parent_style.map_or(false, |s| s.get_visited_style().is_some()) ||
inputs.visited_rules.is_some() {
style_if_visited = Some(self.cascade_style(
inputs.visited_rules.as_ref(),
/* style_if_visited = */ None,
parent_style,
layout_parent_style,
CascadeVisitedMode::Visited,
pseudo,
));
}
self.cascade_style(
inputs.rules.as_ref(),
style_if_visited,
parent_style,
layout_parent_style,
CascadeVisitedMode::Unvisited,
pseudo,
)
}
/// Cascade the element and pseudo-element styles with the default parents.
pub fn cascade_styles_with_default_parents(
&mut self,
inputs: ElementCascadeInputs,
) -> ElementStyles {
use properties::longhands::display::computed_value::T as display;
with_default_parent_styles(self.element, move |parent_style, layout_parent_style| {
let primary_style = PrimaryStyle {
style: self.cascade_style_and_visited(
inputs.primary,
parent_style,
layout_parent_style,
/* pseudo = */ None,
),
};
let mut pseudo_styles = EagerPseudoStyles::default();
let pseudo_array = inputs.pseudos.into_array();
if pseudo_array.is_none() ||
primary_style.style.get_box().clone_display() == display::none {
return ElementStyles {
primary: Some(primary_style.style),
pseudos: pseudo_styles,
}
}
{
let layout_parent_style_for_pseudo =
if primary_style.style.is_display_contents() {
layout_parent_style
} else {
Some(&*primary_style.style)
};
for (i, mut inputs) in pseudo_array.unwrap().iter_mut().enumerate() {
if let Some(inputs) = inputs.take() {
let pseudo = PseudoElement::from_eager_index(i);
pseudo_styles.set(
&pseudo,
self.cascade_style_and_visited(
inputs,
Some(&*primary_style.style),
layout_parent_style_for_pseudo,
Some(&pseudo),
)
)
}
}
}
ElementStyles {
primary: Some(primary_style.style),
pseudos: pseudo_styles,
}
})
}
fn resolve_pseudo_style(
&mut self,
pseudo: &PseudoElement,
@ -179,33 +314,23 @@ where
None => return None,
};
let mut visited_style = None;
if originating_element_style.relevant_link_found {
let visited_rules = self.match_pseudo(
let mut visited_rules = None;
if originating_element_style.style.get_visited_style().is_some() {
visited_rules = self.match_pseudo(
&originating_element_style.style,
pseudo,
VisitedHandlingMode::RelevantLinkVisited,
);
if let Some(ref rules) = visited_rules {
visited_style = Some(self.cascade_style(
rules,
/* style_if_visited = */ None,
Some(&originating_element_style.style),
layout_parent_style,
CascadeVisitedMode::Visited,
Some(pseudo),
));
}
}
Some(self.cascade_style(
&rules,
visited_style,
Some(self.cascade_style_and_visited(
CascadeInputs {
rules: Some(rules),
visited_rules
},
Some(&originating_element_style.style),
layout_parent_style,
CascadeVisitedMode::Unvisited,
Some(pseudo)
Some(pseudo),
))
}
@ -335,9 +460,9 @@ where
fn cascade_style(
&mut self,
rules: &StrongRuleNode,
rules: Option<&StrongRuleNode>,
style_if_visited: Option<Arc<ComputedValues>>,
parent_style: Option<&ComputedValues>,
mut parent_style: Option<&ComputedValues>,
layout_parent_style: Option<&ComputedValues>,
cascade_visited: CascadeVisitedMode,
pseudo: Option<&PseudoElement>,
@ -349,6 +474,9 @@ where
cascade_flags.insert(SKIP_ROOT_AND_ITEM_BASED_DISPLAY_FIXUP);
}
if cascade_visited.visited_dependent_only() {
parent_style = parent_style.map(|s| {
s.get_visited_style().map(|s| &**s).unwrap_or(s)
});
cascade_flags.insert(VISITED_DEPENDENT_ONLY);
}
if self.element.is_native_anonymous() || pseudo.is_some() {
@ -360,13 +488,12 @@ where
let values =
Arc::new(cascade(
self.context.shared.stylist.device(),
rules,
rules.unwrap_or(self.context.shared.stylist.rule_tree().root()),
&self.context.shared.guards,
parent_style,
layout_parent_style,
style_if_visited,
Some(&mut cascade_info),
&*self.context.shared.error_reporter,
&self.context.thread_local.font_metrics_provider,
cascade_flags,
self.context.shared.quirks_mode

View file

@ -15,7 +15,6 @@ use font_metrics::FontMetricsProvider;
use gecko_bindings::structs::{nsIAtom, StyleRuleInclusion};
use invalidation::element::invalidation_map::InvalidationMap;
use invalidation::media_queries::{EffectiveMediaQueryResults, ToMediaListKey};
use matching::CascadeVisitedMode;
use media_queries::Device;
use properties::{self, CascadeFlags, ComputedValues};
use properties::{AnimationRules, PropertyDeclarationBlock};
@ -719,24 +718,30 @@ impl Stylist {
{
// We may have only visited rules in cases when we are actually
// resolving, not probing, pseudo-element style.
if !inputs.has_rules() && !inputs.has_visited_rules() {
if inputs.rules.is_none() && inputs.visited_rules.is_none() {
return None
}
// We need to compute visited values if we have visited rules or if our
// parent has visited values.
let visited_values = if inputs.has_visited_rules() || parent_style.get_visited_style().is_some() {
let visited_values = if inputs.visited_rules.is_some() || parent_style.get_visited_style().is_some() {
// Slightly annoying: we know that inputs has either rules or
// visited rules, but we can't do inputs.rules() up front because
// maybe it just has visited rules, so can't unwrap_or.
let rule_node = match inputs.get_visited_rules() {
let rule_node = match inputs.visited_rules.as_ref() {
Some(rules) => rules,
None => inputs.rules()
None => inputs.rules.as_ref().unwrap(),
};
// We want to use the visited bits (if any) from our parent style as
// our parent.
let mode = CascadeVisitedMode::Visited;
let inherited_style = mode.values(parent_style);
let inherited_style =
parent_style.get_visited_style().unwrap_or(&*parent_style);
// FIXME(emilio): The lack of layout_parent_style here could be
// worrying, but we're probably dropping the display fixup for
// pseudos other than before and after, so it's probably ok.
//
// (Though the flags don't indicate so!)
let computed =
properties::cascade(&self.device,
rule_node,
@ -756,7 +761,7 @@ impl Stylist {
// We may not have non-visited rules, if we only had visited ones. In
// that case we want to use the root rulenode for our non-visited rules.
let rules = inputs.get_rules().unwrap_or(self.rule_tree.root());
let rules = inputs.rules.as_ref().unwrap_or(self.rule_tree.root());
// Read the comment on `precomputed_values_for_pseudo` to see why it's
// difficult to assert that display: contents nodes never arrive here
@ -849,10 +854,10 @@ impl Stylist {
let rule_node =
self.rule_tree.compute_rule_node(&mut declarations, guards);
debug_assert!(rule_node != *self.rule_tree.root());
inputs.set_rules(VisitedHandlingMode::AllLinksUnvisited, rule_node);
inputs.rules = Some(rule_node);
}
if is_probe && !inputs.has_rules() {
if is_probe && inputs.rules.is_none() {
// When probing, don't compute visited styles if we have no
// unvisited styles.
return inputs;
@ -880,8 +885,7 @@ impl Stylist {
declarations.into_iter().map(|a| a.order_and_level()),
guards);
if rule_node != *self.rule_tree.root() {
inputs.set_rules(VisitedHandlingMode::RelevantLinkVisited,
rule_node);
inputs.visited_rules = Some(rule_node);
}
}
}

View file

@ -10,8 +10,9 @@ use data::{ElementData, ElementStyles};
use dom::{NodeInfo, OpaqueNode, TElement, TNode};
use invalidation::element::restyle_hints::{RECASCADE_SELF, RECASCADE_DESCENDANTS, RestyleHint};
use matching::{ChildCascadeRequirement, MatchMethods};
use sharing::{StyleSharingBehavior, StyleSharingTarget};
use sharing::StyleSharingTarget;
use smallvec::SmallVec;
use style_resolver::StyleResolverForElement;
use stylist::RuleInclusion;
/// A per-traversal-level chunk of data. This is sent down by the traversal, and
@ -765,7 +766,8 @@ where
data.restyle.set_restyled();
}
match kind {
let mut important_rules_changed = false;
let new_styles = match kind {
MatchAndCascade => {
debug_assert!(!context.shared.traversal_flags.for_animation_only(),
"MatchAndCascade shouldn't be processed during \
@ -790,35 +792,62 @@ where
context.thread_local.statistics.elements_matched += 1;
important_rules_changed = true;
// Perform the matching and cascading.
element.match_and_cascade(
context,
data,
StyleSharingBehavior::Allow
)
let new_styles =
StyleResolverForElement::new(element, context, RuleInclusion::All)
.resolve_style_with_default_parents();
// If we previously tried to match this element against the cache,
// the revalidation match results will already be cached. Otherwise
// we'll have None, and compute them later on-demand.
//
// If we do have the results, grab them here to satisfy the borrow
// checker.
let validation_data =
context.thread_local
.current_element_info
.as_mut().unwrap()
.validation_data
.take();
let dom_depth = context.thread_local.bloom_filter.matching_depth();
context.thread_local
.style_sharing_candidate_cache
.insert_if_possible(
&element,
new_styles.primary(),
validation_data,
dom_depth
);
new_styles
}
CascadeWithReplacements(flags) => {
// Skipping full matching, load cascade inputs from previous values.
*context.cascade_inputs_mut() =
let mut cascade_inputs =
ElementCascadeInputs::new_from_element_data(data);
let important_rules_changed = element.replace_rules(flags, context);
element.cascade_primary_and_pseudos(
context,
data,
important_rules_changed
)
important_rules_changed =
element.replace_rules(flags, context, &mut cascade_inputs);
StyleResolverForElement::new(element, context, RuleInclusion::All)
.cascade_styles_with_default_parents(cascade_inputs)
}
CascadeOnly => {
// Skipping full matching, load cascade inputs from previous values.
*context.cascade_inputs_mut() =
let cascade_inputs =
ElementCascadeInputs::new_from_element_data(data);
element.cascade_primary_and_pseudos(
context,
data,
/* important_rules_changed = */ false
)
StyleResolverForElement::new(element, context, RuleInclusion::All)
.cascade_styles_with_default_parents(cascade_inputs)
}
}
};
element.finish_restyle(
context,
data,
new_styles,
important_rules_changed
)
}
fn preprocess_children<E, D>(

View file

@ -664,32 +664,35 @@ pub extern "C" fn Servo_StyleSet_GetBaseComputedValuesForElement(raw_data: RawSe
let doc_data = PerDocumentStyleData::from_ffi(raw_data).borrow();
let global_style_data = &*GLOBAL_STYLE_DATA;
let guard = global_style_data.shared_lock.read();
let shared_context = create_shared_context(&global_style_data,
&guard,
&doc_data,
TraversalFlags::empty(),
unsafe { &*snapshots });
let element = GeckoElement(element);
let element_data = element.borrow_data().unwrap();
let styles = &element_data.styles;
let pseudo = PseudoElement::from_pseudo_type(pseudo_type);
let pseudos = &styles.pseudos;
let pseudo_style = match pseudo {
Some(ref p) => {
let style = pseudos.get(p);
debug_assert!(style.is_some());
style
}
None => None,
if let Some(pseudo) = PseudoElement::from_pseudo_type(pseudo_type) {
// This style already doesn't have animations.
return styles
.pseudos
.get(&pseudo)
.expect("GetBaseComputedValuesForElement for an unexisting pseudo?")
.clone().into_strong();
}
let shared = create_shared_context(&global_style_data,
&guard,
&doc_data,
TraversalFlags::empty(),
unsafe { &*snapshots });
let mut tlc = ThreadLocalStyleContext::new(&shared);
let mut context = StyleContext {
shared: &shared,
thread_local: &mut tlc,
};
let provider = get_metrics_provider_for_product();
element.get_base_style(&shared_context,
&provider,
styles.primary(),
pseudo_style)
.into_strong()
element.get_base_style(
&mut context,
styles.primary(),
).into_strong()
}
#[no_mangle]