servo: Merge #13600 - Invert conditional and return early for AddEventListener/RemoveEventL… (from slester:13586); r=jdm
authorStephen Lester <stephen@lester.io>
Wed, 05 Oct 2016 12:04:03 -0500
changeset 339840 87fc57c8457def5fa65210f7e957998e14dc5ad6
parent 339839 9bb1e7415bd101835a773421b5a5ae288a06c98f
child 339841 3cce2d176a7fe234364c429b7d9918797a0792b6
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
servo: Merge #13600 - Invert conditional and return early for AddEventListener/RemoveEventL… (from slester:13586); r=jdm <!-- Please describe your changes on the following line: --> Cleans up code as described in #13586 by returning early instead of nesting everything in an `if` statement. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #13586 <!-- Either: --> - [x] These changes do not require tests because the change is a cleanup of code and the author of the issue stated that new tests were not required. <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> …istener Source-Repo: https://github.com/servo/servo Source-Revision: 596fdf2fef0c2e0a3d02ce9f7ed3cd5a1c0480a6
servo/components/script/dom/eventtarget.rs
--- a/servo/components/script/dom/eventtarget.rs
+++ b/servo/components/script/dom/eventtarget.rs
@@ -510,51 +510,55 @@ impl EventTarget {
 }
 
 impl EventTargetMethods for EventTarget {
     // https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener
     fn AddEventListener(&self,
                         ty: DOMString,
                         listener: Option<Rc<EventListener>>,
                         capture: bool) {
-        if let Some(listener) = listener {
-            let mut handlers = self.handlers.borrow_mut();
-            let entry = match handlers.entry(Atom::from(ty)) {
-                Occupied(entry) => entry.into_mut(),
-                Vacant(entry) => entry.insert(EventListeners(vec!())),
-            };
+        let listener = match listener {
+            Some(l) => l,
+            None => return,
+        };
+        let mut handlers = self.handlers.borrow_mut();
+        let entry = match handlers.entry(Atom::from(ty)) {
+            Occupied(entry) => entry.into_mut(),
+            Vacant(entry) => entry.insert(EventListeners(vec!())),
+        };
 
-            let phase = if capture { ListenerPhase::Capturing } else { ListenerPhase::Bubbling };
-            let new_entry = EventListenerEntry {
-                phase: phase,
-                listener: EventListenerType::Additive(listener)
-            };
-            if !entry.contains(&new_entry) {
-                entry.push(new_entry);
-            }
+        let phase = if capture { ListenerPhase::Capturing } else { ListenerPhase::Bubbling };
+        let new_entry = EventListenerEntry {
+            phase: phase,
+            listener: EventListenerType::Additive(listener)
+        };
+        if !entry.contains(&new_entry) {
+            entry.push(new_entry);
         }
     }
 
     // https://dom.spec.whatwg.org/#dom-eventtarget-removeeventlistener
     fn RemoveEventListener(&self,
                            ty: DOMString,
                            listener: Option<Rc<EventListener>>,
                            capture: bool) {
-        if let Some(ref listener) = listener {
-            let mut handlers = self.handlers.borrow_mut();
-            let entry = handlers.get_mut(&Atom::from(ty));
-            for entry in entry {
-                let phase = if capture { ListenerPhase::Capturing } else { ListenerPhase::Bubbling };
-                let old_entry = EventListenerEntry {
-                    phase: phase,
-                    listener: EventListenerType::Additive(listener.clone())
-                };
-                if let Some(position) = entry.iter().position(|e| *e == old_entry) {
-                    entry.remove(position);
-                }
+        let ref listener = match listener {
+            Some(l) => l,
+            None => return,
+        };
+        let mut handlers = self.handlers.borrow_mut();
+        let entry = handlers.get_mut(&Atom::from(ty));
+        for entry in entry {
+            let phase = if capture { ListenerPhase::Capturing } else { ListenerPhase::Bubbling };
+            let old_entry = EventListenerEntry {
+                phase: phase,
+                listener: EventListenerType::Additive(listener.clone())
+            };
+            if let Some(position) = entry.iter().position(|e| *e == old_entry) {
+                entry.remove(position);
             }
         }
     }
 
     // https://dom.spec.whatwg.org/#dom-eventtarget-dispatchevent
     fn DispatchEvent(&self, event: &Event) -> Fallible<bool> {
         if event.dispatching() || !event.initialized() {
             return Err(Error::InvalidState);