Privatize Attr

This commit is contained in:
Tim Taubert 2014-10-11 23:25:42 +02:00
parent 9a52bb8310
commit 78fef7eec5
4 changed files with 33 additions and 17 deletions

View file

@ -72,13 +72,14 @@ impl Str for AttrValue {
#[jstraceable] #[jstraceable]
#[must_root] #[must_root]
#[privatize]
pub struct Attr { pub struct Attr {
reflector_: Reflector, reflector_: Reflector,
local_name: Atom, local_name: Atom,
value: RefCell<AttrValue>, value: RefCell<AttrValue>,
pub name: Atom, name: Atom,
pub namespace: Namespace, namespace: Namespace,
pub prefix: Option<DOMString>, prefix: Option<DOMString>,
/// the element that owns this attribute. /// the element that owns this attribute.
owner: JS<Element>, owner: JS<Element>,
@ -111,6 +112,21 @@ impl Attr {
reflect_dom_object(box Attr::new_inherited(local_name, value, name, namespace, prefix, owner), reflect_dom_object(box Attr::new_inherited(local_name, value, name, namespace, prefix, owner),
&global::Window(window), AttrBinding::Wrap) &global::Window(window), AttrBinding::Wrap)
} }
#[inline]
pub fn name<'a>(&'a self) -> &'a Atom {
&self.name
}
#[inline]
pub fn namespace<'a>(&'a self) -> &'a Namespace {
&self.namespace
}
#[inline]
pub fn prefix<'a>(&'a self) -> &'a Option<DOMString> {
&self.prefix
}
} }
impl<'a> AttrMethods for JSRef<'a, Attr> { impl<'a> AttrMethods for JSRef<'a, Attr> {

View file

@ -191,7 +191,7 @@ impl RawLayoutElementHelpers for Element {
(*attrs).iter().find(|attr: & &JS<Attr>| { (*attrs).iter().find(|attr: & &JS<Attr>| {
let attr = attr.unsafe_get(); let attr = attr.unsafe_get();
name == (*attr).local_name_atom_forever().as_slice() && name == (*attr).local_name_atom_forever().as_slice() &&
(*attr).namespace == *namespace *(*attr).namespace() == *namespace
}).map(|attr| { }).map(|attr| {
let attr = attr.unsafe_get(); let attr = attr.unsafe_get();
(*attr).value_ref_forever() (*attr).value_ref_forever()
@ -222,7 +222,7 @@ impl RawLayoutElementHelpers for Element {
(*attrs).iter().find(|attr: & &JS<Attr>| { (*attrs).iter().find(|attr: & &JS<Attr>| {
let attr = attr.unsafe_get(); let attr = attr.unsafe_get();
name == (*attr).local_name_atom_forever().as_slice() && name == (*attr).local_name_atom_forever().as_slice() &&
(*attr).namespace == *namespace *(*attr).namespace() == *namespace
}).and_then(|attr| { }).and_then(|attr| {
let attr = attr.unsafe_get(); let attr = attr.unsafe_get();
(*attr).value_atom_forever() (*attr).value_atom_forever()
@ -360,7 +360,7 @@ pub trait AttributeHandlers {
impl<'a> AttributeHandlers for JSRef<'a, Element> { impl<'a> AttributeHandlers for JSRef<'a, Element> {
fn get_attribute(self, namespace: Namespace, local_name: &str) -> Option<Temporary<Attr>> { fn get_attribute(self, namespace: Namespace, local_name: &str) -> Option<Temporary<Attr>> {
self.get_attributes(local_name).iter().map(|attr| attr.root()) self.get_attributes(local_name).iter().map(|attr| attr.root())
.find(|attr| attr.namespace == namespace) .find(|attr| *attr.namespace() == namespace)
.map(|x| Temporary::from_rooted(*x)) .map(|x| Temporary::from_rooted(*x))
} }
@ -496,7 +496,7 @@ impl<'a> AttributeHandlers for JSRef<'a, Element> {
false => Atom::from_slice(name) false => Atom::from_slice(name)
}; };
self.attrs.borrow().iter().map(|attr| attr.root()).any(|attr| { self.attrs.borrow().iter().map(|attr| attr.root()).any(|attr| {
*attr.local_name() == name && attr.namespace == ns!("") *attr.local_name() == name && *attr.namespace() == ns!("")
}) })
} }
@ -684,7 +684,7 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
let name = Atom::from_slice(name.as_slice()); let name = Atom::from_slice(name.as_slice());
let value = self.parse_attribute(&ns!(""), &name, value); let value = self.parse_attribute(&ns!(""), &name, value);
self.do_set_attribute(name.clone(), value, name.clone(), ns!(""), None, |attr| { self.do_set_attribute(name.clone(), value, name.clone(), ns!(""), None, |attr| {
attr.name.as_slice() == name.as_slice() attr.name().as_slice() == name.as_slice()
}); });
Ok(()) Ok(())
} }
@ -753,7 +753,7 @@ impl<'a> ElementMethods for JSRef<'a, Element> {
namespace.clone(), prefix.map(|s| s.to_string()), namespace.clone(), prefix.map(|s| s.to_string()),
|attr| { |attr| {
*attr.local_name() == local_name && *attr.local_name() == local_name &&
attr.namespace == namespace *attr.namespace() == namespace
}); });
Ok(()) Ok(())
} }

View file

@ -137,20 +137,20 @@ fn serialize_elem(elem: JSRef<Element>, open_elements: &mut Vec<String>, html: &
fn serialize_attr(attr: JSRef<Attr>, html: &mut String) { fn serialize_attr(attr: JSRef<Attr>, html: &mut String) {
html.push_char(' '); html.push_char(' ');
if attr.namespace == ns!(XML) { if *attr.namespace() == ns!(XML) {
html.push_str("xml:"); html.push_str("xml:");
html.push_str(attr.local_name().as_slice()); html.push_str(attr.local_name().as_slice());
} else if attr.namespace == ns!(XMLNS) && } else if *attr.namespace() == ns!(XMLNS) &&
*attr.local_name() == Atom::from_slice("xmlns") { *attr.local_name() == Atom::from_slice("xmlns") {
html.push_str("xmlns"); html.push_str("xmlns");
} else if attr.namespace == ns!(XMLNS) { } else if *attr.namespace() == ns!(XMLNS) {
html.push_str("xmlns:"); html.push_str("xmlns:");
html.push_str(attr.local_name().as_slice()); html.push_str(attr.local_name().as_slice());
} else if attr.namespace == ns!(XLink) { } else if *attr.namespace() == ns!(XLink) {
html.push_str("xlink:"); html.push_str("xlink:");
html.push_str(attr.local_name().as_slice()); html.push_str(attr.local_name().as_slice());
} else { } else {
html.push_str(attr.name.as_slice()); html.push_str(attr.name().as_slice());
}; };
html.push_str("=\""); html.push_str("=\"");
escape(attr.value().as_slice(), true, html); escape(attr.value().as_slice(), true, html);

View file

@ -1529,8 +1529,8 @@ impl Node {
copy_elem.attrs.borrow_mut().push_unrooted( copy_elem.attrs.borrow_mut().push_unrooted(
&Attr::new(*window, &Attr::new(*window,
attr.local_name().clone(), attr.value().clone(), attr.local_name().clone(), attr.value().clone(),
attr.name.clone(), attr.namespace.clone(), attr.name().clone(), attr.namespace().clone(),
attr.prefix.clone(), copy_elem)); attr.prefix().clone(), copy_elem));
} }
}, },
_ => () _ => ()
@ -1988,7 +1988,7 @@ impl<'a> NodeMethods for JSRef<'a, Node> {
assert!(element.attrs.borrow().len() == other_element.attrs.borrow().len()); assert!(element.attrs.borrow().len() == other_element.attrs.borrow().len());
element.attrs.borrow().iter().map(|attr| attr.root()).all(|attr| { element.attrs.borrow().iter().map(|attr| attr.root()).all(|attr| {
other_element.attrs.borrow().iter().map(|attr| attr.root()).any(|other_attr| { other_element.attrs.borrow().iter().map(|attr| attr.root()).any(|other_attr| {
(attr.namespace == other_attr.namespace) && (*attr.namespace() == *other_attr.namespace()) &&
(attr.local_name() == other_attr.local_name()) && (attr.local_name() == other_attr.local_name()) &&
(attr.value().as_slice() == other_attr.value().as_slice()) (attr.value().as_slice() == other_attr.value().as_slice())
}) })