style: Make GeckoElement::has_class more specialized.

Bug: 1431421
Reviewed-by: xidorn
MozReview-Commit-ID: 7LiSEamTCkX
This commit is contained in:
Emilio Cobos Álvarez 2018-01-18 16:19:26 +01:00
parent b900664c1a
commit 0466e0b2bc
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
3 changed files with 26 additions and 31 deletions

View file

@ -185,10 +185,12 @@ impl ElementSnapshot for GeckoElementSnapshot {
return false; return false;
} }
snapshot_helpers::has_class(self.as_ptr(), snapshot_helpers::has_class(
name, self.as_ptr(),
case_sensitivity, name,
bindings::Gecko_SnapshotClassOrClassList) case_sensitivity,
bindings::Gecko_SnapshotHasClass,
)
} }
#[inline] #[inline]
@ -200,9 +202,11 @@ impl ElementSnapshot for GeckoElementSnapshot {
return; return;
} }
snapshot_helpers::each_class(self.as_ptr(), snapshot_helpers::each_class(
callback, self.as_ptr(),
bindings::Gecko_SnapshotClassOrClassList) callback,
bindings::Gecko_SnapshotClassOrClassList,
)
} }
#[inline] #[inline]

View file

@ -4,9 +4,7 @@
//! Element an snapshot common logic. //! Element an snapshot common logic.
use CaseSensitivityExt;
use gecko_bindings::structs::nsAtom; use gecko_bindings::structs::nsAtom;
use gecko_string_cache::WeakAtom;
use selectors::attr::CaseSensitivity; use selectors::attr::CaseSensitivity;
use std::{ptr, slice}; use std::{ptr, slice};
use string_cache::Atom; use string_cache::Atom;
@ -15,34 +13,27 @@ use string_cache::Atom;
/// class or a class list. /// class or a class list.
pub type ClassOrClassList<T> = unsafe extern fn (T, *mut *mut nsAtom, *mut *mut *mut nsAtom) -> u32; pub type ClassOrClassList<T> = unsafe extern fn (T, *mut *mut nsAtom, *mut *mut *mut nsAtom) -> u32;
/// A function to return whether an element of type `T` has a given class.
///
/// The `bool` argument represents whether it should compare case-insensitively
/// or not.
pub type HasClass<T> = unsafe extern fn (T, *mut nsAtom, bool) -> bool;
/// Given an item `T`, a class name, and a getter function, return whether that /// Given an item `T`, a class name, and a getter function, return whether that
/// element has the class that `name` represents. /// element has the class that `name` represents.
#[inline(always)]
pub fn has_class<T>( pub fn has_class<T>(
item: T, item: T,
name: &Atom, name: &Atom,
case_sensitivity: CaseSensitivity, case_sensitivity: CaseSensitivity,
getter: ClassOrClassList<T>, getter: HasClass<T>,
) -> bool { ) -> bool {
unsafe { let ignore_case = match case_sensitivity {
let mut class: *mut nsAtom = ptr::null_mut(); CaseSensitivity::CaseSensitive => false,
let mut list: *mut *mut nsAtom = ptr::null_mut(); CaseSensitivity::AsciiCaseInsensitive => true,
let length = getter(item, &mut class, &mut list); };
match length {
0 => false, unsafe { getter(item, name.as_ptr(), ignore_case) }
1 => case_sensitivity.eq_atom(name, WeakAtom::new(class)),
n => {
let classes = slice::from_raw_parts(list, n as usize);
match case_sensitivity {
CaseSensitivity::CaseSensitive => {
classes.iter().any(|ptr| &**name == WeakAtom::new(*ptr))
}
CaseSensitivity::AsciiCaseInsensitive => {
classes.iter().any(|ptr| name.eq_ignore_ascii_case(WeakAtom::new(*ptr)))
}
}
}
}
}
} }

View file

@ -2195,7 +2195,7 @@ impl<'le> ::selectors::Element for GeckoElement<'le> {
self.0, self.0,
name, name,
case_sensitivity, case_sensitivity,
Gecko_ClassOrClassList, bindings::Gecko_HasClass,
) )
} }