style: Rewrite get*Style using StyleResolverForElement.

Removing the ugly.

MozReview-Commit-ID: BvahbMKS7QU
This commit is contained in:
Emilio Cobos Álvarez 2017-07-09 22:12:59 +02:00
parent b4c8ba3029
commit 0ad2d39c30
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
4 changed files with 126 additions and 178 deletions

View file

@ -7,12 +7,12 @@
use atomic_refcell::AtomicRefCell;
use context::{ElementCascadeInputs, StyleContext, SharedStyleContext};
use data::{ElementData, ElementStyles};
use dom::{DirtyDescendants, NodeInfo, OpaqueNode, TElement, TNode};
use dom::{NodeInfo, OpaqueNode, TElement, TNode};
use invalidation::element::restyle_hints::{RECASCADE_SELF, RECASCADE_DESCENDANTS, RestyleHint};
use matching::{ChildCascadeRequirement, MatchMethods};
use sharing::{StyleSharingBehavior, StyleSharingTarget};
#[cfg(feature = "servo")] use servo_config::opts;
use smallvec::SmallVec;
use stylist::RuleInclusion;
/// A per-traversal-level chunk of data. This is sent down by the traversal, and
/// currently only holds the dom depth for the bloom filter.
@ -126,6 +126,8 @@ impl TraversalDriver {
#[cfg(feature = "servo")]
fn is_servo_nonincremental_layout() -> bool {
use servo_config::opts;
opts::get().nonincremental_layout
}
@ -520,151 +522,87 @@ pub trait DomTraversal<E: TElement> : Sync {
fn is_parallel(&self) -> bool;
}
/// Helper for the function below.
fn resolve_style_internal<E, F>(
context: &mut StyleContext<E>,
element: E, ensure_data: &F
) -> Option<E>
where E: TElement,
F: Fn(E),
{
ensure_data(element);
let mut data = element.mutate_data().unwrap();
let mut display_none_root = None;
// If the Element isn't styled, we need to compute its style.
if !data.has_styles() {
// Compute the parent style if necessary.
let parent = element.traversal_parent();
if let Some(p) = parent {
display_none_root = resolve_style_internal(context, p, ensure_data);
}
// Maintain the bloom filter. If it doesn't exist, we need to build it
// from scratch. Otherwise we just need to push the parent.
if context.thread_local.bloom_filter.is_empty() {
context.thread_local.bloom_filter.rebuild(element);
} else {
context.thread_local.bloom_filter.push(parent.unwrap());
context.thread_local.bloom_filter.assert_complete(element);
}
// Compute our style.
context.thread_local.begin_element(element, &data);
element.match_and_cascade(context,
&mut data,
StyleSharingBehavior::Disallow);
context.thread_local.end_element(element);
if !context.shared.traversal_flags.for_default_styles() {
// Conservatively mark us as having dirty descendants, since there
// might be other unstyled siblings we miss when walking straight up
// the parent chain.
//
// No need to do this if we're computing default styles, since
// resolve_default_style will want the tree to be left as it is.
unsafe { element.note_descendants::<DirtyDescendants>() };
}
}
// If we're display:none and none of our ancestors are, we're the root of a
// display:none subtree.
if display_none_root.is_none() && data.styles.is_display_none() {
display_none_root = Some(element);
}
return display_none_root
}
/// Manually resolve style by sequentially walking up the parent chain to the
/// first styled Element, ignoring pending restyles. The resolved style is made
/// available via a callback, and can be dropped by the time this function
/// returns in the display:none subtree case.
pub fn resolve_style<E, F, G, H>(context: &mut StyleContext<E>, element: E,
ensure_data: &F, clear_data: &G, callback: H)
where E: TElement,
F: Fn(E),
G: Fn(E),
H: FnOnce(&ElementStyles)
pub fn resolve_style<E>(
context: &mut StyleContext<E>,
element: E,
rule_inclusion: RuleInclusion,
) -> ElementStyles
where
E: TElement,
{
use style_resolver::StyleResolverForElement;
debug_assert!(rule_inclusion == RuleInclusion::DefaultOnly ||
element.borrow_data().map_or(true, |d| !d.has_styles()),
"Why are we here?");
let mut ancestors_requiring_style_resolution = SmallVec::<[E; 16]>::new();
// Clear the bloom filter, just in case the caller is reusing TLS.
context.thread_local.bloom_filter.clear();
// Resolve styles up the tree.
let display_none_root = resolve_style_internal(context, element, ensure_data);
// Make them available for the scope of the callback. The callee may use the
// argument, or perform any other processing that requires the styles to
// exist on the Element.
callback(&element.borrow_data().unwrap().styles);
// Clear any styles in display:none subtrees or subtrees not in the
// document, to leave the tree in a valid state. For display:none subtrees,
// we leave the styles on the display:none root, but for subtrees not in the
// document, we clear styles all the way up to the root of the disconnected
// subtree.
let in_doc = element.as_node().is_in_doc();
if !in_doc || display_none_root.is_some() {
let mut curr = element;
loop {
unsafe {
curr.unset_dirty_descendants();
curr.unset_animation_only_dirty_descendants();
}
if in_doc && curr == display_none_root.unwrap() {
break;
}
clear_data(curr);
curr = match curr.traversal_parent() {
Some(parent) => parent,
None => break,
};
}
}
}
/// Manually resolve default styles for the given Element, which are the styles
/// only taking into account user agent and user cascade levels. The resolved
/// style is made available via a callback, and will be dropped by the time this
/// function returns.
pub fn resolve_default_style<E, F, G, H>(
context: &mut StyleContext<E>,
element: E,
ensure_data: &F,
set_data: &G,
callback: H
)
where
E: TElement,
F: Fn(E),
G: Fn(E, Option<ElementData>) -> Option<ElementData>,
H: FnOnce(&ElementStyles),
{
// Save and clear out element data from the element and its ancestors.
let mut old_data: SmallVec<[(E, Option<ElementData>); 8]> = SmallVec::new();
{
let mut e = element;
loop {
old_data.push((e, set_data(e, None)));
match e.traversal_parent() {
Some(parent) => e = parent,
None => break,
let mut style = None;
let mut ancestor = element.traversal_parent();
while let Some(current) = ancestor {
if rule_inclusion == RuleInclusion::All {
if let Some(data) = element.borrow_data() {
if let Some(ancestor_style) = data.styles.get_primary() {
style = Some(ancestor_style.clone());
break;
}
}
}
ancestors_requiring_style_resolution.push(current);
ancestor = current.traversal_parent();
}
// Resolve styles up the tree.
resolve_style_internal(context, element, ensure_data);
// Make them available for the scope of the callback. The callee may use the
// argument, or perform any other processing that requires the styles to
// exist on the Element.
callback(&element.borrow_data().unwrap().styles);
// Swap the old element data back into the element and its ancestors.
for entry in old_data {
set_data(entry.0, entry.1);
if let Some(ancestor) = ancestor {
context.thread_local.bloom_filter.rebuild(ancestor);
context.thread_local.bloom_filter.push(ancestor);
}
let mut layout_parent_style = style.clone();
while let Some(style) = layout_parent_style.take() {
if !style.is_display_contents() {
layout_parent_style = Some(style);
break;
}
ancestor = ancestor.unwrap().traversal_parent();
layout_parent_style = ancestor.map(|a| {
a.borrow_data().unwrap().styles.primary().clone()
});
}
for ancestor in ancestors_requiring_style_resolution.iter().rev() {
context.thread_local.bloom_filter.assert_complete(*ancestor);
let primary_style =
StyleResolverForElement::new(*ancestor, context, rule_inclusion)
.resolve_primary_style(
style.as_ref().map(|s| &**s),
layout_parent_style.as_ref().map(|s| &**s)
);
let is_display_contents = primary_style.style.is_display_contents();
style = Some(primary_style.style);
if !is_display_contents {
layout_parent_style = style.clone();
}
context.thread_local.bloom_filter.push(*ancestor);
}
context.thread_local.bloom_filter.assert_complete(element);
StyleResolverForElement::new(element, context, rule_inclusion)
.resolve_style(
style.as_ref().map(|s| &**s),
layout_parent_style.as_ref().map(|s| &**s)
)
}
/// Calculates the style for a single node.