From a743394ec705a298703f21cdd8eb26a3180008fc Mon Sep 17 00:00:00 2001 From: "jst%mozilla.jstenback.com" Date: Tue, 18 Nov 2003 01:59:51 +0000 Subject: [PATCH] Re-landing fix related to regression bug 147811. Make declaring on* functions trigger the event listener registration code so that event handlers get registerd by simply declaring a global (or whatever the scope) function with the name of a event handler. r=caillon@aillon.org, sr=brendan@mozilla.org. --- content/events/src/nsEventListenerManager.cpp | 26 +++++++++---------- dom/src/base/nsDOMClassInfo.cpp | 20 +++++++++++--- dom/src/base/nsDOMClassInfo.h | 2 ++ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/content/events/src/nsEventListenerManager.cpp b/content/events/src/nsEventListenerManager.cpp index 93dae21557e..7adea19d1bb 100644 --- a/content/events/src/nsEventListenerManager.cpp +++ b/content/events/src/nsEventListenerManager.cpp @@ -697,7 +697,7 @@ nsEventListenerManager::AddEventListener(nsIDOMEventListener *aListener, //method to verify equality. nsCOMPtr regSel = do_QueryInterface(ls->mListener, &rv); if (NS_SUCCEEDED(rv) && regSel) { - PRBool equal; + PRBool equal; if (NS_SUCCEEDED(regSel->CheckIfEqual(sel, &equal)) && equal) { if (ls->mFlags & aFlags && ls->mSubType & aSubType) { found = PR_TRUE; @@ -1214,10 +1214,10 @@ nsEventListenerManager::RegisterScriptEventListener(nsIScriptContext *aContext, nsCOMPtr stack = do_GetService("@mozilla.org/js/xpc/ContextStack;1", &rv); if (NS_FAILED(rv)) - return rv; + return rv; JSContext *cx; - if (NS_FAILED(stack->Peek(&cx))) - return nsnull; + if (NS_FAILED(rv = stack->Peek(&cx))) + return rv; JSContext *current_cx = (JSContext *)aContext->GetNativeContext(); @@ -1235,18 +1235,18 @@ nsEventListenerManager::RegisterScriptEventListener(nsIScriptContext *aContext, nsCOMPtr classInfo = do_QueryInterface(aObject); - if (sAddListenerID == JSVAL_VOID) { + if (sAddListenerID == JSVAL_VOID && cx) { sAddListenerID = STRING_TO_JSVAL(::JS_InternString(cx, "addEventListener")); - } - rv = nsContentUtils::GetSecurityManager()-> - CheckPropertyAccess(cx, jsobj, - "EventTarget", - sAddListenerID, - nsIXPCSecurityManager::ACCESS_SET_PROPERTY); - if (NS_FAILED(rv)) { + rv = nsContentUtils::GetSecurityManager()-> + CheckPropertyAccess(cx, jsobj, + "EventTarget", + sAddListenerID, + nsIXPCSecurityManager::ACCESS_SET_PROPERTY); + if (NS_FAILED(rv)) { // XXX set pending exception on the native call context? - return rv; + return rv; + } } return SetJSEventListener(aContext, aObject, aName, PR_FALSE); diff --git a/dom/src/base/nsDOMClassInfo.cpp b/dom/src/base/nsDOMClassInfo.cpp index 5158d3623ba..ce1f2b7b90c 100644 --- a/dom/src/base/nsDOMClassInfo.cpp +++ b/dom/src/base/nsDOMClassInfo.cpp @@ -3214,9 +3214,11 @@ nsWindowSH::AddProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, // exception, we must make sure that exception is propagated. *_retval = PR_FALSE; + + return NS_OK; } - return NS_OK; + return nsEventReceiverSH::AddProperty(wrapper, cx, obj, id, vp, _retval); } NS_IMETHODIMP @@ -4502,7 +4504,7 @@ nsNodeSH::AddProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, doc->AddReference(content, wrapper); } - return NS_OK; + return nsEventReceiverSH::AddProperty(wrapper, cx, obj, id, vp, _retval); } NS_IMETHODIMP @@ -4615,7 +4617,9 @@ nsEventReceiverSH::NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval id, PRUint32 flags, JSObject **objp, PRBool *_retval) { - if (!JSVAL_IS_STRING(id)) { + // If we're assigning to an on* property, we'll register the handler + // in our ::SetProperty() hook, so no need to do it here too. + if (!JSVAL_IS_STRING(id) || (flags & JSRESOLVE_ASSIGNING)) { return NS_OK; } @@ -4647,6 +4651,14 @@ nsEventReceiverSH::SetProperty(nsIXPConnectWrappedNative *wrapper, return RegisterCompileHandler(wrapper, cx, obj, id, PR_FALSE, &did_compile); } +NS_IMETHODIMP +nsEventReceiverSH::AddProperty(nsIXPConnectWrappedNative *wrapper, + JSContext *cx, JSObject *obj, jsval id, + jsval *vp, PRBool *_retval) +{ + return nsEventReceiverSH::SetProperty(wrapper, cx, obj, id, vp, _retval); +} + /* NS_IMETHODIMP nsEventReceiverSH::OnFinalize(...) @@ -5292,7 +5304,7 @@ nsHTMLDocumentSH::DocumentOpen(JSContext *cx, JSObject *obj, uintN argc, nsresult rv = sXPConnect->GetWrappedNativeOfJSObject(cx, obj, getter_AddRefs(wrapper)); if (NS_FAILED(rv)) { - ThrowJSException(cx, rv); + nsDOMClassInfo::ThrowJSException(cx, rv); return JS_FALSE; } diff --git a/dom/src/base/nsDOMClassInfo.h b/dom/src/base/nsDOMClassInfo.h index 9aadf7b51a2..1bf4022c5b3 100644 --- a/dom/src/base/nsDOMClassInfo.h +++ b/dom/src/base/nsDOMClassInfo.h @@ -310,6 +310,8 @@ public: NS_IMETHOD SetProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, JSObject *obj, jsval id, jsval *vp, PRBool *_retval); + NS_IMETHOD AddProperty(nsIXPConnectWrappedNative *wrapper, JSContext *cx, + JSObject *obj, jsval id, jsval *vp, PRBool *_retval); };