From 3347c3ef80cf64684f36edfba675e0dd0e47f8af Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Mon, 7 Apr 2014 17:36:49 +0200 Subject: [PATCH] Split out a do_set_attribute method. --- src/components/script/dom/element.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/components/script/dom/element.rs b/src/components/script/dom/element.rs index 7b0ab7bd54e..a9cee2c8650 100644 --- a/src/components/script/dom/element.rs +++ b/src/components/script/dom/element.rs @@ -195,6 +195,10 @@ pub trait AttributeHandlers { fn set_attr(&mut self, name: DOMString, value: DOMString) -> ErrorResult; fn set_attribute(&mut self, namespace: Namespace, name: DOMString, value: DOMString) -> ErrorResult; + fn do_set_attribute(&mut self, local_name: DOMString, value: DOMString, + name: DOMString, namespace: Namespace, + prefix: Option, cb: |&JS| -> bool); + fn after_set_attr(&mut self, local_name: DOMString, value: DOMString); fn remove_attribute(&mut self, namespace: Namespace, name: DOMString) -> ErrorResult; fn before_remove_attr(&mut self, local_name: DOMString, old_value: DOMString); @@ -253,15 +257,20 @@ impl AttributeHandlers for JS { let node: JS = NodeCast::from(self); node.get().wait_until_safe_to_modify_dom(); - // FIXME: reduce the time of `value.clone()`. - let idx = self.get().attrs.iter().position(|attr| { + let position: |&JS| -> bool = if self.get().html_element_in_html_document() { - attr.get().local_name.eq_ignore_ascii_case(local_name) + |attr| attr.get().local_name.eq_ignore_ascii_case(local_name) } else { - attr.get().local_name == local_name - } - }); + |attr| attr.get().local_name == local_name + }; + self.do_set_attribute(name.clone(), value, name.clone(), namespace::Null, None, position); + Ok(()) + } + fn do_set_attribute(&mut self, local_name: DOMString, value: DOMString, + name: DOMString, namespace: Namespace, + prefix: Option, cb: |&JS| -> bool) { + let idx = self.get().attrs.iter().position(cb); match idx { Some(idx) => { if namespace == namespace::Null { @@ -270,12 +279,12 @@ impl AttributeHandlers for JS { } self.get_mut().attrs[idx].get_mut().set_value(value.clone()); } + None => { let node: JS = NodeCast::from(self); let doc = node.get().owner_doc().get(); let new_attr = Attr::new_ns(&doc.window, local_name.clone(), value.clone(), - name.clone(), namespace.clone(), - prefix); + name, namespace.clone(), prefix); self.get_mut().attrs.push(new_attr); } } @@ -283,7 +292,6 @@ impl AttributeHandlers for JS { if namespace == namespace::Null { self.after_set_attr(local_name, value); } - Ok(()) } fn after_set_attr(&mut self, local_name: DOMString, value: DOMString) {