diff --git a/content/base/public/nsContentUtils.h b/content/base/public/nsContentUtils.h index 265daf6c335a..80bec801724f 100644 --- a/content/base/public/nsContentUtils.h +++ b/content/base/public/nsContentUtils.h @@ -67,6 +67,7 @@ class nsIXPConnect; class nsINode; class nsIContent; class nsIDOMNode; +class nsIDOMKeyEvent; class nsIDocument; class nsIDocShell; class nsINameSpaceManager; @@ -1190,19 +1191,19 @@ public: PRBool aGetCharCode); /** - * Get the candidates for accelkeys for aDOMEvent. + * Get the candidates for accelkeys for aDOMKeyEvent. * - * @param aDOMEvent [in] the input event for accelkey handling. + * @param aDOMKeyEvent [in] the key event for accelkey handling. * @param aCandidates [out] the candidate shortcut key combination list. * the first item is most preferred. */ - static void GetAccelKeyCandidates(nsIDOMEvent* aDOMEvent, + static void GetAccelKeyCandidates(nsIDOMKeyEvent* aDOMKeyEvent, nsTArray& aCandidates); /** - * Get the candidates for accesskeys for aDOMEvent. + * Get the candidates for accesskeys for aNativeKeyEvent. * - * @param aNativeKeyEvent [in] the input event for accesskey handling. + * @param aNativeKeyEvent [in] the key event for accesskey handling. * @param aCandidates [out] the candidate access key list. * the first item is most preferred. */ diff --git a/content/base/src/nsContentUtils.cpp b/content/base/src/nsContentUtils.cpp index bae746620c05..fc8490b9cc07 100644 --- a/content/base/src/nsContentUtils.cpp +++ b/content/base/src/nsContentUtils.cpp @@ -4002,20 +4002,22 @@ IsCaseChangeableChar(PRUint32 aChar) /* static */ void -nsContentUtils::GetAccelKeyCandidates(nsIDOMEvent* aDOMEvent, +nsContentUtils::GetAccelKeyCandidates(nsIDOMKeyEvent* aDOMKeyEvent, nsTArray& aCandidates) { NS_PRECONDITION(aCandidates.IsEmpty(), "aCandidates must be empty"); nsAutoString eventType; - aDOMEvent->GetType(eventType); - // Don't process if aDOMEvent is not a keypress event. + aDOMKeyEvent->GetType(eventType); + // Don't process if aDOMKeyEvent is not a keypress event. if (!eventType.EqualsLiteral("keypress")) return; nsKeyEvent* nativeKeyEvent = - static_cast(GetNativeEvent(aDOMEvent)); + static_cast(GetNativeEvent(aDOMKeyEvent)); if (nativeKeyEvent) { + NS_ASSERTION(nativeKeyEvent->eventStructType == NS_KEY_EVENT, + "wrong type of native event"); // nsShortcutCandidate::mCharCode is a candidate charCode. // nsShoftcutCandidate::mIgnoreShift means the mCharCode should be tried to // execute a command with/without shift key state. If this is TRUE, the @@ -4092,9 +4094,8 @@ nsContentUtils::GetAccelKeyCandidates(nsIDOMEvent* aDOMEvent, } } } else { - nsCOMPtr key(do_QueryInterface(aDOMEvent)); PRUint32 charCode; - key->GetCharCode(&charCode); + aDOMKeyEvent->GetCharCode(&charCode); if (charCode) { nsShortcutCandidate key(charCode, PR_FALSE); aCandidates.AppendElement(key); diff --git a/content/xbl/src/nsXBLEventHandler.cpp b/content/xbl/src/nsXBLEventHandler.cpp index e280dda9407d..b85be41cb8a4 100644 --- a/content/xbl/src/nsXBLEventHandler.cpp +++ b/content/xbl/src/nsXBLEventHandler.cpp @@ -166,9 +166,11 @@ nsXBLKeyEventHandler::HandleEvent(nsIDOMEvent* aEvent) } nsCOMPtr key(do_QueryInterface(aEvent)); + if (!key) + return NS_OK; nsAutoTArray accessKeys; - nsContentUtils::GetAccelKeyCandidates(aEvent, accessKeys); + nsContentUtils::GetAccelKeyCandidates(key, accessKeys); if (accessKeys.IsEmpty()) { ExecuteMatchedHandlers(key, 0, PR_FALSE); diff --git a/content/xbl/src/nsXBLWindowKeyHandler.cpp b/content/xbl/src/nsXBLWindowKeyHandler.cpp index c501904e77cd..8d5a30f746c2 100644 --- a/content/xbl/src/nsXBLWindowKeyHandler.cpp +++ b/content/xbl/src/nsXBLWindowKeyHandler.cpp @@ -318,7 +318,7 @@ DoCommandCallback(const char *aCommand, void *aData) } nsresult -nsXBLWindowKeyHandler::WalkHandlers(nsIDOMEvent* aKeyEvent, nsIAtom* aEventType) +nsXBLWindowKeyHandler::WalkHandlers(nsIDOMKeyEvent* aKeyEvent, nsIAtom* aEventType) { nsCOMPtr evt = do_QueryInterface(aKeyEvent); PRBool prevent; @@ -337,11 +337,6 @@ nsXBLWindowKeyHandler::WalkHandlers(nsIDOMEvent* aKeyEvent, nsIAtom* aEventType) if (!trustedEvent) return NS_OK; - // Make sure our event is really a key event - nsCOMPtr keyEvent(do_QueryInterface(aKeyEvent)); - if (!keyEvent) - return NS_OK; - PRBool isEditor; nsresult rv = EnsureHandlers(&isEditor); NS_ENSURE_SUCCESS(rv, rv); @@ -402,19 +397,25 @@ nsXBLWindowKeyHandler::WalkHandlers(nsIDOMEvent* aKeyEvent, nsIAtom* aEventType) return NS_OK; } -nsresult nsXBLWindowKeyHandler::KeyUp(nsIDOMEvent* aKeyEvent) +nsresult nsXBLWindowKeyHandler::KeyUp(nsIDOMEvent* aEvent) { - return WalkHandlers(aKeyEvent, nsGkAtoms::keyup); + nsCOMPtr keyEvent(do_QueryInterface(aEvent)); + NS_ENSURE_TRUE(keyEvent, NS_ERROR_INVALID_ARG); + return WalkHandlers(keyEvent, nsGkAtoms::keyup); } -nsresult nsXBLWindowKeyHandler::KeyDown(nsIDOMEvent* aKeyEvent) +nsresult nsXBLWindowKeyHandler::KeyDown(nsIDOMEvent* aEvent) { - return WalkHandlers(aKeyEvent, nsGkAtoms::keydown); + nsCOMPtr keyEvent(do_QueryInterface(aEvent)); + NS_ENSURE_TRUE(keyEvent, NS_ERROR_INVALID_ARG); + return WalkHandlers(keyEvent, nsGkAtoms::keydown); } -nsresult nsXBLWindowKeyHandler::KeyPress(nsIDOMEvent* aKeyEvent) +nsresult nsXBLWindowKeyHandler::KeyPress(nsIDOMEvent* aEvent) { - return WalkHandlers(aKeyEvent, nsGkAtoms::keypress); + nsCOMPtr keyEvent(do_QueryInterface(aEvent)); + NS_ENSURE_TRUE(keyEvent, NS_ERROR_INVALID_ARG); + return WalkHandlers(keyEvent, nsGkAtoms::keypress); } @@ -425,15 +426,12 @@ nsresult nsXBLWindowKeyHandler::KeyPress(nsIDOMEvent* aKeyEvent) // PRBool nsXBLWindowKeyHandler::EventMatched(nsXBLPrototypeHandler* inHandler, - nsIAtom* inEventType, nsIDOMEvent* inEvent, + nsIAtom* inEventType, + nsIDOMKeyEvent* inEvent, PRUint32 aCharCode, PRBool aIgnoreShiftKey) { - nsCOMPtr keyEvent(do_QueryInterface(inEvent)); - if (keyEvent) - return inHandler->KeyEventMatched(inEventType, keyEvent, aCharCode, - aIgnoreShiftKey); - - return PR_FALSE; + return inHandler->KeyEventMatched(inEventType, inEvent, aCharCode, + aIgnoreShiftKey); } /* static */ void @@ -487,21 +485,21 @@ nsXBLWindowKeyHandler::IsEditor() // so. // nsresult -nsXBLWindowKeyHandler::WalkHandlersInternal(nsIDOMEvent* aEvent, +nsXBLWindowKeyHandler::WalkHandlersInternal(nsIDOMKeyEvent* aKeyEvent, nsIAtom* aEventType, nsXBLPrototypeHandler* aHandler) { nsAutoTArray accessKeys; - nsContentUtils::GetAccelKeyCandidates(aEvent, accessKeys); + nsContentUtils::GetAccelKeyCandidates(aKeyEvent, accessKeys); if (accessKeys.IsEmpty()) { - WalkHandlersAndExecute(aEvent, aEventType, aHandler, 0, PR_FALSE); + WalkHandlersAndExecute(aKeyEvent, aEventType, aHandler, 0, PR_FALSE); return NS_OK; } for (PRUint32 i = 0; i < accessKeys.Length(); ++i) { nsShortcutCandidate &key = accessKeys[i]; - if (WalkHandlersAndExecute(aEvent, aEventType, aHandler, + if (WalkHandlersAndExecute(aKeyEvent, aEventType, aHandler, key.mCharCode, key.mIgnoreShift)) return NS_OK; } @@ -509,14 +507,14 @@ nsXBLWindowKeyHandler::WalkHandlersInternal(nsIDOMEvent* aEvent, } PRBool -nsXBLWindowKeyHandler::WalkHandlersAndExecute(nsIDOMEvent* aEvent, +nsXBLWindowKeyHandler::WalkHandlersAndExecute(nsIDOMKeyEvent* aKeyEvent, nsIAtom* aEventType, nsXBLPrototypeHandler* aHandler, PRUint32 aCharCode, PRBool aIgnoreShiftKey) { nsresult rv; - nsCOMPtr privateEvent(do_QueryInterface(aEvent)); + nsCOMPtr privateEvent(do_QueryInterface(aKeyEvent)); // Try all of the handlers until we find one that matches the event. for (nsXBLPrototypeHandler *currHandler = aHandler; currHandler; @@ -528,7 +526,7 @@ nsXBLWindowKeyHandler::WalkHandlersAndExecute(nsIDOMEvent* aEvent, return NS_OK; } - if (!EventMatched(currHandler, aEventType, aEvent, + if (!EventMatched(currHandler, aEventType, aKeyEvent, aCharCode, aIgnoreShiftKey)) continue; // try the next one @@ -586,7 +584,7 @@ nsXBLWindowKeyHandler::WalkHandlersAndExecute(nsIDOMEvent* aEvent, piTarget = mTarget; } - rv = currHandler->ExecuteHandler(piTarget, aEvent); + rv = currHandler->ExecuteHandler(piTarget, aKeyEvent); if (NS_SUCCEEDED(rv)) { return PR_TRUE; } diff --git a/content/xbl/src/nsXBLWindowKeyHandler.h b/content/xbl/src/nsXBLWindowKeyHandler.h index 245d805b8226..3277ec8c4968 100644 --- a/content/xbl/src/nsXBLWindowKeyHandler.h +++ b/content/xbl/src/nsXBLWindowKeyHandler.h @@ -46,6 +46,7 @@ class nsIAtom; class nsIDOMElement; class nsIDOMEventTarget; +class nsIDOMKeyEvent; class nsPIDOMEventTarget; class nsIXBLDocumentInfo; class nsXBLSpecialDocInfo; @@ -73,15 +74,15 @@ public: static NS_HIDDEN_(void) ShutDown(); protected: - nsresult WalkHandlers(nsIDOMEvent* aKeyEvent, nsIAtom* aEventType); + nsresult WalkHandlers(nsIDOMKeyEvent* aKeyEvent, nsIAtom* aEventType); // walk the handlers, looking for one to handle the event - nsresult WalkHandlersInternal(nsIDOMEvent* aKeyEvent, + nsresult WalkHandlersInternal(nsIDOMKeyEvent* aKeyEvent, nsIAtom* aEventType, nsXBLPrototypeHandler* aHandler); // walk the handlers for aEvent, aCharCode and aIgnoreShiftKey - PRBool WalkHandlersAndExecute(nsIDOMEvent* aEvent, nsIAtom* aEventType, + PRBool WalkHandlersAndExecute(nsIDOMKeyEvent* aKeyEvent, nsIAtom* aEventType, nsXBLPrototypeHandler* aHandler, PRUint32 aCharCode, PRBool aIgnoreShiftKey); @@ -91,7 +92,7 @@ protected: // check if the given handler cares about the given key event PRBool EventMatched(nsXBLPrototypeHandler* inHandler, nsIAtom* inEventType, - nsIDOMEvent* inEvent, PRUint32 aCharCode, + nsIDOMKeyEvent* inEvent, PRUint32 aCharCode, PRBool aIgnoreShiftKey); // are we working with editor or browser?