mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
Bug 1364412: Convert pseudo-elements to an enum. r=hiro,xidorn
This commit is contained in:
parent
0bc185f1c2
commit
5820e3ecac
17 changed files with 2260 additions and 1505 deletions
|
@ -8,213 +8,16 @@ use cssparser::{Parser, ToCss};
|
|||
use element_state::{IN_ACTIVE_STATE, IN_FOCUS_STATE, IN_HOVER_STATE};
|
||||
use element_state::ElementState;
|
||||
use gecko_bindings::structs::CSSPseudoClassType;
|
||||
use gecko_bindings::structs::nsIAtom;
|
||||
use selector_parser::{SelectorParser, PseudoElementCascadeType};
|
||||
use selectors::parser::{ComplexSelector, SelectorMethods};
|
||||
use selectors::visitor::SelectorVisitor;
|
||||
use std::borrow::Cow;
|
||||
use std::fmt;
|
||||
use std::ptr;
|
||||
use string_cache::{Atom, Namespace, WeakAtom, WeakNamespace};
|
||||
|
||||
pub use gecko::pseudo_element::{PseudoElement, EAGER_PSEUDOS, EAGER_PSEUDO_COUNT};
|
||||
pub use gecko::snapshot::SnapshotMap;
|
||||
|
||||
/// A representation of a CSS pseudo-element.
|
||||
///
|
||||
/// In Gecko, we represent pseudo-elements as plain `Atom`s.
|
||||
///
|
||||
/// The boolean field represents whether this element is an anonymous box. This
|
||||
/// is just for convenience, instead of recomputing it.
|
||||
///
|
||||
/// Also, note that the `Atom` member is always a static atom, so if space is a
|
||||
/// concern, we can use the raw pointer and use the lower bit to represent it
|
||||
/// without space overhead.
|
||||
///
|
||||
/// FIXME(emilio): we know all these atoms are static. Patches are starting to
|
||||
/// pile up, but a further potential optimisation is generating bindings without
|
||||
/// `-no-gen-bitfield-methods` (that was removed to compile on stable, but it no
|
||||
/// longer depends on it), and using the raw *mut nsIAtom (properly asserting
|
||||
/// we're a static atom).
|
||||
///
|
||||
/// This should allow us to avoid random FFI overhead when cloning/dropping
|
||||
/// pseudos.
|
||||
///
|
||||
/// Also, we can further optimize PartialEq and hash comparing/hashing only the
|
||||
/// atoms.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct PseudoElement(Atom, bool);
|
||||
|
||||
/// List of eager pseudos. Keep this in sync with the count below.
|
||||
macro_rules! each_eager_pseudo {
|
||||
($macro_name:ident, $atom_macro:ident) => {
|
||||
$macro_name!($atom_macro!(":after"), 0);
|
||||
$macro_name!($atom_macro!(":before"), 1);
|
||||
}
|
||||
}
|
||||
|
||||
/// The number of eager pseudo-elements (just ::before and ::after).
|
||||
pub const EAGER_PSEUDO_COUNT: usize = 2;
|
||||
|
||||
|
||||
impl PseudoElement {
|
||||
/// Returns the kind of cascade type that a given pseudo is going to use.
|
||||
///
|
||||
/// In Gecko we only compute ::before and ::after eagerly. We save the rules
|
||||
/// for anonymous boxes separately, so we resolve them as precomputed
|
||||
/// pseudos.
|
||||
///
|
||||
/// We resolve the others lazily, see `Servo_ResolvePseudoStyle`.
|
||||
pub fn cascade_type(&self) -> PseudoElementCascadeType {
|
||||
if self.is_eager() {
|
||||
debug_assert!(!self.is_anon_box());
|
||||
return PseudoElementCascadeType::Eager
|
||||
}
|
||||
|
||||
if self.is_anon_box() {
|
||||
return PseudoElementCascadeType::Precomputed
|
||||
}
|
||||
|
||||
PseudoElementCascadeType::Lazy
|
||||
}
|
||||
|
||||
/// Gets the canonical index of this eagerly-cascaded pseudo-element.
|
||||
#[inline]
|
||||
pub fn eager_index(&self) -> usize {
|
||||
macro_rules! case {
|
||||
($atom:expr, $idx:expr) => { if *self.as_atom() == $atom { return $idx; } }
|
||||
}
|
||||
each_eager_pseudo!(case, atom);
|
||||
panic!("Not eager")
|
||||
}
|
||||
|
||||
/// Creates a pseudo-element from an eager index.
|
||||
#[inline]
|
||||
pub fn from_eager_index(i: usize) -> Self {
|
||||
macro_rules! case {
|
||||
($atom:expr, $idx:expr) => { if i == $idx { return PseudoElement($atom, false); } }
|
||||
}
|
||||
each_eager_pseudo!(case, atom);
|
||||
panic!("Not eager")
|
||||
}
|
||||
|
||||
/// Get the pseudo-element as an atom.
|
||||
#[inline]
|
||||
pub fn as_atom(&self) -> &Atom {
|
||||
&self.0
|
||||
}
|
||||
|
||||
/// Whether this pseudo-element is an anonymous box.
|
||||
#[inline]
|
||||
fn is_anon_box(&self) -> bool {
|
||||
self.1
|
||||
}
|
||||
|
||||
/// Whether this pseudo-element is ::before or ::after.
|
||||
#[inline]
|
||||
pub fn is_before_or_after(&self) -> bool {
|
||||
*self.as_atom() == atom!(":before") ||
|
||||
*self.as_atom() == atom!(":after")
|
||||
}
|
||||
|
||||
/// Whether this pseudo-element is eagerly-cascaded.
|
||||
#[inline]
|
||||
pub fn is_eager(&self) -> bool {
|
||||
macro_rules! case {
|
||||
($atom:expr, $idx:expr) => { if *self.as_atom() == $atom { return true; } }
|
||||
}
|
||||
each_eager_pseudo!(case, atom);
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Whether this pseudo-element is lazily-cascaded.
|
||||
#[inline]
|
||||
pub fn is_lazy(&self) -> bool {
|
||||
!self.is_eager() && !self.is_precomputed()
|
||||
}
|
||||
|
||||
/// Whether this pseudo-element is precomputed.
|
||||
#[inline]
|
||||
pub fn is_precomputed(&self) -> bool {
|
||||
self.is_anon_box()
|
||||
}
|
||||
|
||||
/// Construct a pseudo-element from an `Atom`, receiving whether it is also
|
||||
/// an anonymous box, and don't check it on release builds.
|
||||
///
|
||||
/// On debug builds we assert it's the result we expect.
|
||||
#[inline]
|
||||
pub fn from_atom_unchecked(atom: Atom, is_anon_box: bool) -> Self {
|
||||
if cfg!(debug_assertions) {
|
||||
// Do the check on debug regardless.
|
||||
match Self::from_atom(&*atom, true) {
|
||||
Some(pseudo) => {
|
||||
assert_eq!(pseudo.is_anon_box(), is_anon_box);
|
||||
return pseudo;
|
||||
}
|
||||
None => panic!("Unknown pseudo: {:?}", atom),
|
||||
}
|
||||
}
|
||||
|
||||
PseudoElement(atom, is_anon_box)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_atom(atom: &WeakAtom, _in_ua: bool) -> Option<Self> {
|
||||
macro_rules! pseudo_element {
|
||||
($pseudo_str_with_colon:expr, $atom:expr, $is_anon_box:expr) => {{
|
||||
if atom == &*$atom {
|
||||
return Some(PseudoElement($atom, $is_anon_box));
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/gecko/pseudo_element_helper.rs"));
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Constructs an atom from a string of text, and whether we're in a
|
||||
/// user-agent stylesheet.
|
||||
///
|
||||
/// If we're not in a user-agent stylesheet, we will never parse anonymous
|
||||
/// box pseudo-elements.
|
||||
///
|
||||
/// Returns `None` if the pseudo-element is not recognised.
|
||||
#[inline]
|
||||
fn from_slice(s: &str, in_ua_stylesheet: bool) -> Option<Self> {
|
||||
use std::ascii::AsciiExt;
|
||||
macro_rules! pseudo_element {
|
||||
($pseudo_str_with_colon:expr, $atom:expr, $is_anon_box:expr) => {{
|
||||
if !$is_anon_box || in_ua_stylesheet {
|
||||
if s.eq_ignore_ascii_case(&$pseudo_str_with_colon[1..]) {
|
||||
return Some(PseudoElement($atom, $is_anon_box))
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/gecko/pseudo_element_helper.rs"));
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Returns null or nsIAtom pointer corresponding to a given PseudoElement.
|
||||
#[inline]
|
||||
pub fn ns_atom_or_null_from_opt(pseudo: Option<&PseudoElement>) -> *mut nsIAtom {
|
||||
pseudo.map(|p| p.as_atom().as_ptr()).unwrap_or(ptr::null_mut())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToCss for PseudoElement {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
// FIXME: why does the atom contain one colon? Pseudo-element has two
|
||||
debug_assert!(self.0.as_slice().starts_with(&[b':' as u16]) &&
|
||||
!self.0.as_slice().starts_with(&[b':' as u16, b':' as u16]));
|
||||
try!(dest.write_char(':'));
|
||||
write!(dest, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
flags NonTSPseudoClassFlag: u8 {
|
||||
// See NonTSPseudoClass::is_internal()
|
||||
|
@ -561,25 +364,18 @@ impl SelectorImpl {
|
|||
pub fn each_eagerly_cascaded_pseudo_element<F>(mut fun: F)
|
||||
where F: FnMut(PseudoElement),
|
||||
{
|
||||
macro_rules! case {
|
||||
($atom:expr, $idx:expr) => { fun(PseudoElement($atom, false)); }
|
||||
for pseudo in &EAGER_PSEUDOS {
|
||||
fun(pseudo.clone())
|
||||
}
|
||||
each_eager_pseudo!(case, atom);
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
/// Executes a function for each pseudo-element.
|
||||
pub fn each_pseudo_element<F>(mut fun: F)
|
||||
pub fn each_pseudo_element<F>(fun: F)
|
||||
where F: FnMut(PseudoElement),
|
||||
{
|
||||
macro_rules! pseudo_element {
|
||||
($pseudo_str_with_colon:expr, $atom:expr, $is_anon_box:expr) => {{
|
||||
fun(PseudoElement($atom, $is_anon_box));
|
||||
}}
|
||||
}
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/gecko/pseudo_element_helper.rs"));
|
||||
PseudoElement::each(fun)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue