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)]
|
|
|
|
|
servo: Merge #14043 - Update to string-cache 0.3 (from servo:string-cache-up); r=nox
Previously, `string-cache` defined:
* An string-like `Atom` type,
* An `atom!("foo")` macro that expands to a value of that type, for a set of strings known at compile-time,
* A `struct Namespace(Atom);` type
* A `ns!(html)` macro that maps known prefixed to `Namespace` values with the corresponding namespace URL.
Adding a string to the static set required making a change to the `string-cache` crate.
With 0.3, the `Atom` type is now generic, with a type parameter that provides a set of static strings. We can have multiple such sets, defined in different crates. The `string_cache_codegen` crate, to be used in build scripts, generates code that defines such a set, a new atom type (a type alias for `Atom<_>` with the type parameter set), and an `atom!`-like macro.
The html5ever repository has a new `html5ever_atoms` crate that defines three such types: `Prefix`, `Namespace`, and `LocalName` (with respective `namespace_prefix!`, `namespace_url!`, and `local_name!` macros). It also defines the `ns!` macro like before.
This repository has a new `servo_atoms` crate in `components/atoms` that, for now, defines a single `Atom` type (and `atom!`) macro. (`servo_atoms::Atom` is defined as something like `type Atom = string_cache::Atom<ServoStaticStringSet>;`, so overall there’s now two types named `Atom`.)
In this PR, `servo_atoms::Atom` is used for everything else that was `string_cache::Atom` before. But more atom types can be defined as needed. Two reasons to do this are to auto-generate the set of static strings (I’m planning to do this for CSS property names, which is the motivation for this change), or to have the type system help us avoid mix up unrelated things (this is why we had a `Namespace` type ever before this change).
Introducing new types helped me find a bug: when creating a new attribute `dom::Element::set_style_attr`, would pass `Some(atom!("style"))` instead of `None` (now `Option<html5ever_atoms::Prefix>` instead of `Option<string_cache::Atom>`) to the `prefix` argument of `Attr::new`. I suppose the author of that code confused it with the `local_name` argument.
---
Note that Stylo is not affected by any of this. The `gecko_string_cache` module is unchanged, with a single `Atom` type. The `style` crate conditionally compiles `Prefix` and `LocalName` re-exports for that are both `gecko_string_cache::Atom` on stylo.
---
<!-- 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
- [ ] These changes fix #__ (github issue number if applicable).
<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____
<!-- 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: 5b4cc9568dbd5c15e5d2fbc62719172f11566ffa
2016-11-03 19:19:44 +03:00
|
|
|
use {Atom, Namespace, LocalName};
|
2016-11-25 20:00:44 +03:00
|
|
|
use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
|
2016-10-30 01:14:10 +03:00
|
|
|
use data::{ElementStyles, ElementData};
|
2016-02-03 04:34:11 +03:00
|
|
|
use element_state::ElementState;
|
2016-10-04 19:58:56 +03:00
|
|
|
use parking_lot::RwLock;
|
2016-08-21 11:43:25 +03:00
|
|
|
use properties::{ComputedValues, PropertyDeclarationBlock};
|
2016-11-20 18:21:52 +03:00
|
|
|
use selector_parser::{ElementExt, PseudoElement, RestyleDamage};
|
2016-07-04 22:57:00 +03:00
|
|
|
use sink::Push;
|
2016-08-11 05:02:30 +03:00
|
|
|
use std::fmt::Debug;
|
2016-11-25 20:00:44 +03:00
|
|
|
use std::ops::{BitOr, BitOrAssign};
|
2016-01-04 21:04:35 +03:00
|
|
|
use std::sync::Arc;
|
2016-11-20 18:21:52 +03:00
|
|
|
use stylist::ApplicableDeclarationBlock;
|
2016-10-26 14:36:06 +03:00
|
|
|
use util::opts;
|
2015-12-30 07:34:14 +03:00
|
|
|
|
2016-10-12 10:08:37 +03:00
|
|
|
pub use style_traits::UnsafeNode;
|
2015-12-30 07:34:14 +03:00
|
|
|
|
|
|
|
/// 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-11-25 20:00:44 +03:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
2016-10-26 14:36:06 +03:00
|
|
|
pub enum StylingMode {
|
|
|
|
/// The node has never been styled before, and needs a full style computation.
|
|
|
|
Initial,
|
|
|
|
/// The node has been styled before, but needs some amount of recomputation.
|
|
|
|
Restyle,
|
|
|
|
/// The node does not need any style processing, but one or more of its
|
|
|
|
/// descendants do.
|
|
|
|
Traverse,
|
|
|
|
/// No nodes in this subtree require style processing.
|
|
|
|
Stop,
|
|
|
|
}
|
|
|
|
|
2016-11-25 20:00:44 +03:00
|
|
|
pub trait TRestyleDamage : BitOr<Output=Self> + BitOrAssign + Copy + Debug + PartialEq {
|
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;
|
2016-11-25 20:00:44 +03:00
|
|
|
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
*self == Self::empty()
|
|
|
|
}
|
2016-01-04 21:04:35 +03:00
|
|
|
}
|
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 {
|
2016-11-17 21:25:52 +03:00
|
|
|
type ConcreteElement: TElement<ConcreteNode = Self>;
|
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;
|
|
|
|
|
2016-11-28 21:30:19 +03:00
|
|
|
fn parent_element(&self) -> Option<Self::ConcreteElement> {
|
|
|
|
self.parent_node().and_then(|n| n.as_element())
|
2016-11-25 20:00:44 +03:00
|
|
|
}
|
2015-12-30 07:34:14 +03:00
|
|
|
|
|
|
|
fn debug_id(self) -> usize;
|
|
|
|
|
|
|
|
fn as_element(&self) -> Option<Self::ConcreteElement>;
|
|
|
|
|
2016-10-30 01:14:10 +03:00
|
|
|
fn needs_dirty_on_viewport_size_changed(&self) -> bool;
|
|
|
|
|
|
|
|
unsafe fn set_dirty_on_viewport_size_changed(&self);
|
|
|
|
|
|
|
|
fn can_be_fragmented(&self) -> bool;
|
|
|
|
|
|
|
|
unsafe fn set_can_be_fragmented(&self, value: bool);
|
|
|
|
|
|
|
|
fn parent_node(&self) -> Option<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait PresentationalHintsSynthetizer {
|
|
|
|
fn synthesize_presentational_hints_for_legacy_attributes<V>(&self, hints: &mut V)
|
|
|
|
where V: Push<ApplicableDeclarationBlock>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait TElement : PartialEq + Debug + Sized + Copy + Clone + ElementExt + PresentationalHintsSynthetizer {
|
2016-11-17 21:25:52 +03:00
|
|
|
type ConcreteNode: TNode<ConcreteElement = Self>;
|
2016-10-30 01:14:10 +03:00
|
|
|
|
|
|
|
fn as_node(&self) -> Self::ConcreteNode;
|
|
|
|
|
2016-11-25 20:00:44 +03:00
|
|
|
/// While doing a reflow, the element at the root has no parent, as far as we're
|
|
|
|
/// concerned. This method returns `None` at the reflow root.
|
|
|
|
fn layout_parent_element(self, reflow_root: OpaqueNode) -> Option<Self> {
|
|
|
|
if self.as_node().opaque() == reflow_root {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
self.parent_element()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-30 01:14:10 +03:00
|
|
|
fn style_attribute(&self) -> Option<&Arc<RwLock<PropertyDeclarationBlock>>>;
|
|
|
|
|
|
|
|
fn get_state(&self) -> ElementState;
|
|
|
|
|
servo: Merge #14043 - Update to string-cache 0.3 (from servo:string-cache-up); r=nox
Previously, `string-cache` defined:
* An string-like `Atom` type,
* An `atom!("foo")` macro that expands to a value of that type, for a set of strings known at compile-time,
* A `struct Namespace(Atom);` type
* A `ns!(html)` macro that maps known prefixed to `Namespace` values with the corresponding namespace URL.
Adding a string to the static set required making a change to the `string-cache` crate.
With 0.3, the `Atom` type is now generic, with a type parameter that provides a set of static strings. We can have multiple such sets, defined in different crates. The `string_cache_codegen` crate, to be used in build scripts, generates code that defines such a set, a new atom type (a type alias for `Atom<_>` with the type parameter set), and an `atom!`-like macro.
The html5ever repository has a new `html5ever_atoms` crate that defines three such types: `Prefix`, `Namespace`, and `LocalName` (with respective `namespace_prefix!`, `namespace_url!`, and `local_name!` macros). It also defines the `ns!` macro like before.
This repository has a new `servo_atoms` crate in `components/atoms` that, for now, defines a single `Atom` type (and `atom!`) macro. (`servo_atoms::Atom` is defined as something like `type Atom = string_cache::Atom<ServoStaticStringSet>;`, so overall there’s now two types named `Atom`.)
In this PR, `servo_atoms::Atom` is used for everything else that was `string_cache::Atom` before. But more atom types can be defined as needed. Two reasons to do this are to auto-generate the set of static strings (I’m planning to do this for CSS property names, which is the motivation for this change), or to have the type system help us avoid mix up unrelated things (this is why we had a `Namespace` type ever before this change).
Introducing new types helped me find a bug: when creating a new attribute `dom::Element::set_style_attr`, would pass `Some(atom!("style"))` instead of `None` (now `Option<html5ever_atoms::Prefix>` instead of `Option<string_cache::Atom>`) to the `prefix` argument of `Attr::new`. I suppose the author of that code confused it with the `local_name` argument.
---
Note that Stylo is not affected by any of this. The `gecko_string_cache` module is unchanged, with a single `Atom` type. The `style` crate conditionally compiles `Prefix` and `LocalName` re-exports for that are both `gecko_string_cache::Atom` on stylo.
---
<!-- 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
- [ ] These changes fix #__ (github issue number if applicable).
<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____
<!-- 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: 5b4cc9568dbd5c15e5d2fbc62719172f11566ffa
2016-11-03 19:19:44 +03:00
|
|
|
fn has_attr(&self, namespace: &Namespace, attr: &LocalName) -> bool;
|
|
|
|
fn attr_equals(&self, namespace: &Namespace, attr: &LocalName, value: &Atom) -> bool;
|
2016-10-30 01:14:10 +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,
|
|
|
|
current_computed_values: Option<&'a Arc<ComputedValues>>,
|
|
|
|
pseudo: Option<&PseudoElement>)
|
2016-11-08 01:31:10 +03:00
|
|
|
-> Option<&'a <RestyleDamage as TRestyleDamage>::PreExistingComputedValues>;
|
2016-10-30 01:14:10 +03:00
|
|
|
|
2016-11-25 20:00:44 +03:00
|
|
|
/// Returns true if this element may have a descendant needing style processing.
|
|
|
|
///
|
|
|
|
/// Note that we cannot guarantee the existence of such an element, because
|
|
|
|
/// it may have been removed from the DOM between marking it for restyle and
|
|
|
|
/// the actual restyle traversal.
|
2015-12-30 07:34:14 +03:00
|
|
|
fn has_dirty_descendants(&self) -> bool;
|
|
|
|
|
2016-11-25 20:00:44 +03:00
|
|
|
/// Flag that this element has a descendant for style processing.
|
|
|
|
///
|
|
|
|
/// Only safe to call with exclusive access to the element.
|
2016-10-20 20:40:58 +03:00
|
|
|
unsafe fn set_dirty_descendants(&self);
|
2015-12-30 07:34:14 +03:00
|
|
|
|
2016-11-25 20:00:44 +03:00
|
|
|
/// Flag that this element has no descendant for style processing.
|
|
|
|
///
|
|
|
|
/// Only safe to call with exclusive access to the element.
|
|
|
|
unsafe fn unset_dirty_descendants(&self);
|
|
|
|
|
2016-10-09 09:37:42 +03:00
|
|
|
/// Atomically stores the number of children of this node that we will
|
|
|
|
/// need to process during bottom-up traversal.
|
|
|
|
fn store_children_to_process(&self, n: isize);
|
|
|
|
|
|
|
|
/// Atomically notes that a child has been processed during bottom-up
|
|
|
|
/// traversal. Returns the number of children left to process.
|
|
|
|
fn did_process_child(&self) -> isize;
|
|
|
|
|
2016-11-01 21:05:46 +03:00
|
|
|
/// Returns true if this element's current style is display:none. Only valid
|
|
|
|
/// to call after styling.
|
|
|
|
fn is_display_none(&self) -> bool {
|
2016-11-25 20:00:44 +03:00
|
|
|
self.borrow_data().unwrap().current_styles().is_display_none()
|
2016-11-01 21:05:46 +03:00
|
|
|
}
|
2016-10-30 01:14:10 +03:00
|
|
|
|
2016-10-26 14:36:06 +03:00
|
|
|
/// Returns true if this node has a styled layout frame that owns the style.
|
|
|
|
fn frame_has_style(&self) -> bool { false }
|
|
|
|
|
|
|
|
/// Returns the styles from the layout frame that owns them, if any.
|
|
|
|
///
|
2016-10-30 01:14:10 +03:00
|
|
|
/// FIXME(bholley): Once we start dropping ElementData from nodes when
|
2016-10-26 14:36:06 +03:00
|
|
|
/// creating frames, we'll want to teach this method to actually get
|
|
|
|
/// style data from the frame.
|
2016-10-30 01:14:10 +03:00
|
|
|
fn get_styles_from_frame(&self) -> Option<ElementStyles> { None }
|
2016-10-26 14:36:06 +03:00
|
|
|
|
|
|
|
/// Returns the styling mode for this node. This is only valid to call before
|
|
|
|
/// and during restyling, before finish_styling is invoked.
|
|
|
|
///
|
|
|
|
/// See the comments around StylingMode.
|
|
|
|
fn styling_mode(&self) -> StylingMode {
|
|
|
|
use self::StylingMode::*;
|
|
|
|
|
|
|
|
// Non-incremental layout impersonates Initial.
|
|
|
|
if opts::get().nonincremental_layout {
|
|
|
|
return Initial;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the default result if this node doesn't require processing.
|
|
|
|
let mode_for_descendants = if self.has_dirty_descendants() {
|
|
|
|
Traverse
|
|
|
|
} else {
|
|
|
|
Stop
|
|
|
|
};
|
|
|
|
|
2016-11-25 20:00:44 +03:00
|
|
|
match self.borrow_data() {
|
2016-11-01 21:05:46 +03:00
|
|
|
// No element data, no style on the frame.
|
2016-10-26 14:36:06 +03:00
|
|
|
None if !self.frame_has_style() => Initial,
|
2016-11-01 21:05:46 +03:00
|
|
|
// No element data, style on the frame.
|
2016-10-26 14:36:06 +03:00
|
|
|
None => mode_for_descendants,
|
2016-11-01 21:05:46 +03:00
|
|
|
// We have element data. Decide below.
|
2016-11-25 20:00:44 +03:00
|
|
|
Some(d) => match *d {
|
|
|
|
ElementData::Restyle(_) => Restyle,
|
|
|
|
ElementData::Persistent(_) => mode_for_descendants,
|
|
|
|
ElementData::Initial(None) => Initial,
|
|
|
|
// We previously computed the initial style for this element
|
|
|
|
// and then never consumed it. This is arguably a bug, since
|
|
|
|
// it means we either styled an element unnecessarily, or missed
|
|
|
|
// an opportunity to coalesce style traversals. However, this
|
|
|
|
// happens now for various reasons, so we just let it slide and
|
|
|
|
// treat it as persistent for now.
|
|
|
|
ElementData::Initial(Some(_)) => mode_for_descendants,
|
2016-10-26 14:36:06 +03:00
|
|
|
},
|
|
|
|
}
|
2016-11-01 21:05:46 +03:00
|
|
|
}
|
2016-08-31 05:05:56 +03:00
|
|
|
|
2016-11-01 21:05:46 +03:00
|
|
|
/// Gets a reference to the ElementData container.
|
|
|
|
fn get_data(&self) -> Option<&AtomicRefCell<ElementData>>;
|
|
|
|
|
2016-11-25 20:00:44 +03:00
|
|
|
/// Immutably borrows the ElementData.
|
|
|
|
fn borrow_data(&self) -> Option<AtomicRef<ElementData>> {
|
|
|
|
self.get_data().map(|x| x.borrow())
|
|
|
|
}
|
2016-07-28 01:56:26 +03:00
|
|
|
|
2016-11-25 20:00:44 +03:00
|
|
|
/// Mutably borrows the ElementData.
|
|
|
|
fn mutate_data(&self) -> Option<AtomicRefMut<ElementData>> {
|
|
|
|
self.get_data().map(|x| x.borrow_mut())
|
2015-12-30 07:34:14 +03:00
|
|
|
}
|
|
|
|
}
|