Move core pseudo-element handling to ThreadSafeLayoutElement.

This commit is contained in:
Bobby Holley 2016-10-27 16:19:03 -07:00
parent 2fce2fbb0c
commit 46e29184bd
7 changed files with 314 additions and 301 deletions

View file

@ -25,7 +25,7 @@ use style::properties::ServoComputedValues;
use style::selector_impl::{PseudoElement, PseudoElementCascadeType, ServoSelectorImpl};
use url::Url;
#[derive(Copy, PartialEq, Clone)]
#[derive(Copy, PartialEq, Clone, Debug)]
pub enum PseudoElementType<T> {
Normal,
Before(T),
@ -70,9 +70,14 @@ impl<T> PseudoElementType<T> {
}
}
/// Trait to abstract access to layout data across various data structures.
pub trait GetLayoutData {
fn get_style_and_layout_data(&self) -> Option<OpaqueStyleAndLayoutData>;
}
/// A wrapper so that layout can access only the methods that it should have access to. Layout must
/// only ever see these and must never see instances of `LayoutJS`.
pub trait LayoutNode: TNode {
pub trait LayoutNode: GetLayoutData + TNode {
type ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode;
fn to_threadsafe(&self) -> Self::ConcreteThreadSafeLayoutNode;
@ -86,7 +91,6 @@ pub trait LayoutNode: TNode {
fn get_style_data(&self) -> Option<&AtomicRefCell<NodeData>>;
fn init_style_and_layout_data(&self, data: OpaqueStyleAndLayoutData);
fn get_style_and_layout_data(&self) -> Option<OpaqueStyleAndLayoutData>;
fn rev_children(self) -> LayoutIterator<ReverseChildrenIterator<Self>> {
LayoutIterator(ReverseChildrenIterator {
@ -144,16 +148,12 @@ impl<ConcreteNode> Iterator for TreeIterator<ConcreteNode>
/// A thread-safe version of `LayoutNode`, used during flow construction. This type of layout
/// node does not allow any parents or siblings of nodes to be accessed, to avoid races.
pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
pub trait ThreadSafeLayoutNode: Clone + Copy + GetLayoutData + NodeInfo + PartialEq + Sized {
type ConcreteThreadSafeLayoutElement:
ThreadSafeLayoutElement<ConcreteThreadSafeLayoutNode = Self>
+ ::selectors::Element<Impl=ServoSelectorImpl>;
type ChildrenIterator: Iterator<Item = Self> + Sized;
/// Creates a new `ThreadSafeLayoutNode` for the same `LayoutNode`
/// with a different pseudo-element type.
fn with_pseudo(&self, pseudo: PseudoElementType<Option<display::T>>) -> Self;
/// Converts self into an `OpaqueNode`.
fn opaque(&self) -> OpaqueNode;
@ -183,172 +183,54 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
}
}
fn get_before_pseudo(&self) -> Option<Self> {
self.as_element().and_then(|el| el.get_before_pseudo()).map(|el| el.as_node())
}
fn get_after_pseudo(&self) -> Option<Self> {
self.as_element().and_then(|el| el.get_after_pseudo()).map(|el| el.as_node())
}
fn get_details_summary_pseudo(&self) -> Option<Self> {
self.as_element().and_then(|el| el.get_details_summary_pseudo()).map(|el| el.as_node())
}
fn get_details_content_pseudo(&self) -> Option<Self> {
self.as_element().and_then(|el| el.get_details_content_pseudo()).map(|el| el.as_node())
}
fn debug_id(self) -> usize;
/// Returns an iterator over this node's children.
fn children(&self) -> LayoutIterator<Self::ChildrenIterator>;
/// If this is an element, accesses the element data. Fails if this is not an element node.
/// Returns a ThreadSafeLayoutElement if this is an element, None otherwise.
#[inline]
fn as_element(&self) -> Self::ConcreteThreadSafeLayoutElement;
fn as_element(&self) -> Option<Self::ConcreteThreadSafeLayoutElement>;
#[inline]
fn get_pseudo_element_type(&self) -> PseudoElementType<Option<display::T>>;
#[inline]
fn get_before_pseudo(&self) -> Option<Self> {
if self.is_element() &&
self.get_style_data()
.unwrap()
.borrow()
.current_styles().pseudos
.contains_key(&PseudoElement::Before) {
Some(self.with_pseudo(PseudoElementType::Before(None)))
} else {
None
}
}
#[inline]
fn get_after_pseudo(&self) -> Option<Self> {
if self.is_element() &&
self.get_style_data()
.unwrap()
.borrow()
.current_styles().pseudos
.contains_key(&PseudoElement::After) {
Some(self.with_pseudo(PseudoElementType::After(None)))
} else {
None
}
}
#[inline]
fn get_details_summary_pseudo(&self) -> Option<Self> {
if self.is_element() &&
self.as_element().get_local_name() == &atom!("details") &&
self.as_element().get_namespace() == &ns!(html) {
Some(self.with_pseudo(PseudoElementType::DetailsSummary(None)))
} else {
None
}
}
#[inline]
fn get_details_content_pseudo(&self) -> Option<Self> {
if self.is_element() &&
self.as_element().get_local_name() == &atom!("details") &&
self.as_element().get_namespace() == &ns!(html) {
let display = if self.as_element().get_attr(&ns!(), &atom!("open")).is_some() {
None // Specified by the stylesheet
} else {
Some(display::T::none)
};
Some(self.with_pseudo(PseudoElementType::DetailsContent(display)))
} else {
None
}
fn get_pseudo_element_type(&self) -> PseudoElementType<Option<display::T>> {
self.as_element().map_or(PseudoElementType::Normal, |el| el.get_pseudo_element_type())
}
fn get_style_and_layout_data(&self) -> Option<OpaqueStyleAndLayoutData>;
/// Returns the style results for the given node. If CSS selector matching
/// has not yet been performed, fails.
///
/// Unlike the version on TNode, this handles pseudo-elements.
#[inline]
fn style(&self, context: &SharedStyleContext) -> Arc<ServoComputedValues> {
match self.get_pseudo_element_type() {
PseudoElementType::Normal => {
match self.type_id().unwrap() {
LayoutNodeType::Text => self.style_for_text_node(),
LayoutNodeType::Element(_) => self.get_style_data().unwrap().borrow()
.current_styles().primary.clone(),
}
},
other => {
// Precompute non-eagerly-cascaded pseudo-element styles if not
// cached before.
let style_pseudo = other.style_pseudo_element();
match style_pseudo.cascade_type() {
// Already computed during the cascade.
PseudoElementCascadeType::Eager => {},
PseudoElementCascadeType::Precomputed => {
if !self.get_style_data()
.unwrap()
.borrow()
.current_styles().pseudos.contains_key(&style_pseudo) {
let mut data = self.get_style_data().unwrap().borrow_mut();
let new_style =
context.stylist.precomputed_values_for_pseudo(
&style_pseudo,
Some(&data.current_styles().primary),
false);
data.current_pseudos_mut()
.insert(style_pseudo.clone(), new_style.unwrap());
}
}
PseudoElementCascadeType::Lazy => {
debug_assert!(self.is_element_or_elements_pseudo());
if !self.get_style_data()
.unwrap()
.borrow()
.current_styles().pseudos.contains_key(&style_pseudo) {
let mut data = self.get_style_data().unwrap().borrow_mut();
let new_style =
context.stylist
.lazily_compute_pseudo_element_style(
&self.as_element(),
&style_pseudo,
&data.current_styles().primary);
data.current_pseudos_mut()
.insert(style_pseudo.clone(), new_style.unwrap());
}
}
}
self.get_style_data().unwrap().borrow()
.current_styles().pseudos.get(&style_pseudo)
.unwrap().clone()
}
if let Some(el) = self.as_element() {
el.style(context)
} else {
debug_assert!(self.is_text_node());
self.style_for_text_node()
}
}
/// Returns the already resolved style of the node.
///
/// This differs from `style(ctx)` in that if the pseudo-element has not yet
/// been computed it would panic.
///
/// This should be used just for querying layout, or when we know the
/// element style is precomputed, not from general layout itself.
#[inline]
fn resolved_style(&self) -> Arc<ServoComputedValues> {
// FIXME(bholley): This should move to Element and lose the text node check.
if self.is_text_node() {
return self.style_for_text_node();
fn selected_style(&self) -> Arc<ServoComputedValues> {
if let Some(el) = self.as_element() {
el.selected_style()
} else {
debug_assert!(self.is_text_node());
self.style_for_text_node()
}
let data = self.get_style_data().unwrap().borrow();
match self.get_pseudo_element_type() {
PseudoElementType::Normal
=> data.current_styles().primary.clone(),
other
=> data.current_styles().pseudos.get(&other.style_pseudo_element()).unwrap().clone(),
}
}
#[inline]
fn selected_style(&self, _context: &SharedStyleContext) -> Arc<ServoComputedValues> {
// FIXME(bholley): This should move to Element and lose the text node check.
if self.is_text_node() {
return self.style_for_text_node();
}
let data = self.get_style_data().unwrap().borrow();
data.current_styles().pseudos
.get(&PseudoElement::Selection)
.unwrap_or(&data.current_styles().primary)
.clone()
}
fn is_ignorable_whitespace(&self, context: &SharedStyleContext) -> bool;
@ -360,10 +242,7 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
/// Returns true if this node contributes content. This is used in the implementation of
/// `empty_cells` per CSS 2.1 § 17.6.1.1.
fn is_content(&self) -> bool {
match self.type_id() {
Some(LayoutNodeType::Element(..)) | Some(LayoutNodeType::Text) => true,
_ => false
}
self.type_id().is_some()
}
fn can_be_fragmented(&self) -> bool;
@ -387,8 +266,6 @@ pub trait ThreadSafeLayoutNode: Clone + Copy + NodeInfo + PartialEq + Sized {
fn iframe_pipeline_id(&self) -> PipelineId;
fn get_colspan(&self) -> u32;
fn get_style_data(&self) -> Option<&AtomicRefCell<NodeData>>;
}
// This trait is only public so that it can be implemented by the gecko wrapper.
@ -401,15 +278,160 @@ pub trait DangerousThreadSafeLayoutNode: ThreadSafeLayoutNode {
pub trait ThreadSafeLayoutElement: Clone + Copy + Sized + Debug +
::selectors::Element<Impl=ServoSelectorImpl> +
GetLayoutData +
PresentationalHintsSynthetizer {
type ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode<ConcreteThreadSafeLayoutElement = Self>;
fn as_node(&self) -> Self::ConcreteThreadSafeLayoutNode;
/// Creates a new `ThreadSafeLayoutElement` for the same `LayoutElement`
/// with a different pseudo-element type.
fn with_pseudo(&self, pseudo: PseudoElementType<Option<display::T>>) -> Self;
/// Returns the type ID of this node.
/// Returns `None` if this is a pseudo-element; otherwise, returns `Some`.
fn type_id(&self) -> Option<LayoutNodeType>;
#[inline]
fn get_attr(&self, namespace: &Namespace, name: &Atom) -> Option<&str>;
#[inline]
fn get_local_name(&self) -> &Atom;
fn get_style_data(&self) -> Option<&AtomicRefCell<NodeData>>;
#[inline]
fn get_namespace(&self) -> &Namespace;
fn get_pseudo_element_type(&self) -> PseudoElementType<Option<display::T>>;
#[inline]
fn get_before_pseudo(&self) -> Option<Self> {
if self.get_style_data()
.unwrap()
.borrow()
.current_styles().pseudos
.contains_key(&PseudoElement::Before) {
Some(self.with_pseudo(PseudoElementType::Before(None)))
} else {
None
}
}
#[inline]
fn get_after_pseudo(&self) -> Option<Self> {
if self.get_style_data()
.unwrap()
.borrow()
.current_styles().pseudos
.contains_key(&PseudoElement::After) {
Some(self.with_pseudo(PseudoElementType::After(None)))
} else {
None
}
}
#[inline]
fn get_details_summary_pseudo(&self) -> Option<Self> {
if self.get_local_name() == &atom!("details") &&
self.get_namespace() == &ns!(html) {
Some(self.with_pseudo(PseudoElementType::DetailsSummary(None)))
} else {
None
}
}
#[inline]
fn get_details_content_pseudo(&self) -> Option<Self> {
if self.get_local_name() == &atom!("details") &&
self.get_namespace() == &ns!(html) {
let display = if self.get_attr(&ns!(), &atom!("open")).is_some() {
None // Specified by the stylesheet
} else {
Some(display::T::none)
};
Some(self.with_pseudo(PseudoElementType::DetailsContent(display)))
} else {
None
}
}
/// Returns the style results for the given node. If CSS selector matching
/// has not yet been performed, fails.
///
/// Unlike the version on TNode, this handles pseudo-elements.
#[inline]
fn style(&self, context: &SharedStyleContext) -> Arc<ServoComputedValues> {
match self.get_pseudo_element_type() {
PseudoElementType::Normal => self.get_style_data().unwrap().borrow()
.current_styles().primary.clone(),
other => {
// Precompute non-eagerly-cascaded pseudo-element styles if not
// cached before.
let style_pseudo = other.style_pseudo_element();
match style_pseudo.cascade_type() {
// Already computed during the cascade.
PseudoElementCascadeType::Eager => {},
PseudoElementCascadeType::Precomputed => {
if !self.get_style_data()
.unwrap()
.borrow()
.current_styles().pseudos.contains_key(&style_pseudo) {
let mut data = self.get_style_data().unwrap().borrow_mut();
let new_style =
context.stylist.precomputed_values_for_pseudo(
&style_pseudo,
Some(&data.current_styles().primary),
false);
data.current_pseudos_mut()
.insert(style_pseudo.clone(), new_style.unwrap());
}
}
PseudoElementCascadeType::Lazy => {
if !self.get_style_data()
.unwrap()
.borrow()
.current_styles().pseudos.contains_key(&style_pseudo) {
let mut data = self.get_style_data().unwrap().borrow_mut();
let new_style =
context.stylist
.lazily_compute_pseudo_element_style(
self,
&style_pseudo,
&data.current_styles().primary);
data.current_pseudos_mut()
.insert(style_pseudo.clone(), new_style.unwrap());
}
}
}
self.get_style_data().unwrap().borrow()
.current_styles().pseudos.get(&style_pseudo)
.unwrap().clone()
}
}
}
#[inline]
fn selected_style(&self) -> Arc<ServoComputedValues> {
let data = self.get_style_data().unwrap().borrow();
data.current_styles().pseudos
.get(&PseudoElement::Selection)
.unwrap_or(&data.current_styles().primary)
.clone()
}
/// Returns the already resolved style of the node.
///
/// This differs from `style(ctx)` in that if the pseudo-element has not yet
/// been computed it would panic.
///
/// This should be used just for querying layout, or when we know the
/// element style is precomputed, not from general layout itself.
#[inline]
fn resolved_style(&self) -> Arc<ServoComputedValues> {
let data = self.get_style_data().unwrap().borrow();
match self.get_pseudo_element_type() {
PseudoElementType::Normal
=> data.current_styles().primary.clone(),
other
=> data.current_styles().pseudos.get(&other.style_pseudo_element()).unwrap().clone(),
}
}
}