2015-12-30 07:34:14 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2016-06-28 01:14:55 +03:00
|
|
|
//! Types and traits used to access the DOM from style calculation.
|
|
|
|
|
2015-12-30 07:34:14 +03:00
|
|
|
#![allow(unsafe_code)]
|
|
|
|
|
2016-10-03 05:24:11 +03:00
|
|
|
use atomic_refcell::{AtomicRef, AtomicRefMut};
|
|
|
|
use data::PersistentStyleData;
|
2016-02-03 04:34:11 +03:00
|
|
|
use element_state::ElementState;
|
2016-08-21 11:43:25 +03:00
|
|
|
use properties::{ComputedValues, PropertyDeclarationBlock};
|
2016-07-22 00:54:34 +03:00
|
|
|
use restyle_hints::{RESTYLE_DESCENDANTS, RESTYLE_LATER_SIBLINGS, RESTYLE_SELF, RestyleHint};
|
2016-08-11 05:02:30 +03:00
|
|
|
use selector_impl::{ElementExt, PseudoElement};
|
2016-09-06 09:48:04 +03:00
|
|
|
use selector_matching::ApplicableDeclarationBlock;
|
2016-07-04 22:57:00 +03:00
|
|
|
use sink::Push;
|
2016-08-11 05:02:30 +03:00
|
|
|
use std::fmt::Debug;
|
2016-01-04 21:04:35 +03:00
|
|
|
use std::ops::BitOr;
|
|
|
|
use std::sync::Arc;
|
2015-12-30 07:34:14 +03:00
|
|
|
use string_cache::{Atom, Namespace};
|
|
|
|
|
|
|
|
/// Opaque type stored in type-unsafe work queues for parallel layout.
|
|
|
|
/// Must be transmutable to and from TNode.
|
|
|
|
pub type UnsafeNode = (usize, usize);
|
|
|
|
|
|
|
|
/// An opaque handle to a node, which, unlike UnsafeNode, cannot be transformed
|
|
|
|
/// back into a non-opaque representation. The only safe operation that can be
|
|
|
|
/// performed on this node is to compare it to another opaque handle or to another
|
|
|
|
/// OpaqueNode.
|
|
|
|
///
|
|
|
|
/// Layout and Graphics use this to safely represent nodes for comparison purposes.
|
|
|
|
/// Because the script task's GC does not trace layout, node data cannot be safely stored in layout
|
|
|
|
/// data structures. Also, layout code tends to be faster when the DOM is not being accessed, for
|
|
|
|
/// locality reasons. Using `OpaqueNode` enforces this invariant.
|
2016-06-22 17:43:20 +03:00
|
|
|
#[derive(Clone, PartialEq, Copy, Debug, Hash, Eq)]
|
|
|
|
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))]
|
2015-12-30 07:34:14 +03:00
|
|
|
pub struct OpaqueNode(pub usize);
|
|
|
|
|
|
|
|
impl OpaqueNode {
|
|
|
|
/// Returns the address of this node, for debugging purposes.
|
|
|
|
#[inline]
|
|
|
|
pub fn id(&self) -> usize {
|
2016-03-28 01:42:31 +03:00
|
|
|
self.0
|
2015-12-30 07:34:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 05:02:30 +03:00
|
|
|
pub trait TRestyleDamage : Debug + PartialEq + BitOr<Output=Self> + Copy {
|
2016-08-04 03:02:26 +03:00
|
|
|
/// The source for our current computed values in the cascade. This is a
|
|
|
|
/// ComputedValues in Servo and a StyleContext in Gecko.
|
|
|
|
///
|
|
|
|
/// This is needed because Gecko has a few optimisations for the calculation
|
|
|
|
/// of the difference depending on which values have been used during
|
|
|
|
/// layout.
|
|
|
|
///
|
|
|
|
/// This should be obtained via TNode::existing_style_for_restyle_damage
|
|
|
|
type PreExistingComputedValues;
|
|
|
|
|
2016-08-11 05:02:30 +03:00
|
|
|
fn compute(old: &Self::PreExistingComputedValues,
|
2016-08-04 03:02:26 +03:00
|
|
|
new: &Arc<ComputedValues>) -> Self;
|
|
|
|
|
2016-08-11 05:02:30 +03:00
|
|
|
fn empty() -> Self;
|
|
|
|
|
2016-01-04 21:04:35 +03:00
|
|
|
fn rebuild_and_reflow() -> Self;
|
|
|
|
}
|
2015-12-30 07:34:14 +03:00
|
|
|
|
2016-09-22 03:59:52 +03:00
|
|
|
/// Simple trait to provide basic information about the type of an element.
|
|
|
|
///
|
|
|
|
/// We avoid exposing the full type id, since computing it in the general case
|
|
|
|
/// would be difficult for Gecko nodes.
|
|
|
|
pub trait NodeInfo {
|
|
|
|
fn is_element(&self) -> bool;
|
|
|
|
fn is_text_node(&self) -> bool;
|
|
|
|
|
|
|
|
// Comments, doctypes, etc are ignored by layout algorithms.
|
|
|
|
fn needs_layout(&self) -> bool { self.is_element() || self.is_text_node() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LayoutIterator<T>(pub T);
|
|
|
|
impl<T, I> Iterator for LayoutIterator<T> where T: Iterator<Item=I>, I: NodeInfo {
|
|
|
|
type Item = I;
|
|
|
|
fn next(&mut self) -> Option<I> {
|
|
|
|
loop {
|
|
|
|
// Filter out nodes that layout should ignore.
|
|
|
|
let n = self.0.next();
|
|
|
|
if n.is_none() || n.as_ref().unwrap().needs_layout() {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait TNode : Sized + Copy + Clone + NodeInfo {
|
servo: Merge #9976 - Remove lifetimes from Style/Layout traits (from bholley:remove_trait_lifetimes); r=SimonSapin
Right now, there's a huge amount of complexity in T{Node,Element,Document} and friends because of the lifetime parameter.
Before I started generalizing this code for use by Gecko, these wrappers were plain structs. They had (and still have) a phantom lifetime associated with them to prevent references to DOM nodes from leaking past the end of restyle, when they might be invalidated by a GC.
When I generalized them, I decided to put the lifetime on the trait as well, since there are some situations where the lifetime is, in fact, necessary. Specifically, they are necessary for the compiler to understand that all the things borrowed from all the nodes and elements and so on have the same lifetime (the lifetime of the restyle), rather than the lifetime of whichever particular element or node pointer the value was borrowed from. This come up in situations where we do |let el = node.as_element()| or |let n = el.as_node()| and then borrow something from the result. The compiler thinks the borrow lifetime is that of |el| or |n|, when it's actually longer.
In practice though, I think the style and layout algorithms we use don't run into this issue much, and we can hack around it where it comes up. So I think we should remove the lifetimes from the traits, which will let us aggregate the embedding-provided traits together onto a single meta-trait and significantly simplify the code.
Source-Repo: https://github.com/servo/servo
Source-Revision: aea8d8959dcb157a8cc381f1403246ce8ca1ca00
2016-03-15 00:33:53 +03:00
|
|
|
type ConcreteElement: TElement<ConcreteNode = Self, ConcreteDocument = Self::ConcreteDocument>;
|
|
|
|
type ConcreteDocument: TDocument<ConcreteNode = Self, ConcreteElement = Self::ConcreteElement>;
|
servo: Merge #12515 - Make the style crate more concrete (from servo:concrete-style); r=bholley
Background:
The changes to Servo code to support Stylo began in the `selectors` crate with making pseudo-elements generic, defined be the user, so that different users (such as Servo and Gecko/Stylo) could have a different set of pseudo-elements supported and parsed. Adding a trait makes sense there since `selectors` is in its own repository and has others users (or at least [one](https://github.com/SimonSapin/kuchiki)).
Then we kind of kept going with the same pattern and added a bunch of traits in the `style` crate to make everything generic, allowing Servo and Gecko/Stylo to do things differently. But we’ve also added a `gecko` Cargo feature to do conditional compilation, at first to enable or disable some CSS properties and values in the Mako templates. Since we’re doing conditional compilation anyway, it’s often easier and simpler to do it more (with `#[cfg(feature = "gecko")]` and `#[cfg(feature = "servo")]`) that to keep adding traits and making everything generic. When a type is generic, any method that we want to call on it needs to be part of some trait.
----
The first several commits move some code around, mostly from `geckolib` to `style` (with `#[cfg(feature = "gecko")]`) but otherwise don’t change much.
The following commits remove some traits and many type parameters through the `style` crate, replacing them with pairs of conditionally-compiled API-compatible items (types, methods, …).
Simplifying code is nice to make it more maintainable, but this is motivated by another change described in https://github.com/servo/servo/pull/12391#issuecomment-232183942. (Porting Servo for that change proved difficult because some code in the `style` crate was becoming generic over `String` vs `Atom`, and this PR will help make that concrete. That change, in turn, is motivated by removing geckolib’s `[replace]` override for string-cache, in order to enable using a single Cargo "workspace" in this repository.)
r? @bholley
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #__ (github issue number if applicable).
<!-- Either: -->
- [ ] There are tests for these changes OR
- [x] These changes do not require new tests because refactoring
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Source-Repo: https://github.com/servo/servo
Source-Revision: 2d01d41a506bcbc7f26a2284b9f42390d6ef96ab
--HG--
rename : servo/ports/geckolib/selector_impl.rs => servo/components/style/gecko_selector_impl.rs
rename : servo/ports/geckolib/values.rs => servo/components/style/gecko_values.rs
rename : servo/ports/geckolib/properties.mako.rs => servo/components/style/properties/gecko.mako.rs
2016-07-20 10:58:34 +03:00
|
|
|
type ConcreteRestyleDamage: TRestyleDamage;
|
2016-08-26 20:17:40 +03:00
|
|
|
type ConcreteChildrenIterator: Iterator<Item = Self>;
|
2015-12-30 07:34:14 +03:00
|
|
|
|
|
|
|
fn to_unsafe(&self) -> UnsafeNode;
|
|
|
|
unsafe fn from_unsafe(n: &UnsafeNode) -> Self;
|
|
|
|
|
|
|
|
fn dump(self);
|
|
|
|
|
2016-08-13 04:55:27 +03:00
|
|
|
fn dump_style(self);
|
|
|
|
|
2015-12-30 07:34:14 +03:00
|
|
|
/// Returns an iterator over this node's children.
|
2016-09-22 03:59:52 +03:00
|
|
|
fn children(self) -> LayoutIterator<Self::ConcreteChildrenIterator>;
|
2015-12-30 07:34:14 +03:00
|
|
|
|
|
|
|
/// Converts self into an `OpaqueNode`.
|
|
|
|
fn opaque(&self) -> OpaqueNode;
|
|
|
|
|
|
|
|
/// While doing a reflow, the node at the root has no parent, as far as we're
|
|
|
|
/// concerned. This method returns `None` at the reflow root.
|
|
|
|
fn layout_parent_node(self, reflow_root: OpaqueNode) -> Option<Self>;
|
|
|
|
|
|
|
|
fn debug_id(self) -> usize;
|
|
|
|
|
|
|
|
fn as_element(&self) -> Option<Self::ConcreteElement>;
|
|
|
|
|
|
|
|
fn as_document(&self) -> Option<Self::ConcreteDocument>;
|
|
|
|
|
|
|
|
fn has_changed(&self) -> bool;
|
|
|
|
|
|
|
|
unsafe fn set_changed(&self, value: bool);
|
|
|
|
|
|
|
|
fn is_dirty(&self) -> bool;
|
|
|
|
|
|
|
|
unsafe fn set_dirty(&self, value: bool);
|
|
|
|
|
|
|
|
fn has_dirty_descendants(&self) -> bool;
|
|
|
|
|
|
|
|
unsafe fn set_dirty_descendants(&self, value: bool);
|
|
|
|
|
2016-07-18 06:46:24 +03:00
|
|
|
fn needs_dirty_on_viewport_size_changed(&self) -> bool;
|
|
|
|
|
|
|
|
unsafe fn set_dirty_on_viewport_size_changed(&self);
|
|
|
|
|
2016-01-29 22:19:14 +03:00
|
|
|
fn can_be_fragmented(&self) -> bool;
|
|
|
|
|
|
|
|
unsafe fn set_can_be_fragmented(&self, value: bool);
|
|
|
|
|
2016-10-03 05:24:11 +03:00
|
|
|
/// Borrows the style data immutably. Fails on a conflicting borrow.
|
2015-12-30 07:34:14 +03:00
|
|
|
#[inline(always)]
|
2016-10-03 05:24:11 +03:00
|
|
|
fn borrow_data(&self) -> Option<AtomicRef<PersistentStyleData>>;
|
2015-12-30 07:34:14 +03:00
|
|
|
|
2016-10-03 05:24:11 +03:00
|
|
|
/// Borrows the style data mutably. Fails on a conflicting borrow.
|
2015-12-30 07:34:14 +03:00
|
|
|
#[inline(always)]
|
2016-10-03 05:24:11 +03:00
|
|
|
fn mutate_data(&self) -> Option<AtomicRefMut<PersistentStyleData>>;
|
2015-12-30 07:34:14 +03:00
|
|
|
|
2016-01-04 21:04:35 +03:00
|
|
|
/// Get the description of how to account for recent style changes.
|
|
|
|
fn restyle_damage(self) -> Self::ConcreteRestyleDamage;
|
|
|
|
|
|
|
|
/// Set the restyle damage field.
|
|
|
|
fn set_restyle_damage(self, damage: Self::ConcreteRestyleDamage);
|
|
|
|
|
2015-12-30 07:34:14 +03:00
|
|
|
fn parent_node(&self) -> Option<Self>;
|
|
|
|
|
|
|
|
fn first_child(&self) -> Option<Self>;
|
|
|
|
|
|
|
|
fn last_child(&self) -> Option<Self>;
|
|
|
|
|
|
|
|
fn prev_sibling(&self) -> Option<Self>;
|
|
|
|
|
|
|
|
fn next_sibling(&self) -> Option<Self>;
|
2016-01-08 11:27:39 +03:00
|
|
|
|
|
|
|
/// Removes the style from this node.
|
2016-01-19 11:49:10 +03:00
|
|
|
fn unstyle(self) {
|
|
|
|
self.mutate_data().unwrap().style = None;
|
|
|
|
}
|
2016-08-04 03:02:26 +03:00
|
|
|
|
|
|
|
/// XXX: It's a bit unfortunate we need to pass the current computed values
|
|
|
|
/// as an argument here, but otherwise Servo would crash due to double
|
|
|
|
/// borrows to return it.
|
|
|
|
fn existing_style_for_restyle_damage<'a>(&'a self,
|
2016-08-11 05:02:30 +03:00
|
|
|
current_computed_values: Option<&'a Arc<ComputedValues>>,
|
|
|
|
pseudo: Option<&PseudoElement>)
|
2016-08-04 03:02:26 +03:00
|
|
|
-> Option<&'a <Self::ConcreteRestyleDamage as TRestyleDamage>::PreExistingComputedValues>;
|
2015-12-30 07:34:14 +03:00
|
|
|
}
|
|
|
|
|
servo: Merge #9976 - Remove lifetimes from Style/Layout traits (from bholley:remove_trait_lifetimes); r=SimonSapin
Right now, there's a huge amount of complexity in T{Node,Element,Document} and friends because of the lifetime parameter.
Before I started generalizing this code for use by Gecko, these wrappers were plain structs. They had (and still have) a phantom lifetime associated with them to prevent references to DOM nodes from leaking past the end of restyle, when they might be invalidated by a GC.
When I generalized them, I decided to put the lifetime on the trait as well, since there are some situations where the lifetime is, in fact, necessary. Specifically, they are necessary for the compiler to understand that all the things borrowed from all the nodes and elements and so on have the same lifetime (the lifetime of the restyle), rather than the lifetime of whichever particular element or node pointer the value was borrowed from. This come up in situations where we do |let el = node.as_element()| or |let n = el.as_node()| and then borrow something from the result. The compiler thinks the borrow lifetime is that of |el| or |n|, when it's actually longer.
In practice though, I think the style and layout algorithms we use don't run into this issue much, and we can hack around it where it comes up. So I think we should remove the lifetimes from the traits, which will let us aggregate the embedding-provided traits together onto a single meta-trait and significantly simplify the code.
Source-Repo: https://github.com/servo/servo
Source-Revision: aea8d8959dcb157a8cc381f1403246ce8ca1ca00
2016-03-15 00:33:53 +03:00
|
|
|
pub trait TDocument : Sized + Copy + Clone {
|
|
|
|
type ConcreteNode: TNode<ConcreteElement = Self::ConcreteElement, ConcreteDocument = Self>;
|
|
|
|
type ConcreteElement: TElement<ConcreteNode = Self::ConcreteNode, ConcreteDocument = Self>;
|
2015-12-30 07:34:14 +03:00
|
|
|
|
|
|
|
fn as_node(&self) -> Self::ConcreteNode;
|
|
|
|
|
|
|
|
fn root_node(&self) -> Option<Self::ConcreteNode>;
|
|
|
|
|
2016-07-22 00:54:34 +03:00
|
|
|
fn drain_modified_elements(&self) -> Vec<(Self::ConcreteElement,
|
|
|
|
<Self::ConcreteElement as ElementExt>::Snapshot)>;
|
2016-08-31 05:05:56 +03:00
|
|
|
|
|
|
|
fn needs_paint_from_layout(&self);
|
|
|
|
fn will_paint(&self);
|
2015-12-30 07:34:14 +03:00
|
|
|
}
|
|
|
|
|
2016-05-04 13:42:23 +03:00
|
|
|
pub trait PresentationalHintsSynthetizer {
|
|
|
|
fn synthesize_presentational_hints_for_legacy_attributes<V>(&self, hints: &mut V)
|
2016-09-06 09:48:04 +03:00
|
|
|
where V: Push<ApplicableDeclarationBlock>;
|
2016-05-04 13:42:23 +03:00
|
|
|
}
|
|
|
|
|
2016-08-21 11:43:25 +03:00
|
|
|
pub trait TElement : PartialEq + Debug + Sized + Copy + Clone + ElementExt + PresentationalHintsSynthetizer {
|
servo: Merge #9976 - Remove lifetimes from Style/Layout traits (from bholley:remove_trait_lifetimes); r=SimonSapin
Right now, there's a huge amount of complexity in T{Node,Element,Document} and friends because of the lifetime parameter.
Before I started generalizing this code for use by Gecko, these wrappers were plain structs. They had (and still have) a phantom lifetime associated with them to prevent references to DOM nodes from leaking past the end of restyle, when they might be invalidated by a GC.
When I generalized them, I decided to put the lifetime on the trait as well, since there are some situations where the lifetime is, in fact, necessary. Specifically, they are necessary for the compiler to understand that all the things borrowed from all the nodes and elements and so on have the same lifetime (the lifetime of the restyle), rather than the lifetime of whichever particular element or node pointer the value was borrowed from. This come up in situations where we do |let el = node.as_element()| or |let n = el.as_node()| and then borrow something from the result. The compiler thinks the borrow lifetime is that of |el| or |n|, when it's actually longer.
In practice though, I think the style and layout algorithms we use don't run into this issue much, and we can hack around it where it comes up. So I think we should remove the lifetimes from the traits, which will let us aggregate the embedding-provided traits together onto a single meta-trait and significantly simplify the code.
Source-Repo: https://github.com/servo/servo
Source-Revision: aea8d8959dcb157a8cc381f1403246ce8ca1ca00
2016-03-15 00:33:53 +03:00
|
|
|
type ConcreteNode: TNode<ConcreteElement = Self, ConcreteDocument = Self::ConcreteDocument>;
|
|
|
|
type ConcreteDocument: TDocument<ConcreteNode = Self::ConcreteNode, ConcreteElement = Self>;
|
2015-12-30 07:34:14 +03:00
|
|
|
|
|
|
|
fn as_node(&self) -> Self::ConcreteNode;
|
|
|
|
|
2016-09-01 00:30:56 +03:00
|
|
|
fn style_attribute(&self) -> Option<&Arc<PropertyDeclarationBlock>>;
|
2015-12-30 07:34:14 +03:00
|
|
|
|
|
|
|
fn get_state(&self) -> ElementState;
|
|
|
|
|
2016-07-06 04:55:36 +03:00
|
|
|
fn has_attr(&self, namespace: &Namespace, attr: &Atom) -> bool;
|
|
|
|
fn attr_equals(&self, namespace: &Namespace, attr: &Atom, value: &Atom) -> bool;
|
2015-12-30 07:34:14 +03:00
|
|
|
|
|
|
|
/// Properly marks nodes as dirty in response to restyle hints.
|
2016-07-28 01:56:26 +03:00
|
|
|
fn note_restyle_hint(&self, hint: RestyleHint) {
|
2015-12-30 07:34:14 +03:00
|
|
|
// Bail early if there's no restyling to do.
|
|
|
|
if hint.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the restyle hint is non-empty, we need to restyle either this element
|
|
|
|
// or one of its siblings. Mark our ancestor chain as having dirty descendants.
|
|
|
|
let node = self.as_node();
|
|
|
|
let mut curr = node;
|
|
|
|
while let Some(parent) = curr.parent_node() {
|
|
|
|
if parent.has_dirty_descendants() { break }
|
|
|
|
unsafe { parent.set_dirty_descendants(true); }
|
|
|
|
curr = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process hints.
|
|
|
|
if hint.contains(RESTYLE_SELF) {
|
2016-07-28 01:56:26 +03:00
|
|
|
unsafe { node.set_dirty(true); }
|
|
|
|
// XXX(emilio): For now, dirty implies dirty descendants if found.
|
|
|
|
} else if hint.contains(RESTYLE_DESCENDANTS) {
|
2016-08-18 00:34:30 +03:00
|
|
|
unsafe { node.set_dirty_descendants(true); }
|
2016-07-28 01:56:26 +03:00
|
|
|
let mut current = node.first_child();
|
|
|
|
while let Some(node) = current {
|
|
|
|
unsafe { node.set_dirty(true); }
|
|
|
|
current = node.next_sibling();
|
|
|
|
}
|
2015-12-30 07:34:14 +03:00
|
|
|
}
|
2016-07-28 01:56:26 +03:00
|
|
|
|
2015-12-30 07:34:14 +03:00
|
|
|
if hint.contains(RESTYLE_LATER_SIBLINGS) {
|
|
|
|
let mut next = ::selectors::Element::next_sibling_element(self);
|
|
|
|
while let Some(sib) = next {
|
|
|
|
let sib_node = sib.as_node();
|
2016-07-28 01:56:26 +03:00
|
|
|
unsafe { sib_node.set_dirty(true) };
|
2015-12-30 07:34:14 +03:00
|
|
|
next = ::selectors::Element::next_sibling_element(&sib);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|