Implement AttrHelpersForLayout for LayoutJS<Attr> rather than Attr itself.

This commit is contained in:
Ms2ger 2015-06-20 18:51:25 +02:00
parent fa45688191
commit c019897a50
3 changed files with 21 additions and 24 deletions

View file

@ -7,7 +7,7 @@ use dom::bindings::codegen::Bindings::AttrBinding::{self, AttrMethods};
use dom::bindings::codegen::InheritTypes::NodeCast;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutNullableHeap};
use dom::bindings::js::{Root, RootedReference};
use dom::bindings::js::{Root, RootedReference, LayoutJS};
use dom::bindings::utils::{Reflector, reflect_dom_object};
use dom::element::{Element, AttributeHandlers};
use dom::window::Window;
@ -313,23 +313,21 @@ pub trait AttrHelpersForLayout {
}
#[allow(unsafe_code)]
impl AttrHelpersForLayout for Attr {
impl AttrHelpersForLayout for LayoutJS<Attr> {
#[inline]
unsafe fn value_forever(&self) -> &'static AttrValue {
// This transmute is used to cheat the lifetime restriction.
mem::transmute::<&AttrValue, &AttrValue>(self.value.borrow_for_layout())
mem::transmute::<&AttrValue, &AttrValue>((*self.unsafe_get()).value.borrow_for_layout())
}
#[inline]
unsafe fn value_ref_forever(&self) -> &'static str {
// This transmute is used to cheat the lifetime restriction.
let value = mem::transmute::<&AttrValue, &AttrValue>(self.value.borrow_for_layout());
&**value
&**self.value_forever()
}
#[inline]
unsafe fn value_atom_forever(&self) -> Option<Atom> {
let value = self.value.borrow_for_layout();
let value = (*self.unsafe_get()).value.borrow_for_layout();
match *value {
AttrValue::Atom(ref val) => Some(val.clone()),
_ => None,
@ -339,8 +337,7 @@ impl AttrHelpersForLayout for Attr {
#[inline]
unsafe fn value_tokens_forever(&self) -> Option<&'static [Atom]> {
// This transmute is used to cheat the lifetime restriction.
let value = mem::transmute::<&AttrValue, &AttrValue>(self.value.borrow_for_layout());
match *value {
match *self.value_forever() {
AttrValue::TokenList(_, ref tokens) => Some(tokens),
_ => None,
}
@ -348,11 +345,11 @@ impl AttrHelpersForLayout for Attr {
#[inline]
unsafe fn local_name_atom_forever(&self) -> Atom {
self.local_name.clone()
(*self.unsafe_get()).local_name.clone()
}
#[inline]
unsafe fn value_for_layout(&self) -> &AttrValue {
self.value.borrow_for_layout()
(*self.unsafe_get()).value.borrow_for_layout()
}
}

View file

@ -194,9 +194,9 @@ pub unsafe fn get_attr_for_layout<'a>(elem: &'a Element, namespace: &Namespace,
// cast to point to T in RefCell<T> directly
let attrs = elem.attrs.borrow_for_layout();
attrs.iter().find(|attr: & &JS<Attr>| {
let attr = attr.to_layout().unsafe_get();
*name == (*attr).local_name_atom_forever() &&
(*attr).namespace() == namespace
let attr = attr.to_layout();
*name == attr.local_name_atom_forever() &&
(*attr.unsafe_get()).namespace() == namespace
}).map(|attr| attr.to_layout())
}
@ -206,14 +206,14 @@ impl RawLayoutElementHelpers for Element {
unsafe fn get_attr_for_layout<'a>(&'a self, namespace: &Namespace, name: &Atom)
-> Option<&'a AttrValue> {
get_attr_for_layout(self, namespace, name).map(|attr| {
(*attr.unsafe_get()).value_forever()
attr.value_forever()
})
}
unsafe fn get_attr_val_for_layout<'a>(&'a self, namespace: &Namespace, name: &Atom)
-> Option<&'a str> {
get_attr_for_layout(self, namespace, name).map(|attr| {
(*attr.unsafe_get()).value_ref_forever()
attr.value_ref_forever()
})
}
@ -221,9 +221,9 @@ impl RawLayoutElementHelpers for Element {
unsafe fn get_attr_vals_for_layout<'a>(&'a self, name: &Atom) -> Vec<&'a str> {
let attrs = self.attrs.borrow_for_layout();
(*attrs).iter().filter_map(|attr: &JS<Attr>| {
let attr = attr.to_layout().unsafe_get();
if *name == (*attr).local_name_atom_forever() {
Some((*attr).value_ref_forever())
let attr = attr.to_layout();
if *name == attr.local_name_atom_forever() {
Some(attr.value_ref_forever())
} else {
None
}
@ -234,21 +234,21 @@ impl RawLayoutElementHelpers for Element {
unsafe fn get_attr_atom_for_layout(&self, namespace: &Namespace, name: &Atom)
-> Option<Atom> {
get_attr_for_layout(self, namespace, name).and_then(|attr| {
(*attr.unsafe_get()).value_atom_forever()
attr.value_atom_forever()
})
}
#[inline]
unsafe fn has_class_for_layout(&self, name: &Atom) -> bool {
get_attr_for_layout(self, &ns!(""), &atom!("class")).map_or(false, |attr| {
(*attr.unsafe_get()).value_tokens_forever().unwrap().iter().any(|atom| atom == name)
attr.value_tokens_forever().unwrap().iter().any(|atom| atom == name)
})
}
#[inline]
unsafe fn get_classes_for_layout(&self) -> Option<&'static [Atom]> {
get_attr_for_layout(self, &ns!(""), &atom!("class")).map(|attr| {
(*attr.unsafe_get()).value_tokens_forever().unwrap()
attr.value_tokens_forever().unwrap()
})
}

View file

@ -180,7 +180,7 @@ impl RawHTMLIFrameElementHelpers for HTMLIFrameElement {
element::get_attr_for_layout(ElementCast::from_actual(&*self),
&ns!(""),
&atom!("width")).map(|attribute| {
str::parse_length(&**(*attribute.unsafe_get()).value_for_layout())
str::parse_length(&**attribute.value_for_layout())
}).unwrap_or(LengthOrPercentageOrAuto::Auto)
}
}
@ -191,7 +191,7 @@ impl RawHTMLIFrameElementHelpers for HTMLIFrameElement {
element::get_attr_for_layout(ElementCast::from_actual(&*self),
&ns!(""),
&atom!("height")).map(|attribute| {
str::parse_length(&**(*attribute.unsafe_get()).value_for_layout())
str::parse_length(&**attribute.value_for_layout())
}).unwrap_or(LengthOrPercentageOrAuto::Auto)
}
}