Cache the id attribute on Element

This commit is contained in:
Ali Sabil 2015-07-23 23:04:48 +02:00 committed by Anthony Ramine
parent cba8856309
commit ea655ada10
2 changed files with 22 additions and 24 deletions

View file

@ -95,6 +95,7 @@ pub struct Element {
namespace: Namespace,
prefix: Option<DOMString>,
attrs: DOMRefCell<Vec<JS<Attr>>>,
id_attribute: DOMRefCell<Option<Atom>>,
style_attribute: DOMRefCell<Option<PropertyDeclarationBlock>>,
attr_list: MutNullableHeap<JS<NamedNodeMap>>,
class_list: MutNullableHeap<JS<DOMTokenList>>,
@ -149,6 +150,7 @@ impl Element {
attrs: DOMRefCell::new(vec!()),
attr_list: Default::default(),
class_list: Default::default(),
id_attribute: DOMRefCell::new(None),
style_attribute: DOMRefCell::new(None),
}
}
@ -1466,6 +1468,15 @@ impl VirtualMethods for Element {
NodeDamage::NodeStyleDamaged
},
&atom!(id) => {
*self.id_attribute.borrow_mut() =
mutation.new_value(attr).and_then(|value| {
let value = value.as_atom();
if value != &atom!("") {
Some(value.clone())
} else {
None
}
});
if node.is_in_doc() {
let value = attr.value().as_atom().clone();
match mutation {
@ -1512,13 +1523,9 @@ impl VirtualMethods for Element {
if !tree_in_doc { return; }
if let Some(ref attr) = self.get_attribute(&ns!(""), &atom!("id")) {
let value = attr.value();
if !value.is_empty() {
let doc = document_from_node(self);
let value = Atom::from_slice(&value);
doc.register_named_element(self, value.to_owned());
}
if let Some(ref value) = *self.id_attribute.borrow() {
let doc = document_from_node(self);
doc.register_named_element(self, value.clone());
}
}
@ -1529,13 +1536,9 @@ impl VirtualMethods for Element {
if !tree_in_doc { return; }
if let Some(ref attr) = self.get_attribute(&ns!(""), &atom!("id")) {
let value = attr.value();
if !value.is_empty() {
let doc = document_from_node(self);
let value = Atom::from_slice(&value);
doc.unregister_named_element(self, value.to_owned());
}
if let Some(ref value) = *self.id_attribute.borrow() {
let doc = document_from_node(self);
doc.unregister_named_element(self, value.clone());
}
}
}
@ -1621,12 +1624,7 @@ impl<'a> ::selectors::Element for Root<Element> {
node.get_focus_state()
}
fn get_id(&self) -> Option<Atom> {
self.get_attribute(&ns!(""), &atom!("id")).map(|attr| {
match *attr.r().value() {
AttrValue::Atom(ref val) => val.clone(),
_ => panic!("`id` attribute should be AttrValue::Atom"),
}
})
self.id_attribute.borrow().clone()
}
fn get_disabled_state(&self) -> bool {
let node = NodeCast::from_ref(&**self);