Auto merge of #19911 - emilio:has-class, r=xidorn

style: Make GeckoElement::has_class more specialized

Bug: 1431421
Reviewed-by: xidorn
MozReview-Commit-ID: 7LiSEamTCkX

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/19911)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-01-31 04:45:07 -06:00 committed by GitHub
commit c88dc51d03
4 changed files with 40 additions and 31 deletions

View file

@ -724,6 +724,13 @@ extern "C" {
classList: *mut *mut *mut nsAtom,
) -> u32;
}
extern "C" {
pub fn Gecko_HasClass(
element: RawGeckoElementBorrowed,
class_: *mut nsAtom,
ignore_case: bool,
) -> bool;
}
extern "C" {
pub fn Gecko_SnapshotAtomAttrValue(
element: *const ServoElementSnapshot,
@ -801,6 +808,13 @@ extern "C" {
classList: *mut *mut *mut nsAtom,
) -> u32;
}
extern "C" {
pub fn Gecko_SnapshotHasClass(
element: *const ServoElementSnapshot,
class_: *mut nsAtom,
ignore_case: bool,
) -> bool;
}
extern "C" {
pub fn Gecko_GetStyleAttrDeclarationBlock(
element: RawGeckoElementBorrowed,

View file

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

View file

@ -4,9 +4,7 @@
//! Element an snapshot common logic.
use CaseSensitivityExt;
use gecko_bindings::structs::nsAtom;
use gecko_string_cache::WeakAtom;
use selectors::attr::CaseSensitivity;
use std::{ptr, slice};
use string_cache::Atom;
@ -15,34 +13,27 @@ use string_cache::Atom;
/// class or a class list.
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
/// element has the class that `name` represents.
#[inline(always)]
pub fn has_class<T>(
item: T,
name: &Atom,
case_sensitivity: CaseSensitivity,
getter: ClassOrClassList<T>,
getter: HasClass<T>,
) -> bool {
unsafe {
let mut class: *mut nsAtom = ptr::null_mut();
let mut list: *mut *mut nsAtom = ptr::null_mut();
let length = getter(item, &mut class, &mut list);
match length {
0 => false,
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)))
}
}
}
}
}
let ignore_case = match case_sensitivity {
CaseSensitivity::CaseSensitive => false,
CaseSensitivity::AsciiCaseInsensitive => true,
};
unsafe { getter(item, name.as_ptr(), ignore_case) }
}

View file

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