diff --git a/CHANGELOG b/CHANGELOG index 5dfb673..66ab8ea 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +* Performance optimizations for Event#findElement. Make Event#findElement's expression argument optional, in which case the extended target element is returned (same as Event#element). [Tobie Langel] + +* Ensure Event#fire always returns an extended event. [Tobie Langel] + *1.6.0* (November 6, 2007) * Fix Class#addMethods for "toString" and "valueOf" methods in Internet Explorer. Closes #9901. [sam] diff --git a/src/event.js b/src/event.js index 63379cd..6265d51 100644 --- a/src/event.js +++ b/src/event.js @@ -65,7 +65,9 @@ Event.Methods = (function() { findElement: function(event, expression) { var element = Event.element(event); - return element.match(expression) ? element : element.up(expression); + if (!expression) return element; + var elements = [element].concat(element.ancestors()); + return Selector.findElement(elements, expression, 0); }, pointer: function(event) { @@ -257,7 +259,7 @@ Object.extend(Event, (function() { element.fireEvent(event.eventType, event); } - return event; + return Event.extend(event); } }; })()); diff --git a/test/unit/event.html b/test/unit/event.html index 036f203..f8b0459 100644 --- a/test/unit/event.html +++ b/test/unit/event.html @@ -111,6 +111,9 @@ assertEqual(event, observedEvent); assertEqual(Event.Methods.stop.methodize(), event.stop); span.stopObserving("test:somethingHappened", observer); + + event = span.fire("test:somethingHappenedButNoOneIsListening"); + assertEqual(Event.Methods.stop.methodize(), event.stop); }}, testEventObserversAreBoundToTheObservedElement: function() { with(this) { @@ -222,6 +225,14 @@ event = span.fire("test:somethingHappened"); assert(event.stopped, "event.stopped should be true for an observer that calls stop"); span.stopObserving("test:somethingHappened"); + }}, + + testEventFindElement: function() { with(this) { + var span = $("span"), event; + event = span.fire("test:somethingHappened"); + assertEqual(span, event.findElement()); + assertEqual(span, event.findElement('span')); + assertEqual(null, event.findElement('div.does_not_exist')); }}