From 91b787ae177d5f57919d416955e8d8c85e79019d Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Sat, 21 Feb 2009 23:27:18 -0600 Subject: [PATCH] Ensure the `target` property on events is never undefined in IE. [#383 state:resolved] --- CHANGELOG | 2 ++ src/dom/event.js | 14 +++++++++----- test/functional/event.html | 9 +++++++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d3050b5..bd3bcf3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Ensure the `target` property on events is never undefined in IE. [#383 state:resolved] (Mathias Karstädt, Diego Perini, Andrew Dupont) + * Ensure Element#descendants always returns an array. [#373 state:resolved] (kangax) * Don't switch fixed position elements to absolute in Element.getDimensions [#543 state:resolved] (James Wheare) diff --git a/src/dom/event.js b/src/dom/event.js index 9ab4217..65e5b01 100644 --- a/src/dom/event.js +++ b/src/dom/event.js @@ -142,18 +142,22 @@ }); // IE's method for extending events. - Event.extend = function(event) { + Event.extend = function(event, element) { if (!event) return false; if (event._extendedByPrototype) return event; event._extendedByPrototype = Prototype.emptyFunction; var pointer = Event.pointer(event); + + // The optional `element` argument gives us a fallback value for the + // `target` property in case IE doesn't give us through `srcElement`. Object.extend(event, { - target: event.srcElement, + target: event.srcElement || element, relatedTarget: _relatedTarget(event), pageX: pointer.x, pageY: pointer.y }); + return Object.extend(event, methods); }; } else { @@ -171,7 +175,7 @@ // First time we've handled this element. Put it into the cache. CACHE.push(element); registry = Element.retrieve(element, 'prototype_event_registry', $H()); - } + } var respondersForEvent = registry.get(eventName); if (Object.isUndefined()) { @@ -195,13 +199,13 @@ if (event.eventName !== eventName) return false; - Event.extend(event); + Event.extend(event, element); handler.call(element, event); }; } else { // Ordinary event. responder = function(event) { - Event.extend(event); + Event.extend(event, element); handler.call(element, event); }; } diff --git a/test/functional/event.html b/test/functional/event.html index 378b2fb..611d5e6 100644 --- a/test/functional/event.html +++ b/test/functional/event.html @@ -229,10 +229,15 @@