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::codegen::InheritTypes::NodeCast;
use dom::bindings::global::GlobalRef; use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutNullableHeap}; 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::bindings::utils::{Reflector, reflect_dom_object};
use dom::element::{Element, AttributeHandlers}; use dom::element::{Element, AttributeHandlers};
use dom::window::Window; use dom::window::Window;
@ -313,23 +313,21 @@ pub trait AttrHelpersForLayout {
} }
#[allow(unsafe_code)] #[allow(unsafe_code)]
impl AttrHelpersForLayout for Attr { impl AttrHelpersForLayout for LayoutJS<Attr> {
#[inline] #[inline]
unsafe fn value_forever(&self) -> &'static AttrValue { unsafe fn value_forever(&self) -> &'static AttrValue {
// This transmute is used to cheat the lifetime restriction. // 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] #[inline]
unsafe fn value_ref_forever(&self) -> &'static str { unsafe fn value_ref_forever(&self) -> &'static str {
// This transmute is used to cheat the lifetime restriction. &**self.value_forever()
let value = mem::transmute::<&AttrValue, &AttrValue>(self.value.borrow_for_layout());
&**value
} }
#[inline] #[inline]
unsafe fn value_atom_forever(&self) -> Option<Atom> { 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 { match *value {
AttrValue::Atom(ref val) => Some(val.clone()), AttrValue::Atom(ref val) => Some(val.clone()),
_ => None, _ => None,
@ -339,8 +337,7 @@ impl AttrHelpersForLayout for Attr {
#[inline] #[inline]
unsafe fn value_tokens_forever(&self) -> Option<&'static [Atom]> { unsafe fn value_tokens_forever(&self) -> Option<&'static [Atom]> {
// This transmute is used to cheat the lifetime restriction. // This transmute is used to cheat the lifetime restriction.
let value = mem::transmute::<&AttrValue, &AttrValue>(self.value.borrow_for_layout()); match *self.value_forever() {
match *value {
AttrValue::TokenList(_, ref tokens) => Some(tokens), AttrValue::TokenList(_, ref tokens) => Some(tokens),
_ => None, _ => None,
} }
@ -348,11 +345,11 @@ impl AttrHelpersForLayout for Attr {
#[inline] #[inline]
unsafe fn local_name_atom_forever(&self) -> Atom { unsafe fn local_name_atom_forever(&self) -> Atom {
self.local_name.clone() (*self.unsafe_get()).local_name.clone()
} }
#[inline] #[inline]
unsafe fn value_for_layout(&self) -> &AttrValue { 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 // cast to point to T in RefCell<T> directly
let attrs = elem.attrs.borrow_for_layout(); let attrs = elem.attrs.borrow_for_layout();
attrs.iter().find(|attr: & &JS<Attr>| { attrs.iter().find(|attr: & &JS<Attr>| {
let attr = attr.to_layout().unsafe_get(); let attr = attr.to_layout();
*name == (*attr).local_name_atom_forever() && *name == attr.local_name_atom_forever() &&
(*attr).namespace() == namespace (*attr.unsafe_get()).namespace() == namespace
}).map(|attr| attr.to_layout()) }).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) unsafe fn get_attr_for_layout<'a>(&'a self, namespace: &Namespace, name: &Atom)
-> Option<&'a AttrValue> { -> Option<&'a AttrValue> {
get_attr_for_layout(self, namespace, name).map(|attr| { 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) unsafe fn get_attr_val_for_layout<'a>(&'a self, namespace: &Namespace, name: &Atom)
-> Option<&'a str> { -> Option<&'a str> {
get_attr_for_layout(self, namespace, name).map(|attr| { 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> { unsafe fn get_attr_vals_for_layout<'a>(&'a self, name: &Atom) -> Vec<&'a str> {
let attrs = self.attrs.borrow_for_layout(); let attrs = self.attrs.borrow_for_layout();
(*attrs).iter().filter_map(|attr: &JS<Attr>| { (*attrs).iter().filter_map(|attr: &JS<Attr>| {
let attr = attr.to_layout().unsafe_get(); let attr = attr.to_layout();
if *name == (*attr).local_name_atom_forever() { if *name == attr.local_name_atom_forever() {
Some((*attr).value_ref_forever()) Some(attr.value_ref_forever())
} else { } else {
None None
} }
@ -234,21 +234,21 @@ impl RawLayoutElementHelpers for Element {
unsafe fn get_attr_atom_for_layout(&self, namespace: &Namespace, name: &Atom) unsafe fn get_attr_atom_for_layout(&self, namespace: &Namespace, name: &Atom)
-> Option<Atom> { -> Option<Atom> {
get_attr_for_layout(self, namespace, name).and_then(|attr| { get_attr_for_layout(self, namespace, name).and_then(|attr| {
(*attr.unsafe_get()).value_atom_forever() attr.value_atom_forever()
}) })
} }
#[inline] #[inline]
unsafe fn has_class_for_layout(&self, name: &Atom) -> bool { unsafe fn has_class_for_layout(&self, name: &Atom) -> bool {
get_attr_for_layout(self, &ns!(""), &atom!("class")).map_or(false, |attr| { 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] #[inline]
unsafe fn get_classes_for_layout(&self) -> Option<&'static [Atom]> { unsafe fn get_classes_for_layout(&self) -> Option<&'static [Atom]> {
get_attr_for_layout(self, &ns!(""), &atom!("class")).map(|attr| { 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), element::get_attr_for_layout(ElementCast::from_actual(&*self),
&ns!(""), &ns!(""),
&atom!("width")).map(|attribute| { &atom!("width")).map(|attribute| {
str::parse_length(&**(*attribute.unsafe_get()).value_for_layout()) str::parse_length(&**attribute.value_for_layout())
}).unwrap_or(LengthOrPercentageOrAuto::Auto) }).unwrap_or(LengthOrPercentageOrAuto::Auto)
} }
} }
@ -191,7 +191,7 @@ impl RawHTMLIFrameElementHelpers for HTMLIFrameElement {
element::get_attr_for_layout(ElementCast::from_actual(&*self), element::get_attr_for_layout(ElementCast::from_actual(&*self),
&ns!(""), &ns!(""),
&atom!("height")).map(|attribute| { &atom!("height")).map(|attribute| {
str::parse_length(&**(*attribute.unsafe_get()).value_for_layout()) str::parse_length(&**attribute.value_for_layout())
}).unwrap_or(LengthOrPercentageOrAuto::Auto) }).unwrap_or(LengthOrPercentageOrAuto::Auto)
} }
} }