servo: Merge #3213 - Make use of the list of Atoms in the class attribute in selector matchin (from gilles-leblanc:issue-3111)

Source-Repo: https://github.com/servo/servo
Source-Revision: 97cabf204e2793fa54bcf58375af4ed8ee8c6ae4
This commit is contained in:
Gilles Leblanc 2014-09-12 08:50:07 -04:00
parent 8d80c9dea7
commit b88507d8b2
5 changed files with 34 additions and 5 deletions

View File

@ -417,6 +417,12 @@ impl<'le> TElement for LayoutElement<'le> {
self.element.node.get_enabled_state_for_layout()
}
}
fn has_class(&self, name: &str) -> bool {
unsafe {
self.element.has_class_for_layout(name)
}
}
}
fn get_content(content_list: &content::T) -> String {

View File

@ -188,6 +188,7 @@ impl<'a> AttrHelpers for JSRef<'a, Attr> {
pub trait AttrHelpersForLayout {
unsafe fn value_ref_forever(&self) -> &'static str;
unsafe fn value_atom_forever(&self) -> Option<Atom>;
unsafe fn value_tokens_forever(&self) -> Option<Items<Atom>>;
unsafe fn local_name_atom_forever(&self) -> Atom;
}
@ -207,6 +208,15 @@ impl AttrHelpersForLayout for Attr {
}
}
unsafe fn value_tokens_forever(&self) -> Option<Items<Atom>> {
// cast to point to T in RefCell<T> directly
let value = mem::transmute::<&RefCell<AttrValue>, &AttrValue>(self.value.deref());
match *value {
TokenListAttrValue(_, ref tokens) => Some(tokens.iter()),
_ => None,
}
}
unsafe fn local_name_atom_forever(&self) -> Atom {
self.local_name.clone()
}

View File

@ -168,6 +168,7 @@ impl Element {
pub trait RawLayoutElementHelpers {
unsafe fn get_attr_val_for_layout(&self, namespace: &Namespace, name: &str) -> Option<&'static str>;
unsafe fn get_attr_atom_for_layout(&self, namespace: &Namespace, name: &str) -> Option<Atom>;
unsafe fn has_class_for_layout(&self, name: &str) -> bool;
}
impl RawLayoutElementHelpers for Element {
@ -200,6 +201,18 @@ impl RawLayoutElementHelpers for Element {
(*attr).value_atom_forever()
})
}
#[inline]
unsafe fn has_class_for_layout(&self, name: &str) -> bool {
let attrs: *const Vec<JS<Attr>> = mem::transmute(&self.attrs);
(*attrs).iter().find(|attr: & &JS<Attr>| {
let attr = attr.unsafe_get();
(*attr).local_name_atom_forever().as_slice() == "class"
}).map_or(false, |attr| {
let attr = attr.unsafe_get();
(*attr).value_tokens_forever().map(|mut tokens| { tokens.any(|atom| atom.as_slice() == name) })
}.take().unwrap())
}
}
pub trait LayoutElementHelpers {
@ -955,4 +968,7 @@ impl<'a> style::TElement for JSRef<'a, Element> {
let node: &JSRef<Node> = NodeCast::from_ref(self);
node.get_enabled_state()
}
fn has_class(&self, name: &str) -> bool {
(self as &AttributeHandlers).has_class(name)
}
}

View File

@ -30,5 +30,6 @@ pub trait TElement {
fn get_id(&self) -> Option<Atom>;
fn get_disabled_state(&self) -> bool;
fn get_enabled_state(&self) -> bool;
fn has_class(&self, name: &str) -> bool;
}

View File

@ -626,11 +626,7 @@ fn matches_simple_selector<E:TElement,
// TODO: cache and intern class names on elements.
ClassSelector(ref class) => {
let element = element.as_element();
element.get_attr(&namespace::Null, "class")
.map_or(false, |attr| {
// TODO: case-sensitivity depends on the document type and quirks mode
attr.split(SELECTOR_WHITESPACE).any(|c| c == class.as_slice())
})
element.has_class(class.as_slice())
}
AttrExists(ref attr) => {