Remove unnecessary allocation with getElementById

This commit is contained in:
Corey Farwell 2015-10-24 17:45:28 -04:00
parent 354e75a447
commit 48ea5959c7

View file

@ -419,7 +419,7 @@ impl Document {
/// Attempt to find a named element in this page's document. /// Attempt to find a named element in this page's document.
/// https://html.spec.whatwg.org/multipage/#the-indicated-part-of-the-document /// https://html.spec.whatwg.org/multipage/#the-indicated-part-of-the-document
pub fn find_fragment_node(&self, fragid: &str) -> Option<Root<Element>> { pub fn find_fragment_node(&self, fragid: &str) -> Option<Root<Element>> {
self.GetElementById(fragid.to_owned()).or_else(|| { self.get_element_by_id(&Atom::from_slice(fragid)).or_else(|| {
let check_anchor = |node: &HTMLAnchorElement| { let check_anchor = |node: &HTMLAnchorElement| {
let elem = node.upcast::<Element>(); let elem = node.upcast::<Element>();
elem.get_attribute(&ns!(""), &atom!("name")).map_or(false, |attr| { elem.get_attribute(&ns!(""), &atom!("name")).map_or(false, |attr| {
@ -1146,6 +1146,10 @@ impl Document {
new_doc new_doc
}) })
} }
fn get_element_by_id(&self, id: &Atom) -> Option<Root<Element>> {
self.idmap.borrow().get(&id).map(|ref elements| (*elements)[0].root())
}
} }
@ -1264,8 +1268,7 @@ impl DocumentMethods for Document {
// https://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid // https://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid
fn GetElementById(&self, id: DOMString) -> Option<Root<Element>> { fn GetElementById(&self, id: DOMString) -> Option<Root<Element>> {
let id = Atom::from_slice(&id); self.get_element_by_id(&Atom::from_slice(&id))
self.idmap.borrow().get(&id).map(|ref elements| (*elements)[0].root())
} }
// https://dom.spec.whatwg.org/#dom-document-createelement // https://dom.spec.whatwg.org/#dom-document-createelement