From 098159977ec411c4a03204e3473a87e85545163f Mon Sep 17 00:00:00 2001 From: "joki%netscape.com" Date: Fri, 31 Jul 1998 18:21:37 +0000 Subject: [PATCH] Adding support for event handlers in html tags and moving event handler IIDs to central file. git-svn-id: svn://10.0.0.236/trunk@6962 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/content/base/src/nsDocument.cpp | 8 +- .../events/public/nsIEventListenerManager.h | 19 ++- mozilla/content/events/src/MANIFEST | 1 + mozilla/content/events/src/makefile.win | 4 +- .../content/events/src/nsDOMEventsIIDs.cpp | 31 ++++ mozilla/content/events/src/nsDOMEventsIIDs.h | 30 ++++ .../events/src/nsEventListenerManager.cpp | 69 +++++++++ .../events/src/nsEventListenerManager.h | 20 ++- .../content/html/content/src/nsHTMLAtoms.cpp | 39 +++++ .../content/html/content/src/nsHTMLAtoms.h | 13 ++ mozilla/content/shared/public/nsHTMLAtoms.h | 13 ++ mozilla/content/shared/src/nsHTMLAtoms.cpp | 39 +++++ mozilla/layout/base/src/nsDocument.cpp | 8 +- .../events/public/nsIEventListenerManager.h | 19 ++- mozilla/layout/events/src/MANIFEST | 1 + mozilla/layout/events/src/Makefile | 2 + mozilla/layout/events/src/makefile.win | 4 +- mozilla/layout/events/src/nsDOMEventsIIDs.cpp | 31 ++++ mozilla/layout/events/src/nsDOMEventsIIDs.h | 30 ++++ .../events/src/nsEventListenerManager.cpp | 69 +++++++++ .../events/src/nsEventListenerManager.h | 20 ++- mozilla/layout/html/base/src/nsHTMLAtoms.cpp | 39 +++++ mozilla/layout/html/base/src/nsHTMLAtoms.h | 13 ++ .../layout/html/base/src/nsHTMLTagContent.cpp | 138 +++++++++++------- .../layout/html/base/src/nsHTMLTagContent.h | 9 +- 25 files changed, 589 insertions(+), 80 deletions(-) create mode 100644 mozilla/content/events/src/nsDOMEventsIIDs.cpp create mode 100644 mozilla/content/events/src/nsDOMEventsIIDs.h create mode 100644 mozilla/layout/events/src/nsDOMEventsIIDs.cpp create mode 100644 mozilla/layout/events/src/nsDOMEventsIIDs.h diff --git a/mozilla/content/base/src/nsDocument.cpp b/mozilla/content/base/src/nsDocument.cpp index 3d2a19fa1c2..b09185c7a7b 100644 --- a/mozilla/content/base/src/nsDocument.cpp +++ b/mozilla/content/base/src/nsDocument.cpp @@ -31,12 +31,11 @@ #include "nsIScriptContextOwner.h" #include "nsIScriptEventListener.h" #include "nsDOMEvent.h" +#include "nsDOMEventsIIDs.h" #include "nsIPrivateDOMEvent.h" #include "nsIEventStateManager.h" -#include "nsIDOMMouseListener.h" -#include "nsIDOMKeyListener.h" -#include "nsIDOMMouseMotionListener.h" #include "nsContentList.h" +#include "nsIDOMEventListener.h" #include "nsCSSPropIDs.h" #include "nsCSSProps.h" @@ -63,9 +62,6 @@ static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); static NS_DEFINE_IID(kIScriptEventListenerIID, NS_ISCRIPTEVENTLISTENER_IID); static NS_DEFINE_IID(kIDOMEventCapturerIID, NS_IDOMEVENTCAPTURER_IID); static NS_DEFINE_IID(kIDOMEventReceiverIID, NS_IDOMEVENTRECEIVER_IID); -static NS_DEFINE_IID(kIDOMMouseListenerIID, NS_IDOMMOUSELISTENER_IID); -static NS_DEFINE_IID(kIDOMKeyListenerIID, NS_IDOMKEYLISTENER_IID); -static NS_DEFINE_IID(kIDOMMouseMotionListenerIID, NS_IDOMMOUSEMOTIONLISTENER_IID); static NS_DEFINE_IID(kIPrivateDOMEventIID, NS_IPRIVATEDOMEVENT_IID); static NS_DEFINE_IID(kIEventListenerManagerIID, NS_IEVENTLISTENERMANAGER_IID); static NS_DEFINE_IID(kIPostDataIID, NS_IPOSTDATA_IID); diff --git a/mozilla/content/events/public/nsIEventListenerManager.h b/mozilla/content/events/public/nsIEventListenerManager.h index 28b8f402f57..f7b7822fc1c 100644 --- a/mozilla/content/events/public/nsIEventListenerManager.h +++ b/mozilla/content/events/public/nsIEventListenerManager.h @@ -25,7 +25,7 @@ class nsIPresContext; class nsIDOMEventListener; -class nsIDOMEvent; +class nsIScriptObjectOwner; /* * Event listener manager interface. @@ -52,6 +52,23 @@ public: */ virtual nsresult AddEventListener(nsIDOMEventListener *aListener, const nsIID& aIID) = 0; + + /** + * Creates a script event listener for the given script object with name mName and function + * content mFunc. + * @param an event listener + */ + + virtual nsresult AddScriptEventListener(nsIScriptContext*aContext, nsIScriptObjectOwner *aScriptObjectOwner, + nsIAtom *aName, const nsString& aFunc, REFNSIID aIID) = 0; + /** + * Registers an event listners which already exists on the given script object with the event + * listener manager. + * @param an event listener + */ + + virtual nsresult RegisterScriptEventListener(nsIScriptContext *aContext, nsIScriptObjectOwner *aScriptObjectOwner, + REFNSIID aIID) = 0; /** * Removes events listeners of all types. diff --git a/mozilla/content/events/src/MANIFEST b/mozilla/content/events/src/MANIFEST index 0c01cf7c6c3..3f21351764f 100644 --- a/mozilla/content/events/src/MANIFEST +++ b/mozilla/content/events/src/MANIFEST @@ -4,3 +4,4 @@ nsEventListenerManager.h nsEventStateManager.h nsDOMEvent.h +nsDOMEventsIIDs.h diff --git a/mozilla/content/events/src/makefile.win b/mozilla/content/events/src/makefile.win index fb627926014..f952b3035ad 100644 --- a/mozilla/content/events/src/makefile.win +++ b/mozilla/content/events/src/makefile.win @@ -29,15 +29,17 @@ DEFINES=-D_IMPL_NS_HTML -DWIN32_LEAN_AND_MEAN CPPSRCS= nsEventListenerManager.cpp \ nsEventStateManager.cpp \ nsDOMEvent.cpp \ + nsDOMEventsIIDs.cpp \ $(NULL) CPP_OBJS= .\$(OBJDIR)\nsEventListenerManager.obj \ .\$(OBJDIR)\nsEventStateManager.obj \ .\$(OBJDIR)\nsDOMEvent.obj \ + .\$(OBJDIR)\nsDOMEventsIIDs.obj \ $(NULL) -EXPORTS= nsEventListenerManager.h nsEventStateManager.h nsDOMEvent.h +EXPORTS= nsEventListenerManager.h nsEventStateManager.h nsDOMEvent.h nsDOMEventsIIDs.h LINCS=-I$(PUBLIC)\xpcom -I$(PUBLIC)\raptor \ -I$(PUBLIC)\dom -I$(PUBLIC)\js diff --git a/mozilla/content/events/src/nsDOMEventsIIDs.cpp b/mozilla/content/events/src/nsDOMEventsIIDs.cpp new file mode 100644 index 00000000000..c3b86931972 --- /dev/null +++ b/mozilla/content/events/src/nsDOMEventsIIDs.cpp @@ -0,0 +1,31 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ +#include "nsDOMEventsIIDs.h" +#include "nsIDOMMouseListener.h" +#include "nsIDOMKeyListener.h" +#include "nsIDOMMouseMotionListener.h" +#include "nsIDOMFocusListener.h" +#include "nsIDOMLoadListener.h" +#include "nsIDOMDragListener.h" + +NS_DEFINE_IID(kIDOMMouseListenerIID, NS_IDOMMOUSELISTENER_IID); +NS_DEFINE_IID(kIDOMKeyListenerIID, NS_IDOMKEYLISTENER_IID); +NS_DEFINE_IID(kIDOMMouseMotionListenerIID, NS_IDOMMOUSEMOTIONLISTENER_IID); +NS_DEFINE_IID(kIDOMFocusListenerIID, NS_IDOMFOCUSLISTENER_IID); +NS_DEFINE_IID(kIDOMLoadListenerIID, NS_IDOMLOADLISTENER_IID); +NS_DEFINE_IID(kIDOMDragListenerIID, NS_IDOMDRAGLISTENER_IID); diff --git a/mozilla/content/events/src/nsDOMEventsIIDs.h b/mozilla/content/events/src/nsDOMEventsIIDs.h new file mode 100644 index 00000000000..814feabfbf3 --- /dev/null +++ b/mozilla/content/events/src/nsDOMEventsIIDs.h @@ -0,0 +1,30 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ +#ifndef nsDOMEVENTSIIDs_h___ +#define nsDOMEVENTSIIDs_h___ + +#include "nsISupports.h" + +extern const nsIID kIDOMMouseListenerIID; +extern const nsIID kIDOMKeyListenerIID; +extern const nsIID kIDOMMouseMotionListenerIID; +extern const nsIID kIDOMFocusListenerIID; +extern const nsIID kIDOMLoadListenerIID; +extern const nsIID kIDOMDragListenerIID; + +#endif /* nsDOMEVENTSIIDs_h___ */ diff --git a/mozilla/content/events/src/nsEventListenerManager.cpp b/mozilla/content/events/src/nsEventListenerManager.cpp index 936e3d272d8..add0af673c0 100644 --- a/mozilla/content/events/src/nsEventListenerManager.cpp +++ b/mozilla/content/events/src/nsEventListenerManager.cpp @@ -30,6 +30,8 @@ #include "nsIDOMDragListener.h" #include "nsIEventStateManager.h" #include "nsIPrivateDOMEvent.h" +#include "nsIScriptObjectOwner.h" +#include "nsIScriptEventListener.h" static NS_DEFINE_IID(kIEventListenerManagerIID, NS_IEVENTLISTENERMANAGER_IID); static NS_DEFINE_IID(kIDOMEventListenerIID, NS_IDOMEVENTLISTENER_IID); @@ -40,6 +42,7 @@ static NS_DEFINE_IID(kIDOMFocusListenerIID, NS_IDOMFOCUSLISTENER_IID); static NS_DEFINE_IID(kIDOMLoadListenerIID, NS_IDOMLOADLISTENER_IID); static NS_DEFINE_IID(kIDOMDragListenerIID, NS_IDOMDRAGLISTENER_IID); static NS_DEFINE_IID(kIDOMEventIID, NS_IDOMEVENT_IID); +static NS_DEFINE_IID(kIScriptEventListenerIID, NS_ISCRIPTEVENTLISTENER_IID); nsEventListenerManager::nsEventListenerManager() { @@ -153,6 +156,72 @@ nsresult nsEventListenerManager::AddEventListener(nsIDOMEventListener *aListener return NS_OK; } +const char *mEventArgv[] = {"event"}; + +nsresult nsEventListenerManager::SetJSEventListener(nsIScriptContext *aContext, JSObject *aObject, REFNSIID aIID) +{ + nsVoidArray *mListeners; + + if (NS_OK == GetEventListeners(&mListeners, aIID)) { + //Run through the listeners for this IID and see if a script listener is registered + //If so, we're set. + if (nsnull != mListeners) { + nsIScriptEventListener *mScriptListener; + nsIDOMEventListener *mEventListener; + for (int i=0; iCount(); i++) { + mEventListener = (nsIDOMEventListener*)mListeners->ElementAt(i); + if (NS_OK == mEventListener->QueryInterface(kIScriptEventListenerIID, (void**)&mScriptListener)) { + NS_RELEASE(mScriptListener); + return NS_OK; + } + } + } + //If we didn't find a script listener or no listeners existed create and add a new one. + nsIDOMEventListener *mScriptListener; + if (NS_OK == NS_NewScriptEventListener(&mScriptListener, aContext, aObject)) { + AddEventListener(mScriptListener, aIID); + NS_RELEASE(mScriptListener); + return NS_OK; + } + } + return NS_ERROR_FAILURE; +} + +nsresult nsEventListenerManager::AddScriptEventListener(nsIScriptContext* aContext, nsIScriptObjectOwner *aScriptObjectOwner, + nsIAtom *aName, const nsString& aFunc, REFNSIID aIID) +{ + JSObject *mScriptObject; + + if (NS_OK == aScriptObjectOwner->GetScriptObject(aContext, (void**)&mScriptObject)) { + JSContext* mJSContext = (JSContext*)aContext->GetNativeContext(); + nsString mName, mLowerName; + char* mCharName; + + aName->ToString(mName); + mName.ToLowerCase(mLowerName); + mCharName = mLowerName.ToNewCString(); + + if (nsnull != mCharName) { + JS_CompileUCFunction(mJSContext, mScriptObject, mCharName, + 1, mEventArgv, (jschar*)aFunc.GetUnicode(), aFunc.Length(), + nsnull, 0); + delete mCharName; + return SetJSEventListener(aContext, mScriptObject, aIID); + } + } + return NS_ERROR_FAILURE; +} + +nsresult nsEventListenerManager::RegisterScriptEventListener(nsIScriptContext *aContext, nsIScriptObjectOwner *aScriptObjectOwner, + REFNSIID aIID) +{ + JSObject *mScriptObject; + if (NS_OK == aScriptObjectOwner->GetScriptObject(aContext, (void**)&mScriptObject)) { + return SetJSEventListener(aContext, mScriptObject, aIID); + } + return NS_ERROR_FAILURE; +} + /** * Removes event listeners of all types. * @param an event listener diff --git a/mozilla/content/events/src/nsEventListenerManager.h b/mozilla/content/events/src/nsEventListenerManager.h index f0371d30a0f..d35d9f9613d 100644 --- a/mozilla/content/events/src/nsEventListenerManager.h +++ b/mozilla/content/events/src/nsEventListenerManager.h @@ -20,6 +20,8 @@ #define nsEventListenerManager_h__ #include "nsIEventListenerManager.h" +#include "jsapi.h" + class nsIDOMEvent; /* @@ -52,16 +54,28 @@ public: * @param an event listener */ - virtual nsresult AddEventListener(nsIDOMEventListener *aListener, const nsIID& aIID); + virtual nsresult AddEventListener(nsIDOMEventListener *aListener, REFNSIID aIID); + virtual nsresult AddScriptEventListener(nsIScriptContext* aContext, + nsIScriptObjectOwner *aScriptObjectOwner, + nsIAtom *aName, + const nsString& aFunc, + const nsIID& aIID); + virtual nsresult RegisterScriptEventListener(nsIScriptContext *aContext, + nsIScriptObjectOwner *aScriptObjectOwner, + const nsIID& aIID); - virtual nsresult RemoveEventListener(nsIDOMEventListener *aListener, const nsIID& aIID); + virtual nsresult RemoveEventListener(nsIDOMEventListener *aListener, REFNSIID aIID); virtual nsresult CaptureEvent(nsIDOMEventListener *aListener); virtual nsresult ReleaseEvent(nsIDOMEventListener *aListener); - virtual nsresult HandleEvent(nsIPresContext& aPresContext, nsEvent* aEvent, nsIDOMEvent** aDOMEvent, nsEventStatus& aEventStatus); + virtual nsresult HandleEvent(nsIPresContext& aPresContext, + nsEvent* aEvent, + nsIDOMEvent** aDOMEvent, + nsEventStatus& aEventStatus); protected: + nsresult SetJSEventListener(nsIScriptContext *aContext, JSObject *aObject, REFNSIID aIID); PRUint32 mRefCnt : 31; diff --git a/mozilla/content/html/content/src/nsHTMLAtoms.cpp b/mozilla/content/html/content/src/nsHTMLAtoms.cpp index 125c1a246cf..a6c3a416407 100644 --- a/mozilla/content/html/content/src/nsHTMLAtoms.cpp +++ b/mozilla/content/html/content/src/nsHTMLAtoms.cpp @@ -97,9 +97,22 @@ nsIAtom* nsHTMLAtoms::noresize; nsIAtom* nsHTMLAtoms::noshade; nsIAtom* nsHTMLAtoms::nowrap; nsIAtom* nsHTMLAtoms::ol; +nsIAtom* nsHTMLAtoms::onabort; nsIAtom* nsHTMLAtoms::onblur; +nsIAtom* nsHTMLAtoms::onclick; +nsIAtom* nsHTMLAtoms::ondblclick; +nsIAtom* nsHTMLAtoms::ondragdrop; +nsIAtom* nsHTMLAtoms::onerror; nsIAtom* nsHTMLAtoms::onfocus; +nsIAtom* nsHTMLAtoms::onkeydown; +nsIAtom* nsHTMLAtoms::onkeypress; +nsIAtom* nsHTMLAtoms::onkeyup; nsIAtom* nsHTMLAtoms::onload; +nsIAtom* nsHTMLAtoms::onmousedown; +nsIAtom* nsHTMLAtoms::onmousemove; +nsIAtom* nsHTMLAtoms::onmouseover; +nsIAtom* nsHTMLAtoms::onmouseout; +nsIAtom* nsHTMLAtoms::onmouseup; nsIAtom* nsHTMLAtoms::onunload; nsIAtom* nsHTMLAtoms::outOfDate; nsIAtom* nsHTMLAtoms::overflow; @@ -232,9 +245,22 @@ void nsHTMLAtoms::AddrefAtoms() noshade = NS_NewAtom("NOSHADE"); nowrap = NS_NewAtom("NOWRAP"); ol = NS_NewAtom("OL"); + onabort = NS_NewAtom("ONABORT"); onblur = NS_NewAtom("ONBLUR"); + onclick = NS_NewAtom("ONCLICK"); + ondragdrop = NS_NewAtom("ONDBLCLICK"); + ondragdrop = NS_NewAtom("ONDRAGDROP"); + onerror = NS_NewAtom("ONERROR"); onfocus = NS_NewAtom("ONFOCUS"); + onkeydown = NS_NewAtom("ONKEYDOWN"); + onkeypress = NS_NewAtom("ONKEYPRESS"); + onkeyup = NS_NewAtom("ONKEYUP"); onload = NS_NewAtom("ONLOAD"); + onmousedown = NS_NewAtom("ONMOUSEDOWN"); + onmousemove = NS_NewAtom("ONMOUSEMOVE"); + onmouseover = NS_NewAtom("ONMOUSEOVER"); + onmouseout = NS_NewAtom("ONMOUSEOUT"); + onmouseup = NS_NewAtom("ONMOUSEUP"); onunload = NS_NewAtom("ONUNLOAD"); outOfDate = NS_NewAtom("OUT-OF-DATE"); overflow = NS_NewAtom("OVERFLOW"); @@ -365,9 +391,22 @@ void nsHTMLAtoms::ReleaseAtoms() NS_RELEASE(noshade); NS_RELEASE(nowrap); NS_RELEASE(ol); + NS_RELEASE(onabort); NS_RELEASE(onblur); + NS_RELEASE(onclick); + NS_RELEASE(ondblclick); + NS_RELEASE(ondragdrop); + NS_RELEASE(onerror); NS_RELEASE(onfocus); + NS_RELEASE(onkeydown); + NS_RELEASE(onkeypress); + NS_RELEASE(onkeyup); NS_RELEASE(onload); + NS_RELEASE(onmousedown); + NS_RELEASE(onmousemove); + NS_RELEASE(onmouseover); + NS_RELEASE(onmouseout); + NS_RELEASE(onmouseup); NS_RELEASE(onunload); NS_RELEASE(outOfDate); NS_RELEASE(overflow); diff --git a/mozilla/content/html/content/src/nsHTMLAtoms.h b/mozilla/content/html/content/src/nsHTMLAtoms.h index 8f0d7dab4e6..124d1a1c55b 100644 --- a/mozilla/content/html/content/src/nsHTMLAtoms.h +++ b/mozilla/content/html/content/src/nsHTMLAtoms.h @@ -130,9 +130,22 @@ public: static nsIAtom* nowrap; static nsIAtom* ol; + static nsIAtom* onabort; static nsIAtom* onblur; + static nsIAtom* onclick; + static nsIAtom* ondblclick; + static nsIAtom* ondragdrop; + static nsIAtom* onerror; static nsIAtom* onfocus; + static nsIAtom* onkeydown; + static nsIAtom* onkeypress; + static nsIAtom* onkeyup; static nsIAtom* onload; + static nsIAtom* onmousedown; + static nsIAtom* onmousemove; + static nsIAtom* onmouseover; + static nsIAtom* onmouseout; + static nsIAtom* onmouseup; static nsIAtom* onunload; static nsIAtom* outOfDate; static nsIAtom* overflow; diff --git a/mozilla/content/shared/public/nsHTMLAtoms.h b/mozilla/content/shared/public/nsHTMLAtoms.h index 8f0d7dab4e6..124d1a1c55b 100644 --- a/mozilla/content/shared/public/nsHTMLAtoms.h +++ b/mozilla/content/shared/public/nsHTMLAtoms.h @@ -130,9 +130,22 @@ public: static nsIAtom* nowrap; static nsIAtom* ol; + static nsIAtom* onabort; static nsIAtom* onblur; + static nsIAtom* onclick; + static nsIAtom* ondblclick; + static nsIAtom* ondragdrop; + static nsIAtom* onerror; static nsIAtom* onfocus; + static nsIAtom* onkeydown; + static nsIAtom* onkeypress; + static nsIAtom* onkeyup; static nsIAtom* onload; + static nsIAtom* onmousedown; + static nsIAtom* onmousemove; + static nsIAtom* onmouseover; + static nsIAtom* onmouseout; + static nsIAtom* onmouseup; static nsIAtom* onunload; static nsIAtom* outOfDate; static nsIAtom* overflow; diff --git a/mozilla/content/shared/src/nsHTMLAtoms.cpp b/mozilla/content/shared/src/nsHTMLAtoms.cpp index 125c1a246cf..a6c3a416407 100644 --- a/mozilla/content/shared/src/nsHTMLAtoms.cpp +++ b/mozilla/content/shared/src/nsHTMLAtoms.cpp @@ -97,9 +97,22 @@ nsIAtom* nsHTMLAtoms::noresize; nsIAtom* nsHTMLAtoms::noshade; nsIAtom* nsHTMLAtoms::nowrap; nsIAtom* nsHTMLAtoms::ol; +nsIAtom* nsHTMLAtoms::onabort; nsIAtom* nsHTMLAtoms::onblur; +nsIAtom* nsHTMLAtoms::onclick; +nsIAtom* nsHTMLAtoms::ondblclick; +nsIAtom* nsHTMLAtoms::ondragdrop; +nsIAtom* nsHTMLAtoms::onerror; nsIAtom* nsHTMLAtoms::onfocus; +nsIAtom* nsHTMLAtoms::onkeydown; +nsIAtom* nsHTMLAtoms::onkeypress; +nsIAtom* nsHTMLAtoms::onkeyup; nsIAtom* nsHTMLAtoms::onload; +nsIAtom* nsHTMLAtoms::onmousedown; +nsIAtom* nsHTMLAtoms::onmousemove; +nsIAtom* nsHTMLAtoms::onmouseover; +nsIAtom* nsHTMLAtoms::onmouseout; +nsIAtom* nsHTMLAtoms::onmouseup; nsIAtom* nsHTMLAtoms::onunload; nsIAtom* nsHTMLAtoms::outOfDate; nsIAtom* nsHTMLAtoms::overflow; @@ -232,9 +245,22 @@ void nsHTMLAtoms::AddrefAtoms() noshade = NS_NewAtom("NOSHADE"); nowrap = NS_NewAtom("NOWRAP"); ol = NS_NewAtom("OL"); + onabort = NS_NewAtom("ONABORT"); onblur = NS_NewAtom("ONBLUR"); + onclick = NS_NewAtom("ONCLICK"); + ondragdrop = NS_NewAtom("ONDBLCLICK"); + ondragdrop = NS_NewAtom("ONDRAGDROP"); + onerror = NS_NewAtom("ONERROR"); onfocus = NS_NewAtom("ONFOCUS"); + onkeydown = NS_NewAtom("ONKEYDOWN"); + onkeypress = NS_NewAtom("ONKEYPRESS"); + onkeyup = NS_NewAtom("ONKEYUP"); onload = NS_NewAtom("ONLOAD"); + onmousedown = NS_NewAtom("ONMOUSEDOWN"); + onmousemove = NS_NewAtom("ONMOUSEMOVE"); + onmouseover = NS_NewAtom("ONMOUSEOVER"); + onmouseout = NS_NewAtom("ONMOUSEOUT"); + onmouseup = NS_NewAtom("ONMOUSEUP"); onunload = NS_NewAtom("ONUNLOAD"); outOfDate = NS_NewAtom("OUT-OF-DATE"); overflow = NS_NewAtom("OVERFLOW"); @@ -365,9 +391,22 @@ void nsHTMLAtoms::ReleaseAtoms() NS_RELEASE(noshade); NS_RELEASE(nowrap); NS_RELEASE(ol); + NS_RELEASE(onabort); NS_RELEASE(onblur); + NS_RELEASE(onclick); + NS_RELEASE(ondblclick); + NS_RELEASE(ondragdrop); + NS_RELEASE(onerror); NS_RELEASE(onfocus); + NS_RELEASE(onkeydown); + NS_RELEASE(onkeypress); + NS_RELEASE(onkeyup); NS_RELEASE(onload); + NS_RELEASE(onmousedown); + NS_RELEASE(onmousemove); + NS_RELEASE(onmouseover); + NS_RELEASE(onmouseout); + NS_RELEASE(onmouseup); NS_RELEASE(onunload); NS_RELEASE(outOfDate); NS_RELEASE(overflow); diff --git a/mozilla/layout/base/src/nsDocument.cpp b/mozilla/layout/base/src/nsDocument.cpp index 3d2a19fa1c2..b09185c7a7b 100644 --- a/mozilla/layout/base/src/nsDocument.cpp +++ b/mozilla/layout/base/src/nsDocument.cpp @@ -31,12 +31,11 @@ #include "nsIScriptContextOwner.h" #include "nsIScriptEventListener.h" #include "nsDOMEvent.h" +#include "nsDOMEventsIIDs.h" #include "nsIPrivateDOMEvent.h" #include "nsIEventStateManager.h" -#include "nsIDOMMouseListener.h" -#include "nsIDOMKeyListener.h" -#include "nsIDOMMouseMotionListener.h" #include "nsContentList.h" +#include "nsIDOMEventListener.h" #include "nsCSSPropIDs.h" #include "nsCSSProps.h" @@ -63,9 +62,6 @@ static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); static NS_DEFINE_IID(kIScriptEventListenerIID, NS_ISCRIPTEVENTLISTENER_IID); static NS_DEFINE_IID(kIDOMEventCapturerIID, NS_IDOMEVENTCAPTURER_IID); static NS_DEFINE_IID(kIDOMEventReceiverIID, NS_IDOMEVENTRECEIVER_IID); -static NS_DEFINE_IID(kIDOMMouseListenerIID, NS_IDOMMOUSELISTENER_IID); -static NS_DEFINE_IID(kIDOMKeyListenerIID, NS_IDOMKEYLISTENER_IID); -static NS_DEFINE_IID(kIDOMMouseMotionListenerIID, NS_IDOMMOUSEMOTIONLISTENER_IID); static NS_DEFINE_IID(kIPrivateDOMEventIID, NS_IPRIVATEDOMEVENT_IID); static NS_DEFINE_IID(kIEventListenerManagerIID, NS_IEVENTLISTENERMANAGER_IID); static NS_DEFINE_IID(kIPostDataIID, NS_IPOSTDATA_IID); diff --git a/mozilla/layout/events/public/nsIEventListenerManager.h b/mozilla/layout/events/public/nsIEventListenerManager.h index 28b8f402f57..f7b7822fc1c 100644 --- a/mozilla/layout/events/public/nsIEventListenerManager.h +++ b/mozilla/layout/events/public/nsIEventListenerManager.h @@ -25,7 +25,7 @@ class nsIPresContext; class nsIDOMEventListener; -class nsIDOMEvent; +class nsIScriptObjectOwner; /* * Event listener manager interface. @@ -52,6 +52,23 @@ public: */ virtual nsresult AddEventListener(nsIDOMEventListener *aListener, const nsIID& aIID) = 0; + + /** + * Creates a script event listener for the given script object with name mName and function + * content mFunc. + * @param an event listener + */ + + virtual nsresult AddScriptEventListener(nsIScriptContext*aContext, nsIScriptObjectOwner *aScriptObjectOwner, + nsIAtom *aName, const nsString& aFunc, REFNSIID aIID) = 0; + /** + * Registers an event listners which already exists on the given script object with the event + * listener manager. + * @param an event listener + */ + + virtual nsresult RegisterScriptEventListener(nsIScriptContext *aContext, nsIScriptObjectOwner *aScriptObjectOwner, + REFNSIID aIID) = 0; /** * Removes events listeners of all types. diff --git a/mozilla/layout/events/src/MANIFEST b/mozilla/layout/events/src/MANIFEST index 0c01cf7c6c3..3f21351764f 100644 --- a/mozilla/layout/events/src/MANIFEST +++ b/mozilla/layout/events/src/MANIFEST @@ -4,3 +4,4 @@ nsEventListenerManager.h nsEventStateManager.h nsDOMEvent.h +nsDOMEventsIIDs.h diff --git a/mozilla/layout/events/src/Makefile b/mozilla/layout/events/src/Makefile index 973a0bd7a78..763290a1bf1 100644 --- a/mozilla/layout/events/src/Makefile +++ b/mozilla/layout/events/src/Makefile @@ -25,6 +25,7 @@ CPPSRCS = \ nsEventListenerManager.cpp \ nsEventStateManager.cpp \ nsDOMEvent.cpp \ + nsDOMEventsIIDs.cpp \ $(NULL) MODULE = raptor @@ -33,6 +34,7 @@ EXPORTS = \ nsEventListenerManager.h \ nsEventStateManager.h \ nsDOMEvent.h \ + nsDOMEventsIIDs.h \ $(NULL) REQUIRES = xpcom raptor dom js diff --git a/mozilla/layout/events/src/makefile.win b/mozilla/layout/events/src/makefile.win index fb627926014..f952b3035ad 100644 --- a/mozilla/layout/events/src/makefile.win +++ b/mozilla/layout/events/src/makefile.win @@ -29,15 +29,17 @@ DEFINES=-D_IMPL_NS_HTML -DWIN32_LEAN_AND_MEAN CPPSRCS= nsEventListenerManager.cpp \ nsEventStateManager.cpp \ nsDOMEvent.cpp \ + nsDOMEventsIIDs.cpp \ $(NULL) CPP_OBJS= .\$(OBJDIR)\nsEventListenerManager.obj \ .\$(OBJDIR)\nsEventStateManager.obj \ .\$(OBJDIR)\nsDOMEvent.obj \ + .\$(OBJDIR)\nsDOMEventsIIDs.obj \ $(NULL) -EXPORTS= nsEventListenerManager.h nsEventStateManager.h nsDOMEvent.h +EXPORTS= nsEventListenerManager.h nsEventStateManager.h nsDOMEvent.h nsDOMEventsIIDs.h LINCS=-I$(PUBLIC)\xpcom -I$(PUBLIC)\raptor \ -I$(PUBLIC)\dom -I$(PUBLIC)\js diff --git a/mozilla/layout/events/src/nsDOMEventsIIDs.cpp b/mozilla/layout/events/src/nsDOMEventsIIDs.cpp new file mode 100644 index 00000000000..c3b86931972 --- /dev/null +++ b/mozilla/layout/events/src/nsDOMEventsIIDs.cpp @@ -0,0 +1,31 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ +#include "nsDOMEventsIIDs.h" +#include "nsIDOMMouseListener.h" +#include "nsIDOMKeyListener.h" +#include "nsIDOMMouseMotionListener.h" +#include "nsIDOMFocusListener.h" +#include "nsIDOMLoadListener.h" +#include "nsIDOMDragListener.h" + +NS_DEFINE_IID(kIDOMMouseListenerIID, NS_IDOMMOUSELISTENER_IID); +NS_DEFINE_IID(kIDOMKeyListenerIID, NS_IDOMKEYLISTENER_IID); +NS_DEFINE_IID(kIDOMMouseMotionListenerIID, NS_IDOMMOUSEMOTIONLISTENER_IID); +NS_DEFINE_IID(kIDOMFocusListenerIID, NS_IDOMFOCUSLISTENER_IID); +NS_DEFINE_IID(kIDOMLoadListenerIID, NS_IDOMLOADLISTENER_IID); +NS_DEFINE_IID(kIDOMDragListenerIID, NS_IDOMDRAGLISTENER_IID); diff --git a/mozilla/layout/events/src/nsDOMEventsIIDs.h b/mozilla/layout/events/src/nsDOMEventsIIDs.h new file mode 100644 index 00000000000..814feabfbf3 --- /dev/null +++ b/mozilla/layout/events/src/nsDOMEventsIIDs.h @@ -0,0 +1,30 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ +#ifndef nsDOMEVENTSIIDs_h___ +#define nsDOMEVENTSIIDs_h___ + +#include "nsISupports.h" + +extern const nsIID kIDOMMouseListenerIID; +extern const nsIID kIDOMKeyListenerIID; +extern const nsIID kIDOMMouseMotionListenerIID; +extern const nsIID kIDOMFocusListenerIID; +extern const nsIID kIDOMLoadListenerIID; +extern const nsIID kIDOMDragListenerIID; + +#endif /* nsDOMEVENTSIIDs_h___ */ diff --git a/mozilla/layout/events/src/nsEventListenerManager.cpp b/mozilla/layout/events/src/nsEventListenerManager.cpp index 936e3d272d8..add0af673c0 100644 --- a/mozilla/layout/events/src/nsEventListenerManager.cpp +++ b/mozilla/layout/events/src/nsEventListenerManager.cpp @@ -30,6 +30,8 @@ #include "nsIDOMDragListener.h" #include "nsIEventStateManager.h" #include "nsIPrivateDOMEvent.h" +#include "nsIScriptObjectOwner.h" +#include "nsIScriptEventListener.h" static NS_DEFINE_IID(kIEventListenerManagerIID, NS_IEVENTLISTENERMANAGER_IID); static NS_DEFINE_IID(kIDOMEventListenerIID, NS_IDOMEVENTLISTENER_IID); @@ -40,6 +42,7 @@ static NS_DEFINE_IID(kIDOMFocusListenerIID, NS_IDOMFOCUSLISTENER_IID); static NS_DEFINE_IID(kIDOMLoadListenerIID, NS_IDOMLOADLISTENER_IID); static NS_DEFINE_IID(kIDOMDragListenerIID, NS_IDOMDRAGLISTENER_IID); static NS_DEFINE_IID(kIDOMEventIID, NS_IDOMEVENT_IID); +static NS_DEFINE_IID(kIScriptEventListenerIID, NS_ISCRIPTEVENTLISTENER_IID); nsEventListenerManager::nsEventListenerManager() { @@ -153,6 +156,72 @@ nsresult nsEventListenerManager::AddEventListener(nsIDOMEventListener *aListener return NS_OK; } +const char *mEventArgv[] = {"event"}; + +nsresult nsEventListenerManager::SetJSEventListener(nsIScriptContext *aContext, JSObject *aObject, REFNSIID aIID) +{ + nsVoidArray *mListeners; + + if (NS_OK == GetEventListeners(&mListeners, aIID)) { + //Run through the listeners for this IID and see if a script listener is registered + //If so, we're set. + if (nsnull != mListeners) { + nsIScriptEventListener *mScriptListener; + nsIDOMEventListener *mEventListener; + for (int i=0; iCount(); i++) { + mEventListener = (nsIDOMEventListener*)mListeners->ElementAt(i); + if (NS_OK == mEventListener->QueryInterface(kIScriptEventListenerIID, (void**)&mScriptListener)) { + NS_RELEASE(mScriptListener); + return NS_OK; + } + } + } + //If we didn't find a script listener or no listeners existed create and add a new one. + nsIDOMEventListener *mScriptListener; + if (NS_OK == NS_NewScriptEventListener(&mScriptListener, aContext, aObject)) { + AddEventListener(mScriptListener, aIID); + NS_RELEASE(mScriptListener); + return NS_OK; + } + } + return NS_ERROR_FAILURE; +} + +nsresult nsEventListenerManager::AddScriptEventListener(nsIScriptContext* aContext, nsIScriptObjectOwner *aScriptObjectOwner, + nsIAtom *aName, const nsString& aFunc, REFNSIID aIID) +{ + JSObject *mScriptObject; + + if (NS_OK == aScriptObjectOwner->GetScriptObject(aContext, (void**)&mScriptObject)) { + JSContext* mJSContext = (JSContext*)aContext->GetNativeContext(); + nsString mName, mLowerName; + char* mCharName; + + aName->ToString(mName); + mName.ToLowerCase(mLowerName); + mCharName = mLowerName.ToNewCString(); + + if (nsnull != mCharName) { + JS_CompileUCFunction(mJSContext, mScriptObject, mCharName, + 1, mEventArgv, (jschar*)aFunc.GetUnicode(), aFunc.Length(), + nsnull, 0); + delete mCharName; + return SetJSEventListener(aContext, mScriptObject, aIID); + } + } + return NS_ERROR_FAILURE; +} + +nsresult nsEventListenerManager::RegisterScriptEventListener(nsIScriptContext *aContext, nsIScriptObjectOwner *aScriptObjectOwner, + REFNSIID aIID) +{ + JSObject *mScriptObject; + if (NS_OK == aScriptObjectOwner->GetScriptObject(aContext, (void**)&mScriptObject)) { + return SetJSEventListener(aContext, mScriptObject, aIID); + } + return NS_ERROR_FAILURE; +} + /** * Removes event listeners of all types. * @param an event listener diff --git a/mozilla/layout/events/src/nsEventListenerManager.h b/mozilla/layout/events/src/nsEventListenerManager.h index f0371d30a0f..d35d9f9613d 100644 --- a/mozilla/layout/events/src/nsEventListenerManager.h +++ b/mozilla/layout/events/src/nsEventListenerManager.h @@ -20,6 +20,8 @@ #define nsEventListenerManager_h__ #include "nsIEventListenerManager.h" +#include "jsapi.h" + class nsIDOMEvent; /* @@ -52,16 +54,28 @@ public: * @param an event listener */ - virtual nsresult AddEventListener(nsIDOMEventListener *aListener, const nsIID& aIID); + virtual nsresult AddEventListener(nsIDOMEventListener *aListener, REFNSIID aIID); + virtual nsresult AddScriptEventListener(nsIScriptContext* aContext, + nsIScriptObjectOwner *aScriptObjectOwner, + nsIAtom *aName, + const nsString& aFunc, + const nsIID& aIID); + virtual nsresult RegisterScriptEventListener(nsIScriptContext *aContext, + nsIScriptObjectOwner *aScriptObjectOwner, + const nsIID& aIID); - virtual nsresult RemoveEventListener(nsIDOMEventListener *aListener, const nsIID& aIID); + virtual nsresult RemoveEventListener(nsIDOMEventListener *aListener, REFNSIID aIID); virtual nsresult CaptureEvent(nsIDOMEventListener *aListener); virtual nsresult ReleaseEvent(nsIDOMEventListener *aListener); - virtual nsresult HandleEvent(nsIPresContext& aPresContext, nsEvent* aEvent, nsIDOMEvent** aDOMEvent, nsEventStatus& aEventStatus); + virtual nsresult HandleEvent(nsIPresContext& aPresContext, + nsEvent* aEvent, + nsIDOMEvent** aDOMEvent, + nsEventStatus& aEventStatus); protected: + nsresult SetJSEventListener(nsIScriptContext *aContext, JSObject *aObject, REFNSIID aIID); PRUint32 mRefCnt : 31; diff --git a/mozilla/layout/html/base/src/nsHTMLAtoms.cpp b/mozilla/layout/html/base/src/nsHTMLAtoms.cpp index 125c1a246cf..a6c3a416407 100644 --- a/mozilla/layout/html/base/src/nsHTMLAtoms.cpp +++ b/mozilla/layout/html/base/src/nsHTMLAtoms.cpp @@ -97,9 +97,22 @@ nsIAtom* nsHTMLAtoms::noresize; nsIAtom* nsHTMLAtoms::noshade; nsIAtom* nsHTMLAtoms::nowrap; nsIAtom* nsHTMLAtoms::ol; +nsIAtom* nsHTMLAtoms::onabort; nsIAtom* nsHTMLAtoms::onblur; +nsIAtom* nsHTMLAtoms::onclick; +nsIAtom* nsHTMLAtoms::ondblclick; +nsIAtom* nsHTMLAtoms::ondragdrop; +nsIAtom* nsHTMLAtoms::onerror; nsIAtom* nsHTMLAtoms::onfocus; +nsIAtom* nsHTMLAtoms::onkeydown; +nsIAtom* nsHTMLAtoms::onkeypress; +nsIAtom* nsHTMLAtoms::onkeyup; nsIAtom* nsHTMLAtoms::onload; +nsIAtom* nsHTMLAtoms::onmousedown; +nsIAtom* nsHTMLAtoms::onmousemove; +nsIAtom* nsHTMLAtoms::onmouseover; +nsIAtom* nsHTMLAtoms::onmouseout; +nsIAtom* nsHTMLAtoms::onmouseup; nsIAtom* nsHTMLAtoms::onunload; nsIAtom* nsHTMLAtoms::outOfDate; nsIAtom* nsHTMLAtoms::overflow; @@ -232,9 +245,22 @@ void nsHTMLAtoms::AddrefAtoms() noshade = NS_NewAtom("NOSHADE"); nowrap = NS_NewAtom("NOWRAP"); ol = NS_NewAtom("OL"); + onabort = NS_NewAtom("ONABORT"); onblur = NS_NewAtom("ONBLUR"); + onclick = NS_NewAtom("ONCLICK"); + ondragdrop = NS_NewAtom("ONDBLCLICK"); + ondragdrop = NS_NewAtom("ONDRAGDROP"); + onerror = NS_NewAtom("ONERROR"); onfocus = NS_NewAtom("ONFOCUS"); + onkeydown = NS_NewAtom("ONKEYDOWN"); + onkeypress = NS_NewAtom("ONKEYPRESS"); + onkeyup = NS_NewAtom("ONKEYUP"); onload = NS_NewAtom("ONLOAD"); + onmousedown = NS_NewAtom("ONMOUSEDOWN"); + onmousemove = NS_NewAtom("ONMOUSEMOVE"); + onmouseover = NS_NewAtom("ONMOUSEOVER"); + onmouseout = NS_NewAtom("ONMOUSEOUT"); + onmouseup = NS_NewAtom("ONMOUSEUP"); onunload = NS_NewAtom("ONUNLOAD"); outOfDate = NS_NewAtom("OUT-OF-DATE"); overflow = NS_NewAtom("OVERFLOW"); @@ -365,9 +391,22 @@ void nsHTMLAtoms::ReleaseAtoms() NS_RELEASE(noshade); NS_RELEASE(nowrap); NS_RELEASE(ol); + NS_RELEASE(onabort); NS_RELEASE(onblur); + NS_RELEASE(onclick); + NS_RELEASE(ondblclick); + NS_RELEASE(ondragdrop); + NS_RELEASE(onerror); NS_RELEASE(onfocus); + NS_RELEASE(onkeydown); + NS_RELEASE(onkeypress); + NS_RELEASE(onkeyup); NS_RELEASE(onload); + NS_RELEASE(onmousedown); + NS_RELEASE(onmousemove); + NS_RELEASE(onmouseover); + NS_RELEASE(onmouseout); + NS_RELEASE(onmouseup); NS_RELEASE(onunload); NS_RELEASE(outOfDate); NS_RELEASE(overflow); diff --git a/mozilla/layout/html/base/src/nsHTMLAtoms.h b/mozilla/layout/html/base/src/nsHTMLAtoms.h index 8f0d7dab4e6..124d1a1c55b 100644 --- a/mozilla/layout/html/base/src/nsHTMLAtoms.h +++ b/mozilla/layout/html/base/src/nsHTMLAtoms.h @@ -130,9 +130,22 @@ public: static nsIAtom* nowrap; static nsIAtom* ol; + static nsIAtom* onabort; static nsIAtom* onblur; + static nsIAtom* onclick; + static nsIAtom* ondblclick; + static nsIAtom* ondragdrop; + static nsIAtom* onerror; static nsIAtom* onfocus; + static nsIAtom* onkeydown; + static nsIAtom* onkeypress; + static nsIAtom* onkeyup; static nsIAtom* onload; + static nsIAtom* onmousedown; + static nsIAtom* onmousemove; + static nsIAtom* onmouseover; + static nsIAtom* onmouseout; + static nsIAtom* onmouseup; static nsIAtom* onunload; static nsIAtom* outOfDate; static nsIAtom* overflow; diff --git a/mozilla/layout/html/base/src/nsHTMLTagContent.cpp b/mozilla/layout/html/base/src/nsHTMLTagContent.cpp index 6ae950f188f..227dcbcc449 100644 --- a/mozilla/layout/html/base/src/nsHTMLTagContent.cpp +++ b/mozilla/layout/html/base/src/nsHTMLTagContent.cpp @@ -36,9 +36,8 @@ #include "nsISizeOfHandler.h" #include "nsIEventListenerManager.h" #include "nsIScriptEventListener.h" -#include "nsIDOMMouseListener.h" -#include "nsIDOMKeyListener.h" -#include "nsIDOMMouseMotionListener.h" +#include "nsIScriptContextOwner.h" +#include "nsDOMEventsIIDs.h" #include "jsapi.h" #include "nsXIFConverter.h" @@ -46,10 +45,8 @@ static NS_DEFINE_IID(kIStyleRuleIID, NS_ISTYLE_RULE_IID); static NS_DEFINE_IID(kIDOMElementIID, NS_IDOMELEMENT_IID); static NS_DEFINE_IID(kIDOMDocumentIID, NS_IDOMDOCUMENT_IID); +static NS_DEFINE_IID(kIScriptObjectOwner, NS_ISCRIPTOBJECTOWNER_IID); static NS_DEFINE_IID(kIScriptEventListenerIID, NS_ISCRIPTEVENTLISTENER_IID); -static NS_DEFINE_IID(kIDOMMouseListenerIID, NS_IDOMMOUSELISTENER_IID); -static NS_DEFINE_IID(kIDOMKeyListenerIID, NS_IDOMKEYLISTENER_IID); -static NS_DEFINE_IID(kIDOMMouseMotionListenerIID, NS_IDOMMOUSEMOTIONLISTENER_IID); static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID); @@ -244,8 +241,49 @@ void nsHTMLTagContent::ToHTMLString(nsString& aBuf) const aBuf.Append('>'); } +void nsHTMLTagContent::SetDocument(nsIDocument* aDocument) +{ + nsHTMLContent::SetDocument(aDocument); - + // Once the element is added to the doc tree we need to check if event handler + // were registered on it. Unfortunately, this means doing a GetAttribute for + // every type of handler. + if (nsnull != mAttributes) { + nsHTMLValue mValue; + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onclick, mValue)) + AddScriptEventListener(nsHTMLAtoms::onclick, mValue, kIDOMMouseListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::ondblclick, mValue)) + AddScriptEventListener(nsHTMLAtoms::onclick, mValue, kIDOMMouseListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onmousedown, mValue)) + AddScriptEventListener(nsHTMLAtoms::onmousedown, mValue, kIDOMMouseListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onmouseup, mValue)) + AddScriptEventListener(nsHTMLAtoms::onmouseup, mValue, kIDOMMouseListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onmouseover, mValue)) + AddScriptEventListener(nsHTMLAtoms::onmouseover, mValue, kIDOMMouseListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onmouseout, mValue)) + AddScriptEventListener(nsHTMLAtoms::onmouseout, mValue, kIDOMMouseListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onkeydown, mValue)) + AddScriptEventListener(nsHTMLAtoms::onkeydown, mValue, kIDOMKeyListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onkeyup, mValue)) + AddScriptEventListener(nsHTMLAtoms::onkeyup, mValue, kIDOMKeyListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onkeypress, mValue)) + AddScriptEventListener(nsHTMLAtoms::onkeypress, mValue, kIDOMKeyListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onmousemove, mValue)) + AddScriptEventListener(nsHTMLAtoms::onmousemove, mValue, kIDOMMouseMotionListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onload, mValue)) + AddScriptEventListener(nsHTMLAtoms::onload, mValue, kIDOMLoadListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onunload, mValue)) + AddScriptEventListener(nsHTMLAtoms::onunload, mValue, kIDOMLoadListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onabort, mValue)) + AddScriptEventListener(nsHTMLAtoms::onabort, mValue, kIDOMLoadListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onerror, mValue)) + AddScriptEventListener(nsHTMLAtoms::onerror, mValue, kIDOMLoadListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onfocus, mValue)) + AddScriptEventListener(nsHTMLAtoms::onfocus, mValue, kIDOMFocusListenerIID); + if (eContentAttr_HasValue == mAttributes->GetAttribute(nsHTMLAtoms::onblur, mValue)) + AddScriptEventListener(nsHTMLAtoms::onblur, mValue, kIDOMFocusListenerIID); + } +} void nsHTMLTagContent::SetAttribute(const nsString& aName, const nsString& aValue) @@ -339,6 +377,28 @@ nsHTMLTagContent::AttributeToString(nsIAtom* aAttribute, return eContentAttr_NotThere; } +nsresult +nsHTMLTagContent::AddScriptEventListener(nsIAtom* aAttribute, nsHTMLValue& aValue, REFNSIID aIID) +{ + nsresult mRet = NS_OK; + nsIScriptContext* mContext; + nsIScriptContextOwner* mOwner; + + if (nsnull != mDocument) { + mOwner = mDocument->GetScriptContextOwner(); + if (NS_OK == mOwner->GetScriptContext(&mContext)) { + if (nsnull != mListenerManager || NS_OK == GetListenerManager(&mListenerManager)) { + nsString mValue; + aValue.GetStringValue(mValue); + mRet = mListenerManager->AddScriptEventListener(mContext, this, aAttribute, mValue, aIID); + } + NS_RELEASE(mContext); + } + NS_RELEASE(mOwner); + } + return mRet; +} + // Note: Subclasses should override to parse the value string; in // addition, when they see an unknown attribute they should call this // so that global attributes are handled (like CLASS, ID, STYLE, etc.) @@ -524,18 +584,27 @@ PRBool nsHTMLTagContent::SetProperty(JSContext *aContext, jsval aID, jsval *a if (mPrefix == "on") { if (mPropName == "onmousedown" || mPropName == "onmouseup" || mPropName == "onclick" || mPropName == "onmouseover" || mPropName == "onmouseout") { - if (NS_OK != SetScriptEventListener(aContext, kIDOMMouseListenerIID)) { - return PR_FALSE; + if (nsnull != mListenerManager || NS_OK == GetListenerManager(&mListenerManager)) { + nsIScriptContext *mScriptCX = (nsIScriptContext *)JS_GetContextPrivate(aContext); + if (NS_OK != mListenerManager->RegisterScriptEventListener(mScriptCX, this, kIDOMMouseListenerIID)) { + return PR_FALSE; + } } } else if (mPropName == "onkeydown" || mPropName == "onkeyup" || mPropName == "onkeypress" ) { - if (NS_OK != SetScriptEventListener(aContext, kIDOMKeyListenerIID)) { - return PR_FALSE; + if (nsnull != mListenerManager || NS_OK == GetListenerManager(&mListenerManager)) { + nsIScriptContext *mScriptCX = (nsIScriptContext *)JS_GetContextPrivate(aContext); + if (NS_OK != mListenerManager->RegisterScriptEventListener(mScriptCX, this, kIDOMKeyListenerIID)) { + return PR_FALSE; + } } } else if (mPropName == "onmousemove" ) { - if (NS_OK != SetScriptEventListener(aContext, kIDOMMouseMotionListenerIID)) { - return PR_FALSE; + if (nsnull != mListenerManager || NS_OK == GetListenerManager(&mListenerManager)) { + nsIScriptContext *mScriptCX = (nsIScriptContext *)JS_GetContextPrivate(aContext); + if (NS_OK != mListenerManager->RegisterScriptEventListener(mScriptCX, this, kIDOMMouseMotionListenerIID)) { + return PR_FALSE; + } } } } @@ -562,49 +631,6 @@ void nsHTMLTagContent::Finalize(JSContext *aContext) { } -nsresult nsHTMLTagContent::SetScriptEventListener(JSContext *aContext, REFNSIID aListenerTypeIID) -{ - //First get the mScriptObject or make one if we don't have one. - nsIScriptContext *mScriptCX = (nsIScriptContext *)JS_GetContextPrivate(aContext); - - if (nsnull == mScriptObject) { - NS_NewScriptElement(mScriptCX, this, mParent, (void**)&mScriptObject); - } - - if (nsnull != mScriptObject) { - nsIEventListenerManager *mManager = nsnull; - nsVoidArray *mListeners; - - if (NS_OK == GetListenerManager(&mManager) && - NS_OK == mManager->GetEventListeners(&mListeners, aListenerTypeIID)) { - //Run through the listeners for this IID and see if a script listener is registered - //If so, we're set. - if (nsnull != mListeners) { - nsIScriptEventListener *mScriptListener; - nsIDOMEventListener *mEventListener; - for (int i=0; iCount(); i++) { - mEventListener = (nsIDOMEventListener*)mListeners->ElementAt(i); - if (NS_OK == mEventListener->QueryInterface(kIScriptEventListenerIID, (void**)&mScriptListener)) { - NS_RELEASE(mScriptListener); - NS_RELEASE(mManager); - return NS_OK; - } - } - } - //If we didn't find a script listener or no listeners existed create and add a new one. - nsIDOMEventListener *mScriptListener; - if (NS_OK == NS_NewScriptEventListener(&mScriptListener, mScriptCX, mScriptObject)) { - mManager->AddEventListener(mScriptListener, aListenerTypeIID); - NS_RELEASE(mScriptListener); - NS_RELEASE(mManager); - return NS_OK; - } - } - NS_IF_RELEASE(mManager); - } - return NS_ERROR_FAILURE; -} - // // Implementation of nsIDOMNode interface // diff --git a/mozilla/layout/html/base/src/nsHTMLTagContent.h b/mozilla/layout/html/base/src/nsHTMLTagContent.h index 6766ef7a915..15a4f25163e 100644 --- a/mozilla/layout/html/base/src/nsHTMLTagContent.h +++ b/mozilla/layout/html/base/src/nsHTMLTagContent.h @@ -103,6 +103,10 @@ public: virtual nsIStyleRule* GetStyleRule(void); + // Override from nsHTMLContent to allow setting of event handlers once + // tag content is added to the doc tree. + virtual void SetDocument(nsIDocument* aDocument); + // nsIScriptObjectOwner interface NS_IMETHOD GetScriptObject(nsIScriptContext *aContext, void** aScriptObject); @@ -260,14 +264,15 @@ protected: nsHTMLValue& aValue, nsString& aResult) const; + + virtual nsresult AddScriptEventListener(nsIAtom* aAttribute, nsHTMLValue& aValue, REFNSIID aIID); + void TriggerLink(nsIPresContext& aPresContext, const nsString& aBase, const nsString& aURLSpec, const nsString& aTargetSpec, PRBool aClick); - nsresult SetScriptEventListener(JSContext *aContext, REFNSIID aListenerTypeIID); - nsIAtom* mTag; nsIHTMLAttributes* mAttributes; };